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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/webapp/unittests/apps_v2_migration_unittest.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/webapp/unittests/chrome_mocks.js
diff --git a/remoting/webapp/unittests/chrome_mocks.js b/remoting/webapp/unittests/chrome_mocks.js
index 15d8a6a549f09a32341dbac74e9fc823d006f963..7a21da3c288d2bdf02d3e3056c7d8306a5972913 100644
--- a/remoting/webapp/unittests/chrome_mocks.js
+++ b/remoting/webapp/unittests/chrome_mocks.js
@@ -47,6 +47,90 @@ chromeMocks.runtime.Port = function() {
chromeMocks.runtime.Port.prototype.disconnect = function() {};
chromeMocks.runtime.Port.prototype.postMessage = function() {};
+chromeMocks.storage = {};
+
+// Sample implementation of chrome.StorageArea according to
+// https://developer.chrome.com/apps/storage#type-StorageArea
+chromeMocks.StorageArea = function() {
+ this.storage_ = {};
+};
+
+function deepCopy(value) {
+ return JSON.parse(JSON.stringify(value));
+}
+
+function getKeys(keys) {
+ if (typeof keys === 'string') {
+ return [keys];
+ } else if (typeof keys === 'object') {
+ return Object.keys(keys);
+ }
+ return [];
+}
+
+chromeMocks.StorageArea.prototype.get = function(keys, onDone) {
+ if (!keys) {
+ onDone(deepCopy(this.storage_));
+ return;
+ }
+
+ var result = (typeof keys === 'object') ? keys : {};
+ getKeys(keys).forEach(function(key) {
+ if (key in this.storage_) {
+ result[key] = deepCopy(this.storage_[key]);
+ }
+ }, this);
+ onDone(result);
+};
+
+chromeMocks.StorageArea.prototype.set = function(value) {
+ for (var key in value) {
+ this.storage_[key] = deepCopy(value[key]);
+ }
+};
+
+chromeMocks.StorageArea.prototype.remove = function(keys) {
+ getKeys(keys).forEach(function(key) {
+ delete this.storage_[key];
+ }, this);
+};
+
+chromeMocks.StorageArea.prototype.clear = function() {
+ this.storage_ = null;
+};
+
+chromeMocks.storage.local = new chromeMocks.StorageArea();
+
+var originals_ = null;
+
+/**
+ * Activates a list of Chrome components to mock
+ * @param {Array.<string>} components
+ */
+chromeMocks.activate = function(components) {
+ if (originals_) {
+ throw new Error('chromeMocks.activate() can only be called once.');
+ }
+ originals_ = {};
+ components.forEach(function(component) {
+ if (!chromeMocks[component]) {
+ throw new Error('No mocks defined for chrome.' + component);
+ }
+ originals_[component] = chrome[component];
+ chrome[component] = chromeMocks[component];
+ });
+};
+
+chromeMocks.restore = function() {
+ if (!originals_) {
+ throw new Error('You must call activate() before restore().');
+ }
+ for (var components in originals_) {
+ chrome[components] = originals_[components];
+ }
+ originals_ = null;
+};
+
scope.chromeMocks = chromeMocks;
})(window);
« 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