Debian Codegraph @

closure-library/closure/goog/ui/rangemodel_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.ui.RangeModelTest');
16 goog.setTestOnly('goog.ui.RangeModelTest');
17
18 goog.require('goog.testing.jsunit');
19 goog.require('goog.ui.RangeModel');
20
21 function reset(rm, step) {
22 rm.setStep(step || 1);
23 rm.setExtent(0);
24 rm.setMinimum(0);
25 rm.setMaximum(100);
26 rm.setValue(0);
27 }
28
29 function getDescriptiveString(rm) {
30 return rm.getMinimum() + ' < ' + rm.getValue() + '[' + rm.getExtent() +
31 '] < ' + rm.getMaximum();
32 }
33
34 function testValue() {
35 var rm = new goog.ui.RangeModel;
36
37 assertEquals(0, rm.getValue());
38 rm.setValue(50);
39 assertEquals(50, rm.getValue());
40
41 // setting smaller than min should keep min
42 rm.setValue(-1);
43 assertEquals(0, rm.getValue());
44
45 // setting larger than max should keep max - extent
46 rm.setValue(101);
47 assertEquals(100, rm.getValue());
48 rm.setValue(0);
49 rm.setExtent(10);
50 rm.setValue(100);
51 assertEquals(90, rm.getValue());
52
53 ////////////////
54 // Change step
55 reset(rm, 3);
56
57 assertEquals(0, rm.getValue());
58 rm.setValue(50);
59 assertEquals(51, rm.getValue());
60
61 // setting smaller than min should keep min
62 rm.setValue(-1);
63 assertEquals(0, rm.getValue());
64
65 // setting larger than max should keep max - extent
66 rm.setValue(101);
67 assertEquals(99, rm.getValue());
68 rm.setValue(0);
69 rm.setExtent(10);
70 rm.setValue(100);
71 assertEquals(90, rm.getValue());
72 }
73
74 function testMinium() {
75 var rm = new goog.ui.RangeModel;
76
77 rm.setValue(50);
78 rm.setMinimum(10);
79 assertEquals(10, rm.getMinimum());
80
81 reset(rm);
82
83 // setting larger than value should change value
84 rm.setMinimum(10);
85 assertEquals(10, rm.getMinimum());
86 assertEquals(10, rm.getValue());
87
88 // setting larger than max should set max = min
89 rm.setMinimum(200);
90 assertEquals(200, rm.getMinimum());
91 assertEquals(200, rm.getValue());
92 assertEquals(200, rm.getMaximum());
93 assertEquals(0, rm.getExtent());
94
95 reset(rm);
96
97 // should change extent
98 rm.setExtent(10);
99 rm.setMinimum(95);
100 assertEquals(95, rm.getMinimum());
101 assertEquals(95, rm.getValue());
102 assertEquals(100, rm.getMaximum());
103 assertEquals(5, rm.getExtent());
104
105 ////////////////
106 // Change step
107 reset(rm, 3);
108
109 rm.setValue(50);
110 rm.setMinimum(10);
111 assertEquals(10, rm.getMinimum());
112
113 reset(rm, 3);
114
115 // setting larger than value should change value
116 rm.setMinimum(10);
117 assertEquals(10, rm.getMinimum());
118 assertEquals(10, rm.getValue());
119
120 // setting larger than max should set max = min
121 rm.setMinimum(200);
122 assertEquals(200, rm.getMinimum());
123 assertEquals(200, rm.getValue());
124 assertEquals(200, rm.getMaximum());
125 assertEquals(0, rm.getExtent());
126
127 reset(rm, 3);
128
129 // should change extent
130 rm.setExtent(10); // 0 < 0[9] < 99
131 rm.setMinimum(95); // 95 < 95[3] < 98
132 assertEquals(95, rm.getMinimum());
133 assertEquals(95, rm.getValue());
134 assertEquals(98, rm.getMaximum());
135 assertEquals(3, rm.getExtent());
136 }
137
138 function testMaximum() {
139 var rm = new goog.ui.RangeModel;
140
141 rm.setMaximum(50);
142 assertEquals(50, rm.getMaximum());
143
144 reset(rm);
145
146 // setting to smaller than minimum should change minimum, value and extent
147 rm.setValue(5);
148 rm.setExtent(10);
149 rm.setMinimum(50);
150 rm.setMaximum(40);
151 assertEquals(40, rm.getMaximum());
152 assertEquals(0, rm.getExtent());
153 assertEquals(40, rm.getValue());
154 assertEquals(40, rm.getMinimum());
155
156 reset(rm);
157
158 // setting smaller than value should change value to max - extent
159 rm.setExtent(10);
160 rm.setValue(50);
161 rm.setMaximum(40);
162 assertEquals(40, rm.getMaximum());
163 assertEquals(30, rm.getValue());
164
165 reset(rm);
166
167 // should change value, and keep extent constant,
168 // unless extent is > max - min.
169 rm.setExtent(10);
170 rm.setValue(90);
171 rm.setMaximum(95);
172 assertEquals(95, rm.getMaximum());
173 assertEquals(10, rm.getExtent());
174 assertEquals(85, rm.getValue());
175
176 ////////////////
177 // Change step
178 reset(rm, 3);
179
180 rm.setMaximum(50); // 0 < 0[0] < 51
181 assertEquals(51, rm.getMaximum());
182
183 reset(rm, 3);
184
185 // setting to smaller than minimum should change minimum, value and extent
186 rm.setValue(5); // 0 < 6[0] < 99
187 rm.setExtent(10); // 0 < 6[9] < 99
188 rm.setMinimum(50); // 50 < 50[9] < 98
189 rm.setMaximum(40); // 41 < 41[0] < 41
190 assertEquals(41, rm.getMaximum());
191 assertEquals(0, rm.getExtent());
192 assertEquals(41, rm.getValue());
193 assertEquals(41, rm.getMinimum());
194
195 reset(rm, 3);
196
197 // setting smaller than value should change value to max - extent
198 rm.setExtent(10); // 0 < 0[9] < 99
199 rm.setValue(50); // 0 < 51[9] < 99
200 rm.setMaximum(40); // 0 < 30[9] < 39
201 assertEquals(39, rm.getMaximum());
202 assertEquals(30, rm.getValue());
203
204 reset(rm, 3);
205
206 // should change value, and keep extent constant,
207 // unless extent is > max - min.
208 rm.setExtent(10); // 0 < 0[9] < 99
209 rm.setValue(90); // 0 < 90[9] < 99
210 rm.setMaximum(95); // 0 < 90[6] < 96
211 assertEquals(96, rm.getMaximum());
212 assertEquals(87, rm.getValue());
213 assertEquals(9, rm.getExtent());
214 }
215
216 function testExtent() {
217 var rm = new goog.ui.RangeModel;
218
219 rm.setExtent(10);
220 assertEquals(10, rm.getExtent());
221
222 rm.setExtent(-10);
223 assertEquals(0, rm.getExtent());
224
225 rm.setValue(50);
226 rm.setExtent(100);
227 assertEquals(50, rm.getExtent());
228 assertEquals(50, rm.getValue());
229
230 ////////////////
231 // Change step
232 reset(rm, 3);
233
234 rm.setExtent(10); // 0 < 0[9] < 99
235 assertEquals(9, rm.getExtent());
236
237 rm.setExtent(-10);
238 assertEquals(0, rm.getExtent());
239
240 rm.setValue(50); // 0 < 51[9] < 99
241 rm.setExtent(100); // 0 < 51[48] < 99
242 assertEquals(48, rm.getExtent());
243 assertEquals(51, rm.getValue());
244 }
245
246 function testRoundToStep() {
247 var rm = new goog.ui.RangeModel;
248 rm.setStep(0.5);
249
250 assertEquals(1, rm.roundToStep(1));
251 assertEquals(0.5, rm.roundToStep(0.5));
252 assertEquals(1, rm.roundToStep(0.75));
253 assertEquals(0.5, rm.roundToStep(0.74));
254 }
255
256 function testRoundToStepWithMin() {
257 var rm = new goog.ui.RangeModel;
258 rm.setStep(0.5);
259 rm.setMinimum(0.25);
260
261 assertEquals(1.25, rm.roundToStepWithMin(1));
262 assertEquals(0.75, rm.roundToStepWithMin(0.5));
263 assertEquals(0.75, rm.roundToStepWithMin(0.75));
264 assertEquals(0.75, rm.roundToStepWithMin(0.74));
265 }