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