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"
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 "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using testing::ElementsAre
;
19 using testing::HasSubstr
;
20 using url_matcher::URLMatcher
;
21 using url_matcher::URLMatcherConditionSet
;
23 namespace extensions
{
26 TEST(DeclarativeContentConditionTest
, UnknownConditionName
) {
29 scoped_ptr
<ContentCondition
> result
= ContentCondition::Create(
31 matcher
.condition_factory(),
32 *base::test::ParseJson(
34 " \"invalid\": \"foobar\",\n"
35 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
38 EXPECT_THAT(error
, HasSubstr("Unknown condition attribute"));
41 EXPECT_TRUE(matcher
.IsEmpty()) << "Errors shouldn't add URL conditions";
44 TEST(DeclarativeContentConditionTest
, WrongPageUrlDatatype
) {
47 scoped_ptr
<ContentCondition
> result
= ContentCondition::Create(
49 matcher
.condition_factory(),
50 *base::test::ParseJson(
53 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
56 EXPECT_THAT(error
, HasSubstr("invalid type"));
59 EXPECT_TRUE(matcher
.IsEmpty()) << "Errors shouldn't add URL conditions";
62 TEST(DeclarativeContentConditionTest
, WrongCssDatatype
) {
65 scoped_ptr
<ContentCondition
> result
= ContentCondition::Create(
67 matcher
.condition_factory(),
68 *base::test::ParseJson(
70 " \"css\": \"selector\",\n"
71 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
74 EXPECT_THAT(error
, HasSubstr("invalid type"));
77 EXPECT_TRUE(matcher
.IsEmpty()) << "Errors shouldn't add URL conditions";
80 TEST(DeclarativeContentConditionTest
, ConditionWithUrlAndCss
) {
84 scoped_ptr
<ContentCondition
> result
= ContentCondition::Create(
86 matcher
.condition_factory(),
87 *base::test::ParseJson(
89 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
90 " \"pageUrl\": {\"hostSuffix\": \"example.com\"},\n"
91 " \"css\": [\"input\"],\n"
97 URLMatcherConditionSet::Vector all_new_condition_sets
;
98 result
->GetURLMatcherConditionSets(&all_new_condition_sets
);
99 matcher
.AddConditionSets(all_new_condition_sets
);
100 EXPECT_FALSE(matcher
.IsEmpty());
102 RendererContentMatchData match_data
;
103 match_data
.css_selectors
.insert("input");
105 EXPECT_THAT(matcher
.MatchURL(GURL("http://google.com/")),
106 ElementsAre(/*empty*/));
107 match_data
.page_url_matches
= matcher
.MatchURL(
108 GURL("http://www.example.com/foobar"));
109 EXPECT_THAT(match_data
.page_url_matches
,
110 ElementsAre(result
->url_matcher_condition_set_id()));
112 EXPECT_TRUE(result
->IsFulfilled(match_data
));
114 match_data
.css_selectors
.clear();
115 match_data
.css_selectors
.insert("body");
116 EXPECT_FALSE(result
->IsFulfilled(match_data
));
120 } // namespace extensions