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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/declarative_content/chrome_content_rules_registry.h"
10 #include "base/test/values_test_util.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/extensions/test_extension_environment.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/bookmarks/test/bookmark_test_helpers.h"
16 #include "content/public/browser/navigation_details.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/frame_navigate_params.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace extensions
{
28 scoped_refptr
<Extension
> CreateExtensionWithBookmarksPermission(
29 bool include_bookmarks
) {
30 ListBuilder permissions
;
31 permissions
.Append("declarativeContent");
32 if (include_bookmarks
)
33 permissions
.Append("bookmarks");
34 return ExtensionBuilder()
35 .SetManifest(DictionaryBuilder()
36 .Set("name", "Test extension")
37 .Set("version", "1.0")
38 .Set("manifest_version", 2)
39 .Set("permissions", permissions
))
43 // Thunk to strip |extension| before calling Create.
44 scoped_ptr
<DeclarativeContentCssPredicate
> CreateCssPredicate(
45 const Extension
* extension
,
46 const base::Value
& value
,
48 return DeclarativeContentCssPredicate::Create(
53 // Thunk to strip |extension| and add the |url_matcher_condition_factory| before
55 scoped_ptr
<DeclarativeContentPageUrlPredicate
> CreatePageUrlPredicate(
56 url_matcher::URLMatcherConditionFactory
* url_matcher_condition_factory
,
57 const Extension
* extension
,
58 const base::Value
& value
,
60 return DeclarativeContentPageUrlPredicate::Create(
61 url_matcher_condition_factory
,
68 using testing::HasSubstr
;
70 class DeclarativeChromeContentRulesRegistryTest
: public testing::Test
{
72 DeclarativeChromeContentRulesRegistryTest() {
73 env_
.profile()->CreateBookmarkModel(true);
74 bookmarks::test::WaitForBookmarkModelToLoad(
75 BookmarkModelFactory::GetForProfile(env_
.profile()));
79 TestExtensionEnvironment
* env() { return &env_
; }
82 TestExtensionEnvironment env_
;
84 DISALLOW_COPY_AND_ASSIGN(DeclarativeChromeContentRulesRegistryTest
);
87 TEST(DeclarativeContentConditionTest
, UnknownPredicateName
) {
88 url_matcher::URLMatcher matcher
;
90 scoped_ptr
<ContentCondition
> condition
= CreateContentCondition(
92 base::Bind(&CreateCssPredicate
),
93 base::Bind(&DeclarativeContentIsBookmarkedPredicate::Create
),
94 base::Bind(&CreatePageUrlPredicate
,
95 base::Unretained(matcher
.condition_factory())),
96 *base::test::ParseJson(
98 " \"invalid\": \"foobar\",\n"
99 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
102 EXPECT_THAT(error
, HasSubstr("Unknown condition attribute"));
103 EXPECT_FALSE(condition
);
105 EXPECT_TRUE(matcher
.IsEmpty()) << "Errors shouldn't add URL conditions";
108 TEST(DeclarativeContentConditionTest
,
109 PredicateWithErrorProducesEmptyCondition
) {
110 url_matcher::URLMatcher matcher
;
112 scoped_ptr
<ContentCondition
> condition
= CreateContentCondition(
114 base::Bind(&CreateCssPredicate
),
115 base::Bind(&DeclarativeContentIsBookmarkedPredicate::Create
),
116 base::Bind(&CreatePageUrlPredicate
,
117 base::Unretained(matcher
.condition_factory())),
118 *base::test::ParseJson(
120 " \"css\": \"selector\",\n"
121 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
124 EXPECT_THAT(error
, HasSubstr("invalid type"));
125 EXPECT_FALSE(condition
);
128 TEST(DeclarativeContentConditionTest
, AllSpecifiedPredicatesCreated
) {
129 scoped_refptr
<Extension
> extension
=
130 CreateExtensionWithBookmarksPermission(true);
131 url_matcher::URLMatcher matcher
;
133 scoped_ptr
<ContentCondition
> condition
= CreateContentCondition(
135 base::Bind(&CreateCssPredicate
),
136 base::Bind(&DeclarativeContentIsBookmarkedPredicate::Create
),
137 base::Bind(&CreatePageUrlPredicate
,
138 base::Unretained(matcher
.condition_factory())),
139 *base::test::ParseJson(
141 " \"pageUrl\": {\"hostSuffix\": \"example.com\"},\n"
142 " \"css\": [\"input\"],\n"
143 " \"isBookmarked\": true,\n"
144 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
147 ASSERT_TRUE(condition
);
148 EXPECT_TRUE(condition
->page_url_predicate
);
149 EXPECT_TRUE(condition
->css_predicate
);
150 EXPECT_TRUE(condition
->is_bookmarked_predicate
);
153 TEST_F(DeclarativeChromeContentRulesRegistryTest
, ActiveRulesDoesntGrow
) {
154 scoped_refptr
<ChromeContentRulesRegistry
> registry(
155 new ChromeContentRulesRegistry(env()->profile(), NULL
));
157 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
159 scoped_ptr
<content::WebContents
> tab
= env()->MakeTab();
160 registry
->MonitorWebContentsForRuleEvaluation(tab
.get());
161 registry
->DidNavigateMainFrame(tab
.get(), content::LoadCommittedDetails(),
162 content::FrameNavigateParams());
163 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
166 linked_ptr
<api::events::Rule
> rule(new api::events::Rule
);
167 api::events::Rule::Populate(
168 *base::test::ParseJson(
170 " \"id\": \"rule1\",\n"
171 " \"priority\": 100,\n"
172 " \"conditions\": [\n"
174 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
175 " \"css\": [\"input\"]\n"
178 " { \"instanceType\": \"declarativeContent.ShowPageAction\" }\n"
182 std::vector
<linked_ptr
<api::events::Rule
>> rules
;
183 rules
.push_back(rule
);
185 const Extension
* extension
= env()->MakeExtension(*base::test::ParseJson(
186 "{\"page_action\": {}}"));
187 registry
->AddRulesImpl(extension
->id(), rules
);
189 registry
->DidNavigateMainFrame(tab
.get(), content::LoadCommittedDetails(),
190 content::FrameNavigateParams());
191 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
193 std::vector
<std::string
> css_selectors
;
194 css_selectors
.push_back("input");
195 registry
->UpdateMatchingCssSelectorsForTesting(tab
.get(), css_selectors
);
196 EXPECT_EQ(1u, registry
->GetActiveRulesCountForTesting());
198 // Closing the tab should erase its entry from active_rules_.
200 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
202 tab
= env()->MakeTab();
203 registry
->MonitorWebContentsForRuleEvaluation(tab
.get());
204 registry
->UpdateMatchingCssSelectorsForTesting(tab
.get(), css_selectors
);
205 EXPECT_EQ(1u, registry
->GetActiveRulesCountForTesting());
206 // Navigating the tab should erase its entry from active_rules_ if
207 // it no longer matches.
208 registry
->DidNavigateMainFrame(tab
.get(), content::LoadCommittedDetails(),
209 content::FrameNavigateParams());
210 EXPECT_EQ(0u, registry
->GetActiveRulesCountForTesting());
213 } // namespace extensions