Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: chrome/browser/extensions/api/declarative_content/chrome_content_rules_registry_unittest.cc

Issue 1166393002: Reland "Encapsulate CSS selector declarative content condition tracking" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: address comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "chrome/browser/extensions/test_extension_environment.h" 10 #include "chrome/browser/extensions/test_extension_environment.h"
12 #include "chrome/test/base/testing_profile.h" 11 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/browser/navigation_details.h" 12 #include "content/public/browser/navigation_details.h"
14 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/frame_navigate_params.h" 14 #include "content/public/common/frame_navigate_params.h"
16 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
18 17
19 namespace extensions { 18 namespace extensions {
20 19
21 using base::test::ParseJson; 20 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; 21 TestExtensionEnvironment env;
41 22
42 scoped_refptr<ChromeContentRulesRegistry> registry( 23 scoped_refptr<ChromeContentRulesRegistry> registry(
43 new ChromeContentRulesRegistry(env.profile(), NULL)); 24 new ChromeContentRulesRegistry(env.profile(), NULL));
44 25
45 EXPECT_EQ(0u, active_rules(*registry.get()).size()); 26 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting());
46 27
47 content::LoadCommittedDetails load_details; 28 scoped_ptr<content::WebContents> tab = env.MakeTab();
48 content::FrameNavigateParams navigate_params; 29 registry->MonitorWebContentsForRuleEvaluation(tab.get());
49 scoped_ptr<WebContents> tab = env.MakeTab(); 30 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(),
50 registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); 31 content::FrameNavigateParams());
51 EXPECT_EQ(0u, active_rules(*registry.get()).size()); 32 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting());
52 33
53 // Add a rule. 34 // Add a rule.
54 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 35 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule);
55 RulesRegistry::Rule::Populate( 36 RulesRegistry::Rule::Populate(
56 *ParseJson( 37 *base::test::ParseJson(
57 "{\n" 38 "{\n"
58 " \"id\": \"rule1\",\n" 39 " \"id\": \"rule1\",\n"
59 " \"priority\": 100,\n" 40 " \"priority\": 100,\n"
60 " \"conditions\": [\n" 41 " \"conditions\": [\n"
61 " {\n" 42 " {\n"
62 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" 43 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
63 " \"css\": [\"input\"]\n" 44 " \"css\": [\"input\"]\n"
64 " }],\n" 45 " }],\n"
65 " \"actions\": [\n" 46 " \"actions\": [\n"
66 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n" 47 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n"
67 " ]\n" 48 " ]\n"
68 "}"), 49 "}"),
69 rule.get()); 50 rule.get());
70 std::vector<linked_ptr<RulesRegistry::Rule> > rules; 51 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
71 rules.push_back(rule); 52 rules.push_back(rule);
72 53
73 const Extension* extension = env.MakeExtension(*ParseJson( 54 const Extension* extension = env.MakeExtension(*base::test::ParseJson(
74 "{\"page_action\": {}}")); 55 "{\"page_action\": {}}"));
75 registry->AddRulesImpl(extension->id(), rules); 56 registry->AddRulesImpl(extension->id(), rules);
76 57
77 registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); 58 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(),
78 EXPECT_EQ(0u, active_rules(*registry.get()).size()); 59 content::FrameNavigateParams());
60 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting());
79 61
80 std::vector<std::string> css_selectors; 62 std::vector<std::string> css_selectors;
81 css_selectors.push_back("input"); 63 css_selectors.push_back("input");
82 registry->Apply(tab.get(), css_selectors); 64 registry->UpdateMatchingCssSelectorsForTesting(tab.get(), css_selectors);
83 EXPECT_EQ(1u, active_rules(*registry.get()).size()); 65 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting());
84 66
85 // Closing the tab should erase its entry from active_rules_. 67 // Closing the tab should erase its entry from active_rules_.
86 tab.reset(); 68 tab.reset();
87 EXPECT_EQ(0u, active_rules(*registry.get()).size()); 69 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting());
88 70
89 tab = env.MakeTab(); 71 tab = env.MakeTab();
90 registry->Apply(tab.get(), css_selectors); 72 registry->MonitorWebContentsForRuleEvaluation(tab.get());
91 EXPECT_EQ(1u, active_rules(*registry.get()).size()); 73 registry->UpdateMatchingCssSelectorsForTesting(tab.get(), css_selectors);
74 EXPECT_EQ(1u, registry->GetActiveRulesCountForTesting());
92 // Navigating the tab should erase its entry from active_rules_ if 75 // Navigating the tab should erase its entry from active_rules_ if
93 // it no longer matches. 76 // it no longer matches.
94 registry->DidNavigateMainFrame(tab.get(), load_details, navigate_params); 77 registry->DidNavigateMainFrame(tab.get(), content::LoadCommittedDetails(),
95 EXPECT_EQ(0u, active_rules(*registry.get()).size()); 78 content::FrameNavigateParams());
79 EXPECT_EQ(0u, registry->GetActiveRulesCountForTesting());
96 } 80 }
97 81
98 } // namespace
99 } // namespace extensions 82 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698