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)); | |
stevenjb
2015/11/17 18:24:19
Any reason we can't use Object.assign here?
michaelpg
2015/11/17 22:02:11
it's not recursive
stevenjb
2015/11/17 22:03:55
Ah, right.
| |
14 } | |
15 | |
16 /** | |
17 * Fake of chrome.settingsPrivate API. Override chrome.settingsPrivate by | |
18 * creating a FakeSettingsPrivate and calling register() on it. | |
19 * @constructor | |
20 * @param {Array<!settings.FakeSettingsPrivate.Pref>=} opt_initialPrefs | |
21 * @extends {chrome.settingsPrivate} | |
22 */ | |
23 function FakeSettingsPrivate(opt_initialPrefs) { | |
24 this.prefs = {}; | |
25 | |
26 // Hack alert: bind this instance's onPrefsChanged members to this. | |
27 this.onPrefsChanged = { | |
28 addListener: this.onPrefsChanged.addListener.bind(this), | |
29 removeListener: this.onPrefsChanged.removeListener.bind(this), | |
30 }; | |
31 | |
32 if (!opt_initialPrefs) | |
33 return; | |
34 for (var pref of opt_initialPrefs) | |
35 this.addPref_(pref.type, pref.key, pref.value); | |
36 } | |
37 | |
38 FakeSettingsPrivate.prototype = { | |
39 // chrome.settingsPrivate overrides. | |
40 onPrefsChanged: { | |
41 addListener: function(listener) { | |
42 this.listener_ = listener; | |
43 }, | |
44 | |
45 removeListener: function(listener) { | |
46 this.listener_ = null; | |
47 }, | |
48 }, | |
49 | |
50 getAllPrefs: function(callback) { | |
51 // Send a copy of prefs to keep our internal state private. | |
52 var prefs = []; | |
53 for (var key in this.prefs) | |
54 prefs.push(deepCopy(this.prefs[key])); | |
55 | |
56 // Run the callback asynchronously to test that the prefs aren't actually | |
57 // used before they become available. | |
58 setTimeout(callback.bind(null, prefs)); | |
59 }, | |
60 | |
61 setPref: function(key, value, pageId, callback) { | |
62 var pref = this.prefs[key]; | |
63 assertNotEquals(undefined, pref); | |
64 assertEquals(typeof value, typeof pref.value); | |
65 assertEquals(Array.isArray(value), Array.isArray(pref.value)); | |
66 | |
67 if (this.failNextSetPref_) { | |
68 callback(false); | |
69 this.failNextSetPref_ = false; | |
70 return; | |
71 } | |
72 assertNotEquals(true, this.disallowSetPref_); | |
73 | |
74 var changed = JSON.stringify(pref.value) != JSON.stringify(value); | |
75 pref.value = deepCopy(value); | |
76 callback(true); | |
77 | |
78 // Like chrome.settingsPrivate, send a notification when prefs change. | |
79 if (changed) | |
80 this.sendPrefChanges([{key: key, value: deepCopy(value)}]); | |
81 }, | |
82 | |
83 getPref: function(key, callback) { | |
84 var pref = this.prefs[key]; | |
85 assertNotEquals(undefined, pref); | |
86 callback(deepCopy(pref)); | |
87 }, | |
88 | |
89 // Functions used by tests. | |
90 | |
91 /** Replaces chrome.settingsPrivate with FakeSettingsPrivate. */ | |
92 register: function() { | |
93 this.settingsPrivate_ = chrome.settingsPrivate; | |
94 chrome.settingsPrivate = this; | |
Dan Beam
2015/11/17 23:02:07
why is this better?
michaelpg
2015/11/17 23:51:51
The code already uses chrome.settingsPrivate so th
Dan Beam
2015/11/18 02:01:01
generally prefer dependency injection to changing
michaelpg
2015/11/18 02:03:44
OK, that's doable. Would you help me figure out ho
Dan Beam
2015/11/18 02:26:34
/** @interface */
function SettingsPrivate() {}
S
| |
95 }, | |
96 | |
97 /** Restores chrome.settingsPrivate. */ | |
98 unregister: function() { | |
99 chrome.settingsPrivate = this.settingsPrivate_; | |
100 }, | |
101 | |
102 /** Instructs the API to return a failure when setPref is next called. */ | |
103 failNextSetPref: function() { | |
104 this.failNextSetPref_ = true; | |
105 }, | |
106 | |
107 /** Instructs the API to assert (fail the test) if setPref is called. */ | |
108 disallowSetPref: function() { | |
109 this.disallowSetPref_ = true; | |
110 }, | |
111 | |
112 allowSetPref: function() { | |
113 this.disallowSetPref_ = false; | |
114 }, | |
115 | |
116 /** | |
117 * Notifies the listener of pref changes. | |
118 * @param {!Object<{key: string, value: *}>} changes | |
119 */ | |
120 sendPrefChanges: function(changes) { | |
121 var prefs = []; | |
122 for (var change of changes) { | |
123 var pref = this.prefs[change.key]; | |
124 assertNotEquals(undefined, pref); | |
125 pref.value = change.value; | |
126 prefs.push(deepCopy(pref)); | |
127 } | |
128 this.listener_(prefs); | |
129 }, | |
130 | |
131 // Private methods for use by the fake API. | |
132 | |
133 /** | |
134 * @param {!chrome.settingsPrivate.PrefType} type | |
135 * @param {string} key | |
136 * @param {*} value | |
137 */ | |
138 addPref_: function(type, key, value) { | |
139 this.prefs[key] = { | |
140 type: type, | |
141 key: key, | |
142 value: value, | |
143 }; | |
144 }, | |
145 }; | |
146 | |
147 return {FakeSettingsPrivate: FakeSettingsPrivate}; | |
148 }); | |
149 | |
150 /** | |
151 * @type {Array<{key: string, | |
152 * type: chrome.settingsPrivate.PrefType, | |
153 * values: !Array<*>}>} | |
154 */ | |
155 settings.FakeSettingsPrivate.Pref | |
Dan Beam
2015/11/17 23:01:15
nit: ; at end
michaelpg
2015/11/17 23:51:51
Done.
| |
OLD | NEW |