tizen beta release
[framework/web/webkit-efl.git] / LayoutTests / fast / forms / script-tests / input-stepup-stepdown-from-renderer.js
1 description('Check stepping-up and -down for <input> from renderer.');
2
3 var input = document.createElement('input');
4 var invalidStateErr = '"Error: INVALID_STATE_ERR: DOM Exception 11"';
5
6 function sendKey(keyName) {
7     var event = document.createEvent('KeyboardEvent');
8     event.initKeyboardEvent('keydown', true, true, document.defaultView, keyName);
9     input.dispatchEvent(event);
10 }
11
12 function setInputAttributes(min, max, step, value) {
13     input.min = min;
14     input.max = max;
15     input.step = step;
16     input.value = value;
17 }
18
19 function stepUp(value, step, max, optionalStepCount) {
20     setInputAttributes(null, max, step, value);
21     if (typeof optionalStepCount != "undefined")
22         if (optionalStepCount < 0)
23             for (var i = 0; i < -optionalStepCount; i++)
24                 sendKey('Down');
25         else
26             for (var i = 0; i < optionalStepCount; i++)
27                 sendKey('Up');
28     else
29         sendKey('Up');
30     return input.value;
31 }
32
33 function stepDown(value, step, min, optionalStepCount) {
34     setInputAttributes(min, null, step, value);
35     if (typeof optionalStepCount != "undefined")
36         if (optionalStepCount < 0)
37             for (var i = 0; i < -optionalStepCount; i++)
38                 sendKey('Up');
39         else
40             for (var i = 0; i < optionalStepCount; i++)
41                 sendKey('Down');
42     else
43         sendKey('Down');
44     return input.value;
45 }
46
47 // Range value gets automatically shifted based on bounds,
48 // So always set the min and max first to get expected behavior
49
50 function stepUpExplicitBounds(min, max, step, value, stepCount) {
51     setInputAttributes(min, max, step, value);
52     if (typeof stepCount !== 'undefined')
53         if (stepCount < 0) {
54             for (var i = 0; i < -stepCount; i++)
55                 sendKey('Down');
56         } else {
57             for (var i = 0; i < stepCount; i++)
58                 sendKey('Up');
59         }
60     else
61         sendKey('Up');
62     return input.value;
63 }
64
65 function stepDownExplicitBounds(min, max, step, value, stepCount) {
66     setInputAttributes(min, max, step, value);
67     if (typeof stepCount !== 'undefined')
68         if (stepCount < 0) {
69             for (var i = 0; i < -stepCount; i++)
70                 sendKey('Up');
71         } else {
72             for (var i = 0; i < stepCount; i++)
73                 sendKey('Down');
74         }
75     else
76         sendKey('Down');
77     return input.value;
78 }
79
80 debug('Number type');
81 input.type = 'number';
82 debug('Function arguments are (value, step, {min or max}, [stepCount]).');
83 debug('Invalid value');
84 shouldBe('stepUp("", null, null)', '"1"');
85 shouldBe('stepDown("", null, null)', '"-1"');
86 shouldBe('stepUp("", "any", null)', '"1"');
87 shouldBe('stepDown("", "any", null)', '"-1"');
88 shouldBe('stepUp("", "foo", null)', '"1"');
89 shouldBe('stepDown("", "foo", null)', '"-1"');
90 shouldBe('stepUp("foo", null, null)', '"1"');
91 shouldBe('stepDown("foo", null, null)', '"-1"');
92 shouldBe('stepUp("foo", "any", null)', '"1"');
93 shouldBe('stepDown("foo", "any", null)', '"-1"');
94 shouldBe('stepUp("foo", "foo", null)', '"1"');
95 shouldBe('stepDown("foo", "foo", null)', '"-1"');
96 debug('Normal cases');
97 shouldBe('stepUp("0", null, null)', '"1"');
98 shouldBe('stepUp("1", null, null, 2)', '"3"');
99 shouldBe('stepUp("3", null, null, -1)', '"2"');
100 shouldBe('stepDown("2", null, null)', '"1"');
101 shouldBe('stepDown("1", null, null, 2)', '"-1"');
102 shouldBe('stepDown("-1", null, null, -1)', '"0"');
103 debug('Invalid step value');
104 shouldBe('stepUp("0", "foo", null)', '"1"');
105 shouldBe('stepUp("1", "0", null)', '"2"');
106 shouldBe('stepUp("2", "-1", null)', '"3"');
107 debug('Step=any');
108 shouldBe('stepUp("0", "any", null)', '"1"');
109 shouldBe('stepDown("0", "any", null)', '"-1"');
110 debug('Step=any corner case');
111 shouldBe('stepUpExplicitBounds("0", "100", "any", "1.5", "1")', '"2.5"');
112 shouldBe('stepDownExplicitBounds("0", "100", "any", "1.5", "1")', '"0.5"');
113 debug('Overflow/underflow');
114 shouldBe('stepDown("1", "1", "0")', '"0"');
115 shouldBe('stepDown("0", "1", "0")', '"0"');
116 shouldBe('stepDown("1", "1", "0", 2)', '"0"');
117 shouldBe('stepDown("1", "3.40282346e+38", "", 2)', '"-3.40282346e+38"');
118 shouldBe('stepUp("-1", "1", "0")', '"0"');
119 shouldBe('stepUp("0", "1", "0")', '"0"');
120 shouldBe('stepUp("-1", "1", "0", 2)', '"0"');
121 shouldBe('stepUp("1", "3.40282346e+38", "", 2)', '"3.40282346e+38"');
122 debug('stepDown()/stepUp() for stepMismatch values');
123 shouldBe('stepUp("1", "2", "")', '"2"');
124 shouldBe('input.min = "0"; stepUp("9", "10", "")', '"10"');
125 shouldBe('stepDown("19", "10", "0")', '"10"');
126 shouldBe('stepUp("89", "10", "99")', '"90"');
127 debug('Huge value and small step');
128 shouldBe('input.min = ""; stepUp("1e+38", "1", "", 999)', '"1e+38"');
129 shouldBe('input.max = ""; stepDown("1e+38", "1", "", 999)', '"1e+38"');
130 debug('Fractional numbers');
131 shouldBe('input.min = ""; stepUp("0", "0.33333333333333333", "", 3)', '"1"');
132 shouldBe('stepUp("1", "0.1", "", 10)', '"2"');
133 shouldBe('input.min = "0"; stepUp("0", "0.003921568627450980", "1", 255)', '"1"');
134 debug('Rounding');
135 shouldBe('stepUp("5.005", "0.005", "", 2)', '"5.015"');
136 shouldBe('stepUp("5.005", "0.005", "", 11)', '"5.06"');
137 shouldBe('stepUp("5.005", "0.005", "", 12)', '"5.065"');
138 shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 2)', '"5.015"');
139 shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 11)', '"5.06"');
140 shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 12)', '"5.065"');
141 shouldBe('stepUpExplicitBounds(-4, 4, 1, "")', '"1"');
142 shouldBe('stepDownExplicitBounds(-4, 4, 1, "")', '"-1"');
143 shouldBe('stepDownExplicitBounds(0, 4, 1, "")', '"0"');
144 shouldBe('stepUpExplicitBounds(-4, 0, 1, "")', '"0"');
145 shouldBe('stepDownExplicitBounds(1, 4, 1, "")', '"1"');
146 shouldBe('stepUpExplicitBounds(1, 4, 1, "")', '"1"');
147 shouldBe('stepDownExplicitBounds(-4, -1, 1, "")', '"-1"');
148 shouldBe('stepUpExplicitBounds(-4, -1, 1, "")', '"-1"');
149 shouldBe('stepUpExplicitBounds(-100, null, 3, "")', '"2"');
150 shouldBe('stepDownExplicitBounds(-100, null, 3, "")', '"-1"');
151 shouldBe('stepUpExplicitBounds(1, 4, 1, 0)', '"1"');
152 shouldBe('stepDownExplicitBounds(1, 4, 1, 0)', '"0"');
153 shouldBe('stepDownExplicitBounds(-4, -1, 1, 0)', '"-1"');
154 shouldBe('stepUpExplicitBounds(-4, -1, 1, 0)', '"0"');
155 shouldBe('stepUpExplicitBounds(-100, null, 3, 3)', '"5"');
156 shouldBe('stepDownExplicitBounds(-100, null, 3, 3)', '"2"');
157
158 debug('');