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 // All notifications received. |
| 15 var notifications = []; |
| 16 settings.onChanged.addListener(function(changes) { |
| 17 changes.forEach(function(change) { |
| 18 notifications.push(change); |
| 19 }); |
| 20 }); |
| 21 |
16 // The test from C++ runs "actions", where each action is defined here. | 22 // The test from C++ runs "actions", where each action is defined here. |
17 // This allows the test to be tightly controlled between incognito and | 23 // This allows the test to be tightly controlled between incognito and |
18 // non-incognito modes. | 24 // non-incognito modes. |
| 25 // Each function accepts a callback which should be run when the settings |
| 26 // operation fully completes. |
19 var testActions = { | 27 var testActions = { |
20 noop: function() { | 28 noop: function(callback) { |
21 settings.get("", function() {}); | 29 settings.get("", callback); |
22 }, | 30 }, |
23 assertEmpty: function() { | 31 assertEmpty: function(callback) { |
24 settings.get(null, assertResultEq({})); | 32 settings.get(null, assertResultEq({}, callback)); |
25 }, | 33 }, |
26 assertFoobar: function() { | 34 assertFoo: function(callback) { |
27 settings.get(null, assertResultEq({foo: "bar"})); | 35 settings.get(null, assertResultEq({foo: "bar"}, callback)); |
28 }, | 36 }, |
29 setFoobar: function() { | 37 setFoo: function(callback) { |
30 settings.set({foo: "bar"}, assertResultEq({foo: "bar"})); | 38 settings.set({foo: "bar"}, assertResultEq({foo: "bar"}, callback)); |
31 }, | 39 }, |
32 removeFoo: function() { | 40 removeFoo: function(callback) { |
33 settings.remove("foo", assertResultEq(undefined)); | 41 settings.remove("foo", assertResultEq(undefined, callback)); |
34 }, | 42 }, |
35 clear: function() { | 43 clear: function(callback) { |
36 settings.clear(assertResultEq(undefined)); | 44 settings.clear(assertResultEq(undefined, callback)); |
| 45 }, |
| 46 assertNoNotifications: function(callback) { |
| 47 assertEq([], notifications); |
| 48 callback(); |
| 49 }, |
| 50 clearNotifications: function(callback) { |
| 51 notifications = []; |
| 52 callback(); |
| 53 }, |
| 54 assertAddFooNotification: function(callback) { |
| 55 assertEq(1, notifications.length); |
| 56 assertEq("foo", notifications[0].key); |
| 57 assertEq(undefined, notifications[0].oldValue); |
| 58 assertEq("bar", notifications[0].newValue); |
| 59 callback(); |
| 60 }, |
| 61 assertDeleteFooNotification: function(callback) { |
| 62 assertEq(1, notifications.length); |
| 63 assertEq("foo", notifications[0].key); |
| 64 assertEq("bar", notifications[0].oldValue); |
| 65 assertEq(undefined, notifications[0].newValue); |
| 66 callback(); |
37 } | 67 } |
38 }; | 68 }; |
39 | 69 |
40 // The only test we run. Runs "actions" (as defined above) until told | 70 // The only test we run. Runs "actions" (as defined above) until told |
41 // to stop (when the message has isFinalAction set to true). | 71 // to stop (when the message has isFinalAction set to true). |
42 function testEverything() { | 72 function testEverything() { |
43 var waiting = chrome.extension.inIncognitoContext ? | 73 function next() { |
44 "waiting_incognito" : "waiting"; | 74 var waiting = inIncognitoContext ? "waiting_incognito" : "waiting"; |
45 function runTestAction(messageJson) { | 75 chrome.test.sendMessage(waiting, function(messageJson) { |
46 var message = JSON.parse(messageJson); | 76 var message = JSON.parse(messageJson); |
47 testActions[message.action](); | 77 var action = testActions[message.action]; |
48 if (message.isFinalAction) { | 78 action(message.isFinalAction ? chrome.test.succeed : next); |
49 chrome.test.succeed(); | 79 }); |
50 return; | |
51 } | |
52 chrome.test.sendMessage(waiting, runTestAction); | |
53 } | 80 } |
54 chrome.test.sendMessage(waiting, runTestAction); | 81 next(); |
55 } | 82 } |
56 | 83 |
57 chrome.test.runTests([testEverything]); | 84 chrome.test.runTests([testEverything]); |
58 | |
59 </script> | 85 </script> |
OLD | NEW |