Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: chrome/test/data/webui/settings/prefs_tests.js

Issue 1447103002: MD Settings: FakeSettingsPrivate for tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 settings-prefs. */ 5 /** @fileoverview Suite of tests for settings-prefs. */
6 cr.define('settings_prefs', function() { 6 cr.define('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 /**
17 * Mock of chrome.settingsPrivate API.
18 * @constructor
19 * @extends {chrome.settingsPrivate}
20 */
21 function MockSettingsApi() {
22 this.prefs = {};
23
24 // Hack alert: bind this instance's onPrefsChanged members to this.
25 this.onPrefsChanged = {
26 addListener: this.onPrefsChanged.addListener.bind(this),
27 removeListener: this.onPrefsChanged.removeListener.bind(this),
28 };
29
30 for (var testCase of prefsTestCases)
31 this.addPref_(testCase.type, testCase.key, testCase.values[0]);
32 }
33
34 // Make the listener static because it refers to a singleton.
35 MockSettingsApi.listener_ = null;
36
37 MockSettingsApi.prototype = {
38 // chrome.settingsPrivate overrides.
39 onPrefsChanged: {
40 addListener: function(listener) {
41 MockSettingsApi.listener_ = listener;
42 },
43
44 removeListener: function(listener) {
45 MockSettingsApi.listener_ = null;
46 },
47 },
48
49 getAllPrefs: function(callback) {
50 // Send a copy of prefs to keep our internal state private.
51 var prefs = [];
52 for (var key in this.prefs)
53 prefs.push(deepCopy(this.prefs[key]));
54
55 // Run the callback asynchronously to test that the prefs aren't actually
56 // used before they become available.
57 setTimeout(callback.bind(null, prefs));
58 },
59
60 setPref: function(key, value, pageId, callback) {
61 var pref = this.prefs[key];
62 assertNotEquals(undefined, pref);
63 assertEquals(typeof value, typeof pref.value);
64 assertEquals(Array.isArray(value), Array.isArray(pref.value));
65
66 if (this.failNextSetPref_) {
67 callback(false);
68 this.failNextSetPref_ = false;
69 return;
70 }
71 assertNotEquals(true, this.disallowSetPref_);
72
73 var changed = JSON.stringify(pref.value) != JSON.stringify(value);
74 pref.value = deepCopy(value);
75 callback(true);
76
77 // Like chrome.settingsPrivate, send a notification when prefs change.
78 if (changed)
79 this.sendPrefChanges([{key: key, value: deepCopy(value)}]);
80 },
81
82 getPref: function(key, callback) {
83 var pref = this.prefs[key];
84 assertNotEquals(undefined, pref);
85 callback(deepCopy(pref));
86 },
87
88 // Functions used by tests.
89
90 /** Instructs the API to return a failure when setPref is next called. */
91 failNextSetPref: function() {
92 this.failNextSetPref_ = true;
93 },
94
95 /** Instructs the API to assert (fail the test) if setPref is called. */
96 disallowSetPref: function() {
97 this.disallowSetPref_ = true;
98 },
99
100 allowSetPref: function() {
101 this.disallowSetPref_ = false;
102 },
103
104 /**
105 * Notifies the listener of pref changes.
106 * @param {!Object<{key: string, value: *}>} changes
107 */
108 sendPrefChanges: function(changes) {
109 var prefs = [];
110 for (var change of changes) {
111 var pref = this.prefs[change.key];
112 assertNotEquals(undefined, pref);
113 pref.value = change.value;
114 prefs.push(deepCopy(pref));
115 }
116 MockSettingsApi.listener_(prefs);
117 },
118
119 // Private methods for use by the mock API.
120
121 /**
122 * @param {!chrome.settingsPrivate.PrefType} type
123 * @param {string} key
124 * @param {*} value
125 */
126 addPref_: function(type, key, value) {
127 this.prefs[key] = {
128 type: type,
129 key: key,
130 value: value,
131 };
132 },
133 };
134
135 function registerTests() { 16 function registerTests() {
136 suite('CrSettingsPrefs', function() { 17 suite('CrSettingsPrefs', function() {
137 /** 18 /**
138 * Prefs instance created before each test. 19 * Prefs instance created before each test.
139 * @type {CrSettingsPrefsElement|undefined} 20 * @type {CrSettingsPrefsElement|undefined}
140 */ 21 */
141 var prefs; 22 var prefs;
142 23
143 /** @type {MockSettingsApi} */ 24 /** @type {settings.MockSettingsPrivate} */
144 var mockApi = null; 25 var mockApi = null;
145 26
146 /** 27 /**
147 * @param {!Object} prefStore Pref store from <settings-prefs>. 28 * @param {!Object} prefStore Pref store from <settings-prefs>.
148 * @param {string} key Pref key of the pref to return. 29 * @param {string} key Pref key of the pref to return.
149 * @return {chrome.settingsPrivate.PrefObject|undefined} 30 * @return {chrome.settingsPrivate.PrefObject|undefined}
150 */ 31 */
151 function getPrefFromKey(prefStore, key) { 32 function getPrefFromKey(prefStore, key) {
152 var path = key.split('.'); 33 var path = key.split('.');
153 var pref = prefStore; 34 var pref = prefStore;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 } 70 }
190 71
191 /** 72 /**
192 * List of CrSettingsPref elements created for testing. 73 * List of CrSettingsPref elements created for testing.
193 * @type {!Array<!CrSettingsPrefs>} 74 * @type {!Array<!CrSettingsPrefs>}
194 */ 75 */
195 var createdElements = []; 76 var createdElements = [];
196 77
197 // Initialize <settings-prefs> elements before each test. 78 // Initialize <settings-prefs> elements before each test.
198 setup(function() { 79 setup(function() {
199 mockApi = new MockSettingsApi(); 80 // Override chrome.settingsPrivate with MockSettingsPrivate.
200 // TODO(michaelpg): don't use global variables to inject the API. 81 mockApi = new settings.MockSettingsPrivate();
201 window.mockApi = mockApi; 82 mockApi.register();
202 83
203 // Create and attach the <settings-prefs> elements. Make several of 84 // Create and attach the <settings-prefs> elements. Make several of
204 // them to test that the shared state model scales correctly. 85 // them to test that the shared state model scales correctly.
205 createdElements = []; 86 createdElements = [];
206 for (var i = 0; i < 100; i++) { 87 for (var i = 0; i < 100; i++) {
207 var prefsInstance = document.createElement('settings-prefs'); 88 var prefsInstance = document.createElement('settings-prefs');
208 document.body.appendChild(prefsInstance); 89 document.body.appendChild(prefsInstance);
209 createdElements.push(prefsInstance); 90 createdElements.push(prefsInstance);
210 } 91 }
211 // For simplicity, only use one prefs element in the tests. Use an 92 // For simplicity, only use one prefs element in the tests. Use an
212 // arbitrary index instead of the first or last element created. 93 // arbitrary index instead of the first or last element created.
213 prefs = createdElements[42]; 94 prefs = createdElements[42];
214 95
215 // getAllPrefs is asynchronous, so return the prefs promise. 96 // getAllPrefs is asynchronous, so return the prefs promise.
216 return CrSettingsPrefs.initialized; 97 return CrSettingsPrefs.initialized;
217 }); 98 });
218 99
219 teardown(function() { 100 teardown(function() {
220 CrSettingsPrefs.resetForTesting(); 101 CrSettingsPrefs.resetForTesting();
221 102
222 // Reset each <settings-prefs>. 103 // Reset each <settings-prefs>. TODO(michaelpg): make settings-prefs
104 // less dependent on testing state so we don't have to do this.
223 for (var i = 0; i < createdElements.length; i++) 105 for (var i = 0; i < createdElements.length; i++)
224 createdElements[i].resetForTesting(); 106 createdElements[i].resetForTesting();
225 107
226 PolymerTest.clearBody(); 108 PolymerTest.clearBody();
227 window.mockApi = undefined; 109 mockApi.unregister();
228 }); 110 });
229 111
230 test('receives and caches prefs', function() { 112 test('receives and caches prefs', function() {
231 // Test that each pref has been successfully copied to the Polymer 113 // Test that each pref has been successfully copied to the Polymer
232 // |prefs| property. 114 // |prefs| property.
233 for (var key in mockApi.prefs) { 115 for (var key in mockApi.prefs) {
234 var expectedPref = mockApi.prefs[key]; 116 var expectedPref = mockApi.prefs[key];
235 var actualPref = getPrefFromKey(prefs.prefs, key); 117 var actualPref = getPrefFromKey(prefs.prefs, key);
236 if (!expectNotEquals(undefined, actualPref)) { 118 if (!expectNotEquals(undefined, actualPref)) {
237 // We've already registered an error, so skip the pref. 119 // We've already registered an error, so skip the pref.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 mockApi.sendPrefChanges(prefChanges); 177 mockApi.sendPrefChanges(prefChanges);
296 assertPrefsSet(2); 178 assertPrefsSet(2);
297 }); 179 });
298 }); 180 });
299 } 181 }
300 182
301 return { 183 return {
302 registerTests: registerTests, 184 registerTests: registerTests,
303 }; 185 };
304 }); 186 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698