| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" | 5 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 private: | 28 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(TestPredicate); | 29 DISALLOW_COPY_AND_ASSIGN(TestPredicate); |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 class TestPredicateFactoryGeneratingError : public ContentPredicateFactory { | 32 class TestPredicateFactoryGeneratingError : public ContentPredicateFactory { |
| 33 public: | 33 public: |
| 34 explicit TestPredicateFactoryGeneratingError(const std::string& error) | 34 explicit TestPredicateFactoryGeneratingError(const std::string& error) |
| 35 : error_(error) { | 35 : error_(error) { |
| 36 } | 36 } |
| 37 | 37 |
| 38 scoped_ptr<const ContentPredicate> CreatePredicate( | 38 std::unique_ptr<const ContentPredicate> CreatePredicate( |
| 39 const Extension* extension, | 39 const Extension* extension, |
| 40 const base::Value& value, | 40 const base::Value& value, |
| 41 std::string* error) override { | 41 std::string* error) override { |
| 42 *error = error_; | 42 *error = error_; |
| 43 return scoped_ptr<const ContentPredicate>(); | 43 return std::unique_ptr<const ContentPredicate>(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 const std::string error_; | 47 const std::string error_; |
| 48 | 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingError); | 49 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingError); |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class TestPredicateFactoryGeneratingPredicate : public ContentPredicateFactory { | 52 class TestPredicateFactoryGeneratingPredicate : public ContentPredicateFactory { |
| 53 public: | 53 public: |
| 54 TestPredicateFactoryGeneratingPredicate() {} | 54 TestPredicateFactoryGeneratingPredicate() {} |
| 55 | 55 |
| 56 scoped_ptr<const ContentPredicate> CreatePredicate( | 56 std::unique_ptr<const ContentPredicate> CreatePredicate( |
| 57 const Extension* extension, | 57 const Extension* extension, |
| 58 const base::Value& value, | 58 const base::Value& value, |
| 59 std::string* error) override { | 59 std::string* error) override { |
| 60 scoped_ptr<const ContentPredicate> predicate(new TestPredicate); | 60 std::unique_ptr<const ContentPredicate> predicate(new TestPredicate); |
| 61 created_predicates_.push_back(predicate.get()); | 61 created_predicates_.push_back(predicate.get()); |
| 62 return predicate; | 62 return predicate; |
| 63 } | 63 } |
| 64 | 64 |
| 65 const std::vector<const ContentPredicate*>& created_predicates() const { | 65 const std::vector<const ContentPredicate*>& created_predicates() const { |
| 66 return created_predicates_; | 66 return created_predicates_; |
| 67 } | 67 } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 std::vector<const ContentPredicate*> created_predicates_; | 70 std::vector<const ContentPredicate*> created_predicates_; |
| 71 | 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingPredicate); | 72 DISALLOW_COPY_AND_ASSIGN(TestPredicateFactoryGeneratingPredicate); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 } // namespace | 75 } // namespace |
| 76 | 76 |
| 77 using testing::HasSubstr; | 77 using testing::HasSubstr; |
| 78 using testing::UnorderedElementsAre; | 78 using testing::UnorderedElementsAre; |
| 79 | 79 |
| 80 TEST(DeclarativeContentConditionTest, UnknownPredicateName) { | 80 TEST(DeclarativeContentConditionTest, UnknownPredicateName) { |
| 81 std::string error; | 81 std::string error; |
| 82 scoped_ptr<ContentCondition> condition = CreateContentCondition( | 82 std::unique_ptr<ContentCondition> condition = CreateContentCondition( |
| 83 nullptr, | 83 nullptr, std::map<std::string, ContentPredicateFactory*>(), |
| 84 std::map<std::string, ContentPredicateFactory*>(), | |
| 85 *base::test::ParseJson( | 84 *base::test::ParseJson( |
| 86 "{\n" | 85 "{\n" |
| 87 " \"invalid\": \"foobar\",\n" | 86 " \"invalid\": \"foobar\",\n" |
| 88 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | 87 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" |
| 89 "}"), | 88 "}"), |
| 90 &error); | 89 &error); |
| 91 EXPECT_THAT(error, HasSubstr("Unknown condition attribute")); | 90 EXPECT_THAT(error, HasSubstr("Unknown condition attribute")); |
| 92 EXPECT_FALSE(condition); | 91 EXPECT_FALSE(condition); |
| 93 } | 92 } |
| 94 | 93 |
| 95 TEST(DeclarativeContentConditionTest, | 94 TEST(DeclarativeContentConditionTest, |
| 96 PredicateWithErrorProducesEmptyCondition) { | 95 PredicateWithErrorProducesEmptyCondition) { |
| 97 TestPredicateFactoryGeneratingError factory("error message"); | 96 TestPredicateFactoryGeneratingError factory("error message"); |
| 98 std::map<std::string, ContentPredicateFactory*> predicate_factories; | 97 std::map<std::string, ContentPredicateFactory*> predicate_factories; |
| 99 predicate_factories["test_predicate"] = &factory; | 98 predicate_factories["test_predicate"] = &factory; |
| 100 std::string error; | 99 std::string error; |
| 101 scoped_ptr<ContentCondition> condition = CreateContentCondition( | 100 std::unique_ptr<ContentCondition> condition = CreateContentCondition( |
| 102 nullptr, | 101 nullptr, predicate_factories, |
| 103 predicate_factories, | |
| 104 *base::test::ParseJson( | 102 *base::test::ParseJson( |
| 105 "{\n" | 103 "{\n" |
| 106 " \"test_predicate\": \"\",\n" | 104 " \"test_predicate\": \"\",\n" |
| 107 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | 105 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" |
| 108 "}"), | 106 "}"), |
| 109 &error); | 107 &error); |
| 110 EXPECT_EQ("error message", error); | 108 EXPECT_EQ("error message", error); |
| 111 EXPECT_FALSE(condition); | 109 EXPECT_FALSE(condition); |
| 112 } | 110 } |
| 113 | 111 |
| 114 TEST(DeclarativeContentConditionTest, AllSpecifiedPredicatesCreated) { | 112 TEST(DeclarativeContentConditionTest, AllSpecifiedPredicatesCreated) { |
| 115 TestPredicateFactoryGeneratingPredicate factory1, factory2; | 113 TestPredicateFactoryGeneratingPredicate factory1, factory2; |
| 116 std::map<std::string, ContentPredicateFactory*> predicate_factories; | 114 std::map<std::string, ContentPredicateFactory*> predicate_factories; |
| 117 predicate_factories["test_predicate1"] = &factory1; | 115 predicate_factories["test_predicate1"] = &factory1; |
| 118 predicate_factories["test_predicate2"] = &factory2; | 116 predicate_factories["test_predicate2"] = &factory2; |
| 119 std::string error; | 117 std::string error; |
| 120 scoped_ptr<ContentCondition> condition = CreateContentCondition( | 118 std::unique_ptr<ContentCondition> condition = CreateContentCondition( |
| 121 nullptr, | 119 nullptr, predicate_factories, |
| 122 predicate_factories, | |
| 123 *base::test::ParseJson( | 120 *base::test::ParseJson( |
| 124 "{\n" | 121 "{\n" |
| 125 " \"test_predicate1\": {},\n" | 122 " \"test_predicate1\": {},\n" |
| 126 " \"test_predicate2\": [],\n" | 123 " \"test_predicate2\": [],\n" |
| 127 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | 124 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" |
| 128 "}"), | 125 "}"), |
| 129 &error); | 126 &error); |
| 130 ASSERT_TRUE(condition); | 127 ASSERT_TRUE(condition); |
| 131 ASSERT_EQ(1u, factory1.created_predicates().size()); | 128 ASSERT_EQ(1u, factory1.created_predicates().size()); |
| 132 ASSERT_EQ(1u, factory2.created_predicates().size()); | 129 ASSERT_EQ(1u, factory2.created_predicates().size()); |
| 133 std::vector<const ContentPredicate*> predicates; | 130 std::vector<const ContentPredicate*> predicates; |
| 134 for (const auto& predicate : condition->predicates) | 131 for (const auto& predicate : condition->predicates) |
| 135 predicates.push_back(predicate.get()); | 132 predicates.push_back(predicate.get()); |
| 136 EXPECT_THAT(predicates, | 133 EXPECT_THAT(predicates, |
| 137 UnorderedElementsAre(factory1.created_predicates()[0], | 134 UnorderedElementsAre(factory1.created_predicates()[0], |
| 138 factory2.created_predicates()[0])); | 135 factory2.created_predicates()[0])); |
| 139 } | 136 } |
| 140 | 137 |
| 141 } // namespace extensions | 138 } // namespace extensions |
| OLD | NEW |