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

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: 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
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..fcd2b1680edea02bff9bf7de4c9cac8cbca51105 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,7 +21,7 @@ cr.define('settings_prefs', function() {
*/
var prefs;
- /** @type {MockSettingsApi} */
+ /** @type {settings.MockSettingsPrivate} */
var mockApi = null;
/**
@@ -196,9 +77,9 @@ 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 MockSettingsPrivate.
+ mockApi = new settings.MockSettingsPrivate();
+ mockApi.register();
// Create and attach the <settings-prefs> elements. Make several of
// them to test that the shared state model scales correctly.
@@ -219,12 +100,13 @@ cr.define('settings_prefs', function() {
teardown(function() {
CrSettingsPrefs.resetForTesting();
- // 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;
+ mockApi.unregister();
});
test('receives and caches prefs', function() {

Powered by Google App Engine
This is Rietveld 408576698