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/declarative_content_
page_url_condition_tracker.h" | 5 #include "chrome/browser/extensions/api/declarative_content/declarative_content_
page_url_condition_tracker.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 std::set<content::WebContents*> evaluation_requests_; | 52 std::set<content::WebContents*> evaluation_requests_; |
53 | 53 |
54 DISALLOW_COPY_AND_ASSIGN(Delegate); | 54 DISALLOW_COPY_AND_ASSIGN(Delegate); |
55 }; | 55 }; |
56 | 56 |
57 DeclarativeContentPageUrlConditionTrackerTest() | 57 DeclarativeContentPageUrlConditionTrackerTest() |
58 : tracker_(&delegate_) { | 58 : tracker_(&delegate_) { |
59 } | 59 } |
60 | 60 |
61 // Creates a predicate with appropriate expectations of success. | 61 // Creates a predicate with appropriate expectations of success. |
62 scoped_ptr<const ContentPredicate> CreatePredicate(const std::string& value) { | 62 std::unique_ptr<const ContentPredicate> CreatePredicate( |
63 scoped_ptr<const ContentPredicate> predicate; | 63 const std::string& value) { |
| 64 std::unique_ptr<const ContentPredicate> predicate; |
64 CreatePredicateImpl(value, &predicate); | 65 CreatePredicateImpl(value, &predicate); |
65 return predicate; | 66 return predicate; |
66 } | 67 } |
67 | 68 |
68 void LoadURL(content::WebContents* tab, const GURL& url) { | 69 void LoadURL(content::WebContents* tab, const GURL& url) { |
69 tab->GetController().LoadURL(url, content::Referrer(), | 70 tab->GetController().LoadURL(url, content::Referrer(), |
70 ui::PAGE_TRANSITION_LINK, std::string()); | 71 ui::PAGE_TRANSITION_LINK, std::string()); |
71 } | 72 } |
72 | 73 |
73 Delegate delegate_; | 74 Delegate delegate_; |
74 DeclarativeContentPageUrlConditionTracker tracker_; | 75 DeclarativeContentPageUrlConditionTracker tracker_; |
75 | 76 |
76 private: | 77 private: |
77 // This function exists to work around the gtest limitation that functions | 78 // This function exists to work around the gtest limitation that functions |
78 // with fatal assertions must return void. | 79 // with fatal assertions must return void. |
79 void CreatePredicateImpl(const std::string& value, | 80 void CreatePredicateImpl(const std::string& value, |
80 scoped_ptr<const ContentPredicate>* predicate) { | 81 std::unique_ptr<const ContentPredicate>* predicate) { |
81 std::string error; | 82 std::string error; |
82 *predicate = tracker_.CreatePredicate( | 83 *predicate = tracker_.CreatePredicate( |
83 nullptr, | 84 nullptr, |
84 *base::test::ParseJson(value), | 85 *base::test::ParseJson(value), |
85 &error); | 86 &error); |
86 EXPECT_EQ("", error); | 87 EXPECT_EQ("", error); |
87 ASSERT_TRUE(*predicate); | 88 ASSERT_TRUE(*predicate); |
88 } | 89 } |
89 | 90 |
90 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentPageUrlConditionTrackerTest); | 91 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentPageUrlConditionTrackerTest); |
91 }; | 92 }; |
92 | 93 |
93 TEST(DeclarativeContentPageUrlPredicateTest, WrongPageUrlDatatype) { | 94 TEST(DeclarativeContentPageUrlPredicateTest, WrongPageUrlDatatype) { |
94 url_matcher::URLMatcher matcher; | 95 url_matcher::URLMatcher matcher; |
95 std::string error; | 96 std::string error; |
96 scoped_ptr<DeclarativeContentPageUrlPredicate> predicate = | 97 std::unique_ptr<DeclarativeContentPageUrlPredicate> predicate = |
97 DeclarativeContentPageUrlPredicate::Create(nullptr, | 98 DeclarativeContentPageUrlPredicate::Create( |
98 matcher.condition_factory(), | 99 nullptr, matcher.condition_factory(), *base::test::ParseJson("[]"), |
99 *base::test::ParseJson("[]"), | 100 &error); |
100 &error); | |
101 EXPECT_THAT(error, HasSubstr("invalid type")); | 101 EXPECT_THAT(error, HasSubstr("invalid type")); |
102 EXPECT_FALSE(predicate); | 102 EXPECT_FALSE(predicate); |
103 | 103 |
104 EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions"; | 104 EXPECT_TRUE(matcher.IsEmpty()) << "Errors shouldn't add URL conditions"; |
105 } | 105 } |
106 | 106 |
107 TEST(DeclarativeContentPageUrlPredicateTest, PageUrlPredicate) { | 107 TEST(DeclarativeContentPageUrlPredicateTest, PageUrlPredicate) { |
108 url_matcher::URLMatcher matcher; | 108 url_matcher::URLMatcher matcher; |
109 std::string error; | 109 std::string error; |
110 scoped_ptr<DeclarativeContentPageUrlPredicate> predicate = | 110 std::unique_ptr<DeclarativeContentPageUrlPredicate> predicate = |
111 DeclarativeContentPageUrlPredicate::Create( | 111 DeclarativeContentPageUrlPredicate::Create( |
112 nullptr, | 112 nullptr, matcher.condition_factory(), |
113 matcher.condition_factory(), | 113 *base::test::ParseJson("{\"hostSuffix\": \"example.com\"}"), &error); |
114 *base::test::ParseJson("{\"hostSuffix\": \"example.com\"}"), | |
115 &error); | |
116 EXPECT_EQ("", error); | 114 EXPECT_EQ("", error); |
117 ASSERT_TRUE(predicate); | 115 ASSERT_TRUE(predicate); |
118 | 116 |
119 url_matcher::URLMatcherConditionSet::Vector all_new_condition_sets; | 117 url_matcher::URLMatcherConditionSet::Vector all_new_condition_sets; |
120 all_new_condition_sets.push_back(predicate->url_matcher_condition_set()); | 118 all_new_condition_sets.push_back(predicate->url_matcher_condition_set()); |
121 matcher.AddConditionSets(all_new_condition_sets); | 119 matcher.AddConditionSets(all_new_condition_sets); |
122 EXPECT_FALSE(matcher.IsEmpty()); | 120 EXPECT_FALSE(matcher.IsEmpty()); |
123 | 121 |
124 EXPECT_THAT(matcher.MatchURL(GURL("http://google.com/")), | 122 EXPECT_THAT(matcher.MatchURL(GURL("http://google.com/")), |
125 ElementsAre(/*empty*/)); | 123 ElementsAre(/*empty*/)); |
126 std::set<url_matcher::URLMatcherConditionSet::ID> page_url_matches = | 124 std::set<url_matcher::URLMatcherConditionSet::ID> page_url_matches = |
127 matcher.MatchURL(GURL("http://www.example.com/foobar")); | 125 matcher.MatchURL(GURL("http://www.example.com/foobar")); |
128 EXPECT_THAT( | 126 EXPECT_THAT( |
129 page_url_matches, | 127 page_url_matches, |
130 ElementsAre(predicate->url_matcher_condition_set()->id())); | 128 ElementsAre(predicate->url_matcher_condition_set()->id())); |
131 } | 129 } |
132 | 130 |
133 // Tests that adding and removing condition sets trigger evaluation requests for | 131 // Tests that adding and removing condition sets trigger evaluation requests for |
134 // the matching WebContents. | 132 // the matching WebContents. |
135 TEST_F(DeclarativeContentPageUrlConditionTrackerTest, AddAndRemovePredicates) { | 133 TEST_F(DeclarativeContentPageUrlConditionTrackerTest, AddAndRemovePredicates) { |
136 // Create four tabs. | 134 // Create four tabs. |
137 std::vector<scoped_ptr<content::WebContents>> tabs; | 135 std::vector<std::unique_ptr<content::WebContents>> tabs; |
138 for (int i = 0; i < 4; ++i) { | 136 for (int i = 0; i < 4; ++i) { |
139 tabs.push_back(MakeTab()); | 137 tabs.push_back(MakeTab()); |
140 delegate_.evaluation_requests().clear(); | 138 delegate_.evaluation_requests().clear(); |
141 tracker_.TrackForWebContents(tabs.back().get()); | 139 tracker_.TrackForWebContents(tabs.back().get()); |
142 EXPECT_THAT(delegate_.evaluation_requests(), | 140 EXPECT_THAT(delegate_.evaluation_requests(), |
143 UnorderedElementsAre(tabs.back().get())); | 141 UnorderedElementsAre(tabs.back().get())); |
144 } | 142 } |
145 | 143 |
146 // Navigate three of them to URLs that will match with predicats we're about | 144 // Navigate three of them to URLs that will match with predicats we're about |
147 // to add. | 145 // to add. |
148 LoadURL(tabs[0].get(), GURL("http://test1/")); | 146 LoadURL(tabs[0].get(), GURL("http://test1/")); |
149 LoadURL(tabs[1].get(), GURL("http://test2/")); | 147 LoadURL(tabs[1].get(), GURL("http://test2/")); |
150 LoadURL(tabs[2].get(), GURL("http://test3/")); | 148 LoadURL(tabs[2].get(), GURL("http://test3/")); |
151 | 149 |
152 std::vector<scoped_ptr<const ContentPredicate>> predicates; | 150 std::vector<std::unique_ptr<const ContentPredicate>> predicates; |
153 std::string error; | 151 std::string error; |
154 predicates.push_back(CreatePredicate("{\"hostPrefix\": \"test1\"}")); | 152 predicates.push_back(CreatePredicate("{\"hostPrefix\": \"test1\"}")); |
155 predicates.push_back(CreatePredicate("{\"hostPrefix\": \"test2\"}")); | 153 predicates.push_back(CreatePredicate("{\"hostPrefix\": \"test2\"}")); |
156 predicates.push_back(CreatePredicate("{\"hostPrefix\": \"test3\"}")); | 154 predicates.push_back(CreatePredicate("{\"hostPrefix\": \"test3\"}")); |
157 | 155 |
158 // Add the predicates in two groups: (0, 1) and (2). | 156 // Add the predicates in two groups: (0, 1) and (2). |
159 delegate_.evaluation_requests().clear(); | 157 delegate_.evaluation_requests().clear(); |
160 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups; | 158 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups; |
161 const void* const group1 = GeneratePredicateGroupID(); | 159 const void* const group1 = GeneratePredicateGroupID(); |
162 predicate_groups[group1].push_back(predicates[0].get()); | 160 predicate_groups[group1].push_back(predicates[0].get()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 delegate_.evaluation_requests().clear(); | 192 delegate_.evaluation_requests().clear(); |
195 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group2)); | 193 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group2)); |
196 EXPECT_THAT(delegate_.evaluation_requests(), | 194 EXPECT_THAT(delegate_.evaluation_requests(), |
197 UnorderedElementsAre(tabs[2].get())); | 195 UnorderedElementsAre(tabs[2].get())); |
198 } | 196 } |
199 | 197 |
200 // Tests that tracking WebContents triggers evaluation requests for matching | 198 // Tests that tracking WebContents triggers evaluation requests for matching |
201 // rules. | 199 // rules. |
202 TEST_F(DeclarativeContentPageUrlConditionTrackerTest, TrackWebContents) { | 200 TEST_F(DeclarativeContentPageUrlConditionTrackerTest, TrackWebContents) { |
203 std::string error; | 201 std::string error; |
204 scoped_ptr<const ContentPredicate> predicate = | 202 std::unique_ptr<const ContentPredicate> predicate = |
205 CreatePredicate("{\"hostPrefix\": \"test1\"}"); | 203 CreatePredicate("{\"hostPrefix\": \"test1\"}"); |
206 | 204 |
207 delegate_.evaluation_requests().clear(); | 205 delegate_.evaluation_requests().clear(); |
208 std::map<const void*, std::vector<const ContentPredicate*>> predicates; | 206 std::map<const void*, std::vector<const ContentPredicate*>> predicates; |
209 const void* const group = GeneratePredicateGroupID(); | 207 const void* const group = GeneratePredicateGroupID(); |
210 predicates[group].push_back(predicate.get()); | 208 predicates[group].push_back(predicate.get()); |
211 tracker_.TrackPredicates(predicates); | 209 tracker_.TrackPredicates(predicates); |
212 EXPECT_TRUE(delegate_.evaluation_requests().empty()); | 210 EXPECT_TRUE(delegate_.evaluation_requests().empty()); |
213 | 211 |
214 const scoped_ptr<content::WebContents> matching_tab = MakeTab(); | 212 const std::unique_ptr<content::WebContents> matching_tab = MakeTab(); |
215 LoadURL(matching_tab.get(), GURL("http://test1/")); | 213 LoadURL(matching_tab.get(), GURL("http://test1/")); |
216 | 214 |
217 tracker_.TrackForWebContents(matching_tab.get()); | 215 tracker_.TrackForWebContents(matching_tab.get()); |
218 EXPECT_THAT(delegate_.evaluation_requests(), | 216 EXPECT_THAT(delegate_.evaluation_requests(), |
219 UnorderedElementsAre(matching_tab.get())); | 217 UnorderedElementsAre(matching_tab.get())); |
220 | 218 |
221 delegate_.evaluation_requests().clear(); | 219 delegate_.evaluation_requests().clear(); |
222 const scoped_ptr<content::WebContents> non_matching_tab = MakeTab(); | 220 const std::unique_ptr<content::WebContents> non_matching_tab = MakeTab(); |
223 tracker_.TrackForWebContents(non_matching_tab.get()); | 221 tracker_.TrackForWebContents(non_matching_tab.get()); |
224 EXPECT_THAT(delegate_.evaluation_requests(), | 222 EXPECT_THAT(delegate_.evaluation_requests(), |
225 UnorderedElementsAre(non_matching_tab.get())); | 223 UnorderedElementsAre(non_matching_tab.get())); |
226 | 224 |
227 delegate_.evaluation_requests().clear(); | 225 delegate_.evaluation_requests().clear(); |
228 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group)); | 226 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group)); |
229 EXPECT_THAT(delegate_.evaluation_requests(), | 227 EXPECT_THAT(delegate_.evaluation_requests(), |
230 UnorderedElementsAre(matching_tab.get())); | 228 UnorderedElementsAre(matching_tab.get())); |
231 } | 229 } |
232 | 230 |
233 // Tests that notifying WebContents navigation triggers evaluation requests for | 231 // Tests that notifying WebContents navigation triggers evaluation requests for |
234 // matching rules. | 232 // matching rules. |
235 TEST_F(DeclarativeContentPageUrlConditionTrackerTest, | 233 TEST_F(DeclarativeContentPageUrlConditionTrackerTest, |
236 NotifyWebContentsNavigation) { | 234 NotifyWebContentsNavigation) { |
237 std::string error; | 235 std::string error; |
238 scoped_ptr<const ContentPredicate> predicate = | 236 std::unique_ptr<const ContentPredicate> predicate = |
239 CreatePredicate("{\"hostPrefix\": \"test1\"}"); | 237 CreatePredicate("{\"hostPrefix\": \"test1\"}"); |
240 | 238 |
241 delegate_.evaluation_requests().clear(); | 239 delegate_.evaluation_requests().clear(); |
242 std::map<const void*, std::vector<const ContentPredicate*>> predicates; | 240 std::map<const void*, std::vector<const ContentPredicate*>> predicates; |
243 const void* const group = GeneratePredicateGroupID(); | 241 const void* const group = GeneratePredicateGroupID(); |
244 predicates[group].push_back(predicate.get()); | 242 predicates[group].push_back(predicate.get()); |
245 tracker_.TrackPredicates(predicates); | 243 tracker_.TrackPredicates(predicates); |
246 EXPECT_TRUE(delegate_.evaluation_requests().empty()); | 244 EXPECT_TRUE(delegate_.evaluation_requests().empty()); |
247 | 245 |
248 const scoped_ptr<content::WebContents> tab = MakeTab(); | 246 const std::unique_ptr<content::WebContents> tab = MakeTab(); |
249 tracker_.TrackForWebContents(tab.get()); | 247 tracker_.TrackForWebContents(tab.get()); |
250 EXPECT_THAT(delegate_.evaluation_requests(), | 248 EXPECT_THAT(delegate_.evaluation_requests(), |
251 UnorderedElementsAre(tab.get())); | 249 UnorderedElementsAre(tab.get())); |
252 | 250 |
253 // Check that navigation notification to a matching URL results in an | 251 // Check that navigation notification to a matching URL results in an |
254 // evaluation request. | 252 // evaluation request. |
255 LoadURL(tab.get(), GURL("http://test1/")); | 253 LoadURL(tab.get(), GURL("http://test1/")); |
256 delegate_.evaluation_requests().clear(); | 254 delegate_.evaluation_requests().clear(); |
257 tracker_.OnWebContentsNavigation(tab.get(), content::LoadCommittedDetails(), | 255 tracker_.OnWebContentsNavigation(tab.get(), content::LoadCommittedDetails(), |
258 content::FrameNavigateParams()); | 256 content::FrameNavigateParams()); |
(...skipping 27 matching lines...) Expand all Loading... |
286 EXPECT_THAT(delegate_.evaluation_requests(), | 284 EXPECT_THAT(delegate_.evaluation_requests(), |
287 UnorderedElementsAre(tab.get())); | 285 UnorderedElementsAre(tab.get())); |
288 | 286 |
289 delegate_.evaluation_requests().clear(); | 287 delegate_.evaluation_requests().clear(); |
290 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group)); | 288 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group)); |
291 EXPECT_THAT(delegate_.evaluation_requests(), | 289 EXPECT_THAT(delegate_.evaluation_requests(), |
292 UnorderedElementsAre(/* empty */)); | 290 UnorderedElementsAre(/* empty */)); |
293 } | 291 } |
294 | 292 |
295 } // namespace extensions | 293 } // namespace extensions |
OLD | NEW |