Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / declarative_content / content_condition_unittest.cc
blob0f07761622dc3d851510a4d9c58ea31776d02df7
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"
7 #include "base/bind.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 {
15 namespace {
17 class TestPredicate : public ContentPredicate {
18 public:
19 TestPredicate() {}
21 ContentPredicateEvaluator* GetEvaluator() const override {
22 return nullptr;
25 private:
26 DISALLOW_COPY_AND_ASSIGN(TestPredicate);
29 class TestPredicateFactoryGeneratingError : public ContentPredicateFactory {
30 public:
31 explicit TestPredicateFactoryGeneratingError(const std::string& error)
32 : error_(error) {
35 scoped_ptr<const ContentPredicate> CreatePredicate(
36 const Extension* extension,
37 const base::Value& value,
38 std::string* error) override {
39 *error = error_;
40 return scoped_ptr<const ContentPredicate>();
43 private:
44 const std::string error_;
46 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingError);
49 class TestPredicateFactoryGeneratingPredicate : public ContentPredicateFactory {
50 public:
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());
59 return predicate;
62 const std::vector<const ContentPredicate*>& created_predicates() const {
63 return created_predicates_;
66 private:
67 std::vector<const ContentPredicate*> created_predicates_;
69 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingPredicate);
72 } // namespace
74 using testing::HasSubstr;
75 using testing::UnorderedElementsAre;
77 TEST(DeclarativeContentConditionTest, UnknownPredicateName) {
78 std::string error;
79 scoped_ptr<ContentCondition> condition = CreateContentCondition(
80 nullptr,
81 std::map<std::string, ContentPredicateFactory*>(),
82 *base::test::ParseJson(
83 "{\n"
84 " \"invalid\": \"foobar\",\n"
85 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
86 "}"),
87 &error);
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;
97 std::string error;
98 scoped_ptr<ContentCondition> condition = CreateContentCondition(
99 nullptr,
100 predicate_factories,
101 *base::test::ParseJson(
102 "{\n"
103 " \"test_predicate\": \"\",\n"
104 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
105 "}"),
106 &error);
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;
116 std::string error;
117 scoped_ptr<ContentCondition> condition = CreateContentCondition(
118 nullptr,
119 predicate_factories,
120 *base::test::ParseJson(
121 "{\n"
122 " \"test_predicate1\": {},\n"
123 " \"test_predicate2\": [],\n"
124 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n"
125 "}"),
126 &error);
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