1 // Copyright (c) 2012 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.
5 var api = chrome.storage;
6 var assertEq = chrome.test.assertEq;
7 var inIncognitoContext = chrome.extension.inIncognitoContext;
9 ['sync', 'local'].forEach(function(namespace) {
10 api[namespace].notifications = {};
11 api.onChanged.addListener(function(changes, event_namespace) {
12 if (event_namespace == namespace) {
13 var notifications = api[namespace].notifications;
14 Object.keys(changes).forEach(function(key) {
15 notifications[key] = changes[key];
21 // The test from C++ runs "actions", where each action is defined here.
22 // This allows the test to be tightly controlled between incognito and
23 // non-incognito modes.
24 // Each function accepts a callback which should be run when the settings
25 // operation fully completes.
27 noop: function(callback) {
28 this.get("", callback);
30 assertEmpty: function(callback) {
31 this.get(null, function(settings) {
32 assertEq({}, settings);
36 assertFoo: function(callback) {
37 this.get(null, function(settings) {
38 assertEq({foo: "bar"}, settings);
42 setFoo: function(callback) {
43 this.set({foo: "bar"}, callback);
45 removeFoo: function(callback) {
46 this.remove("foo", callback);
48 clear: function(callback) {
51 assertNoNotifications: function(callback) {
52 assertEq({}, this.notifications);
55 clearNotifications: function(callback) {
56 this.notifications = {};
59 assertAddFooNotification: function(callback) {
60 assertEq({ foo: { newValue: 'bar' } }, this.notifications);
63 assertDeleteFooNotification: function(callback) {
64 assertEq({ foo: { oldValue: 'bar' } }, this.notifications);
69 // The only test we run. Runs "actions" (as defined above) until told
70 // to stop (when the message has isFinalAction set to true).
71 function testEverything() {
73 var waiting = inIncognitoContext ? "waiting_incognito" : "waiting";
74 chrome.test.sendMessage(waiting, function(messageJson) {
75 var message = JSON.parse(messageJson);
76 var action = testActions[message.action];
78 chrome.test.fail("Unknown action: " + message.action);
81 action.bind(api[message.namespace])(
82 message.isFinalAction ? chrome.test.succeed : next);
88 chrome.test.runTests([testEverything]);