OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 var api = chrome.experimental.storage; |
| 6 var assertEq = chrome.test.assertEq; |
| 7 var inIncognitoContext = chrome.extension.inIncognitoContext; |
| 8 |
| 9 ['sync', 'local'].forEach(function(namespace) { |
| 10 api[namespace].notifications = []; |
| 11 api.onChanged.addListener(function(changes, event_namespace) { |
| 12 if (event_namespace == namespace) { |
| 13 changes.forEach(function(change) { |
| 14 api[namespace].notifications.push(change); |
| 15 }); |
| 16 } |
| 17 }); |
| 18 }); |
| 19 |
| 20 // The test from C++ runs "actions", where each action is defined here. |
| 21 // This allows the test to be tightly controlled between incognito and |
| 22 // non-incognito modes. |
| 23 // Each function accepts a callback which should be run when the settings |
| 24 // operation fully completes. |
| 25 var testActions = { |
| 26 noop: function(callback) { |
| 27 this.get("", callback); |
| 28 }, |
| 29 assertEmpty: function(callback) { |
| 30 this.get(null, function(settings) { |
| 31 chrome.test.assertEq({}, settings); |
| 32 callback(); |
| 33 }); |
| 34 }, |
| 35 assertFoo: function(callback) { |
| 36 this.get(null, function(settings) { |
| 37 chrome.test.assertEq({foo: "bar"}, settings); |
| 38 callback(); |
| 39 }); |
| 40 }, |
| 41 setFoo: function(callback) { |
| 42 this.set({foo: "bar"}, callback); |
| 43 }, |
| 44 removeFoo: function(callback) { |
| 45 this.remove("foo", callback); |
| 46 }, |
| 47 clear: function(callback) { |
| 48 this.clear(callback); |
| 49 }, |
| 50 assertNoNotifications: function(callback) { |
| 51 assertEq([], this.notifications); |
| 52 callback(); |
| 53 }, |
| 54 clearNotifications: function(callback) { |
| 55 this.notifications.length = 0; |
| 56 callback(); |
| 57 }, |
| 58 assertAddFooNotification: function(callback) { |
| 59 assertEq([{ key: 'foo', newValue: 'bar' }], this.notifications); |
| 60 callback(); |
| 61 }, |
| 62 assertDeleteFooNotification: function(callback) { |
| 63 assertEq([{ key: 'foo', oldValue: 'bar' }], this.notifications); |
| 64 callback(); |
| 65 } |
| 66 }; |
| 67 |
| 68 // The only test we run. Runs "actions" (as defined above) until told |
| 69 // to stop (when the message has isFinalAction set to true). |
| 70 function testEverything() { |
| 71 function next() { |
| 72 var waiting = inIncognitoContext ? "waiting_incognito" : "waiting"; |
| 73 chrome.test.sendMessage(waiting, function(messageJson) { |
| 74 var message = JSON.parse(messageJson); |
| 75 var action = testActions[message.action]; |
| 76 if (!action) { |
| 77 chrome.test.fail("Unknown action: " + message.action); |
| 78 return; |
| 79 } |
| 80 action.bind(api[message.namespace])( |
| 81 message.isFinalAction ? chrome.test.succeed : next); |
| 82 }); |
| 83 } |
| 84 next(); |
| 85 } |
| 86 |
| 87 chrome.test.runTests([testEverything]); |
OLD | NEW |