| 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 } |
| 65 |
| 66 void SendChanges(const SyncChangeList& change_list) { |
| 67 browser()->profile()->GetExtensionService()-> |
| 68 extension_settings_frontend()->RunWithBackend(base::Bind( |
| 69 &ExtensionSettingsApiTest::SendChangesToBackend, |
| 70 this, |
| 71 change_list)); |
| 72 } |
| 73 |
| 37 private: | 74 private: |
| 38 void MaybeLoadAndReplyWhenSatisfied( | 75 const Extension* MaybeLoadAndReplyWhenSatisfied( |
| 39 const std::string& normal_action, | 76 const std::string& normal_action, |
| 40 const std::string& incognito_action, | 77 const std::string& incognito_action, |
| 41 // May be NULL to imply not loading the extension. | 78 // May be NULL to imply not loading the extension. |
| 42 const std::string* extension_dir, | 79 const std::string* extension_dir, |
| 43 bool is_final_action) { | 80 bool is_final_action) { |
| 44 ExtensionTestMessageListener listener("waiting", true); | 81 ExtensionTestMessageListener listener("waiting", true); |
| 45 ExtensionTestMessageListener listener_incognito("waiting_incognito", true); | 82 ExtensionTestMessageListener listener_incognito("waiting_incognito", true); |
| 46 | 83 |
| 47 // Only load the extension after the listeners have been set up, to avoid | 84 // Only load the extension after the listeners have been set up, to avoid |
| 48 // initialisation race conditions. | 85 // initialisation race conditions. |
| 86 const Extension* extension = NULL; |
| 49 if (extension_dir) { | 87 if (extension_dir) { |
| 50 ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ | 88 extension = LoadExtensionIncognito( |
| 51 .AppendASCII("settings").AppendASCII(*extension_dir))); | 89 test_data_dir_.AppendASCII("settings").AppendASCII(*extension_dir)); |
| 90 EXPECT_TRUE(extension); |
| 52 } | 91 } |
| 53 | 92 |
| 54 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 93 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 55 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied()); | 94 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied()); |
| 56 | 95 |
| 57 listener.Reply(CreateMessage(normal_action, is_final_action)); | 96 listener.Reply(CreateMessage(normal_action, is_final_action)); |
| 58 listener_incognito.Reply(CreateMessage(incognito_action, is_final_action)); | 97 listener_incognito.Reply(CreateMessage(incognito_action, is_final_action)); |
| 98 return extension; |
| 59 } | 99 } |
| 60 | 100 |
| 61 std::string CreateMessage(const std::string& action, bool is_final_action) { | 101 std::string CreateMessage(const std::string& action, bool is_final_action) { |
| 62 scoped_ptr<DictionaryValue> message(new DictionaryValue()); | 102 scoped_ptr<DictionaryValue> message(new DictionaryValue()); |
| 63 message->SetString("action", action); | 103 message->SetString("action", action); |
| 64 message->SetBoolean("isFinalAction", is_final_action); | 104 message->SetBoolean("isFinalAction", is_final_action); |
| 65 std::string message_json; | 105 std::string message_json; |
| 66 base::JSONWriter::Write(message.get(), false, &message_json); | 106 base::JSONWriter::Write(message.get(), false, &message_json); |
| 67 return message_json; | 107 return message_json; |
| 68 } | 108 } |
| 109 |
| 110 void InitSyncWithBackend( |
| 111 SyncChangeProcessor* sync_processor, ExtensionSettingsBackend* backend) { |
| 112 EXPECT_FALSE(backend->MergeDataAndStartSyncing( |
| 113 syncable::EXTENSION_SETTINGS, |
| 114 SyncDataList(), |
| 115 sync_processor).IsSet()); |
| 116 } |
| 117 |
| 118 void SendChangesToBackend( |
| 119 const SyncChangeList& change_list, ExtensionSettingsBackend* backend) { |
| 120 EXPECT_FALSE(backend->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
| 121 } |
| 69 }; | 122 }; |
| 70 | 123 |
| 71 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SimpleTest) { | 124 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SimpleTest) { |
| 72 CommandLine::ForCurrentProcess()->AppendSwitch( | 125 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 73 switches::kEnableExperimentalExtensionApis); | 126 switches::kEnableExperimentalExtensionApis); |
| 74 ASSERT_TRUE(RunExtensionTest("settings/simple_test")) << message_; | 127 ASSERT_TRUE(RunExtensionTest("settings/simple_test")) << message_; |
| 75 } | 128 } |
| 76 | 129 |
| 77 // Structure of this test taken from DISABLED_IncognitoSplitMode, so it's | 130 // Structure of this test taken from DISABLED_IncognitoSplitMode, so it's |
| 78 // likely that they will need to be disabled on the bots, too. See | 131 // likely that they will need to be disabled on the bots, too. See |
| 79 // http://crbug.com/53991. | 132 // http://crbug.com/53991. |
| 80 // | 133 // |
| 81 // Note that only split-mode incognito is tested, because spanning mode | 134 // Note that only split-mode incognito is tested, because spanning mode |
| 82 // incognito looks the same as normal mode when the only API activity comes | 135 // incognito looks the same as normal mode when the only API activity comes |
| 83 // from background pages. | 136 // from background pages. |
| 84 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SplitModeIncognito) { | 137 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SplitModeIncognito) { |
| 85 CommandLine::ForCurrentProcess()->AppendSwitch( | 138 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 86 switches::kEnableExperimentalExtensionApis); | 139 switches::kEnableExperimentalExtensionApis); |
| 87 | 140 |
| 88 // We need 2 ResultCatchers because we'll be running the same test in both | 141 // We need 2 ResultCatchers because we'll be running the same test in both |
| 89 // regular and incognito mode. | 142 // regular and incognito mode. |
| 90 ResultCatcher catcher, catcher_incognito; | 143 ResultCatcher catcher, catcher_incognito; |
| 91 catcher.RestrictToProfile(browser()->profile()); | 144 catcher.RestrictToProfile(browser()->profile()); |
| 92 catcher_incognito.RestrictToProfile( | 145 catcher_incognito.RestrictToProfile( |
| 93 browser()->profile()->GetOffTheRecordProfile()); | 146 browser()->profile()->GetOffTheRecordProfile()); |
| 94 | 147 |
| 95 LoadAndReplyWhenSatisfied("assertEmpty", "assertEmpty", "split_incognito"); | 148 LoadAndReplyWhenSatisfied("assertEmpty", "assertEmpty", "split_incognito"); |
| 96 ReplyWhenSatisfied("noop", "setFoobar"); | 149 ReplyWhenSatisfied("noop", "setFoo"); |
| 97 ReplyWhenSatisfied("assertFoobar", "assertFoobar"); | 150 ReplyWhenSatisfied("assertFoo", "assertFoo"); |
| 98 ReplyWhenSatisfied("clear", "noop"); | 151 ReplyWhenSatisfied("clear", "noop"); |
| 99 ReplyWhenSatisfied("assertEmpty", "assertEmpty"); | 152 ReplyWhenSatisfied("assertEmpty", "assertEmpty"); |
| 100 ReplyWhenSatisfied("setFoobar", "noop"); | 153 ReplyWhenSatisfied("setFoo", "noop"); |
| 101 ReplyWhenSatisfied("assertFoobar", "assertFoobar"); | 154 ReplyWhenSatisfied("assertFoo", "assertFoo"); |
| 102 ReplyWhenSatisfied("noop", "removeFoo"); | 155 ReplyWhenSatisfied("noop", "removeFoo"); |
| 103 FinalReplyWhenSatisfied("assertEmpty", "assertEmpty"); | 156 FinalReplyWhenSatisfied("assertEmpty", "assertEmpty"); |
| 104 | 157 |
| 105 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 158 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 106 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message(); | 159 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message(); |
| 107 } | 160 } |
| 161 |
| 162 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, |
| 163 OnChangedNotificationsBetweenBackgroundPages) { |
| 164 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 165 switches::kEnableExperimentalExtensionApis); |
| 166 |
| 167 // We need 2 ResultCatchers because we'll be running the same test in both |
| 168 // regular and incognito mode. |
| 169 ResultCatcher catcher, catcher_incognito; |
| 170 catcher.RestrictToProfile(browser()->profile()); |
| 171 catcher_incognito.RestrictToProfile( |
| 172 browser()->profile()->GetOffTheRecordProfile()); |
| 173 |
| 174 LoadAndReplyWhenSatisfied( |
| 175 "assertNoNotifications", "assertNoNotifications", "split_incognito"); |
| 176 ReplyWhenSatisfied("noop", "setFoo"); |
| 177 ReplyWhenSatisfied("assertAddFooNotification", "assertNoNotifications"); |
| 178 ReplyWhenSatisfied("clearNotifications", "noop"); |
| 179 ReplyWhenSatisfied("removeFoo", "noop"); |
| 180 FinalReplyWhenSatisfied( |
| 181 "assertNoNotifications", "assertDeleteFooNotification"); |
| 182 |
| 183 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 184 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message(); |
| 185 } |
| 186 |
| 187 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, |
| 188 OnChangedNotificationsFromSync) { |
| 189 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 190 switches::kEnableExperimentalExtensionApis); |
| 191 |
| 192 // We need 2 ResultCatchers because we'll be running the same test in both |
| 193 // regular and incognito mode. |
| 194 ResultCatcher catcher, catcher_incognito; |
| 195 catcher.RestrictToProfile(browser()->profile()); |
| 196 catcher_incognito.RestrictToProfile( |
| 197 browser()->profile()->GetOffTheRecordProfile()); |
| 198 |
| 199 const Extension* extension = |
| 200 LoadAndReplyWhenSatisfied( |
| 201 "assertNoNotifications", "assertNoNotifications", "split_incognito"); |
| 202 const std::string& extension_id = extension->id(); |
| 203 |
| 204 NoopSyncChangeProcessor sync_processor; |
| 205 InitSync(&sync_processor); |
| 206 |
| 207 // Set "foo" to "bar" via sync. |
| 208 SyncChangeList sync_changes; |
| 209 StringValue bar("bar"); |
| 210 sync_changes.push_back(extension_settings_sync_util::CreateAdd( |
| 211 extension_id, "foo", bar)); |
| 212 SendChanges(sync_changes); |
| 213 |
| 214 ReplyWhenSatisfied("assertAddFooNotification", "assertAddFooNotification"); |
| 215 ReplyWhenSatisfied("clearNotifications", "clearNotifications"); |
| 216 |
| 217 // Remove "foo" via sync. |
| 218 sync_changes.clear(); |
| 219 sync_changes.push_back(extension_settings_sync_util::CreateDelete( |
| 220 extension_id, "foo")); |
| 221 SendChanges(sync_changes); |
| 222 |
| 223 FinalReplyWhenSatisfied( |
| 224 "assertDeleteFooNotification", "assertDeleteFooNotification"); |
| 225 |
| 226 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 227 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message(); |
| 228 } |
| OLD | NEW |