rangemodel_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. goog.provide('goog.ui.RangeModelTest');
  15. goog.setTestOnly('goog.ui.RangeModelTest');
  16. goog.require('goog.testing.jsunit');
  17. goog.require('goog.ui.RangeModel');
  18. function reset(rm, step) {
  19. rm.setStep(step || 1);
  20. rm.setExtent(0);
  21. rm.setMinimum(0);
  22. rm.setMaximum(100);
  23. rm.setValue(0);
  24. }
  25. function getDescriptiveString(rm) {
  26. return rm.getMinimum() + ' < ' + rm.getValue() + '[' + rm.getExtent() +
  27. '] < ' + rm.getMaximum();
  28. }
  29. function testValue() {
  30. var rm = new goog.ui.RangeModel;
  31. assertEquals(0, rm.getValue());
  32. rm.setValue(50);
  33. assertEquals(50, rm.getValue());
  34. // setting smaller than min should keep min
  35. rm.setValue(-1);
  36. assertEquals(0, rm.getValue());
  37. // setting larger than max should keep max - extent
  38. rm.setValue(101);
  39. assertEquals(100, rm.getValue());
  40. rm.setValue(0);
  41. rm.setExtent(10);
  42. rm.setValue(100);
  43. assertEquals(90, rm.getValue());
  44. ////////////////
  45. // Change step
  46. reset(rm, 3);
  47. assertEquals(0, rm.getValue());
  48. rm.setValue(50);
  49. assertEquals(51, rm.getValue());
  50. // setting smaller than min should keep min
  51. rm.setValue(-1);
  52. assertEquals(0, rm.getValue());
  53. // setting larger than max should keep max - extent
  54. rm.setValue(101);
  55. assertEquals(99, rm.getValue());
  56. rm.setValue(0);
  57. rm.setExtent(10);
  58. rm.setValue(100);
  59. assertEquals(90, rm.getValue());
  60. }
  61. function testMinium() {
  62. var rm = new goog.ui.RangeModel;
  63. rm.setValue(50);
  64. rm.setMinimum(10);
  65. assertEquals(10, rm.getMinimum());
  66. reset(rm);
  67. // setting larger than value should change value
  68. rm.setMinimum(10);
  69. assertEquals(10, rm.getMinimum());
  70. assertEquals(10, rm.getValue());
  71. // setting larger than max should set max = min
  72. rm.setMinimum(200);
  73. assertEquals(200, rm.getMinimum());
  74. assertEquals(200, rm.getValue());
  75. assertEquals(200, rm.getMaximum());
  76. assertEquals(0, rm.getExtent());
  77. reset(rm);
  78. // should change extent
  79. rm.setExtent(10);
  80. rm.setMinimum(95);
  81. assertEquals(95, rm.getMinimum());
  82. assertEquals(95, rm.getValue());
  83. assertEquals(100, rm.getMaximum());
  84. assertEquals(5, rm.getExtent());
  85. ////////////////
  86. // Change step
  87. reset(rm, 3);
  88. rm.setValue(50);
  89. rm.setMinimum(10);
  90. assertEquals(10, rm.getMinimum());
  91. reset(rm, 3);
  92. // setting larger than value should change value
  93. rm.setMinimum(10);
  94. assertEquals(10, rm.getMinimum());
  95. assertEquals(10, rm.getValue());
  96. // setting larger than max should set max = min
  97. rm.setMinimum(200);
  98. assertEquals(200, rm.getMinimum());
  99. assertEquals(200, rm.getValue());
  100. assertEquals(200, rm.getMaximum());
  101. assertEquals(0, rm.getExtent());
  102. reset(rm, 3);
  103. // should change extent
  104. rm.setExtent(10); // 0 < 0[9] < 99
  105. rm.setMinimum(95); // 95 < 95[3] < 98
  106. assertEquals(95, rm.getMinimum());
  107. assertEquals(95, rm.getValue());
  108. assertEquals(98, rm.getMaximum());
  109. assertEquals(3, rm.getExtent());
  110. }
  111. function testMaximum() {
  112. var rm = new goog.ui.RangeModel;
  113. rm.setMaximum(50);
  114. assertEquals(50, rm.getMaximum());
  115. reset(rm);
  116. // setting to smaller than minimum should change minimum, value and extent
  117. rm.setValue(5);
  118. rm.setExtent(10);
  119. rm.setMinimum(50);
  120. rm.setMaximum(40);
  121. assertEquals(40, rm.getMaximum());
  122. assertEquals(0, rm.getExtent());
  123. assertEquals(40, rm.getValue());
  124. assertEquals(40, rm.getMinimum());
  125. reset(rm);
  126. // setting smaller than value should change value to max - extent
  127. rm.setExtent(10);
  128. rm.setValue(50);
  129. rm.setMaximum(40);
  130. assertEquals(40, rm.getMaximum());
  131. assertEquals(30, rm.getValue());
  132. reset(rm);
  133. // should change value, and keep extent constant,
  134. // unless extent is > max - min.
  135. rm.setExtent(10);
  136. rm.setValue(90);
  137. rm.setMaximum(95);
  138. assertEquals(95, rm.getMaximum());
  139. assertEquals(10, rm.getExtent());
  140. assertEquals(85, rm.getValue());
  141. ////////////////
  142. // Change step
  143. reset(rm, 3);
  144. rm.setMaximum(50); // 0 < 0[0] < 51
  145. assertEquals(51, rm.getMaximum());
  146. reset(rm, 3);
  147. // setting to smaller than minimum should change minimum, value and extent
  148. rm.setValue(5); // 0 < 6[0] < 99
  149. rm.setExtent(10); // 0 < 6[9] < 99
  150. rm.setMinimum(50); // 50 < 50[9] < 98
  151. rm.setMaximum(40); // 41 < 41[0] < 41
  152. assertEquals(41, rm.getMaximum());
  153. assertEquals(0, rm.getExtent());
  154. assertEquals(41, rm.getValue());
  155. assertEquals(41, rm.getMinimum());
  156. reset(rm, 3);
  157. // setting smaller than value should change value to max - extent
  158. rm.setExtent(10); // 0 < 0[9] < 99
  159. rm.setValue(50); // 0 < 51[9] < 99
  160. rm.setMaximum(40); // 0 < 30[9] < 39
  161. assertEquals(39, rm.getMaximum());
  162. assertEquals(30, rm.getValue());
  163. reset(rm, 3);
  164. // should change value, and keep extent constant,
  165. // unless extent is > max - min.
  166. rm.setExtent(10); // 0 < 0[9] < 99
  167. rm.setValue(90); // 0 < 90[9] < 99
  168. rm.setMaximum(95); // 0 < 90[6] < 96
  169. assertEquals(96, rm.getMaximum());
  170. assertEquals(87, rm.getValue());
  171. assertEquals(9, rm.getExtent());
  172. }
  173. function testExtent() {
  174. var rm = new goog.ui.RangeModel;
  175. rm.setExtent(10);
  176. assertEquals(10, rm.getExtent());
  177. rm.setExtent(-10);
  178. assertEquals(0, rm.getExtent());
  179. rm.setValue(50);
  180. rm.setExtent(100);
  181. assertEquals(50, rm.getExtent());
  182. assertEquals(50, rm.getValue());
  183. ////////////////
  184. // Change step
  185. reset(rm, 3);
  186. rm.setExtent(10); // 0 < 0[9] < 99
  187. assertEquals(9, rm.getExtent());
  188. rm.setExtent(-10);
  189. assertEquals(0, rm.getExtent());
  190. rm.setValue(50); // 0 < 51[9] < 99
  191. rm.setExtent(100); // 0 < 51[48] < 99
  192. assertEquals(48, rm.getExtent());
  193. assertEquals(51, rm.getValue());
  194. }
  195. function testRoundToStep() {
  196. var rm = new goog.ui.RangeModel;
  197. rm.setStep(0.5);
  198. assertEquals(1, rm.roundToStep(1));
  199. assertEquals(0.5, rm.roundToStep(0.5));
  200. assertEquals(1, rm.roundToStep(0.75));
  201. assertEquals(0.5, rm.roundToStep(0.74));
  202. }
  203. function testRoundToStepWithMin() {
  204. var rm = new goog.ui.RangeModel;
  205. rm.setStep(0.5);
  206. rm.setMinimum(0.25);
  207. assertEquals(1.25, rm.roundToStepWithMin(1));
  208. assertEquals(0.75, rm.roundToStepWithMin(0.5));
  209. assertEquals(0.75, rm.roundToStepWithMin(0.75));
  210. assertEquals(0.75, rm.roundToStepWithMin(0.74));
  211. }