Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/browser/extensions/api/declarative_content/content_condition_unittest.cc

Issue 11547033: Implement declarativeContent API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 pageUrl.
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698