1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/declarative_content/content_condition.h"
#include <set>
#include "base/message_loop/message_loop.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/declarative_content/content_constants.h"
#include "components/url_matcher/url_matcher.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/value_builder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using testing::ElementsAre;
using testing::HasSubstr;
using url_matcher::URLMatcher;
using url_matcher::URLMatcherConditionSet;
namespace extensions {
namespace {
scoped_refptr<Extension> CreateExtensionWithBookmarksPermission(
bool include_bookmarks) {
ListBuilder permissions;
permissions.Append("declarativeContent");
if (include_bookmarks)
permissions.Append("bookmarks");
return ExtensionBuilder()
.SetManifest(DictionaryBuilder()
.Set("name", "Test extension")
.Set("version", "1.0")
.Set("manifest_version", 2)
.Set("permissions", permissions))
.Build();
}
} // namespace
TEST(DeclarativeContentConditionTest, UnknownConditionName) {
URLMatcher matcher;
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
NULL,
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"invalid\": \"foobar\",\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
"}"),
&error);
EXPECT_THAT(error, HasSubstr("Unknown condition attribute"));
EXPECT_FALSE(result);
EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions";
}
TEST(DeclarativeContentConditionTest, WrongPageUrlDatatype) {
URLMatcher matcher;
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
NULL,
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"pageUrl\": [],\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
"}"),
&error);
EXPECT_THAT(error, HasSubstr("invalid type"));
EXPECT_FALSE(result);
EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions";
}
TEST(DeclarativeContentConditionTest, WrongCssDatatype) {
URLMatcher matcher;
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
NULL,
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"css\": \"selector\",\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
"}"),
&error);
EXPECT_THAT(error, HasSubstr("invalid type"));
EXPECT_FALSE(result);
EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions";
}
TEST(DeclarativeContentConditionTest, ConditionWithUrlAndCss) {
URLMatcher matcher;
scoped_refptr<Extension> extension =
CreateExtensionWithBookmarksPermission(false);
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
extension.get(),
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
" \"pageUrl\": {\"hostSuffix\": \"example.com\"},\n"
" \"css\": [\"input\"],\n"
"}"),
&error);
EXPECT_EQ("", error);
ASSERT_TRUE(result);
URLMatcherConditionSet::Vector all_new_condition_sets;
result->GetURLMatcherConditionSets(&all_new_condition_sets);
matcher.AddConditionSets(all_new_condition_sets);
EXPECT_FALSE(matcher.IsEmpty());
RendererContentMatchData match_data;
match_data.css_selectors.insert("input");
EXPECT_THAT(matcher.MatchURL(GURL("http://google.com/")),
ElementsAre(/*empty*/));
match_data.page_url_matches = matcher.MatchURL(
GURL("http://www.example.com/foobar"));
EXPECT_THAT(match_data.page_url_matches,
ElementsAre(result->url_matcher_condition_set_id()));
EXPECT_TRUE(result->IsFulfilled(match_data));
match_data.css_selectors.clear();
match_data.css_selectors.insert("body");
EXPECT_FALSE(result->IsFulfilled(match_data));
}
// Tests that condition with isBookmarked requires "bookmarks" permission.
TEST(DeclarativeContentConditionTest, IsBookmarkedRequiresBookmarkPermission) {
URLMatcher matcher;
scoped_refptr<Extension> extension =
CreateExtensionWithBookmarksPermission(false);
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
extension.get(),
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"isBookmarked\": true,\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
"}"),
&error);
EXPECT_THAT(error, HasSubstr("requires 'bookmarks' permission"));
ASSERT_FALSE(result);
}
// Tests an invalid isBookmarked value type.
TEST(DeclarativeContentConditionTest, WrongIsBookmarkedDatatype) {
URLMatcher matcher;
scoped_refptr<Extension> extension =
CreateExtensionWithBookmarksPermission(true);
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
extension.get(),
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"isBookmarked\": [],\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
"}"),
&error);
EXPECT_THAT(error, HasSubstr("invalid type"));
EXPECT_FALSE(result);
}
// Tests isBookmark: true.
TEST(DeclarativeContentConditionTest, IsBookmarkedTrue) {
URLMatcher matcher;
scoped_refptr<Extension> extension =
CreateExtensionWithBookmarksPermission(true);
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
extension.get(),
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"isBookmarked\": true,\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
"}"),
&error);
EXPECT_EQ("", error);
ASSERT_TRUE(result);
// TODO(wittman): Getting/adding URL matcher condition sets should not be
// necessary when we haven't specified any pageUrl conditions. Remove this and
// the MatchURL call below, and refactor ContentCondition and
// ChromeContentRulesRegistry to not depend on a ContentCondition always
// having a URL matcher condition.
URLMatcherConditionSet::Vector all_new_condition_sets;
result->GetURLMatcherConditionSets(&all_new_condition_sets);
matcher.AddConditionSets(all_new_condition_sets);
RendererContentMatchData data;
data.page_url_matches = matcher.MatchURL(GURL("http://test/"));
data.is_bookmarked = true;
EXPECT_TRUE(result->IsFulfilled(data));
data.is_bookmarked = false;
EXPECT_FALSE(result->IsFulfilled(data));
}
// Tests isBookmark: false.
TEST(DeclarativeContentConditionTest, IsBookmarkedFalse) {
URLMatcher matcher;
scoped_refptr<Extension> extension =
CreateExtensionWithBookmarksPermission(true);
std::string error;
scoped_ptr<ContentCondition> result = ContentCondition::Create(
extension.get(),
matcher.condition_factory(),
*base::test::ParseJson(
"{\n"
" \"isBookmarked\": false,\n"
" \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
"}"),
&error);
EXPECT_EQ("", error);
ASSERT_TRUE(result);
// TODO(wittman): Getting/adding URL matcher condition sets should not be
// necessary when we haven't specified any pageUrl conditions. Remove this and
// the MatchURL call below, and refactor ContentCondition and
// ChromeContentRulesRegistry to not depend on a ContentCondition always
// having a URL matcher condition.
URLMatcherConditionSet::Vector all_new_condition_sets;
result->GetURLMatcherConditionSets(&all_new_condition_sets);
matcher.AddConditionSets(all_new_condition_sets);
RendererContentMatchData data;
data.page_url_matches = matcher.MatchURL(GURL("http://test/"));
data.is_bookmarked = true;
EXPECT_FALSE(result->IsFulfilled(data));
data.is_bookmarked = false;
EXPECT_TRUE(result->IsFulfilled(data));
}
} // namespace extensions
|