Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/test/thread_test_helper.h" | |
| 10 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" | 11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" |
| 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" |
| 13 #include "chrome/browser/extensions/extension_apitest.h" | 14 #include "chrome/browser/extensions/extension_apitest.h" |
| 14 #include "chrome/browser/extensions/extension_system_factory.h" | 15 #include "chrome/browser/extensions/extension_system_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
| 21 | 22 |
| 23 using content::BrowserThread; | |
| 22 using extensions::RulesRegistry; | 24 using extensions::RulesRegistry; |
| 23 using extensions::RulesRegistryService; | 25 using extensions::RulesRegistryService; |
| 24 using extensions::WebRequestRulesRegistry; | 26 using extensions::WebRequestRulesRegistry; |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 const char kArbitraryUrl[] = "http://www.example.com"; | 30 const char kArbitraryUrl[] = "http://www.example.com"; |
| 29 | 31 |
| 30 // The extension in "declarative/redirect_to_data" redirects every navigation to | 32 // The extension in "declarative/redirect_to_data" redirects every navigation to |
| 31 // a page with title |kTestTitle|. | 33 // a page with title |kTestTitle|. |
| 32 const char kTestTitle[] = ":TEST:"; | 34 const char kTestTitle[] = ":TEST:"; |
| 33 | 35 |
| 34 } // namespace | 36 } // namespace |
| 35 | 37 |
| 36 class DeclarativeApiTest : public ExtensionApiTest { | 38 class DeclarativeApiTest : public ExtensionApiTest { |
| 37 public: | 39 public: |
| 38 std::string GetTitle() { | 40 std::string GetTitle() { |
| 39 string16 title( | 41 string16 title( |
| 40 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); | 42 browser()->tab_strip_model()->GetActiveWebContents()->GetTitle()); |
| 41 return base::UTF16ToUTF8(title); | 43 return base::UTF16ToUTF8(title); |
| 42 } | 44 } |
| 45 | |
| 46 // Reports the number of rules registered for the |extension_id| with the | |
| 47 // non-webview rules registry. | |
| 48 size_t NumberOfRegisteredRules(const std::string& extension_id) { | |
| 49 RulesRegistryService* rules_registry_service = | |
| 50 extensions::RulesRegistryService::Get(browser()->profile()); | |
| 51 scoped_refptr<RulesRegistry> rules_registry = | |
| 52 rules_registry_service->GetRulesRegistry( | |
| 53 RulesRegistry::WebViewKey(0, 0), | |
| 54 extensions::declarative_webrequest_constants::kOnRequest); | |
| 55 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | |
| 56 BrowserThread::PostTask( | |
| 57 BrowserThread::IO, | |
| 58 FROM_HERE, | |
| 59 base::Bind( | |
| 60 &RulesRegistry::GetAllRules, rules_registry, extension_id, &rules)); | |
| 61 scoped_refptr<base::ThreadTestHelper> io_helper(new base::ThreadTestHelper( | |
| 62 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get())); | |
| 63 EXPECT_TRUE(io_helper->Run()); | |
| 64 return rules.size(); | |
| 65 } | |
| 43 }; | 66 }; |
| 44 | 67 |
| 45 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, DeclarativeApi) { | 68 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, DeclarativeApi) { |
| 46 ASSERT_TRUE(RunExtensionTest("declarative/api")) << message_; | 69 ASSERT_TRUE(RunExtensionTest("declarative/api")) << message_; |
| 47 | 70 |
| 48 // Check that unloading the page has removed all rules. | 71 // Check that uninstalling the extension has removed all rules. |
| 49 std::string extension_id = GetSingleLoadedExtension()->id(); | 72 std::string extension_id = GetSingleLoadedExtension()->id(); |
| 50 UnloadExtension(extension_id); | 73 UninstallExtension(extension_id); |
| 51 | 74 |
| 52 // UnloadExtension posts a task to the owner thread of the extension | 75 // UnloadExtension posts a task to the owner thread of the extension |
| 53 // to process this unloading. The next task to retrive all rules | 76 // to process this unloading. The next task to retrive all rules |
| 54 // is therefore processed after the UnloadExtension task has been executed. | 77 // is therefore processed after the UnloadExtension task has been executed. |
| 55 | 78 EXPECT_EQ(0u, NumberOfRegisteredRules(extension_id)); |
| 56 RulesRegistryService* rules_registry_service = | |
| 57 extensions::RulesRegistryService::Get(browser()->profile()); | |
| 58 scoped_refptr<RulesRegistry> rules_registry = | |
| 59 rules_registry_service->GetRulesRegistry( | |
| 60 RulesRegistry::WebViewKey(0, 0), | |
| 61 extensions::declarative_webrequest_constants::kOnRequest); | |
| 62 | |
| 63 std::vector<linked_ptr<RulesRegistry::Rule> > known_rules; | |
| 64 | |
| 65 content::BrowserThread::PostTask( | |
| 66 rules_registry->owner_thread(), | |
| 67 FROM_HERE, | |
| 68 base::Bind(base::IgnoreResult(&RulesRegistry::GetAllRules), | |
| 69 rules_registry, extension_id, &known_rules)); | |
| 70 | |
| 71 content::RunAllPendingInMessageLoop(rules_registry->owner_thread()); | |
| 72 | |
| 73 EXPECT_TRUE(known_rules.empty()); | |
| 74 } | 79 } |
| 75 | 80 |
| 76 // PersistRules test first installs an extension, which registers some rules. | 81 // PersistRules test first installs an extension, which registers some rules. |
| 77 // Then after browser restart, it checks that the rules are still in effect. | 82 // Then after browser restart, it checks that the rules are still in effect. |
| 78 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, PRE_PersistRules) { | 83 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, PRE_PersistRules) { |
| 79 ASSERT_TRUE(RunExtensionTest("declarative/redirect_to_data")) << message_; | 84 ASSERT_TRUE(RunExtensionTest("declarative/redirect_to_data")) << message_; |
| 80 } | 85 } |
| 81 | 86 |
| 82 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, PersistRules) { | 87 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, PersistRules) { |
| 83 ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl)); | 88 ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl)); |
| 84 EXPECT_EQ(kTestTitle, GetTitle()); | 89 EXPECT_EQ(kTestTitle, GetTitle()); |
| 85 } | 90 } |
| 91 | |
| 92 // Test that the rules are correctly persisted and (de)activated during | |
| 93 // changing the "installed" and "enabled" status of an extension. | |
| 94 IN_PROC_BROWSER_TEST_F(DeclarativeApiTest, ExtensionLifetimeRulesHandling) { | |
| 95 ASSERT_TRUE(RunExtensionTest("declarative/redirect_to_data")) << message_; | |
| 96 | |
| 97 const std::string extension_id(GetSingleLoadedExtension()->id()); | |
| 98 | |
| 99 // Rules are active and registered. | |
| 100 ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl)); | |
| 101 EXPECT_EQ(kTestTitle, GetTitle()); | |
| 102 EXPECT_EQ(1u, NumberOfRegisteredRules(extension_id)); | |
| 103 | |
| 104 DisableExtension(extension_id); | |
| 105 | |
| 106 // Rules are not active, but are still registered. | |
| 107 ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl)); | |
| 108 EXPECT_NE(kTestTitle, GetTitle()); | |
| 109 EXPECT_EQ(1u, NumberOfRegisteredRules(extension_id)); | |
| 110 | |
| 111 EnableExtension(extension_id); | |
|
Jeffrey Yasskin
2013/11/20 05:55:28
Could you additionally try ReloadExtension()? I'm
vabr (Chromium)
2013/11/20 17:46:07
I added ReloadExtension to the test, with the expe
Jeffrey Yasskin
2013/11/22 00:35:53
Actually, you've tested that ReloadExtension remov
| |
| 112 | |
| 113 // Rules are active and registered again. | |
| 114 ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl)); | |
| 115 EXPECT_EQ(kTestTitle, GetTitle()); | |
| 116 EXPECT_EQ(1u, NumberOfRegisteredRules(extension_id)); | |
| 117 | |
| 118 // Change in the number of active extensions after update. | |
| 119 //const int kChange = 0; | |
| 120 //EXPECT_TRUE(UpdateExtension(extension_id, extension_path, kChange)); | |
|
vabr (Chromium)
2013/11/19 17:05:31
This part is not yet finished -- when I used Updat
Jeffrey Yasskin
2013/11/20 05:55:28
Use https://code.google.com/p/chromium/codesearch/
vabr (Chromium)
2013/11/20 17:46:07
Done.
| |
| 121 | |
| 122 // Rules are still active and registered. | |
| 123 ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl)); | |
| 124 EXPECT_EQ(kTestTitle, GetTitle()); | |
| 125 EXPECT_EQ(1u, NumberOfRegisteredRules(extension_id)); | |
|
Jeffrey Yasskin
2013/11/20 05:55:28
You're likely to wind up with 2 rules registered h
vabr (Chromium)
2013/11/20 17:46:07
I added bot updating and reloading with no rules.
| |
| 126 | |
| 127 UninstallExtension(extension_id); | |
| 128 | |
| 129 // Rules are gone. | |
| 130 ui_test_utils::NavigateToURL(browser(), GURL(kArbitraryUrl)); | |
| 131 EXPECT_NE(kTestTitle, GetTitle()); | |
| 132 EXPECT_EQ(0u, NumberOfRegisteredRules(extension_id)); | |
| 133 } | |
| OLD | NEW |