1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_settings_sync_util.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/api/sync_change.h"
#include "chrome/browser/sync/api/sync_change_processor.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
namespace {
class NoopSyncChangeProcessor : public SyncChangeProcessor {
public:
virtual SyncError ProcessSyncChanges(
const tracked_objects::Location& from_here,
const SyncChangeList& change_list) OVERRIDE {
return SyncError();
}
virtual ~NoopSyncChangeProcessor() {};
};
} // namespace
class ExtensionSettingsApiTest : public ExtensionApiTest {
protected:
void ReplyWhenSatisfied(
const std::string& normal_action,
const std::string& incognito_action) {
MaybeLoadAndReplyWhenSatisfied(
normal_action, incognito_action, NULL, false);
}
const Extension* LoadAndReplyWhenSatisfied(
const std::string& normal_action,
const std::string& incognito_action,
const std::string& extension_dir) {
return MaybeLoadAndReplyWhenSatisfied(
normal_action, incognito_action, &extension_dir, false);
}
void FinalReplyWhenSatisfied(
const std::string& normal_action,
const std::string& incognito_action) {
MaybeLoadAndReplyWhenSatisfied(normal_action, incognito_action, NULL, true);
}
void InitSync(SyncChangeProcessor* sync_processor) {
browser()->profile()->GetExtensionService()->
extension_settings_frontend()->RunWithSyncableService(
// TODO(kalman): test both EXTENSION_SETTINGS and APP_SETTINGS.
syncable::EXTENSION_SETTINGS,
base::Bind(
&ExtensionSettingsApiTest::InitSyncWithSyncableService,
this,
sync_processor));
MessageLoop::current()->RunAllPending();
}
void SendChanges(const SyncChangeList& change_list) {
browser()->profile()->GetExtensionService()->
extension_settings_frontend()->RunWithSyncableService(
// TODO(kalman): test both EXTENSION_SETTINGS and APP_SETTINGS.
syncable::EXTENSION_SETTINGS,
base::Bind(
&ExtensionSettingsApiTest::SendChangesToSyncableService,
this,
change_list));
MessageLoop::current()->RunAllPending();
}
private:
const Extension* MaybeLoadAndReplyWhenSatisfied(
const std::string& normal_action,
const std::string& incognito_action,
// May be NULL to imply not loading the extension.
const std::string* extension_dir,
bool is_final_action) {
ExtensionTestMessageListener listener("waiting", true);
ExtensionTestMessageListener listener_incognito("waiting_incognito", true);
// Only load the extension after the listeners have been set up, to avoid
// initialisation race conditions.
const Extension* extension = NULL;
if (extension_dir) {
extension = LoadExtensionIncognito(
test_data_dir_.AppendASCII("settings").AppendASCII(*extension_dir));
EXPECT_TRUE(extension);
}
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
listener.Reply(CreateMessage(normal_action, is_final_action));
listener_incognito.Reply(CreateMessage(incognito_action, is_final_action));
return extension;
}
std::string CreateMessage(const std::string& action, bool is_final_action) {
scoped_ptr<DictionaryValue> message(new DictionaryValue());
message->SetString("action", action);
message->SetBoolean("isFinalAction", is_final_action);
std::string message_json;
base::JSONWriter::Write(message.get(), false, &message_json);
return message_json;
}
void InitSyncWithSyncableService(
SyncChangeProcessor* sync_processor, SyncableService* settings_service) {
EXPECT_FALSE(settings_service->MergeDataAndStartSyncing(
syncable::EXTENSION_SETTINGS,
SyncDataList(),
sync_processor).IsSet());
}
void SendChangesToSyncableService(
const SyncChangeList& change_list, SyncableService* settings_service) {
EXPECT_FALSE(
settings_service->ProcessSyncChanges(FROM_HERE, change_list).IsSet());
}
};
IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SimpleTest) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalExtensionApis);
ASSERT_TRUE(RunExtensionTest("settings/simple_test")) << message_;
}
// Structure of this test taken from IncognitoSplitMode.
// Note that only split-mode incognito is tested, because spanning mode
// incognito looks the same as normal mode when the only API activity comes
// from background pages.
IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SplitModeIncognito) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalExtensionApis);
// We need 2 ResultCatchers because we'll be running the same test in both
// regular and incognito mode.
ResultCatcher catcher, catcher_incognito;
catcher.RestrictToProfile(browser()->profile());
catcher_incognito.RestrictToProfile(
browser()->profile()->GetOffTheRecordProfile());
LoadAndReplyWhenSatisfied("assertEmpty", "assertEmpty", "split_incognito");
ReplyWhenSatisfied("noop", "setFoo");
ReplyWhenSatisfied("assertFoo", "assertFoo");
ReplyWhenSatisfied("clear", "noop");
ReplyWhenSatisfied("assertEmpty", "assertEmpty");
ReplyWhenSatisfied("setFoo", "noop");
ReplyWhenSatisfied("assertFoo", "assertFoo");
ReplyWhenSatisfied("noop", "removeFoo");
FinalReplyWhenSatisfied("assertEmpty", "assertEmpty");
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
}
IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
OnChangedNotificationsBetweenBackgroundPages) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalExtensionApis);
// We need 2 ResultCatchers because we'll be running the same test in both
// regular and incognito mode.
ResultCatcher catcher, catcher_incognito;
catcher.RestrictToProfile(browser()->profile());
catcher_incognito.RestrictToProfile(
browser()->profile()->GetOffTheRecordProfile());
LoadAndReplyWhenSatisfied(
"assertNoNotifications", "assertNoNotifications", "split_incognito");
ReplyWhenSatisfied("noop", "setFoo");
ReplyWhenSatisfied("assertAddFooNotification", "assertAddFooNotification");
ReplyWhenSatisfied("clearNotifications", "clearNotifications");
ReplyWhenSatisfied("removeFoo", "noop");
FinalReplyWhenSatisfied(
"assertDeleteFooNotification", "assertDeleteFooNotification");
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
}
// Disabled, see crbug.com/101110
IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
DISABLED_OnChangedNotificationsFromSync) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalExtensionApis);
// We need 2 ResultCatchers because we'll be running the same test in both
// regular and incognito mode.
ResultCatcher catcher, catcher_incognito;
catcher.RestrictToProfile(browser()->profile());
catcher_incognito.RestrictToProfile(
browser()->profile()->GetOffTheRecordProfile());
const Extension* extension =
LoadAndReplyWhenSatisfied(
"assertNoNotifications", "assertNoNotifications", "split_incognito");
const std::string& extension_id = extension->id();
NoopSyncChangeProcessor sync_processor;
InitSync(&sync_processor);
// Set "foo" to "bar" via sync.
SyncChangeList sync_changes;
StringValue bar("bar");
sync_changes.push_back(extension_settings_sync_util::CreateAdd(
extension_id, "foo", bar));
SendChanges(sync_changes);
ReplyWhenSatisfied("assertAddFooNotification", "assertAddFooNotification");
ReplyWhenSatisfied("clearNotifications", "clearNotifications");
// Remove "foo" via sync.
sync_changes.clear();
sync_changes.push_back(extension_settings_sync_util::CreateDelete(
extension_id, "foo"));
SendChanges(sync_changes);
FinalReplyWhenSatisfied(
"assertDeleteFooNotification", "assertDeleteFooNotification");
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
}
|