OLD | NEW |
---|---|
1 <script> | 1 <script> |
2 var settings = chrome.experimental.settings; | |
3 var assertEq = chrome.test.assertEq; | |
4 var inIncognitoContext = chrome.extension.inIncognitoContext; | |
2 | 5 |
3 var settings = chrome.experimental.settings; | 6 // Returns a function that asserts a result is expected then runs a callback. |
4 | 7 function assertResultEq(expected, callback) { |
5 // A different "waiting" message depending on whether this background page runs | |
6 // in incognito mode or not. This is so that the C++ test can differentiate | |
7 // between messages. | |
8 | |
9 // Returns a function that asserts a result is expected. | |
10 function assertResultEq(expected) { | |
11 return function(actual) { | 8 return function(actual) { |
12 chrome.test.assertEq(expected, actual); | 9 chrome.test.assertEq(expected, actual); |
10 callback(); | |
13 }; | 11 }; |
14 } | 12 } |
15 | 13 |
14 // Retries a test action in the future. | |
15 function retry(name, callback) { | |
16 var delayMs = 1000; | |
17 console.log("Retrying " + name + " in " + delayMs + "ms"); | |
18 setTimeout(function() { | |
19 testActions[name](callback); | |
20 }, delayMs); | |
21 } | |
22 | |
23 // All notifications received. | |
24 var notifications = []; | |
25 settings.onChanged.addListener(function(changes) { | |
26 changes.forEach(function(change) { | |
27 notifications.push(change); | |
28 }); | |
29 }); | |
30 | |
16 // The test from C++ runs "actions", where each action is defined here. | 31 // The test from C++ runs "actions", where each action is defined here. |
17 // This allows the test to be tightly controlled between incognito and | 32 // This allows the test to be tightly controlled between incognito and |
18 // non-incognito modes. | 33 // non-incognito modes. |
34 // Each function accepts a callback which should be run when the settings | |
35 // operation fully completes. | |
19 var testActions = { | 36 var testActions = { |
20 noop: function() { | 37 noop: function(callback) { |
21 settings.get("", function() {}); | 38 settings.get("", callback); |
22 }, | 39 }, |
23 assertEmpty: function() { | 40 assertEmpty: function(callback) { |
24 settings.get(null, assertResultEq({})); | 41 settings.get(null, assertResultEq({}, callback)); |
25 }, | 42 }, |
26 assertFoobar: function() { | 43 assertFoo: function(callback) { |
27 settings.get(null, assertResultEq({foo: "bar"})); | 44 settings.get(null, assertResultEq({foo: "bar"}, callback)); |
28 }, | 45 }, |
29 setFoobar: function() { | 46 setFoo: function(callback) { |
30 settings.set({foo: "bar"}, assertResultEq({foo: "bar"})); | 47 settings.set({foo: "bar"}, assertResultEq({foo: "bar"}, callback)); |
31 }, | 48 }, |
32 removeFoo: function() { | 49 removeFoo: function(callback) { |
33 settings.remove("foo", assertResultEq(undefined)); | 50 settings.remove("foo", assertResultEq(undefined, callback)); |
34 }, | 51 }, |
35 clear: function() { | 52 clear: function(callback) { |
36 settings.clear(assertResultEq(undefined)); | 53 settings.clear(assertResultEq(undefined, callback)); |
54 }, | |
55 assertNoNotifications: function(callback) { | |
56 // On Windows, the notifications seem to be unpredicably delivered, so poll | |
57 // until they arrive (or the test times out). | |
58 if (notifications.length !== 0) { | |
59 retry("assertNoNotifications", callback); | |
Matt Perry
2011/10/12 17:23:01
Polling is almost always a bad idea in tests. Can
| |
60 return; | |
61 } | |
62 callback(); | |
63 }, | |
64 clearNotifications: function(callback) { | |
65 notifications = []; | |
66 callback(); | |
67 }, | |
68 assertAddFooNotification: function(callback) { | |
69 // Ditto. | |
70 if (notifications.length !== 1) { | |
71 retry("assertAddFooNotification", callback); | |
72 return; | |
73 } | |
74 assertEq("foo", notifications[0].key); | |
75 assertEq(undefined, notifications[0].oldValue); | |
76 assertEq("bar", notifications[0].newValue); | |
77 callback(); | |
78 }, | |
79 assertDeleteFooNotification: function(callback) { | |
80 // Ditto. | |
81 if (notifications.length !== 1) { | |
82 retry("assertDeleteFooNotification", callback); | |
83 return; | |
84 } | |
85 assertEq("foo", notifications[0].key); | |
86 assertEq("bar", notifications[0].oldValue); | |
87 assertEq(undefined, notifications[0].newValue); | |
88 callback(); | |
37 } | 89 } |
38 }; | 90 }; |
39 | 91 |
40 // The only test we run. Runs "actions" (as defined above) until told | 92 // The only test we run. Runs "actions" (as defined above) until told |
41 // to stop (when the message has isFinalAction set to true). | 93 // to stop (when the message has isFinalAction set to true). |
42 function testEverything() { | 94 function testEverything() { |
43 var waiting = chrome.extension.inIncognitoContext ? | 95 function next() { |
44 "waiting_incognito" : "waiting"; | 96 var waiting = inIncognitoContext ? "waiting_incognito" : "waiting"; |
45 function runTestAction(messageJson) { | 97 chrome.test.sendMessage(waiting, function(messageJson) { |
46 var message = JSON.parse(messageJson); | 98 var message = JSON.parse(messageJson); |
47 testActions[message.action](); | 99 var action = testActions[message.action]; |
48 if (message.isFinalAction) { | 100 action(message.isFinalAction ? chrome.test.succeed : next); |
49 chrome.test.succeed(); | 101 }); |
50 return; | |
51 } | |
52 chrome.test.sendMessage(waiting, runTestAction); | |
53 } | 102 } |
54 chrome.test.sendMessage(waiting, runTestAction); | 103 next(); |
55 } | 104 } |
56 | 105 |
57 chrome.test.runTests([testEverything]); | 106 chrome.test.runTests([testEverything]); |
58 | |
59 </script> | 107 </script> |
OLD | NEW |