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

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry_unittest.cc

Issue 12328079: Update TestURLRequest constructor interface in extensions unit tests. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Rebase onto master. Created 7 years, 9 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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_webrequest/webrequest_rules_ registry.h" 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 rules.push_back(CreateRule2()); 235 rules.push_back(CreateRule2());
236 236
237 error = registry->AddRules(kExtensionId, rules); 237 error = registry->AddRules(kExtensionId, rules);
238 EXPECT_EQ("", error); 238 EXPECT_EQ("", error);
239 EXPECT_EQ(1, registry->num_clear_cache_calls()); 239 EXPECT_EQ(1, registry->num_clear_cache_calls());
240 240
241 std::set<const WebRequestRule*> matches; 241 std::set<const WebRequestRule*> matches;
242 242
243 GURL http_url("http://www.example.com"); 243 GURL http_url("http://www.example.com");
244 net::TestURLRequestContext context; 244 net::TestURLRequestContext context;
245 net::TestURLRequest http_request(http_url, NULL, &context); 245 net::TestURLRequest http_request(http_url, NULL, &context, NULL);
246 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); 246 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST);
247 matches = registry->GetMatches(request_data); 247 matches = registry->GetMatches(request_data);
248 EXPECT_EQ(2u, matches.size()); 248 EXPECT_EQ(2u, matches.size());
249 249
250 std::set<WebRequestRule::GlobalRuleId> matches_ids; 250 std::set<WebRequestRule::GlobalRuleId> matches_ids;
251 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); 251 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin();
252 it != matches.end(); ++it) 252 it != matches.end(); ++it)
253 matches_ids.insert((*it)->id()); 253 matches_ids.insert((*it)->id());
254 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); 254 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1)));
255 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); 255 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2)));
256 256
257 GURL foobar_url("http://www.foobar.com"); 257 GURL foobar_url("http://www.foobar.com");
258 net::TestURLRequest foobar_request(foobar_url, NULL, &context); 258 net::TestURLRequest foobar_request(foobar_url, NULL, &context, NULL);
259 request_data.request = &foobar_request; 259 request_data.request = &foobar_request;
260 matches = registry->GetMatches(request_data); 260 matches = registry->GetMatches(request_data);
261 EXPECT_EQ(1u, matches.size()); 261 EXPECT_EQ(1u, matches.size());
262 WebRequestRule::GlobalRuleId expected_pair = 262 WebRequestRule::GlobalRuleId expected_pair =
263 std::make_pair(kExtensionId, kRuleId2); 263 std::make_pair(kExtensionId, kRuleId2);
264 EXPECT_EQ(expected_pair, (*matches.begin())->id()); 264 EXPECT_EQ(expected_pair, (*matches.begin())->id());
265 } 265 }
266 266
267 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { 267 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) {
268 scoped_refptr<TestWebRequestRulesRegistry> registry( 268 scoped_refptr<TestWebRequestRulesRegistry> registry(
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 error = registry->AddRules(kExtensionId, rules_to_add_1); 374 error = registry->AddRules(kExtensionId, rules_to_add_1);
375 EXPECT_EQ("", error); 375 EXPECT_EQ("", error);
376 376
377 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); 377 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1);
378 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); 378 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com");
379 error = registry->AddRules(kExtensionId2, rules_to_add_2); 379 error = registry->AddRules(kExtensionId2, rules_to_add_2);
380 EXPECT_EQ("", error); 380 EXPECT_EQ("", error);
381 381
382 GURL url("http://www.google.com"); 382 GURL url("http://www.google.com");
383 net::TestURLRequestContext context; 383 net::TestURLRequestContext context;
384 net::TestURLRequest request(url, NULL, &context); 384 net::TestURLRequest request(url, NULL, &context, NULL);
385 WebRequestData request_data(&request, ON_BEFORE_REQUEST); 385 WebRequestData request_data(&request, ON_BEFORE_REQUEST);
386 std::list<LinkedPtrEventResponseDelta> deltas = 386 std::list<LinkedPtrEventResponseDelta> deltas =
387 registry->CreateDeltas(NULL, request_data, false); 387 registry->CreateDeltas(NULL, request_data, false);
388 388
389 // The second extension is installed later and will win for this reason 389 // The second extension is installed later and will win for this reason
390 // in conflict resolution. 390 // in conflict resolution.
391 ASSERT_EQ(2u, deltas.size()); 391 ASSERT_EQ(2u, deltas.size());
392 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); 392 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder);
393 393
394 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); 394 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin();
(...skipping 27 matching lines...) Expand all
422 error = registry->AddRules(kExtensionId2, rules_to_add_2); 422 error = registry->AddRules(kExtensionId2, rules_to_add_2);
423 EXPECT_EQ("", error); 423 EXPECT_EQ("", error);
424 424
425 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); 425 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1);
426 rules_to_add_3[0] = CreateIgnoreRule(); 426 rules_to_add_3[0] = CreateIgnoreRule();
427 error = registry->AddRules(kExtensionId, rules_to_add_3); 427 error = registry->AddRules(kExtensionId, rules_to_add_3);
428 EXPECT_EQ("", error); 428 EXPECT_EQ("", error);
429 429
430 GURL url("http://www.google.com/index.html"); 430 GURL url("http://www.google.com/index.html");
431 net::TestURLRequestContext context; 431 net::TestURLRequestContext context;
432 net::TestURLRequest request(url, NULL, &context); 432 net::TestURLRequest request(url, NULL, &context, NULL);
433 WebRequestData request_data(&request, ON_BEFORE_REQUEST); 433 WebRequestData request_data(&request, ON_BEFORE_REQUEST);
434 std::list<LinkedPtrEventResponseDelta> deltas = 434 std::list<LinkedPtrEventResponseDelta> deltas =
435 registry->CreateDeltas(NULL, request_data, false); 435 registry->CreateDeltas(NULL, request_data, false);
436 436
437 // The redirect by the first extension is ignored due to the ignore rule. 437 // The redirect by the first extension is ignored due to the ignore rule.
438 ASSERT_EQ(1u, deltas.size()); 438 ASSERT_EQ(1u, deltas.size());
439 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); 439 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin());
440 440
441 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); 441 EXPECT_EQ(kExtensionId2, effective_rule->extension_id);
442 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), 442 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2),
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); 475 rules.push_back(CreateCancellingRule(kRuleId3, attributes));
476 476
477 error = registry->AddRules(kExtensionId, rules); 477 error = registry->AddRules(kExtensionId, rules);
478 EXPECT_EQ("", error); 478 EXPECT_EQ("", error);
479 EXPECT_EQ(1, registry->num_clear_cache_calls()); 479 EXPECT_EQ(1, registry->num_clear_cache_calls());
480 480
481 std::set<const WebRequestRule*> matches; 481 std::set<const WebRequestRule*> matches;
482 482
483 GURL http_url("http://www.example.com"); 483 GURL http_url("http://www.example.com");
484 net::TestURLRequestContext context; 484 net::TestURLRequestContext context;
485 net::TestURLRequest http_request(http_url, NULL, &context); 485 net::TestURLRequest http_request(http_url, NULL, &context, NULL);
486 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); 486 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST);
487 matches = registry->GetMatches(request_data); 487 matches = registry->GetMatches(request_data);
488 EXPECT_EQ(1u, matches.size()); 488 EXPECT_EQ(1u, matches.size());
489 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, 489 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId,
490 kRuleId3); 490 kRuleId3);
491 EXPECT_EQ(expected_pair, (*matches.begin())->id()); 491 EXPECT_EQ(expected_pair, (*matches.begin())->id());
492 } 492 }
493 493
494 // Test that the url and firstPartyForCookiesUrl attributes are evaluated 494 // Test that the url and firstPartyForCookiesUrl attributes are evaluated
495 // against corresponding URLs. Tested on requests where these URLs actually 495 // against corresponding URLs. Tested on requests where these URLs actually
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 // Which rules should match in subsequent test iterations. 531 // Which rules should match in subsequent test iterations.
532 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; 532 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 };
533 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), 533 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls),
534 urls_and_firstPartyUrls_need_to_have_the_same_size); 534 urls_and_firstPartyUrls_need_to_have_the_same_size);
535 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), 535 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds),
536 urls_and_matchingRuleIds_need_to_have_the_same_size); 536 urls_and_matchingRuleIds_need_to_have_the_same_size);
537 net::TestURLRequestContext context; 537 net::TestURLRequestContext context;
538 538
539 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { 539 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) {
540 // Construct the inputs. 540 // Construct the inputs.
541 net::TestURLRequest http_request(urls[i], NULL, &context); 541 net::TestURLRequest http_request(urls[i], NULL, &context, NULL);
542 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); 542 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST);
543 http_request.set_first_party_for_cookies(firstPartyUrls[i]); 543 http_request.set_first_party_for_cookies(firstPartyUrls[i]);
544 // Now run both rules on the input. 544 // Now run both rules on the input.
545 matches = registry->GetMatches(request_data); 545 matches = registry->GetMatches(request_data);
546 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " 546 SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = "
547 << matchingRuleIds[i]); 547 << matchingRuleIds[i]);
548 // Make sure that the right rule succeeded. 548 // Make sure that the right rule succeeded.
549 EXPECT_EQ(1u, matches.size()); 549 EXPECT_EQ(1u, matches.size());
550 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, 550 EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId,
551 matchingRuleIds[i])), 551 matchingRuleIds[i])),
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 new TestWebRequestRulesRegistry()); 587 new TestWebRequestRulesRegistry());
588 588
589 URLMatcher matcher; 589 URLMatcher matcher;
590 std::string error = registry->AddRulesImpl(kExtensionId, rules); 590 std::string error = registry->AddRulesImpl(kExtensionId, rules);
591 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); 591 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle"));
592 EXPECT_TRUE(registry->IsEmpty()); 592 EXPECT_TRUE(registry->IsEmpty());
593 } 593 }
594 594
595 595
596 } // namespace extensions 596 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698