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