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

Side by Side Diff: remoting/webapp/unittests/chrome_mocks.js

Issue 848993002: Improve apps v2 upgrade UX (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address outstanding feedbacks Created 5 years, 11 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
« no previous file with comments | « remoting/webapp/unittests/apps_v2_migration_unittest.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 // This file contains various mock objects for the chrome platform to make 5 // This file contains various mock objects for the chrome platform to make
6 // unit testing easier. 6 // unit testing easier.
7 7
8 Entry = function() {}; 8 Entry = function() {};
9 9
10 (function(scope){ 10 (function(scope){
(...skipping 29 matching lines...) Expand all
40 this.onMessage = new chromeMocks.Event(); 40 this.onMessage = new chromeMocks.Event();
41 this.onDisconnect = new chromeMocks.Event(); 41 this.onDisconnect = new chromeMocks.Event();
42 42
43 this.name = ''; 43 this.name = '';
44 this.sender = null; 44 this.sender = null;
45 }; 45 };
46 46
47 chromeMocks.runtime.Port.prototype.disconnect = function() {}; 47 chromeMocks.runtime.Port.prototype.disconnect = function() {};
48 chromeMocks.runtime.Port.prototype.postMessage = function() {}; 48 chromeMocks.runtime.Port.prototype.postMessage = function() {};
49 49
50 chromeMocks.storage = {};
51
52 // Sample implementation of chrome.StorageArea according to
53 // https://developer.chrome.com/apps/storage#type-StorageArea
54 chromeMocks.StorageArea = function() {
55 this.storage_ = {};
56 };
57
58 function deepCopy(value) {
59 return JSON.parse(JSON.stringify(value));
60 }
61
62 function getKeys(keys) {
63 if (typeof keys === 'string') {
64 return [keys];
65 } else if (typeof keys === 'object') {
66 return Object.keys(keys);
67 }
68 return [];
69 }
70
71 chromeMocks.StorageArea.prototype.get = function(keys, onDone) {
72 if (!keys) {
73 onDone(deepCopy(this.storage_));
74 return;
75 }
76
77 var result = (typeof keys === 'object') ? keys : {};
78 getKeys(keys).forEach(function(key) {
79 if (key in this.storage_) {
80 result[key] = deepCopy(this.storage_[key]);
81 }
82 }, this);
83 onDone(result);
84 };
85
86 chromeMocks.StorageArea.prototype.set = function(value) {
87 for (var key in value) {
88 this.storage_[key] = deepCopy(value[key]);
89 }
90 };
91
92 chromeMocks.StorageArea.prototype.remove = function(keys) {
93 getKeys(keys).forEach(function(key) {
94 delete this.storage_[key];
95 }, this);
96 };
97
98 chromeMocks.StorageArea.prototype.clear = function() {
99 this.storage_ = null;
100 };
101
102 chromeMocks.storage.local = new chromeMocks.StorageArea();
103
104 var originals_ = null;
105
106 /**
107 * Activates a list of Chrome components to mock
108 * @param {Array.<string>} components
109 */
110 chromeMocks.activate = function(components) {
111 if (originals_) {
112 throw new Error('chromeMocks.activate() can only be called once.');
113 }
114 originals_ = {};
115 components.forEach(function(component) {
116 if (!chromeMocks[component]) {
117 throw new Error('No mocks defined for chrome.' + component);
118 }
119 originals_[component] = chrome[component];
120 chrome[component] = chromeMocks[component];
121 });
122 };
123
124 chromeMocks.restore = function() {
125 if (!originals_) {
126 throw new Error('You must call activate() before restore().');
127 }
128 for (var components in originals_) {
129 chrome[components] = originals_[components];
130 }
131 originals_ = null;
132 };
133
50 scope.chromeMocks = chromeMocks; 134 scope.chromeMocks = chromeMocks;
51 135
52 })(window); 136 })(window);
OLDNEW
« no previous file with comments | « remoting/webapp/unittests/apps_v2_migration_unittest.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698