1 // Copyright 2015 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"
8 #include "base/test/values_test_util.h"
9 #include "base/values.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions
{
17 class TestPredicate
: public ContentPredicate
{
21 ContentPredicateEvaluator
* GetEvaluator() const override
{
26 DISALLOW_COPY_AND_ASSIGN(TestPredicate
);
29 class TestPredicateFactoryGeneratingError
: public ContentPredicateFactory
{
31 explicit TestPredicateFactoryGeneratingError(const std::string
& error
)
35 scoped_ptr
<const ContentPredicate
> CreatePredicate(
36 const Extension
* extension
,
37 const base::Value
& value
,
38 std::string
* error
) override
{
40 return scoped_ptr
<const ContentPredicate
>();
44 const std::string error_
;
46 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingError
);
49 class TestPredicateFactoryGeneratingPredicate
: public ContentPredicateFactory
{
51 TestPredicateFactoryGeneratingPredicate() {}
53 scoped_ptr
<const ContentPredicate
> CreatePredicate(
54 const Extension
* extension
,
55 const base::Value
& value
,
56 std::string
* error
) override
{
57 scoped_ptr
<const ContentPredicate
> predicate(new TestPredicate
);
58 created_predicates_
.push_back(predicate
.get());
62 const std::vector
<const ContentPredicate
*>& created_predicates() const {
63 return created_predicates_
;
67 std::vector
<const ContentPredicate
*> created_predicates_
;
69 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingPredicate
);
74 using testing::HasSubstr
;
75 using testing::UnorderedElementsAre
;
77 TEST(DeclarativeContentConditionTest
, UnknownPredicateName
) {
79 scoped_ptr
<ContentCondition
> condition
= CreateContentCondition(
81 std::map
<std::string
, ContentPredicateFactory
*>(),
82 *base::test::ParseJson(
84 " \"invalid\": \"foobar\",\n"
85 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
88 EXPECT_THAT(error
, HasSubstr("Unknown condition attribute"));
89 EXPECT_FALSE(condition
);
92 TEST(DeclarativeContentConditionTest
,
93 PredicateWithErrorProducesEmptyCondition
) {
94 TestPredicateFactoryGeneratingError
factory("error message");
95 std::map
<std::string
, ContentPredicateFactory
*> predicate_factories
;
96 predicate_factories
["test_predicate"] = &factory
;
98 scoped_ptr
<ContentCondition
> condition
= CreateContentCondition(
101 *base::test::ParseJson(
103 " \"test_predicate\": \"\",\n"
104 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
107 EXPECT_EQ("error message", error
);
108 EXPECT_FALSE(condition
);
111 TEST(DeclarativeContentConditionTest
, AllSpecifiedPredicatesCreated
) {
112 TestPredicateFactoryGeneratingPredicate factory1
, factory2
;
113 std::map
<std::string
, ContentPredicateFactory
*> predicate_factories
;
114 predicate_factories
["test_predicate1"] = &factory1
;
115 predicate_factories
["test_predicate2"] = &factory2
;
117 scoped_ptr
<ContentCondition
> condition
= CreateContentCondition(
120 *base::test::ParseJson(
122 " \"test_predicate1\": {},\n"
123 " \"test_predicate2\": [],\n"
124 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
127 ASSERT_TRUE(condition
);
128 ASSERT_EQ(1u, factory1
.created_predicates().size());
129 ASSERT_EQ(1u, factory2
.created_predicates().size());
130 EXPECT_THAT(condition
->predicates
,
131 UnorderedElementsAre(factory1
.created_predicates()[0],
132 factory2
.created_predicates()[0]));
135 } // namespace extensions