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/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" 16 #include "url/gurl.h" 17 18 using testing::ElementsAre; 19 using testing::HasSubstr; 20 using url_matcher::URLMatcher; 21 using url_matcher::URLMatcherConditionSet; 22 23 namespace extensions { 24 namespace { 25 26 TEST(DeclarativeContentConditionTest, UnknownConditionName) { 27 URLMatcher matcher; 28 std::string error; 29 scoped_ptr<ContentCondition> result = ContentCondition::Create( 30 NULL, 31 matcher.condition_factory(), 32 *base::test::ParseJson( 33 "{\n" 34 " \"invalid\": \"foobar\",\n" 35 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" 36 "}"), 37 &error); 38 EXPECT_THAT(error, HasSubstr("Unknown condition attribute")); 39 EXPECT_FALSE(result); 40 41 EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions"; 42 } 43 44 TEST(DeclarativeContentConditionTest, WrongPageUrlDatatype) { 45 URLMatcher matcher; 46 std::string error; 47 scoped_ptr<ContentCondition> result = ContentCondition::Create( 48 NULL, 49 matcher.condition_factory(), 50 *base::test::ParseJson( 51 "{\n" 52 " \"pageUrl\": [],\n" 53 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" 54 "}"), 55 &error); 56 EXPECT_THAT(error, HasSubstr("invalid type")); 57 EXPECT_FALSE(result); 58 59 EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions"; 60 } 61 62 TEST(DeclarativeContentConditionTest, WrongCssDatatype) { 63 URLMatcher matcher; 64 std::string error; 65 scoped_ptr<ContentCondition> result = ContentCondition::Create( 66 NULL, 67 matcher.condition_factory(), 68 *base::test::ParseJson( 69 "{\n" 70 " \"css\": \"selector\",\n" 71 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" 72 "}"), 73 &error); 74 EXPECT_THAT(error, HasSubstr("invalid type")); 75 EXPECT_FALSE(result); 76 77 EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions"; 78 } 79 80 TEST(DeclarativeContentConditionTest, ConditionWithUrlAndCss) { 81 URLMatcher matcher; 82 83 std::string error; 84 scoped_ptr<ContentCondition> result = ContentCondition::Create( 85 NULL, 86 matcher.condition_factory(), 87 *base::test::ParseJson( 88 "{\n" 89 " \"instanceType\": \"declarativeContent.PageStateMatcher\",\n" 90 " \"pageUrl\": {\"hostSuffix\": \"example.com\"},\n" 91 " \"css\": [\"input\"],\n" 92 "}"), 93 &error); 94 EXPECT_EQ("", error); 95 ASSERT_TRUE(result); 96 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()); 101 102 RendererContentMatchData match_data; 103 match_data.css_selectors.insert("input"); 104 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())); 111 112 EXPECT_TRUE(result->IsFulfilled(match_data)); 113 114 match_data.css_selectors.clear(); 115 match_data.css_selectors.insert("body"); 116 EXPECT_FALSE(result->IsFulfilled(match_data)); 117 } 118 119 } // namespace 120 } // namespace extensions 121