| 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 (function() { | 5 (function() { |
| 6 function getFakePrefs() { | 6 function getFakePrefs() { |
| 7 return { | 7 return { |
| 8 cros: { | 8 cros: { |
| 9 system: { | 9 system: { |
| 10 timezone: { | 10 timezone: { |
| 11 key: 'cros.system.timezone', | 11 key: 'cros.system.timezone', |
| 12 type: chrome.settingsPrivate.PrefType.STRING, | 12 type: chrome.settingsPrivate.PrefType.STRING, |
| 13 value: 'Westeros/Kings_Landing', | 13 value: 'Westeros/Kings_Landing', |
| 14 }, | 14 }, |
| 15 }, | 15 }, |
| 16 flags: { |
| 17 // TODO(alemate): This test should be run for both values of this |
| 18 // option. |
| 19 per_user_timezone_enabled: { |
| 20 key: 'cros.flags.per_user_timezone_enabled', |
| 21 type: chrome.settingsPrivate.PrefType.BOOLEAN, |
| 22 value: true, |
| 23 }, |
| 24 }, |
| 16 }, | 25 }, |
| 17 settings: { | 26 settings: { |
| 18 clock: { | 27 clock: { |
| 19 use_24hour_clock: { | 28 use_24hour_clock: { |
| 20 key: 'settings.clock.use_24hour_clock', | 29 key: 'settings.clock.use_24hour_clock', |
| 21 type: chrome.settingsPrivate.PrefType.BOOLEAN, | 30 type: chrome.settingsPrivate.PrefType.BOOLEAN, |
| 22 value: false, | 31 value: false, |
| 23 }, | 32 }, |
| 24 }, | 33 }, |
| 25 resolve_timezone_by_geolocation: { | 34 resolve_timezone_by_geolocation: { |
| 26 key: 'settings.resolve_timezone_by_geolocation', | 35 key: 'settings.resolve_timezone_by_geolocation', |
| 27 type: chrome.settingsPrivate.PrefType.BOOLEAN, | 36 type: chrome.settingsPrivate.PrefType.BOOLEAN, |
| 28 value: true, | 37 value: true, |
| 29 }, | 38 }, |
| 39 timezone: { |
| 40 key: 'settings.timezone', |
| 41 type: chrome.settingsPrivate.PrefType.STRING, |
| 42 value: 'Westeros/Kings_Landing', |
| 43 }, |
| 30 }, | 44 }, |
| 31 }; | 45 }; |
| 32 } | 46 } |
| 33 | 47 |
| 48 function updatePrefsWithPolicy(prefs, managed, valueFromPolicy) { |
| 49 var prefsCopy = JSON.parse(JSON.stringify(prefs)); |
| 50 if (managed) { |
| 51 prefsCopy.settings.resolve_timezone_by_geolocation.controlledBy = |
| 52 chrome.settingsPrivate.ControlledBy.USER_POLICY; |
| 53 prefsCopy.settings.resolve_timezone_by_geolocation.enforcement = |
| 54 chrome.settingsPrivate.Enforcement.ENFORCED; |
| 55 prefsCopy.settings.resolve_timezone_by_geolocation.value = |
| 56 valueFromPolicy; |
| 57 prefsCopy.settings.timezone.controlledBy = |
| 58 chrome.settingsPrivate.ControlledBy.USER_POLICY; |
| 59 prefsCopy.settings.timezone.enforcement = |
| 60 chrome.settingsPrivate.Enforcement.ENFORCED; |
| 61 } else { |
| 62 prefsCopy.settings.resolve_timezone_by_geolocation.controlledBy = |
| 63 undefined; |
| 64 prefsCopy.settings.resolve_timezone_by_geolocation.enforcement = |
| 65 undefined; |
| 66 // Auto-resolve defaults to true. |
| 67 prefsCopy.settings.resolve_timezone_by_geolocation.value = true; |
| 68 prefsCopy.settings.timezone.controlledBy = undefined; |
| 69 prefsCopy.settings.timezone.enforcement = undefined; |
| 70 } |
| 71 return prefsCopy; |
| 72 } |
| 73 |
| 34 /** | 74 /** |
| 35 * Sets up fakes and creates the date time element. | 75 * Sets up fakes and creates the date time element. |
| 36 * @param {!Object} prefs | 76 * @param {!Object} prefs |
| 37 * @param {boolean} hasPolicy | 77 * @param {boolean} hasPolicy |
| 38 * @param {boolean=} opt_autoDetectPolicyValue | 78 * @param {boolean=} opt_autoDetectPolicyValue |
| 39 * @return {!SettingsDateTimePage} | 79 * @return {!SettingsDateTimePage} |
| 40 */ | 80 */ |
| 41 function initializeDateTime(prefs, hasPolicy, opt_autoDetectPolicyValue) { | 81 function initializeDateTime(prefs, hasPolicy, opt_autoDetectPolicyValue) { |
| 42 // Find the desired initial time zone by ID. | 82 // Find the desired initial time zone by ID. |
| 43 var timeZone = assert(fakeTimeZones.find(function(timeZonePair) { | 83 var timeZone = assert(fakeTimeZones.find(function(timeZonePair) { |
| 44 return timeZonePair[0] == prefs.cros.system.timezone.value; | 84 return timeZonePair[0] == prefs.cros.system.timezone.value; |
| 45 })); | 85 })); |
| 46 | 86 |
| 47 var data = { | 87 var data = { |
| 48 timeZoneID: timeZone[0], | 88 timeZoneID: timeZone[0], |
| 49 timeZoneName: timeZone[1], | 89 timeZoneName: timeZone[1], |
| 50 controlledSettingPolicy: 'This setting is enforced by your administrator', | 90 controlledSettingPolicy: 'This setting is enforced by your administrator', |
| 51 }; | 91 }; |
| 52 | 92 |
| 53 if (hasPolicy) | 93 if (hasPolicy) |
| 54 data.timeZoneAutoDetectValueFromPolicy = opt_autoDetectPolicyValue; | 94 data.timeZoneAutoDetectValueFromPolicy = opt_autoDetectPolicyValue; |
| 55 | 95 |
| 56 window.loadTimeData = new LoadTimeData; | 96 window.loadTimeData = new LoadTimeData; |
| 57 loadTimeData.data = data; | 97 loadTimeData.data = data; |
| 58 | 98 |
| 59 var dateTime = document.createElement('settings-date-time-page'); | 99 var dateTime = document.createElement('settings-date-time-page'); |
| 60 dateTime.prefs = prefs; | 100 dateTime.prefs = |
| 101 updatePrefsWithPolicy(prefs, hasPolicy, opt_autoDetectPolicyValue); |
| 61 CrSettingsPrefs.setInitialized(); | 102 CrSettingsPrefs.setInitialized(); |
| 62 | 103 |
| 63 document.body.appendChild(dateTime); | 104 document.body.appendChild(dateTime); |
| 105 cr.webUIListenerCallback( |
| 106 'time-zone-auto-detect-policy', hasPolicy, opt_autoDetectPolicyValue); |
| 64 return dateTime; | 107 return dateTime; |
| 65 } | 108 } |
| 66 | 109 |
| 67 // CrOS sends time zones as [id, friendly name] pairs. | 110 // CrOS sends time zones as [id, friendly name] pairs. |
| 68 var fakeTimeZones = [ | 111 var fakeTimeZones = [ |
| 69 ['Westeros/Highgarden', '(KNG-2:00) The Reach Time (Highgarden)'], | 112 ['Westeros/Highgarden', '(KNG-2:00) The Reach Time (Highgarden)'], |
| 70 ['Westeros/Winterfell', '(KNG-1:00) The North Time (Winterfell)'], | 113 ['Westeros/Winterfell', '(KNG-1:00) The North Time (Winterfell)'], |
| 71 ['Westeros/Kings_Landing', | 114 ['Westeros/Kings_Landing', |
| 72 '(KNG+0:00) Westeros Standard Time (King\'s Landing)'], | 115 '(KNG+0:00) Westeros Standard Time (King\'s Landing)'], |
| 73 ['Westeros/TheEyrie', '(KNG+1:00) The Vale Time (The Eyrie)'], | 116 ['Westeros/TheEyrie', '(KNG+1:00) The Vale Time (The Eyrie)'], |
| (...skipping 29 matching lines...) Expand all Loading... |
| 103 }); | 146 }); |
| 104 }); | 147 }); |
| 105 | 148 |
| 106 suiteTeardown(function() { | 149 suiteTeardown(function() { |
| 107 // TODO(michaelpg): Removes the element before exiting, because the | 150 // TODO(michaelpg): Removes the element before exiting, because the |
| 108 // <paper-tooltip> in <cr-policy-indicator> somehow causes warnings | 151 // <paper-tooltip> in <cr-policy-indicator> somehow causes warnings |
| 109 // and/or script errors in axs_testing.js. | 152 // and/or script errors in axs_testing.js. |
| 110 PolymerTest.clearBody(); | 153 PolymerTest.clearBody(); |
| 111 }); | 154 }); |
| 112 | 155 |
| 113 function verifyAutoDetectSetting(autoDetect) { | 156 function verifyAutoDetectSetting(autoDetect, managed) { |
| 114 assertEquals(autoDetect, dateTime.$$('settings-dropdown-menu').disabled); | 157 Polymer.dom.flush(); |
| 115 assertEquals(autoDetect, dateTime.$.timeZoneAutoDetect.checked); | 158 var selector = dateTime.$$('#userTimeZoneSelector'); |
| 159 var selectorHidden = selector ? selector.hidden : true; |
| 160 assertEquals(managed || autoDetect, selectorHidden); |
| 161 |
| 162 var checkButton = dateTime.$$('#timeZoneAutoDetect'); |
| 163 var checkButtonChecked = checkButton ? checkButton.checked : false; |
| 164 if (!managed) |
| 165 assertEquals(autoDetect, checkButtonChecked); |
| 116 } | 166 } |
| 117 | 167 |
| 118 function verifyPolicy(policy) { | 168 function verifyPolicy(policy) { |
| 119 Polymer.dom.flush(); | 169 Polymer.dom.flush(); |
| 120 var indicator = dateTime.$$('cr-policy-indicator'); | 170 var indicator = |
| 171 dateTime.$$('#timeZoneAutoDetect').$$('cr-policy-pref-indicator'); |
| 172 if (indicator && indicator.style.display == 'none') |
| 173 indicator = null; |
| 121 | 174 |
| 122 if (policy) { | 175 if (policy) { |
| 123 assertTrue(!!indicator); | 176 assertTrue(!!indicator); |
| 124 assertTrue(indicator.indicatorVisible); | 177 assertTrue(indicator.indicatorVisible); |
| 125 } else { | 178 } else { |
| 126 // Indicator should be missing dom-ifed out. | 179 // Indicator should be missing dom-ifed out. |
| 127 assertFalse(!!indicator); | 180 assertFalse(!!indicator); |
| 128 } | 181 } |
| 129 | 182 |
| 130 assertEquals(policy, dateTime.$.timeZoneAutoDetect.disabled); | 183 assertEquals( |
| 184 policy, dateTime.$$('#timeZoneAutoDetect').$$('#control').disabled); |
| 131 } | 185 } |
| 132 | 186 |
| 133 function verifyTimeZonesPopulated(populated) { | 187 function verifyTimeZonesPopulated(populated) { |
| 134 var dropdown = dateTime.$$('settings-dropdown-menu'); | 188 Polymer.dom.flush(); |
| 189 var userTimezoneDropdown = dateTime.$$('#userTimeZoneSelector'); |
| 190 var systemTimezoneDropdown = dateTime.$$('#systemTimezoneSelector'); |
| 191 |
| 192 var dropdown = |
| 193 userTimezoneDropdown ? userTimezoneDropdown : systemTimezoneDropdown; |
| 135 if (populated) | 194 if (populated) |
| 136 assertEquals(fakeTimeZones.length, dropdown.menuOptions.length); | 195 assertEquals(fakeTimeZones.length, dropdown.menuOptions.length); |
| 137 else | 196 else |
| 138 assertEquals(1, dropdown.menuOptions.length); | 197 assertEquals(1, dropdown.menuOptions.length); |
| 139 } | 198 } |
| 140 | 199 |
| 200 function updatePolicy(dateTime, managed, valueFromPolicy) { |
| 201 dateTime.prefs = |
| 202 updatePrefsWithPolicy(dateTime.prefs, managed, valueFromPolicy); |
| 203 cr.webUIListenerCallback( |
| 204 'time-zone-auto-detect-policy', managed, valueFromPolicy); |
| 205 Polymer.dom.flush(); |
| 206 } |
| 207 |
| 141 test('auto-detect on', function(done) { | 208 test('auto-detect on', function(done) { |
| 142 var prefs = getFakePrefs(); | 209 var prefs = getFakePrefs(); |
| 143 dateTime = initializeDateTime(prefs, false); | 210 dateTime = initializeDateTime(prefs, false); |
| 144 | 211 |
| 145 assertTrue(dateTimePageReadyCalled); | 212 assertTrue(dateTimePageReadyCalled); |
| 146 assertFalse(getTimeZonesCalled); | 213 assertFalse(getTimeZonesCalled); |
| 147 | 214 |
| 148 verifyAutoDetectSetting(true); | 215 verifyAutoDetectSetting(true, false); |
| 149 verifyTimeZonesPopulated(false); | 216 verifyTimeZonesPopulated(false); |
| 150 verifyPolicy(false); | 217 verifyPolicy(false); |
| 151 | 218 |
| 152 // Disable auto-detect. | 219 // Disable auto-detect. |
| 153 MockInteractions.tap(dateTime.$.timeZoneAutoDetect); | 220 MockInteractions.tap(dateTime.$$('#timeZoneAutoDetect').$$('#control')); |
| 154 verifyAutoDetectSetting(false); | 221 verifyAutoDetectSetting(false, false); |
| 155 assertTrue(getTimeZonesCalled); | 222 assertTrue(getTimeZonesCalled); |
| 156 | 223 |
| 157 setTimeout(function() { | 224 setTimeout(function() { |
| 158 verifyTimeZonesPopulated(true); | 225 verifyTimeZonesPopulated(true); |
| 159 done(); | 226 done(); |
| 160 }); | 227 }); |
| 161 }); | 228 }); |
| 162 | 229 |
| 163 test('auto-detect off', function(done) { | 230 test('auto-detect off', function(done) { |
| 164 var prefs = getFakePrefs(); | 231 dateTime = initializeDateTime(getFakePrefs(), false); |
| 165 prefs.settings.resolve_timezone_by_geolocation.value = false; | 232 dateTime.set( |
| 166 dateTime = initializeDateTime(prefs, false); | 233 'prefs.settings.resolve_timezone_by_geolocation.value', false); |
| 167 cr.webUIListenerCallback('time-zone-auto-detect-policy', false); | |
| 168 | 234 |
| 169 assertTrue(dateTimePageReadyCalled); | 235 assertTrue(dateTimePageReadyCalled); |
| 170 assertTrue(getTimeZonesCalled); | 236 assertTrue(getTimeZonesCalled); |
| 171 | 237 |
| 172 verifyAutoDetectSetting(false); | 238 verifyAutoDetectSetting(false, false); |
| 173 verifyPolicy(false); | 239 verifyPolicy(false); |
| 174 | 240 |
| 175 setTimeout(function() { | 241 setTimeout(function() { |
| 176 verifyTimeZonesPopulated(true); | 242 verifyTimeZonesPopulated(true); |
| 177 | 243 |
| 178 // Enable auto-detect. | 244 // Enable auto-detect. |
| 179 MockInteractions.tap(dateTime.$.timeZoneAutoDetect); | 245 MockInteractions.tap(dateTime.$$('#timeZoneAutoDetect').$$('#control')); |
| 180 verifyAutoDetectSetting(true); | 246 verifyAutoDetectSetting(true); |
| 181 done(); | 247 done(); |
| 182 }); | 248 }); |
| 183 }); | 249 }); |
| 184 | 250 |
| 185 test('auto-detect forced on', function(done) { | 251 test('auto-detect forced on', function(done) { |
| 186 var prefs = getFakePrefs(); | 252 var prefs = getFakePrefs(); |
| 187 prefs.settings.resolve_timezone_by_geolocation.value = false; | |
| 188 dateTime = initializeDateTime(prefs, true, true); | 253 dateTime = initializeDateTime(prefs, true, true); |
| 189 cr.webUIListenerCallback('time-zone-auto-detect-policy', true, true); | 254 dateTime.set( |
| 255 'prefs.settings.resolve_timezone_by_geolocation.value', false); |
| 190 | 256 |
| 191 assertTrue(dateTimePageReadyCalled); | 257 assertTrue(dateTimePageReadyCalled); |
| 192 assertFalse(getTimeZonesCalled); | 258 assertFalse(getTimeZonesCalled); |
| 193 | 259 |
| 194 verifyAutoDetectSetting(true); | 260 verifyAutoDetectSetting(true, true); |
| 195 verifyTimeZonesPopulated(false); | 261 verifyTimeZonesPopulated(false); |
| 196 verifyPolicy(true); | 262 verifyPolicy(true); |
| 197 | 263 |
| 198 // Cannot disable auto-detect. | 264 // Cannot disable auto-detect. |
| 199 MockInteractions.tap(dateTime.$.timeZoneAutoDetect); | 265 MockInteractions.tap(dateTime.$$('#timeZoneAutoDetect').$$('#control')); |
| 200 verifyAutoDetectSetting(true); | 266 verifyAutoDetectSetting(true, true); |
| 201 assertFalse(getTimeZonesCalled); | 267 assertFalse(getTimeZonesCalled); |
| 202 | 268 |
| 203 // Update the policy: force auto-detect off. | 269 // Update the policy: force auto-detect off. |
| 204 cr.webUIListenerCallback('time-zone-auto-detect-policy', true, false); | 270 updatePolicy(dateTime, true, false); |
| 205 verifyAutoDetectSetting(false); | 271 verifyAutoDetectSetting(false, true); |
| 206 verifyPolicy(true); | 272 verifyPolicy(true); |
| 207 | 273 |
| 208 assertTrue(getTimeZonesCalled); | 274 assertTrue(getTimeZonesCalled); |
| 209 setTimeout(function() { | 275 setTimeout(function() { |
| 210 verifyTimeZonesPopulated(true); | 276 verifyTimeZonesPopulated(true); |
| 211 done(); | 277 done(); |
| 212 }); | 278 }); |
| 213 }); | 279 }); |
| 214 | 280 |
| 215 test('auto-detect forced off', function(done) { | 281 test('auto-detect forced off', function(done) { |
| 216 var prefs = getFakePrefs(); | 282 var prefs = getFakePrefs(); |
| 217 dateTime = initializeDateTime(prefs, true, false); | 283 dateTime = initializeDateTime(prefs, true, false); |
| 218 cr.webUIListenerCallback('time-zone-auto-detect-policy', true, false); | |
| 219 | 284 |
| 220 assertTrue(dateTimePageReadyCalled); | 285 assertTrue(dateTimePageReadyCalled); |
| 221 assertTrue(getTimeZonesCalled); | 286 assertTrue(getTimeZonesCalled); |
| 222 | 287 |
| 223 verifyAutoDetectSetting(false); | 288 verifyAutoDetectSetting(false, true); |
| 224 verifyPolicy(true); | 289 verifyPolicy(true); |
| 225 | 290 |
| 226 setTimeout(function() { | 291 setTimeout(function() { |
| 227 verifyTimeZonesPopulated(true); | 292 verifyTimeZonesPopulated(true); |
| 228 | 293 |
| 229 // Remove the policy so user's preference takes effect. | 294 // Remove the policy so user's preference takes effect. |
| 230 cr.webUIListenerCallback('time-zone-auto-detect-policy', false); | 295 updatePolicy(dateTime, false); |
| 231 verifyAutoDetectSetting(true); | 296 verifyAutoDetectSetting(true, false); |
| 232 verifyPolicy(false); | 297 verifyPolicy(false); |
| 233 | 298 |
| 234 // User can disable auto-detect. | 299 // User can disable auto-detect. |
| 235 MockInteractions.tap(dateTime.$.timeZoneAutoDetect); | 300 MockInteractions.tap(dateTime.$$('#timeZoneAutoDetect').$$('#control')); |
| 236 verifyAutoDetectSetting(false); | 301 verifyAutoDetectSetting(false, false); |
| 237 done(); | 302 done(); |
| 238 }); | 303 }); |
| 239 }); | 304 }); |
| 240 | 305 |
| 241 test('set date and time button', function() { | 306 test('set date and time button', function() { |
| 242 dateTime = initializeDateTime(getFakePrefs(), false); | 307 dateTime = initializeDateTime(getFakePrefs(), false); |
| 243 cr.webUIListenerCallback('time-zone-auto-detect-policy', false); | |
| 244 | 308 |
| 245 var showSetDateTimeUICalled = false; | 309 var showSetDateTimeUICalled = false; |
| 246 registerMessageCallback('showSetDateTimeUI', null, function() { | 310 registerMessageCallback('showSetDateTimeUI', null, function() { |
| 247 assertFalse(showSetDateTimeUICalled); | 311 assertFalse(showSetDateTimeUICalled); |
| 248 showSetDateTimeUICalled = true; | 312 showSetDateTimeUICalled = true; |
| 249 }); | 313 }); |
| 250 | 314 |
| 251 var setDateTimeButton = dateTime.$.setDateTime; | 315 var setDateTimeButton = dateTime.$$('#setDateTime'); |
| 252 assertEquals(0, setDateTimeButton.offsetHeight); | 316 assertEquals(0, setDateTimeButton.offsetHeight); |
| 253 | 317 |
| 254 // Make the date and time editable. | 318 // Make the date and time editable. |
| 255 cr.webUIListenerCallback('can-set-date-time-changed', true); | 319 cr.webUIListenerCallback('can-set-date-time-changed', true); |
| 256 assertGT(setDateTimeButton.offsetHeight, 0); | 320 assertGT(setDateTimeButton.offsetHeight, 0); |
| 257 | 321 |
| 258 assertFalse(showSetDateTimeUICalled); | 322 assertFalse(showSetDateTimeUICalled); |
| 259 MockInteractions.tap(setDateTimeButton); | 323 MockInteractions.tap(setDateTimeButton); |
| 260 assertTrue(showSetDateTimeUICalled); | 324 assertTrue(showSetDateTimeUICalled); |
| 261 | 325 |
| 262 // Make the date and time not editable. | 326 // Make the date and time not editable. |
| 263 cr.webUIListenerCallback('can-set-date-time-changed', false); | 327 cr.webUIListenerCallback('can-set-date-time-changed', false); |
| 264 assertEquals(setDateTimeButton.offsetHeight, 0); | 328 assertEquals(setDateTimeButton.offsetHeight, 0); |
| 265 }); | 329 }); |
| 266 }); | 330 }); |
| 267 })(); | 331 })(); |
| OLD | NEW |