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

Unified 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: proto -> interface 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/test/data/webui/settings/prefs_test_cases.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/data/webui/settings/prefs_tests.js
diff --git a/chrome/test/data/webui/settings/prefs_tests.js b/chrome/test/data/webui/settings/prefs_tests.js
index 99eeaf651ef4fb15c2e59b01db9d89294801148b..b20f6573d0940aaad5b7d5b364ab5a4db59b6821 100644
--- a/chrome/test/data/webui/settings/prefs_tests.js
+++ b/chrome/test/data/webui/settings/prefs_tests.js
@@ -13,125 +13,6 @@ cr.define('settings_prefs', function() {
return JSON.parse(JSON.stringify(obj));
}
- /**
- * Mock of chrome.settingsPrivate API.
- * @constructor
- * @extends {chrome.settingsPrivate}
- */
- function MockSettingsApi() {
- this.prefs = {};
-
- // Hack alert: bind this instance's onPrefsChanged members to this.
- this.onPrefsChanged = {
- addListener: this.onPrefsChanged.addListener.bind(this),
- removeListener: this.onPrefsChanged.removeListener.bind(this),
- };
-
- for (var testCase of prefsTestCases)
- this.addPref_(testCase.type, testCase.key, testCase.values[0]);
- }
-
- // Make the listener static because it refers to a singleton.
- MockSettingsApi.listener_ = null;
-
- MockSettingsApi.prototype = {
- // chrome.settingsPrivate overrides.
- onPrefsChanged: {
- addListener: function(listener) {
- MockSettingsApi.listener_ = listener;
- },
-
- removeListener: function(listener) {
- MockSettingsApi.listener_ = null;
- },
- },
-
- getAllPrefs: function(callback) {
- // Send a copy of prefs to keep our internal state private.
- var prefs = [];
- for (var key in this.prefs)
- prefs.push(deepCopy(this.prefs[key]));
-
- // Run the callback asynchronously to test that the prefs aren't actually
- // used before they become available.
- setTimeout(callback.bind(null, prefs));
- },
-
- setPref: function(key, value, pageId, callback) {
- var pref = this.prefs[key];
- assertNotEquals(undefined, pref);
- assertEquals(typeof value, typeof pref.value);
- assertEquals(Array.isArray(value), Array.isArray(pref.value));
-
- if (this.failNextSetPref_) {
- callback(false);
- this.failNextSetPref_ = false;
- return;
- }
- assertNotEquals(true, this.disallowSetPref_);
-
- var changed = JSON.stringify(pref.value) != JSON.stringify(value);
- pref.value = deepCopy(value);
- callback(true);
-
- // Like chrome.settingsPrivate, send a notification when prefs change.
- if (changed)
- this.sendPrefChanges([{key: key, value: deepCopy(value)}]);
- },
-
- getPref: function(key, callback) {
- var pref = this.prefs[key];
- assertNotEquals(undefined, pref);
- callback(deepCopy(pref));
- },
-
- // Functions used by tests.
-
- /** Instructs the API to return a failure when setPref is next called. */
- failNextSetPref: function() {
- this.failNextSetPref_ = true;
- },
-
- /** Instructs the API to assert (fail the test) if setPref is called. */
- disallowSetPref: function() {
- this.disallowSetPref_ = true;
- },
-
- allowSetPref: function() {
- this.disallowSetPref_ = false;
- },
-
- /**
- * Notifies the listener of pref changes.
- * @param {!Object<{key: string, value: *}>} changes
- */
- sendPrefChanges: function(changes) {
- var prefs = [];
- for (var change of changes) {
- var pref = this.prefs[change.key];
- assertNotEquals(undefined, pref);
- pref.value = change.value;
- prefs.push(deepCopy(pref));
- }
- MockSettingsApi.listener_(prefs);
- },
-
- // Private methods for use by the mock API.
-
- /**
- * @param {!chrome.settingsPrivate.PrefType} type
- * @param {string} key
- * @param {*} value
- */
- addPref_: function(type, key, value) {
- this.prefs[key] = {
- type: type,
- key: key,
- value: value,
- };
- },
- };
-
function registerTests() {
suite('CrSettingsPrefs', function() {
/**
@@ -140,8 +21,8 @@ cr.define('settings_prefs', function() {
*/
var prefs;
- /** @type {MockSettingsApi} */
- var mockApi = null;
+ /** @type {settings.FakeSettingsPrivate} */
+ var fakeApi = null;
/**
* @param {!Object} prefStore Pref store from <settings-prefs>.
@@ -160,30 +41,31 @@ cr.define('settings_prefs', function() {
}
/**
- * Checks that the mock API pref store contains the expected values.
- * @param {number} testCaseValueIndex The index of possible values from
- * the test case to check.
+ * Checks that the fake API pref store contains the expected values.
+ * @param {number} testCaseValueIndex The index of possible next values
+ * from the test case to check.
*/
- function assertMockApiPrefsSet(testCaseValueIndex) {
+ function assertFakeApiPrefsSet(testCaseValueIndex) {
for (var testCase of prefsTestCases) {
var expectedValue = JSON.stringify(
- testCase.values[testCaseValueIndex]);
- var actualValue = JSON.stringify(mockApi.prefs[testCase.key].value);
- assertEquals(expectedValue, actualValue);
+ testCase.nextValues[testCaseValueIndex]);
+ var actualValue = JSON.stringify(
+ fakeApi.prefs[testCase.pref.key].value);
+ assertEquals(expectedValue, actualValue, testCase.pref.key);
}
}
/**
* Checks that the <settings-prefs> contains the expected values.
- * @param {number} testCaseValueIndex The index of possible values from
- * the test case to check.
+ * @param {number} testCaseValueIndex The index of possible next values
+ * from the test case to check.
*/
function assertPrefsSet(testCaseValueIndex) {
for (var testCase of prefsTestCases) {
var expectedValue = JSON.stringify(
- testCase.values[testCaseValueIndex]);
+ testCase.nextValues[testCaseValueIndex]);
var actualValue = JSON.stringify(
- prefs.get('prefs.' + testCase.key + '.value'));
+ prefs.get('prefs.' + testCase.pref.key + '.value'));
assertEquals(expectedValue, actualValue);
}
}
@@ -196,9 +78,12 @@ cr.define('settings_prefs', function() {
// Initialize <settings-prefs> elements before each test.
setup(function() {
- mockApi = new MockSettingsApi();
- // TODO(michaelpg): don't use global variables to inject the API.
- window.mockApi = mockApi;
+ // Override chrome.settingsPrivate with FakeSettingsPrivate.
+ fakeApi = new settings.FakeSettingsPrivate(
+ prefsTestCases.map(function(testCase) {
+ return testCase.pref;
+ }));
+ CrSettingsPrefs.deferInitialization = true;
// Create and attach the <settings-prefs> elements. Make several of
// them to test that the shared state model scales correctly.
@@ -207,6 +92,7 @@ cr.define('settings_prefs', function() {
var prefsInstance = document.createElement('settings-prefs');
document.body.appendChild(prefsInstance);
createdElements.push(prefsInstance);
+ prefsInstance.initializeForTesting(fakeApi);
}
// For simplicity, only use one prefs element in the tests. Use an
// arbitrary index instead of the first or last element created.
@@ -218,20 +104,21 @@ cr.define('settings_prefs', function() {
teardown(function() {
CrSettingsPrefs.resetForTesting();
+ CrSettingsPrefs.deferInitialization = false;
- // Reset each <settings-prefs>.
+ // Reset each <settings-prefs>. TODO(michaelpg): make settings-prefs
+ // less dependent on testing state so we don't have to do this.
for (var i = 0; i < createdElements.length; i++)
createdElements[i].resetForTesting();
PolymerTest.clearBody();
- window.mockApi = undefined;
});
- test('receives and caches prefs', function() {
+ test('receives and caches prefs', function testGetPrefs() {
// Test that each pref has been successfully copied to the Polymer
// |prefs| property.
- for (var key in mockApi.prefs) {
- var expectedPref = mockApi.prefs[key];
+ for (var key in fakeApi.prefs) {
+ var expectedPref = fakeApi.prefs[key];
var actualPref = getPrefFromKey(prefs.prefs, key);
if (!expectNotEquals(undefined, actualPref)) {
// We've already registered an error, so skip the pref.
@@ -243,57 +130,61 @@ cr.define('settings_prefs', function() {
}
});
- test('forwards pref changes to API', function() {
+ test('forwards pref changes to API', function testSetPrefs() {
// Test that settings-prefs uses the setPref API.
for (var testCase of prefsTestCases) {
- prefs.set('prefs.' + testCase.key + '.value',
- deepCopy(testCase.values[1]));
+ prefs.set('prefs.' + testCase.pref.key + '.value',
+ deepCopy(testCase.nextValues[0]));
}
// Check that setPref has been called for the right values.
- assertMockApiPrefsSet(1);
+ assertFakeApiPrefsSet(0);
// Test that when setPref fails, the pref is reverted locally.
for (var testCase of prefsTestCases) {
- mockApi.failNextSetPref();
- prefs.set('prefs.' + testCase.key + '.value',
- deepCopy(testCase.values[2]));
+ fakeApi.failNextSetPref();
+ prefs.set('prefs.' + testCase.pref.key + '.value',
+ deepCopy(testCase.nextValues[1]));
}
- assertPrefsSet(1);
+ assertPrefsSet(0);
// Test that setPref is not called when the pref doesn't change.
- mockApi.disallowSetPref();
+ fakeApi.disallowSetPref();
for (var testCase of prefsTestCases) {
- prefs.set('prefs.' + testCase.key + '.value',
- deepCopy(testCase.values[1]));
+ prefs.set('prefs.' + testCase.pref.key + '.value',
+ deepCopy(testCase.nextValues[0]));
}
- assertMockApiPrefsSet(1);
- mockApi.allowSetPref();
+ assertFakeApiPrefsSet(0);
+ fakeApi.allowSetPref();
});
- test('responds to API changes', function() {
+ test('responds to API changes', function testOnChange() {
// Changes from the API should not result in those changes being sent
// back to the API, as this could trigger a race condition.
- mockApi.disallowSetPref();
+ fakeApi.disallowSetPref();
var prefChanges = [];
- for (var testCase of prefsTestCases)
- prefChanges.push({key: testCase.key, value: testCase.values[1]});
+ for (var testCase of prefsTestCases) {
+ prefChanges.push({key: testCase.pref.key,
+ value: testCase.nextValues[0]});
+ }
// Send a set of changes.
- mockApi.sendPrefChanges(prefChanges);
- assertPrefsSet(1);
+ fakeApi.sendPrefChanges(prefChanges);
+ assertPrefsSet(0);
prefChanges = [];
- for (var testCase of prefsTestCases)
- prefChanges.push({key: testCase.key, value: testCase.values[2]});
+ for (var testCase of prefsTestCases) {
+ prefChanges.push({key: testCase.pref.key,
+ value: testCase.nextValues[1]});
+ }
// Send a second set of changes.
- mockApi.sendPrefChanges(prefChanges);
- assertPrefsSet(2);
+ fakeApi.sendPrefChanges(prefChanges);
+ assertPrefsSet(1);
// Send the same set of changes again -- nothing should happen.
- mockApi.sendPrefChanges(prefChanges);
- assertPrefsSet(2);
+ fakeApi.sendPrefChanges(prefChanges);
+ assertPrefsSet(1);
});
});
}
« no previous file with comments | « chrome/test/data/webui/settings/prefs_test_cases.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698