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