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 "chrome/browser/extensions/api/declarative_content/chrome_content_rules
_registry.h" | 5 #include "chrome/browser/extensions/api/declarative_content/chrome_content_rules
_registry.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/test/values_test_util.h" | 9 #include "base/test/values_test_util.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/extensions/extension_tab_util.h" |
11 #include "chrome/browser/extensions/test_extension_environment.h" | 11 #include "chrome/browser/extensions/test_extension_environment.h" |
12 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
13 #include "content/public/browser/navigation_details.h" | 13 #include "content/public/browser/navigation_details.h" |
14 #include "content/public/browser/notification_details.h" | |
15 #include "content/public/browser/notification_source.h" | |
16 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
17 #include "content/public/common/frame_navigate_params.h" | 15 #include "content/public/common/frame_navigate_params.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
20 | 18 |
21 namespace extensions { | 19 namespace extensions { |
22 | 20 |
23 TEST(DeclarativeChromeContentRulesRegistryTest, ActiveRulesDoesntGrow) { | 21 using base::test::ParseJson; |
| 22 using testing::HasSubstr; |
| 23 using content::WebContents; |
| 24 |
| 25 // Must be outside the anonymous namespace to be a friend of |
| 26 // ContentRulesRegistry. |
| 27 class DeclarativeChromeContentRulesRegistryTest : public testing::Test { |
| 28 protected: |
| 29 using RulesMap = |
| 30 std::map<content::WebContents*, std::set<const ContentRule*>>; |
| 31 static const RulesMap& active_rules( |
| 32 const ChromeContentRulesRegistry& registry) { |
| 33 return registry.active_rules_; |
| 34 } |
| 35 }; |
| 36 |
| 37 namespace { |
| 38 |
| 39 TEST_F(DeclarativeChromeContentRulesRegistryTest, ActiveRulesDoesntGrow) { |
24 TestExtensionEnvironment env; | 40 TestExtensionEnvironment env; |
25 | 41 |
26 scoped_refptr<ChromeContentRulesRegistry> registry( | 42 scoped_refptr<ChromeContentRulesRegistry> registry( |
27 new ChromeContentRulesRegistry(env.profile(), NULL)); | 43 new ChromeContentRulesRegistry(env.profile(), NULL)); |
28 | 44 |
29 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | 45 EXPECT_EQ(0u, active_rules(*registry.get()).size()); |
30 | 46 |
31 scoped_ptr<content::WebContents> tab = env.MakeTab(); | 47 content::LoadCommittedDetails load_details; |
32 // Let the registry know about the tab. | 48 content::FrameNavigateParams navigate_params; |
33 registry->Observe(chrome::NOTIFICATION_TAB_ADDED, | 49 scoped_ptr<WebContents> tab = env.MakeTab(); |
34 content::Source<content::WebContentsDelegate>(nullptr), | 50 registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); |
35 content::Details<content::WebContents>(tab.get())); | 51 EXPECT_EQ(0u, active_rules(*registry.get()).size()); |
36 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(), | |
37 content::FrameNavigateParams()); | |
38 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | |
39 | 52 |
40 // Add a rule. | 53 // Add a rule. |
41 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | 54 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
42 RulesRegistry::Rule::Populate( | 55 RulesRegistry::Rule::Populate( |
43 *base::test::ParseJson( | 56 *ParseJson( |
44 "{\n" | 57 "{\n" |
45 " \"id\": \"rule1\",\n" | 58 " \"id\": \"rule1\",\n" |
46 " \"priority\": 100,\n" | 59 " \"priority\": 100,\n" |
47 " \"conditions\": [\n" | 60 " \"conditions\": [\n" |
48 " {\n" | 61 " {\n" |
49 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | 62 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" |
50 " \"css\": [\"input\"]\n" | 63 " \"css\": [\"input\"]\n" |
51 " }],\n" | 64 " }],\n" |
52 " \"actions\": [\n" | 65 " \"actions\": [\n" |
53 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n" | 66 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n" |
54 " ]\n" | 67 " ]\n" |
55 "}"), | 68 "}"), |
56 rule.get()); | 69 rule.get()); |
57 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 70 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
58 rules.push_back(rule); | 71 rules.push_back(rule); |
59 | 72 |
60 const Extension* extension = env.MakeExtension(*base::test::ParseJson( | 73 const Extension* extension = env.MakeExtension(*ParseJson( |
61 "{\"page_action\": {}}")); | 74 "{\"page_action\": {}}")); |
62 registry->AddRulesImpl(extension->id(), rules); | 75 registry->AddRulesImpl(extension->id(), rules); |
63 | 76 |
64 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(), | 77 registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); |
65 content::FrameNavigateParams()); | 78 EXPECT_EQ(0u, active_rules(*registry.get()).size()); |
66 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | |
67 | 79 |
68 std::vector<std::string> css_selectors; | 80 std::vector<std::string> css_selectors; |
69 css_selectors.push_back("input"); | 81 css_selectors.push_back("input"); |
70 registry->UpdateMatchingCssSelectorsForTesting(tab.get(), css_selectors); | 82 registry->Apply(tab.get(), css_selectors); |
71 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting()); | 83 EXPECT_EQ(1u, active_rules(*registry.get()).size()); |
72 | 84 |
73 // Closing the tab should erase its entry from active_rules_. | 85 // Closing the tab should erase its entry from active_rules_. |
74 tab.reset(); | 86 tab.reset(); |
75 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | 87 EXPECT_EQ(0u, active_rules(*registry.get()).size()); |
76 | 88 |
77 tab = env.MakeTab(); | 89 tab = env.MakeTab(); |
78 // Let the registry know about the tab. | 90 registry->Apply(tab.get(), css_selectors); |
79 registry->Observe(chrome::NOTIFICATION_TAB_ADDED, | 91 EXPECT_EQ(1u, active_rules(*registry.get()).size()); |
80 content::Source<content::WebContentsDelegate>(nullptr), | |
81 content::Details<content::WebContents>(tab.get())); | |
82 registry->UpdateMatchingCssSelectorsForTesting(tab.get(), css_selectors); | |
83 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting()); | |
84 // Navigating the tab should erase its entry from active_rules_ if | 92 // Navigating the tab should erase its entry from active_rules_ if |
85 // it no longer matches. | 93 // it no longer matches. |
86 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(), | 94 registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); |
87 content::FrameNavigateParams()); | 95 EXPECT_EQ(0u, active_rules(*registry.get()).size()); |
88 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting()); | |
89 } | 96 } |
90 | 97 |
| 98 } // namespace |
91 } // namespace extensions | 99 } // namespace extensions |
OLD | NEW |