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

Side by Side Diff: chrome/browser/resources/settings/prefs/prefs.js

Issue 1310843010: Add Polymer tests for cr-settings-prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: preempt stevenjb comments :) Created 5 years, 3 months 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 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * 'cr-settings-prefs' models Chrome settings and preferences, listening for 7 * 'cr-settings-prefs' models Chrome settings and preferences, listening for
8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate. 8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate.
9 * When changing prefs in this element's 'prefs' property via the UI, this 9 * When changing prefs in this element's 'prefs' property via the UI, this
10 * element tries to set those preferences in Chrome. Whether or not the calls to 10 * element tries to set those preferences in Chrome. Whether or not the calls to
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 prefWrappers_: { 100 prefWrappers_: {
101 type: Object, 101 type: Object,
102 value: function() { return {}; }, 102 value: function() { return {}; },
103 }, 103 },
104 }, 104 },
105 105
106 observers: [ 106 observers: [
107 'prefsChanged_(prefs.*)', 107 'prefsChanged_(prefs.*)',
108 ], 108 ],
109 109
110 settingsApi_: chrome.settingsPrivate,
111
110 /** @override */ 112 /** @override */
111 created: function() { 113 created: function() {
114 // Set window.mockApi to pass a custom settings API, i.e. for tests.
115 // TODO(michaelpg): don't use a global.
116 if (window.mockApi)
117 this.settingsApi_ = window.mockApi;
112 CrSettingsPrefs.isInitialized = false; 118 CrSettingsPrefs.isInitialized = false;
113 119
114 chrome.settingsPrivate.onPrefsChanged.addListener( 120 this.settingsApi_.onPrefsChanged.addListener(
115 this.onSettingsPrivatePrefsChanged_.bind(this)); 121 this.onSettingsPrivatePrefsChanged_.bind(this));
116 chrome.settingsPrivate.getAllPrefs( 122 this.settingsApi_.getAllPrefs(
117 this.onSettingsPrivatePrefsFetched_.bind(this)); 123 this.onSettingsPrivatePrefsFetched_.bind(this));
118 }, 124 },
119 125
120 /** 126 /**
121 * Polymer callback for changes to this.prefs. 127 * Polymer callback for changes to this.prefs.
122 * @param {!{path: string, value: *}} change 128 * @param {!{path: string, value: *}} change
123 * @private 129 * @private
124 */ 130 */
125 prefsChanged_: function(change) { 131 prefsChanged_: function(change) {
126 if (!CrSettingsPrefs.isInitialized) 132 if (!CrSettingsPrefs.isInitialized)
127 return; 133 return;
128 134
129 var key = this.getPrefKeyFromPath_(change.path); 135 var key = this.getPrefKeyFromPath_(change.path);
136 if (!key)
stevenjb 2015/09/11 21:03:47 Should this normally happen? If not, maybe log an
michaelpg 2015/09/13 03:01:05 I can't recall why or when this may happen. It's p
137 return;
138
130 var prefWrapper = this.prefWrappers_[key]; 139 var prefWrapper = this.prefWrappers_[key];
131 if (!prefWrapper) 140 if (!prefWrapper)
132 return; 141 return;
133 142
134 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( 143 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */(
135 this.get(key, this.prefs)); 144 this.get(key, this.prefs));
136 145
137 // If settingsPrivate already has this value, do nothing. (Otherwise, 146 // If settingsPrivate already has this value, do nothing. (Otherwise,
138 // a change event from settingsPrivate could make us call 147 // a change event from settingsPrivate could make us call
139 // settingsPrivate.setPref and potentially trigger an IPC loop.) 148 // settingsPrivate.setPref and potentially trigger an IPC loop.)
140 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) 149 if (prefWrapper.equals(this.createPrefWrapper_(prefObj)))
141 return; 150 return;
142 151
143 chrome.settingsPrivate.setPref( 152 this.settingsApi_.setPref(
144 key, 153 key,
145 prefObj.value, 154 prefObj.value,
146 /* pageId */ '', 155 /* pageId */ '',
147 /* callback */ this.setPrefCallback_.bind(this, key)); 156 /* callback */ this.setPrefCallback_.bind(this, key));
148 }, 157 },
149 158
150 /** 159 /**
151 * Called when prefs in the underlying Chrome pref store are changed. 160 * Called when prefs in the underlying Chrome pref store are changed.
152 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs 161 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
153 * The prefs that changed. 162 * The prefs that changed.
(...skipping 21 matching lines...) Expand all
175 * @param {string} key The key used in the call to setPref. 184 * @param {string} key The key used in the call to setPref.
176 * @param {boolean} success True if setting the pref succeeded. 185 * @param {boolean} success True if setting the pref succeeded.
177 * @private 186 * @private
178 */ 187 */
179 setPrefCallback_: function(key, success) { 188 setPrefCallback_: function(key, success) {
180 if (success) 189 if (success)
181 return; 190 return;
182 191
183 // Get the current pref value from chrome.settingsPrivate to ensure the 192 // Get the current pref value from chrome.settingsPrivate to ensure the
184 // UI stays up to date. 193 // UI stays up to date.
185 chrome.settingsPrivate.getPref(key, function(pref) { 194 this.settingsApi_.getPref(key, function(pref) {
186 this.updatePrefs_([pref]); 195 this.updatePrefs_([pref]);
187 }.bind(this)); 196 }.bind(this));
188 }, 197 },
189 198
190 /** 199 /**
191 * Updates the prefs model with the given prefs. 200 * Updates the prefs model with the given prefs.
192 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs 201 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
193 * @private 202 * @private
194 */ 203 */
195 updatePrefs_: function(prefs) { 204 updatePrefs_: function(prefs) {
(...skipping 26 matching lines...) Expand all
222 for (let i = 1; i <= parts.length; i++) { 231 for (let i = 1; i <= parts.length; i++) {
223 let key = parts.slice(0, i).join('.'); 232 let key = parts.slice(0, i).join('.');
224 // The prefWrappers_ keys match the pref keys. 233 // The prefWrappers_ keys match the pref keys.
225 if (this.prefWrappers_[key] != undefined) 234 if (this.prefWrappers_[key] != undefined)
226 return key; 235 return key;
227 } 236 }
228 return ''; 237 return '';
229 }, 238 },
230 239
231 /** 240 /**
232 * Sets or updates the pref denoted by newPrefObj.key in the publicy exposed 241 * Sets or updates the pref denoted by newPrefObj.key in the publicly
233 * |prefs| property. 242 * exposed |prefs| property.
234 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to 243 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to
235 * update the pref with. 244 * update the pref with.
236 * @private 245 * @private
237 */ 246 */
238 setPref_: function(newPrefObj) { 247 setPref_: function(newPrefObj) {
239 // Check if the pref exists already in the Polymer |prefs| object. 248 // Add the pref to |prefs|. cr.exportPath doesn't use Polymer.Base.set,
240 if (this.get(newPrefObj.key, this.prefs)) { 249 // which is OK because the nested property update events aren't useful.
241 // Update just the value, notifying listeners of the change. 250 cr.exportPath(newPrefObj.key, newPrefObj, this.prefs);
242 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value); 251 // Notify listeners of the change at the preference key.
243 } else { 252 this.notifyPath('prefs.' + newPrefObj.key, newPrefObj);
244 // Add the pref to |prefs|. cr.exportPath doesn't use Polymer.Base.set,
245 // which is OK because the nested property update events aren't useful.
246 cr.exportPath(newPrefObj.key, newPrefObj, this.prefs);
247 // Notify listeners of the change at the preference key.
248 this.notifyPath('prefs.' + newPrefObj.key, newPrefObj);
249 }
250 }, 253 },
251 254
252 /** 255 /**
253 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. 256 * Creates a PrefWrapper object from a chrome.settingsPrivate pref.
254 * @param {!chrome.settingsPrivate.PrefObject} prefObj 257 * @param {!chrome.settingsPrivate.PrefObject} prefObj
255 * PrefObject received from chrome.settingsPrivate. 258 * PrefObject received from chrome.settingsPrivate.
256 * @return {PrefWrapper} An object containing a copy of the PrefObject's 259 * @return {PrefWrapper} An object containing a copy of the PrefObject's
257 * value. 260 * value.
258 * @private 261 * @private
259 */ 262 */
260 createPrefWrapper_: function(prefObj) { 263 createPrefWrapper_: function(prefObj) {
261 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? 264 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ?
262 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); 265 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj);
263 }, 266 },
264 }); 267 });
265 })(); 268 })();
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | chrome/test/data/webui/polymer_browser_test_base.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698