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

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

Issue 1447103002: MD Settings: FakeSettingsPrivate for tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
(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 Mock 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 * Mock of chrome.settingsPrivate API. Override chrome.settingsPrivate by
18 * creating a MockSettingsPrivate and calling register() on it.
19 * @constructor
20 * @extends {chrome.settingsPrivate}
21 */
22 function MockSettingsPrivate() {
23 this.prefs = {};
24
25 // Hack alert: bind this instance's onPrefsChanged members to this.
26 this.onPrefsChanged = {
27 addListener: this.onPrefsChanged.addListener.bind(this),
28 removeListener: this.onPrefsChanged.removeListener.bind(this),
29 };
30
31 for (var testCase of prefsTestCases)
stevenjb 2015/11/16 18:51:13 Where does prefsTestCases come from?
michaelpg 2015/11/17 01:07:40 oops, it comes from prefs_test_cases.js which is s
32 this.addPref_(testCase.type, testCase.key, testCase.values[0]);
33 }
34
35 MockSettingsPrivate.prototype = {
36 // chrome.settingsPrivate overrides.
37 onPrefsChanged: {
38 addListener: function(listener) {
39 this.listener_ = listener;
40 },
41
42 removeListener: function(listener) {
43 this.listener_ = null;
44 },
45 },
46
47 getAllPrefs: function(callback) {
48 // Send a copy of prefs to keep our internal state private.
49 var prefs = [];
50 for (var key in this.prefs)
51 prefs.push(deepCopy(this.prefs[key]));
52
53 // Run the callback asynchronously to test that the prefs aren't actually
54 // used before they become available.
55 setTimeout(callback.bind(null, prefs));
56 },
57
58 setPref: function(key, value, pageId, callback) {
59 var pref = this.prefs[key];
60 assertNotEquals(undefined, pref);
61 assertEquals(typeof value, typeof pref.value);
62 assertEquals(Array.isArray(value), Array.isArray(pref.value));
63
64 if (this.failNextSetPref_) {
65 callback(false);
66 this.failNextSetPref_ = false;
67 return;
68 }
69 assertNotEquals(true, this.disallowSetPref_);
70
71 var changed = JSON.stringify(pref.value) != JSON.stringify(value);
72 pref.value = deepCopy(value);
73 callback(true);
74
75 // Like chrome.settingsPrivate, send a notification when prefs change.
76 if (changed)
77 this.sendPrefChanges([{key: key, value: deepCopy(value)}]);
78 },
79
80 getPref: function(key, callback) {
81 var pref = this.prefs[key];
82 assertNotEquals(undefined, pref);
83 callback(deepCopy(pref));
84 },
85
86 // Functions used by tests.
87
88 /** Replaces chrome.settingsPrivate with MockSettingsPrivate. */
89 register: function() {
90 this.settingsPrivate_ = chrome.settingsPrivate;
91 chrome.settingsPrivate = this;
92 },
93
94 /** Restores chrome.settingsPrivate. */
95 unregister: function() {
96 chrome.settingsPrivate = this.settingsPrivate_;
97 },
98
99 /** Instructs the API to return a failure when setPref is next called. */
100 failNextSetPref: function() {
101 this.failNextSetPref_ = true;
102 },
103
104 /** Instructs the API to assert (fail the test) if setPref is called. */
105 disallowSetPref: function() {
106 this.disallowSetPref_ = true;
107 },
108
109 allowSetPref: function() {
110 this.disallowSetPref_ = false;
111 },
112
113 /**
114 * Notifies the listener of pref changes.
115 * @param {!Object<{key: string, value: *}>} changes
116 */
117 sendPrefChanges: function(changes) {
118 var prefs = [];
119 for (var change of changes) {
120 var pref = this.prefs[change.key];
121 assertNotEquals(undefined, pref);
122 pref.value = change.value;
123 prefs.push(deepCopy(pref));
124 }
125 this.listener_(prefs);
126 },
127
128 // Private methods for use by the mock API.
129
130 /**
131 * @param {!chrome.settingsPrivate.PrefType} type
132 * @param {string} key
133 * @param {*} value
134 */
135 addPref_: function(type, key, value) {
136 this.prefs[key] = {
137 type: type,
138 key: key,
139 value: value,
140 };
141 },
142 };
143
144 return {MockSettingsPrivate: MockSettingsPrivate};
145 });
OLDNEW
« no previous file with comments | « chrome/test/data/webui/settings/cr_settings_browsertest.js ('k') | chrome/test/data/webui/settings/prefs_tests.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698