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