OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** @fileoverview Suite of tests for settings-slider. */ | 5 /** @fileoverview Suite of tests for settings-slider. */ |
6 cr.define('settings_slider', function() { | 6 suite('SettingsSlider', function() { |
7 function registerTests() { | 7 /** @type {!CrSliderElement} */ |
8 suite('SettingsSlider', function() { | 8 var slider; |
9 /** @type {!CrSliderElement} */ | |
10 var slider; | |
11 | 9 |
12 /** | 10 /** |
13 * paper-slider instance wrapped by settings-slider. | 11 * paper-slider instance wrapped by settings-slider. |
14 * @type {!PaperSliderElement} | 12 * @type {!PaperSliderElement} |
15 */ | 13 */ |
16 var paperSlider; | 14 var paperSlider; |
17 | 15 |
18 var tickValues = [2, 4, 8, 16, 32, 64, 128]; | 16 var tickValues = [2, 4, 8, 16, 32, 64, 128]; |
19 | 17 |
20 setup(function() { | 18 setup(function() { |
21 PolymerTest.clearBody(); | 19 PolymerTest.clearBody(); |
22 slider = document.createElement('settings-slider'); | 20 slider = document.createElement('settings-slider'); |
23 slider.pref = { | 21 slider.pref = { |
24 type: chrome.settingsPrivate.PrefType.NUMBER, | 22 type: chrome.settingsPrivate.PrefType.NUMBER, |
25 value: 16, | 23 value: 16, |
26 }; | 24 }; |
27 document.body.appendChild(slider); | 25 document.body.appendChild(slider); |
28 paperSlider = slider.$$('paper-slider'); | 26 paperSlider = slider.$$('paper-slider'); |
29 }); | 27 }); |
30 | 28 |
31 test('enforce value', function() { | 29 test('enforce value', function() { |
32 // Test that the indicator is not present until after the pref is | 30 // Test that the indicator is not present until after the pref is |
33 // enforced. | 31 // enforced. |
34 indicator = slider.$$('cr-policy-pref-indicator'); | 32 indicator = slider.$$('cr-policy-pref-indicator'); |
35 assertFalse(!!indicator); | 33 assertFalse(!!indicator); |
36 slider.pref = { | 34 slider.pref = { |
37 controlledBy: chrome.settingsPrivate.ControlledBy.DEVICE_POLICY, | 35 controlledBy: chrome.settingsPrivate.ControlledBy.DEVICE_POLICY, |
38 enforcement: chrome.settingsPrivate.Enforcement.ENFORCED, | 36 enforcement: chrome.settingsPrivate.Enforcement.ENFORCED, |
39 type: chrome.settingsPrivate.PrefType.NUMBER, | 37 type: chrome.settingsPrivate.PrefType.NUMBER, |
40 value: 16, | 38 value: 16, |
41 }; | 39 }; |
42 Polymer.dom.flush(); | 40 Polymer.dom.flush(); |
43 indicator = slider.$$('cr-policy-pref-indicator'); | 41 indicator = slider.$$('cr-policy-pref-indicator'); |
44 assertTrue(!!indicator); | 42 assertTrue(!!indicator); |
45 }); | 43 }); |
46 | 44 |
47 test('set value', function() { | 45 test('set value', function() { |
48 slider.tickValues = tickValues; | 46 slider.tickValues = tickValues; |
49 slider.set('pref.value', 16); | 47 slider.set('pref.value', 16); |
50 expectEquals(6, paperSlider.max); | 48 expectEquals(6, paperSlider.max); |
51 expectEquals(3, paperSlider.value); | 49 expectEquals(3, paperSlider.value); |
52 expectEquals(3, paperSlider.immediateValue); | 50 expectEquals(3, paperSlider.immediateValue); |
53 | 51 |
54 // settings-slider only supports snapping to a range of tick values. | 52 // settings-slider only supports snapping to a range of tick values. |
55 // Setting to an in-between value should snap to an indexed value. | 53 // Setting to an in-between value should snap to an indexed value. |
56 slider.set('pref.value', 70); | 54 slider.set('pref.value', 70); |
57 expectEquals(5, paperSlider.value); | 55 expectEquals(5, paperSlider.value); |
58 expectEquals(5, paperSlider.immediateValue); | 56 expectEquals(5, paperSlider.immediateValue); |
59 expectEquals(64, slider.pref.value); | 57 expectEquals(64, slider.pref.value); |
60 | 58 |
61 // Setting the value out-of-range should clamp the slider. | 59 // Setting the value out-of-range should clamp the slider. |
62 slider.set('pref.value', -100); | 60 slider.set('pref.value', -100); |
63 expectEquals(0, paperSlider.value); | 61 expectEquals(0, paperSlider.value); |
64 expectEquals(0, paperSlider.immediateValue); | 62 expectEquals(0, paperSlider.immediateValue); |
65 expectEquals(2, slider.pref.value); | 63 expectEquals(2, slider.pref.value); |
66 }); | 64 }); |
67 | 65 |
68 test('move slider', function() { | 66 test('move slider', function() { |
69 slider.tickValues = tickValues; | 67 slider.tickValues = tickValues; |
70 slider.set('pref.value', 30); | 68 slider.set('pref.value', 30); |
71 expectEquals(4, paperSlider.value); | 69 expectEquals(4, paperSlider.value); |
72 | 70 |
73 MockInteractions.pressAndReleaseKeyOn( | 71 MockInteractions.pressAndReleaseKeyOn(paperSlider, 39 /* right */); |
74 paperSlider, 39 /* right */); | 72 expectEquals(5, paperSlider.value); |
75 expectEquals(5, paperSlider.value); | 73 expectEquals(64, slider.pref.value); |
76 expectEquals(64, slider.pref.value); | |
77 | 74 |
78 MockInteractions.pressAndReleaseKeyOn( | 75 MockInteractions.pressAndReleaseKeyOn(paperSlider, 39 /* right */); |
79 paperSlider, 39 /* right */); | 76 expectEquals(6, paperSlider.value); |
80 expectEquals(6, paperSlider.value); | 77 expectEquals(128, slider.pref.value); |
81 expectEquals(128, slider.pref.value); | |
82 | 78 |
83 MockInteractions.pressAndReleaseKeyOn( | 79 MockInteractions.pressAndReleaseKeyOn(paperSlider, 39 /* right */); |
84 paperSlider, 39 /* right */); | 80 expectEquals(6, paperSlider.value); |
85 expectEquals(6, paperSlider.value); | 81 expectEquals(128, slider.pref.value); |
86 expectEquals(128, slider.pref.value); | |
87 | 82 |
88 MockInteractions.pressAndReleaseKeyOn( | 83 MockInteractions.pressAndReleaseKeyOn(paperSlider, 37 /* left */); |
89 paperSlider, 37 /* left */); | 84 expectEquals(5, paperSlider.value); |
90 expectEquals(5, paperSlider.value); | 85 expectEquals(64, slider.pref.value); |
91 expectEquals(64, slider.pref.value); | 86 }); |
92 }); | |
93 | 87 |
94 test('findNearestIndex_', function() { | 88 test('findNearestIndex_', function() { |
95 var slider = document.createElement('settings-slider'); | 89 var slider = document.createElement('settings-slider'); |
96 var testArray = [80, 20, 350, 1000, 200, 100]; | 90 var testArray = [80, 20, 350, 1000, 200, 100]; |
97 var testFindNearestIndex = function(expectedIndex, value) { | 91 var testFindNearestIndex = function(expectedIndex, value) { |
98 expectEquals( | 92 expectEquals(expectedIndex, slider.findNearestIndex_(testArray, value)); |
99 expectedIndex, slider.findNearestIndex_(testArray, value)); | 93 }; |
100 }; | 94 testFindNearestIndex(0, 51); |
101 testFindNearestIndex(0, 51); | 95 testFindNearestIndex(0, 80); |
102 testFindNearestIndex(0, 80); | 96 testFindNearestIndex(0, 89); |
103 testFindNearestIndex(0, 89); | 97 testFindNearestIndex(1, -100); |
104 testFindNearestIndex(1, -100); | 98 testFindNearestIndex(1, 20); |
105 testFindNearestIndex(1, 20); | 99 testFindNearestIndex(1, 49); |
106 testFindNearestIndex(1, 49); | 100 testFindNearestIndex(2, 400); |
107 testFindNearestIndex(2, 400); | 101 testFindNearestIndex(2, 350); |
108 testFindNearestIndex(2, 350); | 102 testFindNearestIndex(2, 300); |
109 testFindNearestIndex(2, 300); | 103 testFindNearestIndex(3, 200000); |
110 testFindNearestIndex(3, 200000); | 104 testFindNearestIndex(3, 1000); |
111 testFindNearestIndex(3, 1000); | 105 testFindNearestIndex(3, 700); |
112 testFindNearestIndex(3, 700); | 106 testFindNearestIndex(4, 220); |
113 testFindNearestIndex(4, 220); | 107 testFindNearestIndex(4, 200); |
114 testFindNearestIndex(4, 200); | 108 testFindNearestIndex(4, 151); |
115 testFindNearestIndex(4, 151); | 109 testFindNearestIndex(5, 149); |
116 testFindNearestIndex(5, 149); | 110 testFindNearestIndex(5, 100); |
117 testFindNearestIndex(5, 100); | 111 testFindNearestIndex(5, 91); |
118 testFindNearestIndex(5, 91); | 112 }); |
119 }); | |
120 }); | |
121 } | |
122 | 113 |
123 return { | 114 test('scaled slider', function() { |
124 registerTests: registerTests, | 115 slider.scale = 10; |
125 }; | 116 slider.set('pref.value', 2); |
| 117 expectEquals(20, paperSlider.value); |
| 118 |
| 119 MockInteractions.pressAndReleaseKeyOn(paperSlider, 39 /* right */); |
| 120 expectEquals(21, paperSlider.value); |
| 121 expectEquals(2.1, slider.pref.value); |
| 122 |
| 123 MockInteractions.pressAndReleaseKeyOn(paperSlider, 39 /* right */); |
| 124 expectEquals(22, paperSlider.value); |
| 125 expectEquals(2.2, slider.pref.value); |
| 126 }); |
126 }); | 127 }); |
OLD | NEW |