Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b6272aec047c934b9ea7de76dab30b1acd64491e |
| --- /dev/null |
| +++ b/chrome/test/data/webui/settings/prefs_tests.js |
| @@ -0,0 +1,291 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** @fileoverview Suite of tests for cr-settings-prefs. */ |
| +cr.define('cr_settings_prefs', function() { |
| + /** |
| + * Mock of chrome.settingsPrivate API. |
| + * @constructor |
| + * @extends {chrome.settingsPrivate} |
| + */ |
| + function MockSettingsApi() { |
| + this.prefs = {}; |
| + this.listener_ = null; |
| + |
| + // Hack alert: bind this instance's onPrefsChanged members to this. |
| + this.onPrefsChanged = { |
| + addListener: this.onPrefsChanged.addListener.bind(this), |
| + removeListener: this.onPrefsChanged.removeListener.bind(this), |
| + }; |
| + |
| + this.addBooleanPref_('top_level_pref', true); |
| + this.addBooleanPref_('browser.enable_flash', false); |
| + this.addBooleanPref_('browser.enable_html5', true); |
| + this.addNumberPref_('device.overclock', .6); |
| + this.addStringPref_('homepage', 'example.com'); |
| + } |
| + |
| + MockSettingsApi.prototype = { |
| + // chrome.settingsPrivate overrides. |
| + onPrefsChanged: { |
| + addListener: function(listener) { |
| + this.listener_ = listener; |
| + }, |
| + |
| + removeListener: function(listener) { |
| + expectNotEquals(null, this.listener_); |
| + this.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(Object.assign({}, this.prefs[key])); |
| + |
| + 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_) { |
| + setTimeout(callback.bind(null, false)); |
| + this.failNextSetPref_ = false; |
| + return; |
| + } |
| + |
| + // TODO(michaelpg): support list and dict prefs. |
| + var changed = this.prefs[key].value != value; |
| + this.prefs[key].value = value; |
| + setTimeout(function() { |
| + callback(true); |
| + // Like chrome.settingsPrivate, send a notification when prefs change. |
| + if (changed) |
| + this.sendPrefChanges([key], [value]); |
| + }.bind(this)); |
| + }, |
| + |
| + getPref: function(key, callback) { |
| + var pref = this.prefs[key]; |
| + assertNotEquals(undefined, pref); |
| + |
| + var prefCopy = Object.assign({}, pref); |
| + setTimeout(callback.bind(null, prefCopy)); |
| + }, |
| + |
| + // Functions used by tests. |
| + |
| + /** Instructs the API to fail when setPref is next called. */ |
| + failNextSetPref: function() { |
| + this.failNextSetPref_ = true; |
| + }, |
| + |
| + /** |
| + * Notifies the listener of pref changes. |
| + * @param {!Array<string>} keys |
| + * @param {!Array<*>} values |
| + */ |
| + sendPrefChanges: function(keys, values) { |
| + var prefs = []; |
| + for (var i = 0; i < keys.length; i++) { |
| + var key = keys[i]; |
| + var pref = this.prefs[key]; |
| + pref.value = values[i]; |
| + prefs.push(Object.assign({}, pref)); |
| + } |
| + |
| + setTimeout(this.listener_.bind(null, 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, |
| + }; |
| + }, |
| + |
| + /** |
| + * @param {string} key |
| + * @param {boolean} value |
| + */ |
| + addBooleanPref_: function(key, value) { |
| + this.addPref_(chrome.settingsPrivate.PrefType.BOOLEAN, key, value); |
| + }, |
| + |
| + /** |
| + * @param {string} key |
| + * @param {number} value |
| + */ |
| + addNumberPref_: function(key, value) { |
| + this.addPref_(chrome.settingsPrivate.PrefType.NUMBER, key, value); |
| + }, |
| + |
| + /** |
| + * @param {string} key |
| + * @param {string} value |
| + */ |
| + addStringPref_: function(key, value) { |
| + this.addPref_(chrome.settingsPrivate.PrefType.STRING, key, value); |
| + }, |
| + }; |
| + |
| + /** |
| + * Helper function to deal with asynchronicity by repeatedly queueing |
| + * tasks to check if the callback returns true. |
| + * @param {function(): boolean} callback |
| + * @return {Promise} |
| + */ |
| + function waitUntilTrue(callback) { |
| + return new Promise(function(resolve, reject) { |
| + var interval = setInterval(function() { |
| + if (callback.call()) { |
| + clearInterval(interval); |
| + resolve(); |
| + } |
| + }); |
| + }); |
| + } |
| + |
| + /** |
| + * @param {!Object} prefStore Pref store from <cr-settings-prefs>. |
| + * @param {string} key Pref key of the pref to return. |
| + * @return {(chrome.settingsPrivate.PrefObject|undefined} |
|
Dan Beam
2015/09/14 23:31:48
nit: https://xkcd.com/859/
michaelpg
2015/09/15 01:17:57
Done.
|
| + */ |
| + function getPrefFromKey(prefStore, key) { |
| + var path = key.split('.'); |
| + var pref = prefStore; |
| + for (var part of path) { |
| + pref = pref[part]; |
| + if (!pref) |
| + return; |
|
stevenjb
2015/09/14 23:18:19
nit: return undefined? (That would be more clear t
Dan Beam
2015/09/14 23:31:48
not all that common
michaelpg
2015/09/15 01:17:58
Done.
|
| + } |
| + return pref; |
| + } |
| + |
| + function registerTests() { |
| + suite('CrSettingsPrefs', function() { |
| + /** |
| + * Prefs instance created before each test. |
| + * @type {CrSettingsPrefs} |
| + */ |
| + var prefs; |
| + |
| + /** @type {MockSettingsApi} */ |
| + var mockApi = null; |
| + |
| + // Initialize a <cr-settings-prefs> element before each test. |
| + setup(function(done) { |
| + mockApi = new MockSettingsApi(); |
| + // TODO(michaelpg): don't use global variables to inject the API. |
| + window.mockApi = mockApi; |
| + |
| + // Create and attach the <cr-settings-prefs> element. |
| + PolymerTest.clearBody(); |
| + prefs = document.createElement('cr-settings-prefs'); |
| + document.body.appendChild(prefs); |
| + |
| + window.mockApi = undefined; |
| + |
| + // Wait for CrSettingsPrefs.INITIALIZED. |
| + if (!CrSettingsPrefs.isInitialized) { |
| + var listener = function() { |
| + document.removeEventListener(CrSettingsPrefs.INITIALIZED, listener); |
| + done(); |
| + }; |
| + document.addEventListener(CrSettingsPrefs.INITIALIZED, listener); |
| + return; |
| + } |
| + |
| + done(); |
| + }); |
| + |
| + test('receives and caches prefs', function() { |
| + // Test that each pref has been successfully copied to the Polymer |
| + // |prefs| property. |
| + for (var key in mockApi.prefs) { |
| + var expectedPref = mockApi.prefs[key]; |
| + var actualPref = getPrefFromKey(prefs.prefs, key); |
| + if (!expectNotEquals(undefined, actualPref)) { |
| + // We've already registered an error, so skip the pref. |
| + continue; |
| + } |
| + |
| + expectEquals(expectedPref.key, actualPref.key); |
| + expectEquals(expectedPref.type, actualPref.type); |
| + if (expectedPref.type != chrome.settingsPrivate.LIST) { |
| + expectEquals(expectedPref.value, actualPref.value); |
| + } else { |
| + expectEquals(JSON.stringify(expectedPref.value), |
| + JSON.stringify(actualPref.value)); |
| + } |
| + } |
| + }); |
| + |
| + test('forwards pref changes to API', function() { |
| + // Test that cr-settings-prefs uses the setPref API. |
| + prefs.set('prefs.browser.enable_flash.value', true); |
| + expectTrue(mockApi.prefs['browser.enable_flash'].value); |
| + |
| + prefs.set('prefs.device.overclock.value', 1.2); |
| + expectEquals(1.2, mockApi.prefs['device.overclock'].value); |
| + |
| + prefs.set('prefs.homepage.value', 'chromium.org'); |
| + expectEquals('chromium.org', mockApi.prefs['homepage'].value); |
| + |
| + // Test that when setPref fails, the pref is reverted locally. |
| + mockApi.failNextSetPref(); |
| + prefs.set('prefs.browser.enable_flash.value', false); |
| + |
| + mockApi.failNextSetPref(); |
| + prefs.set('prefs.device.overclock.value', 2.0); |
| + |
| + mockApi.failNextSetPref(); |
| + prefs.set('prefs.homepage.value', 'invalid-url'); |
| + |
| + // Multiple asynchronous round trips need to happen. |
| + return Promise.all([ |
| + waitUntilTrue(function() { |
| + return prefs.prefs.browser.enable_flash.value; |
| + }), |
| + waitUntilTrue(function() { |
| + return prefs.prefs.device.overclock.value == 1.2; |
| + }), |
| + waitUntilTrue(function() { |
| + return prefs.prefs.homepage.value == 'chromium.org'; |
| + }), |
| + ]); |
| + }); |
| + |
| + test('responds to API changes', function(done) { |
| + mockApi.sendPrefChanges(['top_level_pref'], [false]); |
| + PolymerTest.async(function() { |
| + expectEquals(false, prefs.prefs.top_level_pref.value); |
| + |
| + mockApi.sendPrefChanges(['top_level_pref'], [true]); |
| + }); |
| + PolymerTest.async(function() { |
| + expectEquals(true, prefs.prefs.top_level_pref.value); |
| + done(); |
| + }); |
| + }); |
| + }); |
| + } |
| + |
| + return { |
| + registerTests: registerTests, |
| + }; |
| +}); |