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

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

Issue 2254113002: MD Settings: reduce complexity and overhead of prefs singleton (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@SimplifyLanguages
Patch Set: Nit Created 4 years, 4 months 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
« no previous file with comments | « chrome/test/data/webui/settings/passwords_and_forms_browsertest.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 function registerTests() { 16 function registerTests() {
17 suite('CrSettingsPrefs', function() { 17 suite('CrSettingsPrefs', function() {
18 /** 18 /**
19 * Prefs instance created before each test. 19 * Prefs instance created before each test.
20 * @type {CrSettingsPrefsElement|undefined} 20 * @type {SettingsPrefsElement|undefined}
21 */ 21 */
22 var prefs; 22 var prefs;
23 23
24 /** @type {settings.FakeSettingsPrivate} */ 24 /** @type {settings.FakeSettingsPrivate} */
25 var fakeApi = null; 25 var fakeApi = null;
26 26
27 /** 27 /**
28 * @param {!Object} prefStore Pref store from <settings-prefs>. 28 * @param {!Object} prefStore Pref store from <settings-prefs>.
29 * @param {string} key Pref key of the pref to return. 29 * @param {string} key Pref key of the pref to return.
30 * @return {chrome.settingsPrivate.PrefObject|undefined} 30 * @return {chrome.settingsPrivate.PrefObject|undefined}
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 function assertPrefsSet(testCaseValueIndex) { 63 function assertPrefsSet(testCaseValueIndex) {
64 for (var testCase of prefsTestCases) { 64 for (var testCase of prefsTestCases) {
65 var expectedValue = JSON.stringify( 65 var expectedValue = JSON.stringify(
66 testCase.nextValues[testCaseValueIndex]); 66 testCase.nextValues[testCaseValueIndex]);
67 var actualValue = JSON.stringify( 67 var actualValue = JSON.stringify(
68 prefs.get('prefs.' + testCase.pref.key + '.value')); 68 prefs.get('prefs.' + testCase.pref.key + '.value'));
69 assertEquals(expectedValue, actualValue); 69 assertEquals(expectedValue, actualValue);
70 } 70 }
71 } 71 }
72 72
73 /** 73 // Initialize a <settings-prefs> before each test.
74 * List of CrSettingsPref elements created for testing.
75 * @type {!Array<!CrSettingsPrefs>}
76 */
77 var createdElements = [];
78
79 // Initialize <settings-prefs> elements before each test.
80 setup(function() { 74 setup(function() {
81 // Override chrome.settingsPrivate with FakeSettingsPrivate. 75 // Override chrome.settingsPrivate with FakeSettingsPrivate.
82 fakeApi = new settings.FakeSettingsPrivate( 76 fakeApi = new settings.FakeSettingsPrivate(
83 prefsTestCases.map(function(testCase) { 77 prefsTestCases.map(function(testCase) {
84 return testCase.pref; 78 return testCase.pref;
85 })); 79 }));
86 CrSettingsPrefs.deferInitialization = true; 80 CrSettingsPrefs.deferInitialization = true;
87 81
88 // Create and attach the <settings-prefs> elements. Make several of 82 prefs = document.createElement('settings-prefs');
89 // them to test that the shared state model scales correctly. 83 prefs.initialize(fakeApi);
90 createdElements = [];
91 for (var i = 0; i < 100; i++) {
92 var prefsInstance = document.createElement('settings-prefs');
93 document.body.appendChild(prefsInstance);
94 createdElements.push(prefsInstance);
95 prefsInstance.initializeForTesting(fakeApi);
96 }
97 // For simplicity, only use one prefs element in the tests. Use an
98 // arbitrary index instead of the first or last element created.
99 prefs = createdElements[42];
100 84
101 // getAllPrefs is asynchronous, so return the prefs promise. 85 // getAllPrefs is asynchronous, so return the prefs promise.
102 return CrSettingsPrefs.initialized; 86 return CrSettingsPrefs.initialized;
103 }); 87 });
104 88
105 teardown(function() { 89 teardown(function() {
106 CrSettingsPrefs.resetForTesting(); 90 CrSettingsPrefs.resetForTesting();
107 CrSettingsPrefs.deferInitialization = false; 91 CrSettingsPrefs.deferInitialization = false;
108 92 prefs.resetForTesting();
109 // Reset each <settings-prefs>. TODO(michaelpg): make settings-prefs
110 // less dependent on testing state so we don't have to do this.
111 for (var i = 0; i < createdElements.length; i++)
112 createdElements[i].resetForTesting();
113 93
114 PolymerTest.clearBody(); 94 PolymerTest.clearBody();
115 }); 95 });
116 96
117 test('receives and caches prefs', function testGetPrefs() { 97 test('receives and caches prefs', function testGetPrefs() {
118 // Test that each pref has been successfully copied to the Polymer 98 // Test that each pref has been successfully copied to the Polymer
119 // |prefs| property. 99 // |prefs| property.
120 for (var key in fakeApi.prefs) { 100 for (var key in fakeApi.prefs) {
121 var expectedPref = fakeApi.prefs[key]; 101 var expectedPref = fakeApi.prefs[key];
122 var actualPref = getPrefFromKey(prefs.prefs, key); 102 var actualPref = getPrefFromKey(prefs.prefs, key);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 fakeApi.sendPrefChanges(prefChanges); 166 fakeApi.sendPrefChanges(prefChanges);
187 assertPrefsSet(1); 167 assertPrefsSet(1);
188 }); 168 });
189 }); 169 });
190 } 170 }
191 171
192 return { 172 return {
193 registerTests: registerTests, 173 registerTests: registerTests,
194 }; 174 };
195 }); 175 });
OLDNEW
« no previous file with comments | « chrome/test/data/webui/settings/passwords_and_forms_browsertest.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698