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

Side by Side Diff: chrome/test/data/webui/settings/fake_settings_private.js

Issue 1532503003: Add FakeChromeEvent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use Set for listeners Created 5 years 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** @fileoverview Fake implementation of chrome.settingsPrivate for testing. */ 5 /** @fileoverview Fake implementation of chrome.settingsPrivate for testing. */
6 cr.define('settings', function() { 6 cr.define('settings', function() {
7 /** 7 /**
8 * Creates a deep copy of the object. 8 * Creates a deep copy of the object.
9 * @param {!Object} obj 9 * @param {!Object} obj
10 * @return {!Object} 10 * @return {!Object}
11 */ 11 */
12 function deepCopy(obj) { 12 function deepCopy(obj) {
13 return JSON.parse(JSON.stringify(obj)); 13 return JSON.parse(JSON.stringify(obj));
14 } 14 }
15 15
16 /** 16 /**
17 * Fake of chrome.settingsPrivate API. Use by setting 17 * Fake of chrome.settingsPrivate API. Use by setting
18 * CrSettingsPrefs.deferInitialization to true, then passing a 18 * CrSettingsPrefs.deferInitialization to true, then passing a
19 * FakeSettingsPrivate to SettingsPrefs.initializeForTesting. 19 * FakeSettingsPrivate to SettingsPrefs.initializeForTesting.
20 * @constructor 20 * @constructor
21 * @param {Array<!settings.FakeSettingsPrivate.Pref>=} opt_initialPrefs 21 * @param {Array<!settings.FakeSettingsPrivate.Pref>=} opt_initialPrefs
22 * @implements {SettingsPrivate} 22 * @implements {SettingsPrivate}
23 */ 23 */
24 function FakeSettingsPrivate(opt_initialPrefs) { 24 function FakeSettingsPrivate(opt_initialPrefs) {
25 this.prefs = {}; 25 this.prefs = {};
26 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) 27 if (!opt_initialPrefs)
34 return; 28 return;
35 for (var pref of opt_initialPrefs) 29 for (var pref of opt_initialPrefs)
36 this.addPref_(pref.type, pref.key, pref.value); 30 this.addPref_(pref.type, pref.key, pref.value);
37 } 31 }
38 32
39 FakeSettingsPrivate.prototype = { 33 FakeSettingsPrivate.prototype = {
40 // chrome.settingsPrivate overrides. 34 // chrome.settingsPrivate overrides.
41 onPrefsChanged: { 35 onPrefsChanged: new FakeChromeEvent(),
42 addListener: function(listener) {
43 this.listener_ = listener;
44 },
45
46 removeListener: function(listener) {
47 this.listener_ = null;
48 },
49 },
50 36
51 getAllPrefs: function(callback) { 37 getAllPrefs: function(callback) {
52 // Send a copy of prefs to keep our internal state private. 38 // Send a copy of prefs to keep our internal state private.
53 var prefs = []; 39 var prefs = [];
54 for (var key in this.prefs) 40 for (var key in this.prefs)
55 prefs.push(deepCopy(this.prefs[key])); 41 prefs.push(deepCopy(this.prefs[key]));
56 42
57 // Run the callback asynchronously to test that the prefs aren't actually 43 // Run the callback asynchronously to test that the prefs aren't actually
58 // used before they become available. 44 // used before they become available.
59 setTimeout(callback.bind(null, prefs)); 45 setTimeout(callback.bind(null, prefs));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 /** Instructs the API to assert (fail the test) if setPref is called. */ 83 /** Instructs the API to assert (fail the test) if setPref is called. */
98 disallowSetPref: function() { 84 disallowSetPref: function() {
99 this.disallowSetPref_ = true; 85 this.disallowSetPref_ = true;
100 }, 86 },
101 87
102 allowSetPref: function() { 88 allowSetPref: function() {
103 this.disallowSetPref_ = false; 89 this.disallowSetPref_ = false;
104 }, 90 },
105 91
106 /** 92 /**
107 * Notifies the listener of pref changes. 93 * Notifies the listeners of pref changes.
108 * @param {!Object<{key: string, value: *}>} changes 94 * @param {!Object<{key: string, value: *}>} changes
109 */ 95 */
110 sendPrefChanges: function(changes) { 96 sendPrefChanges: function(changes) {
111 var prefs = []; 97 var prefs = [];
112 for (var change of changes) { 98 for (var change of changes) {
113 var pref = this.prefs[change.key]; 99 var pref = this.prefs[change.key];
114 assertNotEquals(undefined, pref); 100 assertNotEquals(undefined, pref);
115 pref.value = change.value; 101 pref.value = change.value;
116 prefs.push(deepCopy(pref)); 102 prefs.push(deepCopy(pref));
117 } 103 }
118 this.listener_(prefs); 104 this.onPrefsChanged.callListeners(prefs);
119 }, 105 },
120 106
121 // Private methods for use by the fake API. 107 // Private methods for use by the fake API.
122 108
123 /** 109 /**
124 * @param {!chrome.settingsPrivate.PrefType} type 110 * @param {!chrome.settingsPrivate.PrefType} type
125 * @param {string} key 111 * @param {string} key
126 * @param {*} value 112 * @param {*} value
127 */ 113 */
128 addPref_: function(type, key, value) { 114 addPref_: function(type, key, value) {
129 this.prefs[key] = { 115 this.prefs[key] = {
130 type: type, 116 type: type,
131 key: key, 117 key: key,
132 value: value, 118 value: value,
133 }; 119 };
134 }, 120 },
135 }; 121 };
136 122
137 return {FakeSettingsPrivate: FakeSettingsPrivate}; 123 return {FakeSettingsPrivate: FakeSettingsPrivate};
138 }); 124 });
139 125
140 /** 126 /**
141 * @type {Array<{key: string, 127 * @type {Array<{key: string,
142 * type: chrome.settingsPrivate.PrefType, 128 * type: chrome.settingsPrivate.PrefType,
143 * values: !Array<*>}>} 129 * values: !Array<*>}>}
144 */ 130 */
145 settings.FakeSettingsPrivate.Pref; 131 settings.FakeSettingsPrivate.Pref;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698