OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** @fileoverview Fake implementation of chrome.settingsPrivate for testing. */ |
| 6 cr.define('settings', function() { |
| 7 /** |
| 8 * Creates a deep copy of the object. |
| 9 * @param {!Object} obj |
| 10 * @return {!Object} |
| 11 */ |
| 12 function deepCopy(obj) { |
| 13 return JSON.parse(JSON.stringify(obj)); |
| 14 } |
| 15 |
| 16 /** |
| 17 * Fake of chrome.settingsPrivate API. Use by setting |
| 18 * CrSettingsPrefs.deferInitialization to true, then passing a |
| 19 * FakeSettingsPrivate to SettingsPrefs.initializeForTesting. |
| 20 * @constructor |
| 21 * @param {Array<!settings.FakeSettingsPrivate.Pref>=} opt_initialPrefs |
| 22 * @implements {SettingsPrivate} |
| 23 */ |
| 24 function FakeSettingsPrivate(opt_initialPrefs) { |
| 25 this.prefs = {}; |
| 26 |
| 27 // Hack alert: bind this instance's onPrefsChanged members to this. |
| 28 this.onPrefsChanged = { |
| 29 addListener: this.onPrefsChanged.addListener.bind(this), |
| 30 removeListener: this.onPrefsChanged.removeListener.bind(this), |
| 31 }; |
| 32 |
| 33 if (!opt_initialPrefs) |
| 34 return; |
| 35 for (var pref of opt_initialPrefs) |
| 36 this.addPref_(pref.type, pref.key, pref.value); |
| 37 } |
| 38 |
| 39 FakeSettingsPrivate.prototype = { |
| 40 // chrome.settingsPrivate overrides. |
| 41 onPrefsChanged: { |
| 42 addListener: function(listener) { |
| 43 this.listener_ = listener; |
| 44 }, |
| 45 |
| 46 removeListener: function(listener) { |
| 47 this.listener_ = null; |
| 48 }, |
| 49 }, |
| 50 |
| 51 getAllPrefs: function(callback) { |
| 52 // Send a copy of prefs to keep our internal state private. |
| 53 var prefs = []; |
| 54 for (var key in this.prefs) |
| 55 prefs.push(deepCopy(this.prefs[key])); |
| 56 |
| 57 // Run the callback asynchronously to test that the prefs aren't actually |
| 58 // used before they become available. |
| 59 setTimeout(callback.bind(null, prefs)); |
| 60 }, |
| 61 |
| 62 setPref: function(key, value, pageId, callback) { |
| 63 var pref = this.prefs[key]; |
| 64 assertNotEquals(undefined, pref); |
| 65 assertEquals(typeof value, typeof pref.value); |
| 66 assertEquals(Array.isArray(value), Array.isArray(pref.value)); |
| 67 |
| 68 if (this.failNextSetPref_) { |
| 69 callback(false); |
| 70 this.failNextSetPref_ = false; |
| 71 return; |
| 72 } |
| 73 assertNotEquals(true, this.disallowSetPref_); |
| 74 |
| 75 var changed = JSON.stringify(pref.value) != JSON.stringify(value); |
| 76 pref.value = deepCopy(value); |
| 77 callback(true); |
| 78 |
| 79 // Like chrome.settingsPrivate, send a notification when prefs change. |
| 80 if (changed) |
| 81 this.sendPrefChanges([{key: key, value: deepCopy(value)}]); |
| 82 }, |
| 83 |
| 84 getPref: function(key, callback) { |
| 85 var pref = this.prefs[key]; |
| 86 assertNotEquals(undefined, pref); |
| 87 callback(deepCopy(pref)); |
| 88 }, |
| 89 |
| 90 // Functions used by tests. |
| 91 |
| 92 /** Instructs the API to return a failure when setPref is next called. */ |
| 93 failNextSetPref: function() { |
| 94 this.failNextSetPref_ = true; |
| 95 }, |
| 96 |
| 97 /** Instructs the API to assert (fail the test) if setPref is called. */ |
| 98 disallowSetPref: function() { |
| 99 this.disallowSetPref_ = true; |
| 100 }, |
| 101 |
| 102 allowSetPref: function() { |
| 103 this.disallowSetPref_ = false; |
| 104 }, |
| 105 |
| 106 /** |
| 107 * Notifies the listener of pref changes. |
| 108 * @param {!Object<{key: string, value: *}>} changes |
| 109 */ |
| 110 sendPrefChanges: function(changes) { |
| 111 var prefs = []; |
| 112 for (var change of changes) { |
| 113 var pref = this.prefs[change.key]; |
| 114 assertNotEquals(undefined, pref); |
| 115 pref.value = change.value; |
| 116 prefs.push(deepCopy(pref)); |
| 117 } |
| 118 this.listener_(prefs); |
| 119 }, |
| 120 |
| 121 // Private methods for use by the fake API. |
| 122 |
| 123 /** |
| 124 * @param {!chrome.settingsPrivate.PrefType} type |
| 125 * @param {string} key |
| 126 * @param {*} value |
| 127 */ |
| 128 addPref_: function(type, key, value) { |
| 129 this.prefs[key] = { |
| 130 type: type, |
| 131 key: key, |
| 132 value: value, |
| 133 }; |
| 134 }, |
| 135 }; |
| 136 |
| 137 return {FakeSettingsPrivate: FakeSettingsPrivate}; |
| 138 }); |
| 139 |
| 140 /** |
| 141 * @type {Array<{key: string, |
| 142 * type: chrome.settingsPrivate.PrefType, |
| 143 * values: !Array<*>}>} |
| 144 */ |
| 145 settings.FakeSettingsPrivate.Pref; |
OLD | NEW |