| 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..95e8f487c94b30d9c6c726e009466fd1ee62cd3c
|
| --- /dev/null
|
| +++ b/chrome/test/data/webui/settings/prefs_tests.js
|
| @@ -0,0 +1,289 @@
|
| +// 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(this.createCopy_(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);
|
| +
|
| + setTimeout(callback.bind(null, this.createCopy_(pref)));
|
| + },
|
| +
|
| + // 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) {
|
| + assertNotEquals(null, this.listener_);
|
| +
|
| + 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(this.createCopy_(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_('BOOLEAN', key, value);
|
| + },
|
| +
|
| + /**
|
| + * @param {string} key
|
| + * @param {number} value
|
| + */
|
| + addNumberPref_: function(key, value) {
|
| + this.addPref_('NUMBER', key, value);
|
| + },
|
| +
|
| + /**
|
| + * @param {string} key
|
| + * @param {string} value
|
| + */
|
| + addStringPref_: function(key, value) {
|
| + this.addPref_('STRING', key, value);
|
| + },
|
| +
|
| + /**
|
| + * Creates a deep copy of the preference.
|
| + * @param {chrome.settingsPrivate.PrefObject} pref
|
| + * @return {chrome.settingsPrivate.PrefObject}
|
| + */
|
| + createCopy_: function(pref) {
|
| + return JSON.parse(JSON.stringify(pref));
|
| + },
|
| + };
|
| +
|
| + /**
|
| + * 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();
|
| + }
|
| + });
|
| + });
|
| + }
|
| +
|
| + 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 = prefs.prefs;
|
| + // Find the preference.
|
| + var path = key.split('.');
|
| + var skipPref = false;
|
| + for (var part of path) {
|
| + actualPref = actualPref[part];
|
| + if (!expectNotEquals(undefined, actualPref)) {
|
| + // We've already registered an error, so skip the pref.
|
| + skipPref = true;
|
| + break;
|
| + }
|
| + }
|
| + if (skipPref)
|
| + 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]);
|
| + }).async(function() {
|
| + expectEquals(true, prefs.prefs.top_level_pref.value);
|
| + done();
|
| + });
|
| + });
|
| + });
|
| + }
|
| +
|
| + return {
|
| + registerTests: registerTests,
|
| + };
|
| +});
|
|
|