Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" | |
| 6 | |
| 7 #include <set> | |
| 8 | |
| 9 #include "base/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 "chrome/common/extensions/matcher/url_matcher.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace extensions { | |
| 18 namespace { | |
| 19 | |
| 20 using testing::ElementsAre; | |
| 21 using testing::HasSubstr; | |
| 22 | |
| 23 TEST(DeclarativeContentConditionTest, ErroneousCondition) { | |
| 24 URLMatcher matcher; | |
| 25 | |
| 26 std::string error; | |
| 27 scoped_ptr<ContentCondition> result; | |
| 28 | |
| 29 // Test unknown condition name passed. | |
| 30 error.clear(); | |
| 31 result = ContentCondition::Create( | |
| 32 matcher.condition_factory(), | |
| 33 *base::test::ParseJson( | |
| 34 "{\n" | |
| 35 " \"invalid\": \"foobar\",\n" | |
| 36 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | |
| 37 "}"), | |
| 38 &error); | |
| 39 EXPECT_THAT(error, HasSubstr("Unknown condition attribute")); | |
| 40 EXPECT_FALSE(result); | |
| 41 | |
| 42 // Test wrong datatype in host_suffix. | |
|
battre
2013/01/18 10:37:29
This comment is incorrect.
Jeffrey Yasskin
2013/01/22 08:01:22
Fixed.
| |
| 43 error.clear(); | |
| 44 result = ContentCondition::Create( | |
| 45 matcher.condition_factory(), | |
| 46 *base::test::ParseJson( | |
| 47 "{\n" | |
| 48 " \"pageUrl\": [],\n" | |
| 49 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | |
| 50 "}"), | |
| 51 &error); | |
| 52 EXPECT_THAT(error, HasSubstr("invalid type")); | |
| 53 EXPECT_FALSE(result); | |
| 54 | |
| 55 EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions"; | |
| 56 } | |
| 57 | |
| 58 TEST(DeclarativeContentConditionTest, ConditionWithUrlAndCss) { | |
| 59 URLMatcher matcher; | |
| 60 | |
| 61 std::string error; | |
| 62 scoped_ptr<ContentCondition> result = ContentCondition::Create( | |
| 63 matcher.condition_factory(), | |
| 64 *base::test::ParseJson( | |
| 65 "{\n" | |
| 66 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" | |
| 67 " \"pageUrl\": {\"hostSuffix\": \"example.com\"},\n" | |
| 68 " \"css\": [\"input\"],\n" | |
| 69 "}"), | |
| 70 &error); | |
| 71 EXPECT_EQ("", error); | |
| 72 ASSERT_TRUE(result); | |
| 73 | |
| 74 URLMatcherConditionSet::Vector all_new_condition_sets; | |
| 75 result->GetURLMatcherConditionSets(&all_new_condition_sets); | |
| 76 matcher.AddConditionSets(all_new_condition_sets); | |
| 77 EXPECT_FALSE(matcher.IsEmpty()); | |
| 78 | |
| 79 RendererContentMatchData match_data; | |
| 80 match_data.page_url = GURL("http://www.example.com/foobar"); | |
| 81 match_data.css_selectors.insert("input"); | |
| 82 | |
| 83 std::set<URLMatcherConditionSet::ID> matches = | |
| 84 matcher.MatchURL(GURL("http://google.com/")); | |
| 85 EXPECT_THAT(matches, ElementsAre(/*empty*/)); | |
| 86 matches = matcher.MatchURL(match_data.page_url); | |
| 87 EXPECT_THAT(matches, ElementsAre(result->url_matcher_condition_set_id())); | |
| 88 | |
| 89 EXPECT_TRUE(result->IsFulfilled(matches, match_data)); | |
| 90 | |
| 91 match_data.css_selectors.clear(); | |
| 92 match_data.css_selectors.insert("body"); | |
| 93 EXPECT_FALSE(result->IsFulfilled(matches, match_data)); | |
| 94 } | |
| 95 | |
| 96 } // namespace | |
| 97 } // namespace extensions | |
| OLD | NEW |