Chromium Code Reviews| 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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "chrome/browser/extensions/extension_apitest.h" | 6 #include "chrome/browser/extensions/extension_apitest.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/test/base/test_switches.h" | 10 #include "chrome/test/base/test_switches.h" |
| 11 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
| 12 #include "components/crx_file/id_util.h" | 12 #include "components/crx_file/id_util.h" |
| 13 #include "extensions/browser/extension_registry.h" | 13 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
| 15 #include "extensions/common/extensions_client.h" | 15 #include "extensions/common/extensions_client.h" |
| 16 #include "extensions/test/extension_test_message_listener.h" | 16 #include "extensions/test/extension_test_message_listener.h" |
| 17 #include "net/test/embedded_test_server/embedded_test_server.h" | 17 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 18 | 18 |
| 19 namespace extensions { | 19 namespace extensions { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 const std::string kAllUrlsTarget = "/extensions/api_test/all_urls/index.html"; | 22 const std::string kAllUrlsTarget = "/extensions/api_test/all_urls/index.html"; |
| 23 } | 23 } |
| 24 | 24 |
| 25 class AllUrlsApiTest : public ExtensionApiTest { | 25 class AllUrlsApiTest : public ExtensionApiTest, |
| 26 public ExtensionRegistryObserver { | |
| 26 protected: | 27 protected: |
| 27 AllUrlsApiTest() {} | 28 AllUrlsApiTest() : wait_until_reload_(false), |
| 29 content_script_is_reloaded_(false), | |
| 30 execute_script_is_reloaded_(false) {} | |
| 28 ~AllUrlsApiTest() override {} | 31 ~AllUrlsApiTest() override {} |
| 29 | 32 |
| 30 const Extension* content_script() const { return content_script_.get(); } | 33 const Extension* content_script() const { return content_script_.get(); } |
| 31 const Extension* execute_script() const { return execute_script_.get(); } | 34 const Extension* execute_script() const { return execute_script_.get(); } |
| 32 | 35 |
| 36 // ExtensionRegistryObserver implementation | |
| 37 void OnExtensionLoaded( | |
| 38 content::BrowserContext* /*browser_context*/, | |
|
Finnur
2015/11/12 15:27:19
Nit: convert to:
content::BrowserContext*, //
| |
| 39 const Extension* extension) override { | |
| 40 if (!wait_until_reload_) | |
| 41 return; | |
| 42 | |
| 43 if (extension->id() == content_script_->id()) | |
| 44 content_script_is_reloaded_ = true; | |
| 45 else if (extension->id() == execute_script_->id()) | |
| 46 execute_script_is_reloaded_ = true; | |
| 47 if (content_script_is_reloaded_ && execute_script_is_reloaded_) { | |
| 48 base::MessageLoop::current()->QuitWhenIdle(); | |
| 49 wait_until_reload_ = false; | |
| 50 } | |
| 51 } | |
| 52 | |
| 33 void WhitelistExtensions() { | 53 void WhitelistExtensions() { |
| 54 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> observer(this); | |
| 55 observer.Add(ExtensionRegistry::Get(browser()->profile())); | |
| 56 | |
| 34 ExtensionsClient::ScriptingWhitelist whitelist; | 57 ExtensionsClient::ScriptingWhitelist whitelist; |
| 35 whitelist.push_back(content_script_->id()); | 58 whitelist.push_back(content_script_->id()); |
| 36 whitelist.push_back(execute_script_->id()); | 59 whitelist.push_back(execute_script_->id()); |
| 37 ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); | 60 ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); |
| 38 // Extensions will have certain permissions withheld at initialization if | 61 // Extensions will have certain permissions withheld at initialization if |
| 39 // they aren't whitelisted, so we need to reload them. | 62 // they aren't whitelisted, so we need to reload them. |
| 63 content_script_is_reloaded_ = false; | |
| 64 execute_script_is_reloaded_ = false; | |
| 65 wait_until_reload_ = true; | |
| 40 extension_service()->ReloadExtension(content_script_->id()); | 66 extension_service()->ReloadExtension(content_script_->id()); |
| 41 extension_service()->ReloadExtension(execute_script_->id()); | 67 extension_service()->ReloadExtension(execute_script_->id()); |
| 68 base::MessageLoop::current()->Run(); | |
| 42 } | 69 } |
| 43 | 70 |
| 44 private: | 71 private: |
| 45 void SetUpOnMainThread() override { | 72 void SetUpOnMainThread() override { |
| 46 ExtensionApiTest::SetUpOnMainThread(); | 73 ExtensionApiTest::SetUpOnMainThread(); |
| 47 base::FilePath data_dir = test_data_dir_.AppendASCII("all_urls"); | 74 base::FilePath data_dir = test_data_dir_.AppendASCII("all_urls"); |
| 48 content_script_ = LoadExtension(data_dir.AppendASCII("content_script")); | 75 content_script_ = LoadExtension(data_dir.AppendASCII("content_script")); |
| 49 ASSERT_TRUE(content_script_); | 76 ASSERT_TRUE(content_script_); |
| 50 execute_script_ = LoadExtension(data_dir.AppendASCII("execute_script")); | 77 execute_script_ = LoadExtension(data_dir.AppendASCII("execute_script")); |
| 51 ASSERT_TRUE(execute_script_); | 78 ASSERT_TRUE(execute_script_); |
| 52 } | 79 } |
| 53 | 80 |
| 54 scoped_refptr<const Extension> content_script_; | 81 scoped_refptr<const Extension> content_script_; |
| 55 scoped_refptr<const Extension> execute_script_; | 82 scoped_refptr<const Extension> execute_script_; |
| 56 | 83 |
| 84 bool wait_until_reload_; | |
| 85 bool content_script_is_reloaded_; | |
| 86 bool execute_script_is_reloaded_; | |
| 87 | |
| 57 DISALLOW_COPY_AND_ASSIGN(AllUrlsApiTest); | 88 DISALLOW_COPY_AND_ASSIGN(AllUrlsApiTest); |
| 58 }; | 89 }; |
| 59 | 90 |
| 60 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_CHROMEOS) || \ | 91 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, WhitelistedExtension) { |
| 61 (defined(OS_MACOSX) && defined(ADDRESS_SANITIZER)) | |
| 62 // http://crbug.com/174341 | |
| 63 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension | |
| 64 #else | |
| 65 #define MAYBE_WhitelistedExtension WhitelistedExtension | |
| 66 #endif | |
| 67 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, MAYBE_WhitelistedExtension) { | |
| 68 #if defined(OS_WIN) && defined(USE_ASH) | 92 #if defined(OS_WIN) && defined(USE_ASH) |
| 69 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 93 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
| 70 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 94 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 71 switches::kAshBrowserTests)) | 95 switches::kAshBrowserTests)) |
| 72 return; | 96 return; |
| 73 #endif | 97 #endif |
| 74 | 98 |
| 75 WhitelistExtensions(); | 99 WhitelistExtensions(); |
| 76 | 100 |
| 77 std::string url; | 101 std::string url; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 GURL page_url = embedded_test_server()->GetURL(kAllUrlsTarget); | 151 GURL page_url = embedded_test_server()->GetURL(kAllUrlsTarget); |
| 128 ExtensionTestMessageListener listener1a("content script: " + page_url.spec(), | 152 ExtensionTestMessageListener listener1a("content script: " + page_url.spec(), |
| 129 false); | 153 false); |
| 130 ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false); | 154 ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false); |
| 131 ui_test_utils::NavigateToURL(browser(), page_url); | 155 ui_test_utils::NavigateToURL(browser(), page_url); |
| 132 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); | 156 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); |
| 133 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); | 157 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); |
| 134 } | 158 } |
| 135 | 159 |
| 136 // Disabled because sometimes bystander doesn't load. | 160 // Disabled because sometimes bystander doesn't load. |
| 137 // TODO(devlin): Why? | 161 // TODO(devlin): Why? |
|
Finnur
2015/11/12 15:27:19
Can it be enabled now?
| |
| 138 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, | 162 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, |
| 139 DISABLED_WhitelistedExtensionRunsOnExtensionPages) { | 163 DISABLED_WhitelistedExtensionRunsOnExtensionPages) { |
| 140 WhitelistExtensions(); | 164 WhitelistExtensions(); |
| 141 const Extension* bystander = | 165 const Extension* bystander = |
| 142 LoadExtension(test_data_dir_.AppendASCII("all_urls") | 166 LoadExtension(test_data_dir_.AppendASCII("all_urls") |
| 143 .AppendASCII("bystander")); | 167 .AppendASCII("bystander")); |
| 144 ASSERT_TRUE(bystander); | 168 ASSERT_TRUE(bystander); |
| 145 | 169 |
| 146 // TODO(devlin): This test should probably go in the WhitelistedExtension test | 170 // TODO(devlin): This test should probably go in the WhitelistedExtension test |
| 147 // above, but that one has a bunch of disableds, so it wouldn't be very | 171 // above, but that one has a bunch of disableds, so it wouldn't be very |
| 148 // useful. | 172 // useful. |
| 149 GURL url(bystander->GetResourceURL("page.html")); | 173 GURL url(bystander->GetResourceURL("page.html")); |
| 150 ExtensionTestMessageListener listenerA( | 174 ExtensionTestMessageListener listenerA( |
| 151 "content script: " + url.spec(), false); | 175 "content script: " + url.spec(), false); |
| 152 ExtensionTestMessageListener listenerB("execute: " + url.spec(), false); | 176 ExtensionTestMessageListener listenerB("execute: " + url.spec(), false); |
| 153 ui_test_utils::NavigateToURL(browser(), GURL(url)); | 177 ui_test_utils::NavigateToURL(browser(), GURL(url)); |
| 154 ASSERT_TRUE(listenerA.WaitUntilSatisfied()); | 178 ASSERT_TRUE(listenerA.WaitUntilSatisfied()); |
| 155 ASSERT_TRUE(listenerB.WaitUntilSatisfied()); | 179 ASSERT_TRUE(listenerB.WaitUntilSatisfied()); |
| 156 } | 180 } |
| 157 | 181 |
| 158 } // namespace extensions | 182 } // namespace extensions |
| OLD | NEW |