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

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

Issue 1871713002: Convert //chrome/browser/extensions from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and fix header Created 4 years, 8 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
OLDNEW
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_ css_condition_tracker.h" 5 #include "chrome/browser/extensions/api/declarative_content/declarative_content_ css_condition_tracker.h"
6 6
7 #include <memory>
7 #include <tuple> 8 #include <tuple>
8 9
9 #include "base/macros.h" 10 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/test/values_test_util.h" 11 #include "base/test/values_test_util.h"
12 #include "chrome/browser/extensions/api/declarative_content/content_predicate_ev aluator.h" 12 #include "chrome/browser/extensions/api/declarative_content/content_predicate_ev aluator.h"
13 #include "chrome/browser/extensions/api/declarative_content/declarative_content_ condition_tracker_test.h" 13 #include "chrome/browser/extensions/api/declarative_content/declarative_content_ condition_tracker_test.h"
14 #include "content/public/browser/navigation_details.h" 14 #include "content/public/browser/navigation_details.h"
15 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/mock_render_process_host.h" 16 #include "content/public/test/mock_render_process_host.h"
17 #include "extensions/common/extension_messages.h" 17 #include "extensions/common/extension_messages.h"
18 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 19
20 namespace extensions { 20 namespace extensions {
(...skipping 25 matching lines...) Expand all
46 return true; 46 return true;
47 } 47 }
48 48
49 private: 49 private:
50 int evaluation_requests_; 50 int evaluation_requests_;
51 51
52 DISALLOW_COPY_AND_ASSIGN(Delegate); 52 DISALLOW_COPY_AND_ASSIGN(Delegate);
53 }; 53 };
54 54
55 // Creates a predicate with appropriate expectations of success. 55 // Creates a predicate with appropriate expectations of success.
56 scoped_ptr<const ContentPredicate> CreatePredicate(const std::string& value) { 56 std::unique_ptr<const ContentPredicate> CreatePredicate(
57 scoped_ptr<const ContentPredicate> predicate; 57 const std::string& value) {
58 std::unique_ptr<const ContentPredicate> predicate;
58 CreatePredicateImpl(value, &predicate); 59 CreatePredicateImpl(value, &predicate);
59 return predicate; 60 return predicate;
60 } 61 }
61 62
62 // Expects an ExtensionMsg_WatchPages message in |sink| with |selectors| as 63 // Expects an ExtensionMsg_WatchPages message in |sink| with |selectors| as
63 // the param, after invoking |func|. 64 // the param, after invoking |func|.
64 template <class Func> 65 template <class Func>
65 void ExpectWatchPagesMessage(content::WebContents* tab, 66 void ExpectWatchPagesMessage(content::WebContents* tab,
66 const std::set<std::string>& selectors, 67 const std::set<std::string>& selectors,
67 const Func& func) { 68 const Func& func) {
(...skipping 27 matching lines...) Expand all
95 selectors); 96 selectors);
96 EXPECT_TRUE(GetMockRenderProcessHost(tab)->OnMessageReceived(page_change)); 97 EXPECT_TRUE(GetMockRenderProcessHost(tab)->OnMessageReceived(page_change));
97 } 98 }
98 99
99 Delegate delegate_; 100 Delegate delegate_;
100 DeclarativeContentCssConditionTracker tracker_; 101 DeclarativeContentCssConditionTracker tracker_;
101 102
102 private: 103 private:
103 // This function exists to work around the gtest limitation that functions 104 // This function exists to work around the gtest limitation that functions
104 // with fatal assertions must return void. 105 // with fatal assertions must return void.
105 void CreatePredicateImpl( 106 void CreatePredicateImpl(const std::string& value,
106 const std::string& value, 107 std::unique_ptr<const ContentPredicate>* predicate) {
107 scoped_ptr<const ContentPredicate>* predicate) {
108 std::string error; 108 std::string error;
109 *predicate = tracker_.CreatePredicate( 109 *predicate = tracker_.CreatePredicate(
110 nullptr, 110 nullptr,
111 *base::test::ParseJson(value), 111 *base::test::ParseJson(value),
112 &error); 112 &error);
113 EXPECT_EQ("", error); 113 EXPECT_EQ("", error);
114 ASSERT_TRUE(*predicate); 114 ASSERT_TRUE(*predicate);
115 } 115 }
116 116
117 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTrackerTest); 117 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentCssConditionTrackerTest);
118 }; 118 };
119 119
120 TEST(DeclarativeContentCssPredicateTest, WrongCssDatatype) { 120 TEST(DeclarativeContentCssPredicateTest, WrongCssDatatype) {
121 std::string error; 121 std::string error;
122 scoped_ptr<DeclarativeContentCssPredicate> predicate = 122 std::unique_ptr<DeclarativeContentCssPredicate> predicate =
123 DeclarativeContentCssPredicate::Create( 123 DeclarativeContentCssPredicate::Create(
124 nullptr, 124 nullptr, *base::test::ParseJson("\"selector\""), &error);
125 *base::test::ParseJson("\"selector\""),
126 &error);
127 EXPECT_THAT(error, HasSubstr("invalid type")); 125 EXPECT_THAT(error, HasSubstr("invalid type"));
128 EXPECT_FALSE(predicate); 126 EXPECT_FALSE(predicate);
129 } 127 }
130 128
131 TEST(DeclarativeContentCssPredicateTest, CssPredicate) { 129 TEST(DeclarativeContentCssPredicateTest, CssPredicate) {
132 std::string error; 130 std::string error;
133 scoped_ptr<DeclarativeContentCssPredicate> predicate = 131 std::unique_ptr<DeclarativeContentCssPredicate> predicate =
134 DeclarativeContentCssPredicate::Create( 132 DeclarativeContentCssPredicate::Create(
135 nullptr, 133 nullptr, *base::test::ParseJson("[\"input\", \"a\"]"), &error);
136 *base::test::ParseJson("[\"input\", \"a\"]"),
137 &error);
138 EXPECT_EQ("", error); 134 EXPECT_EQ("", error);
139 ASSERT_TRUE(predicate); 135 ASSERT_TRUE(predicate);
140 136
141 EXPECT_THAT(predicate->css_selectors(), UnorderedElementsAre("input", "a")); 137 EXPECT_THAT(predicate->css_selectors(), UnorderedElementsAre("input", "a"));
142 } 138 }
143 139
144 // Tests that adding and removing predicates causes a WatchPages message to be 140 // Tests that adding and removing predicates causes a WatchPages message to be
145 // sent. 141 // sent.
146 TEST_F(DeclarativeContentCssConditionTrackerTest, AddAndRemovePredicates) { 142 TEST_F(DeclarativeContentCssConditionTrackerTest, AddAndRemovePredicates) {
147 const scoped_ptr<content::WebContents> tab = MakeTab(); 143 const std::unique_ptr<content::WebContents> tab = MakeTab();
148 tracker_.TrackForWebContents(tab.get()); 144 tracker_.TrackForWebContents(tab.get());
149 EXPECT_EQ(0, delegate_.evaluation_requests()); 145 EXPECT_EQ(0, delegate_.evaluation_requests());
150 146
151 // Check that adding predicates sends a WatchPages message with the 147 // Check that adding predicates sends a WatchPages message with the
152 // corresponding selectors to the tab's RenderProcessHost. 148 // corresponding selectors to the tab's RenderProcessHost.
153 std::vector<scoped_ptr<const ContentPredicate>> predicates; 149 std::vector<std::unique_ptr<const ContentPredicate>> predicates;
154 predicates.push_back(CreatePredicate("[\"a\", \"div\"]")); 150 predicates.push_back(CreatePredicate("[\"a\", \"div\"]"));
155 predicates.push_back(CreatePredicate("[\"b\"]")); 151 predicates.push_back(CreatePredicate("[\"b\"]"));
156 predicates.push_back(CreatePredicate("[\"input\"]")); 152 predicates.push_back(CreatePredicate("[\"input\"]"));
157 153
158 // Add the predicates in two groups: (0, 1) and (2). 154 // Add the predicates in two groups: (0, 1) and (2).
159 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups; 155 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups;
160 const void* const group1 = GeneratePredicateGroupID(); 156 const void* const group1 = GeneratePredicateGroupID();
161 predicate_groups[group1].push_back(predicates[0].get()); 157 predicate_groups[group1].push_back(predicates[0].get());
162 predicate_groups[group1].push_back(predicates[1].get()); 158 predicate_groups[group1].push_back(predicates[1].get());
163 const void* const group2 = GeneratePredicateGroupID(); 159 const void* const group2 = GeneratePredicateGroupID();
(...skipping 24 matching lines...) Expand all
188 ExpectWatchPagesMessage(tab.get(), watched_selectors, [this, &group2]() { 184 ExpectWatchPagesMessage(tab.get(), watched_selectors, [this, &group2]() {
189 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group2)); 185 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group2));
190 }); 186 });
191 EXPECT_EQ(0, delegate_.evaluation_requests()); 187 EXPECT_EQ(0, delegate_.evaluation_requests());
192 } 188 }
193 189
194 // Tests that adding and removing predicates that specify the same CSS selector 190 // Tests that adding and removing predicates that specify the same CSS selector
195 // as an existing predicate does not cause a WatchPages message to be sent. 191 // as an existing predicate does not cause a WatchPages message to be sent.
196 TEST_F(DeclarativeContentCssConditionTrackerTest, 192 TEST_F(DeclarativeContentCssConditionTrackerTest,
197 AddAndRemovePredicatesWithSameSelectors) { 193 AddAndRemovePredicatesWithSameSelectors) {
198 const scoped_ptr<content::WebContents> tab = MakeTab(); 194 const std::unique_ptr<content::WebContents> tab = MakeTab();
199 tracker_.TrackForWebContents(tab.get()); 195 tracker_.TrackForWebContents(tab.get());
200 EXPECT_EQ(0, delegate_.evaluation_requests()); 196 EXPECT_EQ(0, delegate_.evaluation_requests());
201 197
202 // Add the first predicate and expect a WatchPages message. 198 // Add the first predicate and expect a WatchPages message.
203 std::string error; 199 std::string error;
204 scoped_ptr<const ContentPredicate> predicate1 = CreatePredicate("[\"a\"]"); 200 std::unique_ptr<const ContentPredicate> predicate1 =
201 CreatePredicate("[\"a\"]");
205 202
206 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups1; 203 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups1;
207 const void* const group1 = GeneratePredicateGroupID(); 204 const void* const group1 = GeneratePredicateGroupID();
208 predicate_groups1[group1].push_back(predicate1.get()); 205 predicate_groups1[group1].push_back(predicate1.get());
209 206
210 std::set<std::string> watched_selectors; 207 std::set<std::string> watched_selectors;
211 watched_selectors.insert("a"); 208 watched_selectors.insert("a");
212 ExpectWatchPagesMessage(tab.get(), watched_selectors, 209 ExpectWatchPagesMessage(tab.get(), watched_selectors,
213 [this, &predicate_groups1]() { 210 [this, &predicate_groups1]() {
214 tracker_.TrackPredicates(predicate_groups1); 211 tracker_.TrackPredicates(predicate_groups1);
215 }); 212 });
216 EXPECT_EQ(0, delegate_.evaluation_requests()); 213 EXPECT_EQ(0, delegate_.evaluation_requests());
217 214
218 // Add the second predicate specifying the same selector and expect no 215 // Add the second predicate specifying the same selector and expect no
219 // WatchPages message. 216 // WatchPages message.
220 scoped_ptr<const ContentPredicate> predicate2 = CreatePredicate("[\"a\"]"); 217 std::unique_ptr<const ContentPredicate> predicate2 =
218 CreatePredicate("[\"a\"]");
221 219
222 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups2; 220 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups2;
223 const void* const group2 = GeneratePredicateGroupID(); 221 const void* const group2 = GeneratePredicateGroupID();
224 predicate_groups2[group2].push_back(predicate2.get()); 222 predicate_groups2[group2].push_back(predicate2.get());
225 223
226 ExpectNoWatchPagesMessage(tab.get(), [this, &predicate_groups2]() { 224 ExpectNoWatchPagesMessage(tab.get(), [this, &predicate_groups2]() {
227 tracker_.TrackPredicates(predicate_groups2); 225 tracker_.TrackPredicates(predicate_groups2);
228 }); 226 });
229 EXPECT_EQ(0, delegate_.evaluation_requests()); 227 EXPECT_EQ(0, delegate_.evaluation_requests());
230 228
231 // Remove the first predicate and expect no WatchPages message. 229 // Remove the first predicate and expect no WatchPages message.
232 ExpectNoWatchPagesMessage(tab.get(), [this, group1]() { 230 ExpectNoWatchPagesMessage(tab.get(), [this, group1]() {
233 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group1)); 231 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group1));
234 }); 232 });
235 EXPECT_EQ(0, delegate_.evaluation_requests()); 233 EXPECT_EQ(0, delegate_.evaluation_requests());
236 234
237 // Remove the second predicate and expect an empty WatchPages message. 235 // Remove the second predicate and expect an empty WatchPages message.
238 ExpectWatchPagesMessage(tab.get(), std::set<std::string>(), 236 ExpectWatchPagesMessage(tab.get(), std::set<std::string>(),
239 [this, group2]() { 237 [this, group2]() {
240 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group2)); 238 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group2));
241 }); 239 });
242 EXPECT_EQ(0, delegate_.evaluation_requests()); 240 EXPECT_EQ(0, delegate_.evaluation_requests());
243 } 241 }
244 242
245 // Tests that WatchedPageChange messages result in evaluation requests, and 243 // Tests that WatchedPageChange messages result in evaluation requests, and
246 // cause the appropriate predicates to evaluate to true. 244 // cause the appropriate predicates to evaluate to true.
247 TEST_F(DeclarativeContentCssConditionTrackerTest, WatchedPageChange) { 245 TEST_F(DeclarativeContentCssConditionTrackerTest, WatchedPageChange) {
248 int expected_evaluation_requests = 0; 246 int expected_evaluation_requests = 0;
249 247
250 const scoped_ptr<content::WebContents> tab = MakeTab(); 248 const std::unique_ptr<content::WebContents> tab = MakeTab();
251 tracker_.TrackForWebContents(tab.get()); 249 tracker_.TrackForWebContents(tab.get());
252 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests()); 250 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests());
253 251
254 scoped_ptr<const ContentPredicate> div_predicate = 252 std::unique_ptr<const ContentPredicate> div_predicate =
255 CreatePredicate("[\"div\"]"); 253 CreatePredicate("[\"div\"]");
256 scoped_ptr<const ContentPredicate> a_predicate = CreatePredicate("[\"a\"]"); 254 std::unique_ptr<const ContentPredicate> a_predicate =
255 CreatePredicate("[\"a\"]");
257 256
258 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups; 257 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups;
259 const void* const group = GeneratePredicateGroupID(); 258 const void* const group = GeneratePredicateGroupID();
260 predicate_groups[group].push_back(div_predicate.get()); 259 predicate_groups[group].push_back(div_predicate.get());
261 predicate_groups[group].push_back(a_predicate.get()); 260 predicate_groups[group].push_back(a_predicate.get());
262 tracker_.TrackPredicates(predicate_groups); 261 tracker_.TrackPredicates(predicate_groups);
263 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests()); 262 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests());
264 263
265 // Check that receiving an OnWatchedPageChange message from the tab results in 264 // Check that receiving an OnWatchedPageChange message from the tab results in
266 // a request for condition evaluation. 265 // a request for condition evaluation.
267 const std::vector<std::string> matched_selectors(1, "div"); 266 const std::vector<std::string> matched_selectors(1, "div");
268 SendOnWatchedPageChangeMessage(tab.get(), matched_selectors); 267 SendOnWatchedPageChangeMessage(tab.get(), matched_selectors);
269 EXPECT_EQ(++expected_evaluation_requests, delegate_.evaluation_requests()); 268 EXPECT_EQ(++expected_evaluation_requests, delegate_.evaluation_requests());
270 269
271 // Check that only the div predicate matches. 270 // Check that only the div predicate matches.
272 EXPECT_TRUE(tracker_.EvaluatePredicate(div_predicate.get(), tab.get())); 271 EXPECT_TRUE(tracker_.EvaluatePredicate(div_predicate.get(), tab.get()));
273 EXPECT_FALSE(tracker_.EvaluatePredicate(a_predicate.get(), tab.get())); 272 EXPECT_FALSE(tracker_.EvaluatePredicate(a_predicate.get(), tab.get()));
274 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests()); 273 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests());
275 274
276 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group)); 275 tracker_.StopTrackingPredicates(std::vector<const void*>(1, group));
277 } 276 }
278 277
279 // Tests in-page and non-in-page navigations. Only the latter should reset 278 // Tests in-page and non-in-page navigations. Only the latter should reset
280 // matching selectors and result in an evaluation request. 279 // matching selectors and result in an evaluation request.
281 TEST_F(DeclarativeContentCssConditionTrackerTest, Navigation) { 280 TEST_F(DeclarativeContentCssConditionTrackerTest, Navigation) {
282 DeclarativeContentCssConditionTracker tracker(&delegate_); 281 DeclarativeContentCssConditionTracker tracker(&delegate_);
283 282
284 int expected_evaluation_requests = 0; 283 int expected_evaluation_requests = 0;
285 284
286 const scoped_ptr<content::WebContents> tab = MakeTab(); 285 const std::unique_ptr<content::WebContents> tab = MakeTab();
287 tracker_.TrackForWebContents(tab.get()); 286 tracker_.TrackForWebContents(tab.get());
288 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests()); 287 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests());
289 288
290 scoped_ptr<const ContentPredicate> predicate = CreatePredicate("[\"div\"]"); 289 std::unique_ptr<const ContentPredicate> predicate =
290 CreatePredicate("[\"div\"]");
291 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups; 291 std::map<const void*, std::vector<const ContentPredicate*>> predicate_groups;
292 const void* const group = GeneratePredicateGroupID(); 292 const void* const group = GeneratePredicateGroupID();
293 predicate_groups[group].push_back(predicate.get()); 293 predicate_groups[group].push_back(predicate.get());
294 tracker_.TrackPredicates(predicate_groups); 294 tracker_.TrackPredicates(predicate_groups);
295 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests()); 295 EXPECT_EQ(expected_evaluation_requests, delegate_.evaluation_requests());
296 296
297 // Set up the tab to have a matching selector. 297 // Set up the tab to have a matching selector.
298 const std::vector<std::string> matched_selectors(1, "div"); 298 const std::vector<std::string> matched_selectors(1, "div");
299 SendOnWatchedPageChangeMessage(tab.get(), matched_selectors); 299 SendOnWatchedPageChangeMessage(tab.get(), matched_selectors);
300 EXPECT_EQ(++expected_evaluation_requests, delegate_.evaluation_requests()); 300 EXPECT_EQ(++expected_evaluation_requests, delegate_.evaluation_requests());
(...skipping 15 matching lines...) Expand all
316 details.is_in_page = false; 316 details.is_in_page = false;
317 content::FrameNavigateParams params; 317 content::FrameNavigateParams params;
318 tracker_.OnWebContentsNavigation(tab.get(), details, params); 318 tracker_.OnWebContentsNavigation(tab.get(), details, params);
319 EXPECT_FALSE(tracker_.EvaluatePredicate(predicate.get(), tab.get())); 319 EXPECT_FALSE(tracker_.EvaluatePredicate(predicate.get(), tab.get()));
320 EXPECT_EQ(++expected_evaluation_requests, delegate_.evaluation_requests()); 320 EXPECT_EQ(++expected_evaluation_requests, delegate_.evaluation_requests());
321 } 321 }
322 } 322 }
323 323
324 // https://crbug.com/497586 324 // https://crbug.com/497586
325 TEST_F(DeclarativeContentCssConditionTrackerTest, WebContentsOutlivesTracker) { 325 TEST_F(DeclarativeContentCssConditionTrackerTest, WebContentsOutlivesTracker) {
326 const scoped_ptr<content::WebContents> tab = MakeTab(); 326 const std::unique_ptr<content::WebContents> tab = MakeTab();
327 327
328 { 328 {
329 DeclarativeContentCssConditionTracker tracker(&delegate_); 329 DeclarativeContentCssConditionTracker tracker(&delegate_);
330 tracker_.TrackForWebContents(tab.get()); 330 tracker_.TrackForWebContents(tab.get());
331 } 331 }
332 } 332 }
333 333
334 } // namespace extensions 334 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698