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. |
| 9 * @param {!Object} obj |
| 10 * @return {!Object} |
| 11 */ |
| 12 function deepCopy(obj) { |
| 13 return JSON.parse(JSON.stringify(obj)); |
| 14 } |
| 15 |
| 16 /** |
8 * Mock of chrome.settingsPrivate API. | 17 * Mock of chrome.settingsPrivate API. |
9 * @constructor | 18 * @constructor |
10 * @extends {chrome.settingsPrivate} | 19 * @extends {chrome.settingsPrivate} |
11 */ | 20 */ |
12 function MockSettingsApi() { | 21 function MockSettingsApi() { |
13 this.prefs = {}; | 22 this.prefs = {}; |
14 this.listener_ = null; | 23 this.listener_ = null; |
15 | 24 |
16 // Hack alert: bind this instance's onPrefsChanged members to this. | 25 // Hack alert: bind this instance's onPrefsChanged members to this. |
17 this.onPrefsChanged = { | 26 this.onPrefsChanged = { |
(...skipping 15 matching lines...) Expand all Loading... |
33 removeListener: function(listener) { | 42 removeListener: function(listener) { |
34 expectNotEquals(null, this.listener_); | 43 expectNotEquals(null, this.listener_); |
35 this.listener_ = null; | 44 this.listener_ = null; |
36 }, | 45 }, |
37 }, | 46 }, |
38 | 47 |
39 getAllPrefs: function(callback) { | 48 getAllPrefs: function(callback) { |
40 // Send a copy of prefs to keep our internal state private. | 49 // Send a copy of prefs to keep our internal state private. |
41 var prefs = []; | 50 var prefs = []; |
42 for (var key in this.prefs) | 51 for (var key in this.prefs) |
43 prefs.push(Object.assign({}, this.prefs[key])); | 52 prefs.push(deepCopy(this.prefs[key])); |
44 | 53 |
45 callback(prefs); | 54 callback(prefs); |
46 }, | 55 }, |
47 | 56 |
48 setPref: function(key, value, pageId, callback) { | 57 setPref: function(key, value, pageId, callback) { |
49 var pref = this.prefs[key]; | 58 var pref = this.prefs[key]; |
50 assertNotEquals(undefined, pref); | 59 assertNotEquals(undefined, pref); |
51 assertEquals(typeof value, typeof pref.value); | 60 assertEquals(typeof value, typeof pref.value); |
52 assertEquals(Array.isArray(value), Array.isArray(pref.value)); | 61 assertEquals(Array.isArray(value), Array.isArray(pref.value)); |
53 | 62 |
54 if (this.failNextSetPref_) { | 63 if (this.failNextSetPref_) { |
55 callback(false); | 64 callback(false); |
56 this.failNextSetPref_ = false; | 65 this.failNextSetPref_ = false; |
57 return; | 66 return; |
58 } | 67 } |
59 assertNotEquals(true, this.disallowSetPref_); | 68 assertNotEquals(true, this.disallowSetPref_); |
60 | 69 |
61 // TODO(michaelpg): support list and dict prefs. | 70 var changed = JSON.stringify(pref.value) != JSON.stringify(value); |
62 var changed = pref.value != value; | 71 pref.value = deepCopy(value); |
63 pref.value = value; | |
64 callback(true); | 72 callback(true); |
65 | 73 |
66 // Like chrome.settingsPrivate, send a notification when prefs change. | 74 // Like chrome.settingsPrivate, send a notification when prefs change. |
67 if (changed) | 75 if (changed) |
68 this.sendPrefChanges([{key: key, value: value}]); | 76 this.sendPrefChanges([{key: key, value: deepCopy(value)}]); |
69 }, | 77 }, |
70 | 78 |
71 getPref: function(key, callback) { | 79 getPref: function(key, callback) { |
72 var pref = this.prefs[key]; | 80 var pref = this.prefs[key]; |
73 assertNotEquals(undefined, pref); | 81 assertNotEquals(undefined, pref); |
74 callback(Object.assign({}, pref)); | 82 callback(deepCopy(pref)); |
75 }, | 83 }, |
76 | 84 |
77 // Functions used by tests. | 85 // Functions used by tests. |
78 | 86 |
79 /** Instructs the API to return a failure when setPref is next called. */ | 87 /** Instructs the API to return a failure when setPref is next called. */ |
80 failNextSetPref: function() { | 88 failNextSetPref: function() { |
81 this.failNextSetPref_ = true; | 89 this.failNextSetPref_ = true; |
82 }, | 90 }, |
83 | 91 |
84 /** Instructs the API to assert (fail the test) if setPref is called. */ | 92 /** Instructs the API to assert (fail the test) if setPref is called. */ |
85 disallowSetPref: function() { | 93 disallowSetPref: function() { |
86 this.disallowSetPref_ = true; | 94 this.disallowSetPref_ = true; |
87 }, | 95 }, |
88 | 96 |
89 allowSetPref: function() { | 97 allowSetPref: function() { |
90 this.disallowSetPref_ = false; | 98 this.disallowSetPref_ = false; |
91 }, | 99 }, |
92 | 100 |
93 /** | 101 /** |
94 * Notifies the listener of pref changes. | 102 * Notifies the listener of pref changes. |
95 * @param {!Object<{key: string, value: *}>} changes | 103 * @param {!Object<{key: string, value: *}>} changes |
96 */ | 104 */ |
97 sendPrefChanges: function(changes) { | 105 sendPrefChanges: function(changes) { |
98 var prefs = []; | 106 var prefs = []; |
99 for (var change of changes) { | 107 for (var change of changes) { |
100 var pref = this.prefs[change.key]; | 108 var pref = this.prefs[change.key]; |
101 assertNotEquals(undefined, pref); | 109 assertNotEquals(undefined, pref); |
102 pref.value = change.value; | 110 pref.value = change.value; |
103 prefs.push(Object.assign({}, pref)); | 111 prefs.push(deepCopy(pref)); |
104 } | 112 } |
105 this.listener_(prefs); | 113 this.listener_(prefs); |
106 }, | 114 }, |
107 | 115 |
108 // Private methods for use by the mock API. | 116 // Private methods for use by the mock API. |
109 | 117 |
110 /** | 118 /** |
111 * @param {!chrome.settingsPrivate.PrefType} type | 119 * @param {!chrome.settingsPrivate.PrefType} type |
112 * @param {string} key | 120 * @param {string} key |
113 * @param {*} value | 121 * @param {*} value |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 continue; | 222 continue; |
215 } | 223 } |
216 | 224 |
217 expectEquals(JSON.stringify(expectedPref), | 225 expectEquals(JSON.stringify(expectedPref), |
218 JSON.stringify(actualPref)); | 226 JSON.stringify(actualPref)); |
219 } | 227 } |
220 }); | 228 }); |
221 | 229 |
222 test('forwards pref changes to API', function() { | 230 test('forwards pref changes to API', function() { |
223 // Test that cr-settings-prefs uses the setPref API. | 231 // Test that cr-settings-prefs uses the setPref API. |
224 for (var testCase of prefsTestCases) | 232 for (var testCase of prefsTestCases) { |
225 prefs.set('prefs.' + testCase.key + '.value', testCase.values[1]); | 233 prefs.set('prefs.' + testCase.key + '.value', |
226 | 234 deepCopy(testCase.values[1])); |
| 235 } |
227 // Check that setPref has been called for the right values. | 236 // Check that setPref has been called for the right values. |
228 expectMockApiPrefsSet(1); | 237 expectMockApiPrefsSet(1); |
229 | 238 |
230 // Test that when setPref fails, the pref is reverted locally. | 239 // Test that when setPref fails, the pref is reverted locally. |
231 for (var testCase of prefsTestCases) { | 240 for (var testCase of prefsTestCases) { |
232 mockApi.failNextSetPref(); | 241 mockApi.failNextSetPref(); |
233 prefs.set('prefs.' + testCase.key + '.value', testCase.values[2]); | 242 prefs.set('prefs.' + testCase.key + '.value', |
| 243 deepCopy(testCase.values[2])); |
234 } | 244 } |
235 | 245 |
236 expectPrefsSet(1); | 246 expectPrefsSet(1); |
237 | 247 |
238 // Test that setPref is not called when the pref doesn't change. | 248 // Test that setPref is not called when the pref doesn't change. |
239 mockApi.disallowSetPref(); | 249 mockApi.disallowSetPref(); |
240 for (var testCase of prefsTestCases) | 250 for (var testCase of prefsTestCases) { |
241 prefs.set('prefs.' + testCase.key + '.value', testCase.values[1]); | 251 prefs.set('prefs.' + testCase.key + '.value', |
242 | 252 deepCopy(testCase.values[1])); |
| 253 } |
243 expectMockApiPrefsSet(1); | 254 expectMockApiPrefsSet(1); |
244 mockApi.allowSetPref(); | 255 mockApi.allowSetPref(); |
245 }); | 256 }); |
246 | 257 |
247 test('responds to API changes', function() { | 258 test('responds to API changes', function() { |
248 // Changes from the API should not result in those changes being sent | 259 // Changes from the API should not result in those changes being sent |
249 // back to the API, as this could trigger a race condition. | 260 // back to the API, as this could trigger a race condition. |
250 mockApi.disallowSetPref(); | 261 mockApi.disallowSetPref(); |
251 var prefChanges = []; | 262 var prefChanges = []; |
252 for (var testCase of prefsTestCases) | 263 for (var testCase of prefsTestCases) |
(...skipping 15 matching lines...) Expand all Loading... |
268 mockApi.sendPrefChanges(prefChanges); | 279 mockApi.sendPrefChanges(prefChanges); |
269 expectPrefsSet(2); | 280 expectPrefsSet(2); |
270 }); | 281 }); |
271 }); | 282 }); |
272 } | 283 } |
273 | 284 |
274 return { | 285 return { |
275 registerTests: registerTests, | 286 registerTests: registerTests, |
276 }; | 287 }; |
277 }); | 288 }); |
OLD | NEW |