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

Unified Diff: chrome/test/data/extensions/api_test/settings/split_incognito/background.html

Issue 8177022: Add onChanged events to the extension settings API, both from sync and between (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 2 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
Index: chrome/test/data/extensions/api_test/settings/split_incognito/background.html
diff --git a/chrome/test/data/extensions/api_test/settings/split_incognito/background.html b/chrome/test/data/extensions/api_test/settings/split_incognito/background.html
index 1a8fb7bab04d6a499a9aae06143600ddf42e0a0e..54426cde188650d3c3bfb17a7ff5d4cbe0ea0653 100644
--- a/chrome/test/data/extensions/api_test/settings/split_incognito/background.html
+++ b/chrome/test/data/extensions/api_test/settings/split_incognito/background.html
@@ -1,59 +1,107 @@
<script>
-
var settings = chrome.experimental.settings;
+var assertEq = chrome.test.assertEq;
+var inIncognitoContext = chrome.extension.inIncognitoContext;
-// A different "waiting" message depending on whether this background page runs
-// in incognito mode or not. This is so that the C++ test can differentiate
-// between messages.
-
-// Returns a function that asserts a result is expected.
-function assertResultEq(expected) {
+// Returns a function that asserts a result is expected then runs a callback.
+function assertResultEq(expected, callback) {
return function(actual) {
chrome.test.assertEq(expected, actual);
+ callback();
};
}
+// Retries a test action in the future.
+function retry(name, callback) {
+ var delayMs = 1000;
+ console.log("Retrying " + name + " in " + delayMs + "ms");
+ setTimeout(function() {
+ testActions[name](callback);
+ }, delayMs);
+}
+
+// All notifications received.
+var notifications = [];
+settings.onChanged.addListener(function(changes) {
+ changes.forEach(function(change) {
+ notifications.push(change);
+ });
+});
+
// The test from C++ runs "actions", where each action is defined here.
// This allows the test to be tightly controlled between incognito and
// non-incognito modes.
+// Each function accepts a callback which should be run when the settings
+// operation fully completes.
var testActions = {
- noop: function() {
- settings.get("", function() {});
+ noop: function(callback) {
+ settings.get("", callback);
+ },
+ assertEmpty: function(callback) {
+ settings.get(null, assertResultEq({}, callback));
+ },
+ assertFoo: function(callback) {
+ settings.get(null, assertResultEq({foo: "bar"}, callback));
+ },
+ setFoo: function(callback) {
+ settings.set({foo: "bar"}, assertResultEq({foo: "bar"}, callback));
+ },
+ removeFoo: function(callback) {
+ settings.remove("foo", assertResultEq(undefined, callback));
},
- assertEmpty: function() {
- settings.get(null, assertResultEq({}));
+ clear: function(callback) {
+ settings.clear(assertResultEq(undefined, callback));
},
- assertFoobar: function() {
- settings.get(null, assertResultEq({foo: "bar"}));
+ assertNoNotifications: function(callback) {
+ // On Windows, the notifications seem to be unpredicably delivered, so poll
+ // until they arrive (or the test times out).
+ if (notifications.length !== 0) {
+ retry("assertNoNotifications", callback);
Matt Perry 2011/10/12 17:23:01 Polling is almost always a bad idea in tests. Can
+ return;
+ }
+ callback();
},
- setFoobar: function() {
- settings.set({foo: "bar"}, assertResultEq({foo: "bar"}));
+ clearNotifications: function(callback) {
+ notifications = [];
+ callback();
},
- removeFoo: function() {
- settings.remove("foo", assertResultEq(undefined));
+ assertAddFooNotification: function(callback) {
+ // Ditto.
+ if (notifications.length !== 1) {
+ retry("assertAddFooNotification", callback);
+ return;
+ }
+ assertEq("foo", notifications[0].key);
+ assertEq(undefined, notifications[0].oldValue);
+ assertEq("bar", notifications[0].newValue);
+ callback();
},
- clear: function() {
- settings.clear(assertResultEq(undefined));
+ assertDeleteFooNotification: function(callback) {
+ // Ditto.
+ if (notifications.length !== 1) {
+ retry("assertDeleteFooNotification", callback);
+ return;
+ }
+ assertEq("foo", notifications[0].key);
+ assertEq("bar", notifications[0].oldValue);
+ assertEq(undefined, notifications[0].newValue);
+ callback();
}
};
// The only test we run. Runs "actions" (as defined above) until told
// to stop (when the message has isFinalAction set to true).
function testEverything() {
- var waiting = chrome.extension.inIncognitoContext ?
- "waiting_incognito" : "waiting";
- function runTestAction(messageJson) {
- var message = JSON.parse(messageJson);
- testActions[message.action]();
- if (message.isFinalAction) {
- chrome.test.succeed();
- return;
- }
- chrome.test.sendMessage(waiting, runTestAction);
+ function next() {
+ var waiting = inIncognitoContext ? "waiting_incognito" : "waiting";
+ chrome.test.sendMessage(waiting, function(messageJson) {
+ var message = JSON.parse(messageJson);
+ var action = testActions[message.action];
+ action(message.isFinalAction ? chrome.test.succeed : next);
+ });
}
- chrome.test.sendMessage(waiting, runTestAction);
+ next();
}
chrome.test.runTests([testEverything]);
-
</script>

Powered by Google App Engine
This is Rietveld 408576698