Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 cr-settings-prefs. */ | 5 /** @fileoverview Suite of tests for cr-settings-prefs. */ |
| 6 cr.define('cr_settings_prefs', function() { | 6 cr.define('cr_settings_prefs', function() { |
| 7 /** | 7 /** |
| 8 * Creates a deep copy of the object. | 8 * Creates a deep copy of the object. |
| 9 * @param {!Object} obj | 9 * @param {!Object} obj |
| 10 * @return {!Object} | 10 * @return {!Object} |
| 11 */ | 11 */ |
| 12 function deepCopy(obj) { | 12 function deepCopy(obj) { |
| 13 return JSON.parse(JSON.stringify(obj)); | 13 return JSON.parse(JSON.stringify(obj)); |
| 14 } | 14 } |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Mock of chrome.settingsPrivate API. | 17 * Mock of chrome.settingsPrivate API. |
| 18 * @constructor | 18 * @constructor |
| 19 * @extends {chrome.settingsPrivate} | 19 * @extends {chrome.settingsPrivate} |
| 20 */ | 20 */ |
| 21 function MockSettingsApi() { | 21 function MockSettingsApi() { |
| 22 this.prefs = {}; | 22 this.prefs = {}; |
| 23 this.listener_ = null; | |
| 24 | 23 |
| 25 // Hack alert: bind this instance's onPrefsChanged members to this. | 24 // Hack alert: bind this instance's onPrefsChanged members to this. |
| 26 this.onPrefsChanged = { | 25 this.onPrefsChanged = { |
| 27 addListener: this.onPrefsChanged.addListener.bind(this), | 26 addListener: this.onPrefsChanged.addListener.bind(this), |
| 28 removeListener: this.onPrefsChanged.removeListener.bind(this), | 27 removeListener: this.onPrefsChanged.removeListener.bind(this), |
| 29 }; | 28 }; |
| 30 | 29 |
| 31 for (var testCase of prefsTestCases) | 30 for (var testCase of prefsTestCases) |
| 32 this.addPref_(testCase.type, testCase.key, testCase.values[0]); | 31 this.addPref_(testCase.type, testCase.key, testCase.values[0]); |
| 33 } | 32 } |
| 34 | 33 |
| 34 // Make the listener static because it refers to a singleton. | |
| 35 MockSettingsApi.listener_ = null; | |
| 36 | |
| 35 MockSettingsApi.prototype = { | 37 MockSettingsApi.prototype = { |
| 36 // chrome.settingsPrivate overrides. | 38 // chrome.settingsPrivate overrides. |
| 37 onPrefsChanged: { | 39 onPrefsChanged: { |
| 38 addListener: function(listener) { | 40 addListener: function(listener) { |
| 39 this.listener_ = listener; | 41 MockSettingsApi.listener_ = listener; |
| 40 }, | 42 }, |
| 41 | 43 |
| 42 removeListener: function(listener) { | 44 removeListener: function(listener) { |
| 43 expectNotEquals(null, this.listener_); | 45 assertNotEquals(null, MockSettingsApi.listener_); |
|
michaelpg
2015/09/20 21:01:56
Switched expects to asserts because test_api.js ma
| |
| 44 this.listener_ = null; | 46 MockSettingsApi.listener_ = null; |
| 45 }, | 47 }, |
| 46 }, | 48 }, |
| 47 | 49 |
| 48 getAllPrefs: function(callback) { | 50 getAllPrefs: function(callback) { |
| 49 // Send a copy of prefs to keep our internal state private. | 51 // Send a copy of prefs to keep our internal state private. |
| 50 var prefs = []; | 52 var prefs = []; |
| 51 for (var key in this.prefs) | 53 for (var key in this.prefs) |
| 52 prefs.push(deepCopy(this.prefs[key])); | 54 prefs.push(deepCopy(this.prefs[key])); |
| 53 | 55 |
| 54 callback(prefs); | 56 // Run the callback asynchronously to test that the prefs aren't actually |
| 57 // used before they become available. | |
| 58 setTimeout(callback.bind(null, prefs)); | |
| 55 }, | 59 }, |
| 56 | 60 |
| 57 setPref: function(key, value, pageId, callback) { | 61 setPref: function(key, value, pageId, callback) { |
| 58 var pref = this.prefs[key]; | 62 var pref = this.prefs[key]; |
| 59 assertNotEquals(undefined, pref); | 63 assertNotEquals(undefined, pref); |
| 60 assertEquals(typeof value, typeof pref.value); | 64 assertEquals(typeof value, typeof pref.value); |
| 61 assertEquals(Array.isArray(value), Array.isArray(pref.value)); | 65 assertEquals(Array.isArray(value), Array.isArray(pref.value)); |
| 62 | 66 |
| 63 if (this.failNextSetPref_) { | 67 if (this.failNextSetPref_) { |
| 64 callback(false); | 68 callback(false); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 * @param {!Object<{key: string, value: *}>} changes | 107 * @param {!Object<{key: string, value: *}>} changes |
| 104 */ | 108 */ |
| 105 sendPrefChanges: function(changes) { | 109 sendPrefChanges: function(changes) { |
| 106 var prefs = []; | 110 var prefs = []; |
| 107 for (var change of changes) { | 111 for (var change of changes) { |
| 108 var pref = this.prefs[change.key]; | 112 var pref = this.prefs[change.key]; |
| 109 assertNotEquals(undefined, pref); | 113 assertNotEquals(undefined, pref); |
| 110 pref.value = change.value; | 114 pref.value = change.value; |
| 111 prefs.push(deepCopy(pref)); | 115 prefs.push(deepCopy(pref)); |
| 112 } | 116 } |
| 113 this.listener_(prefs); | 117 MockSettingsApi.listener_(prefs); |
| 114 }, | 118 }, |
| 115 | 119 |
| 116 // Private methods for use by the mock API. | 120 // Private methods for use by the mock API. |
| 117 | 121 |
| 118 /** | 122 /** |
| 119 * @param {!chrome.settingsPrivate.PrefType} type | 123 * @param {!chrome.settingsPrivate.PrefType} type |
| 120 * @param {string} key | 124 * @param {string} key |
| 121 * @param {*} value | 125 * @param {*} value |
| 122 */ | 126 */ |
| 123 addPref_: function(type, key, value) { | 127 addPref_: function(type, key, value) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 154 return undefined; | 158 return undefined; |
| 155 } | 159 } |
| 156 return pref; | 160 return pref; |
| 157 } | 161 } |
| 158 | 162 |
| 159 /** | 163 /** |
| 160 * Checks that the mock API pref store contains the expected values. | 164 * Checks that the mock API pref store contains the expected values. |
| 161 * @param {number} testCaseValueIndex The index of possible values from | 165 * @param {number} testCaseValueIndex The index of possible values from |
| 162 * the test case to check. | 166 * the test case to check. |
| 163 */ | 167 */ |
| 164 function expectMockApiPrefsSet(testCaseValueIndex) { | 168 function assertMockApiPrefsSet(testCaseValueIndex) { |
| 165 for (var testCase of prefsTestCases) { | 169 for (var testCase of prefsTestCases) { |
| 166 var expectedValue = JSON.stringify( | 170 var expectedValue = JSON.stringify( |
| 167 testCase.values[testCaseValueIndex]); | 171 testCase.values[testCaseValueIndex]); |
| 168 var actualValue = JSON.stringify(mockApi.prefs[testCase.key].value); | 172 var actualValue = JSON.stringify(mockApi.prefs[testCase.key].value); |
| 169 expectEquals(expectedValue, actualValue); | 173 assertEquals(expectedValue, actualValue); |
| 170 } | 174 } |
| 171 } | 175 } |
| 172 | 176 |
| 173 /** | 177 /** |
| 174 * Checks that the <cr-settings-prefs> contains the expected values. | 178 * Checks that the <cr-settings-prefs> contains the expected values. |
| 175 * @param {number} testCaseValueIndex The index of possible values from | 179 * @param {number} testCaseValueIndex The index of possible values from |
| 176 * the test case to check. | 180 * the test case to check. |
| 177 */ | 181 */ |
| 178 function expectPrefsSet(testCaseValueIndex) { | 182 function assertPrefsSet(testCaseValueIndex) { |
| 179 for (var testCase of prefsTestCases) { | 183 for (var testCase of prefsTestCases) { |
| 180 var expectedValue = JSON.stringify( | 184 var expectedValue = JSON.stringify( |
| 181 testCase.values[testCaseValueIndex]); | 185 testCase.values[testCaseValueIndex]); |
| 182 var actualValue = JSON.stringify( | 186 var actualValue = JSON.stringify( |
| 183 prefs.get('prefs.' + testCase.key + '.value')); | 187 prefs.get('prefs.' + testCase.key + '.value')); |
| 184 expectEquals(expectedValue, actualValue); | 188 assertEquals(expectedValue, actualValue); |
| 185 } | 189 } |
| 186 } | 190 } |
| 187 | 191 |
| 188 // Initialize a <cr-settings-prefs> element before each test. | 192 /** |
| 189 setup(function(done) { | 193 * List of CrSettingsPref elements created for testing. |
| 194 * @type {!Array<!CrSettingsPrefs>} | |
| 195 */ | |
| 196 var createdElements = []; | |
| 197 | |
| 198 // Initialize <cr-settings-prefs> elements before each test. | |
| 199 setup(function() { | |
| 190 mockApi = new MockSettingsApi(); | 200 mockApi = new MockSettingsApi(); |
| 191 // TODO(michaelpg): don't use global variables to inject the API. | 201 // TODO(michaelpg): don't use global variables to inject the API. |
| 192 window.mockApi = mockApi; | 202 window.mockApi = mockApi; |
| 193 | 203 |
| 194 // Create and attach the <cr-settings-prefs> element. | 204 // Create and attach the <cr-settings-prefs> elements. Make several of |
| 205 // them to test that the shared state model scales correctly. | |
| 206 createdElements = []; | |
| 207 for (var i = 0; i < 200; i++) { | |
| 208 var prefsInstance = document.createElement('cr-settings-prefs'); | |
| 209 document.body.appendChild(prefsInstance); | |
| 210 createdElements.push(prefsInstance); | |
| 211 } | |
| 212 // For simplicity, only use one prefs element in the tests. Use an | |
| 213 // arbitrary index instead of the first or last element created. | |
| 214 prefs = createdElements[42]; | |
| 215 | |
| 216 // getAllPrefs is asynchronous, so return the prefs promise. | |
| 217 return CrSettingsPrefs.initialized; | |
| 218 }); | |
| 219 | |
| 220 teardown(function() { | |
| 221 CrSettingsPrefs.reset(); | |
| 222 | |
| 223 // Reset each <cr-settings-prefs>. | |
| 224 for (var i = 0; i < createdElements.length; i++) | |
| 225 createdElements[i].resetForTesting(); | |
| 226 | |
| 195 PolymerTest.clearBody(); | 227 PolymerTest.clearBody(); |
| 196 prefs = document.createElement('cr-settings-prefs'); | |
| 197 document.body.appendChild(prefs); | |
| 198 | |
| 199 window.mockApi = undefined; | 228 window.mockApi = undefined; |
| 200 | |
| 201 // Wait for CrSettingsPrefs.INITIALIZED. | |
| 202 if (!CrSettingsPrefs.isInitialized) { | |
| 203 var listener = function() { | |
| 204 document.removeEventListener(CrSettingsPrefs.INITIALIZED, listener); | |
| 205 done(); | |
| 206 }; | |
| 207 document.addEventListener(CrSettingsPrefs.INITIALIZED, listener); | |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 done(); | |
| 212 }); | 229 }); |
| 213 | 230 |
| 214 test('receives and caches prefs', function() { | 231 test('receives and caches prefs', function() { |
| 215 // Test that each pref has been successfully copied to the Polymer | 232 // Test that each pref has been successfully copied to the Polymer |
| 216 // |prefs| property. | 233 // |prefs| property. |
| 217 for (var key in mockApi.prefs) { | 234 for (var key in mockApi.prefs) { |
| 218 var expectedPref = mockApi.prefs[key]; | 235 var expectedPref = mockApi.prefs[key]; |
| 219 var actualPref = getPrefFromKey(prefs.prefs, key); | 236 var actualPref = getPrefFromKey(prefs.prefs, key); |
| 220 if (!expectNotEquals(undefined, actualPref)) { | 237 if (!expectNotEquals(undefined, actualPref)) { |
| 221 // We've already registered an error, so skip the pref. | 238 // We've already registered an error, so skip the pref. |
| 222 continue; | 239 continue; |
| 223 } | 240 } |
| 224 | 241 |
| 225 expectEquals(JSON.stringify(expectedPref), | 242 assertEquals(JSON.stringify(expectedPref), |
| 226 JSON.stringify(actualPref)); | 243 JSON.stringify(actualPref)); |
| 227 } | 244 } |
| 228 }); | 245 }); |
| 229 | 246 |
| 230 test('forwards pref changes to API', function() { | 247 test('forwards pref changes to API', function() { |
| 231 // Test that cr-settings-prefs uses the setPref API. | 248 // Test that cr-settings-prefs uses the setPref API. |
| 232 for (var testCase of prefsTestCases) { | 249 for (var testCase of prefsTestCases) { |
| 233 prefs.set('prefs.' + testCase.key + '.value', | 250 prefs.set('prefs.' + testCase.key + '.value', |
| 234 deepCopy(testCase.values[1])); | 251 deepCopy(testCase.values[1])); |
| 235 } | 252 } |
| 236 // Check that setPref has been called for the right values. | 253 // Check that setPref has been called for the right values. |
| 237 expectMockApiPrefsSet(1); | 254 assertMockApiPrefsSet(1); |
| 238 | 255 |
| 239 // Test that when setPref fails, the pref is reverted locally. | 256 // Test that when setPref fails, the pref is reverted locally. |
| 240 for (var testCase of prefsTestCases) { | 257 for (var testCase of prefsTestCases) { |
| 241 mockApi.failNextSetPref(); | 258 mockApi.failNextSetPref(); |
| 242 prefs.set('prefs.' + testCase.key + '.value', | 259 prefs.set('prefs.' + testCase.key + '.value', |
| 243 deepCopy(testCase.values[2])); | 260 deepCopy(testCase.values[2])); |
| 244 } | 261 } |
| 245 | 262 |
| 246 expectPrefsSet(1); | 263 assertPrefsSet(1); |
| 247 | 264 |
| 248 // Test that setPref is not called when the pref doesn't change. | 265 // Test that setPref is not called when the pref doesn't change. |
| 249 mockApi.disallowSetPref(); | 266 mockApi.disallowSetPref(); |
| 250 for (var testCase of prefsTestCases) { | 267 for (var testCase of prefsTestCases) { |
| 251 prefs.set('prefs.' + testCase.key + '.value', | 268 prefs.set('prefs.' + testCase.key + '.value', |
| 252 deepCopy(testCase.values[1])); | 269 deepCopy(testCase.values[1])); |
| 253 } | 270 } |
| 254 expectMockApiPrefsSet(1); | 271 assertMockApiPrefsSet(1); |
| 255 mockApi.allowSetPref(); | 272 mockApi.allowSetPref(); |
| 256 }); | 273 }); |
| 257 | 274 |
| 258 test('responds to API changes', function() { | 275 test('responds to API changes', function() { |
| 259 // Changes from the API should not result in those changes being sent | 276 // Changes from the API should not result in those changes being sent |
| 260 // back to the API, as this could trigger a race condition. | 277 // back to the API, as this could trigger a race condition. |
| 261 mockApi.disallowSetPref(); | 278 mockApi.disallowSetPref(); |
| 262 var prefChanges = []; | 279 var prefChanges = []; |
| 263 for (var testCase of prefsTestCases) | 280 for (var testCase of prefsTestCases) |
| 264 prefChanges.push({key: testCase.key, value: testCase.values[1]}); | 281 prefChanges.push({key: testCase.key, value: testCase.values[1]}); |
| 265 | 282 |
| 266 // Send a set of changes. | 283 // Send a set of changes. |
| 267 mockApi.sendPrefChanges(prefChanges); | 284 mockApi.sendPrefChanges(prefChanges); |
| 268 expectPrefsSet(1); | 285 assertPrefsSet(1); |
| 269 | 286 |
| 270 prefChanges = []; | 287 prefChanges = []; |
| 271 for (var testCase of prefsTestCases) | 288 for (var testCase of prefsTestCases) |
| 272 prefChanges.push({key: testCase.key, value: testCase.values[2]}); | 289 prefChanges.push({key: testCase.key, value: testCase.values[2]}); |
| 273 | 290 |
| 274 // Send a second set of changes. | 291 // Send a second set of changes. |
| 275 mockApi.sendPrefChanges(prefChanges); | 292 mockApi.sendPrefChanges(prefChanges); |
| 276 expectPrefsSet(2); | 293 assertPrefsSet(2); |
| 277 | 294 |
| 278 // Send the same set of changes again -- nothing should happen. | 295 // Send the same set of changes again -- nothing should happen. |
| 279 mockApi.sendPrefChanges(prefChanges); | 296 mockApi.sendPrefChanges(prefChanges); |
| 280 expectPrefsSet(2); | 297 assertPrefsSet(2); |
| 281 }); | 298 }); |
| 282 }); | 299 }); |
| 283 } | 300 } |
| 284 | 301 |
| 285 return { | 302 return { |
| 286 registerTests: registerTests, | 303 registerTests: registerTests, |
| 287 }; | 304 }; |
| 288 }); | 305 }); |
| OLD | NEW |