OLD | NEW |
---|---|
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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 12 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | |
14 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
16 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 const char kExtensionId[] = "ext1"; | 21 const char kExtensionId[] = "ext1"; |
21 const char kExtensionId2[] = "ext2"; | 22 const char kExtensionId2[] = "ext2"; |
22 const char kRuleId1[] = "rule1"; | 23 const char kRuleId1[] = "rule1"; |
23 const char kRuleId2[] = "rule2"; | 24 const char kRuleId2[] = "rule2"; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 rules.push_back(CreateRule2()); | 209 rules.push_back(CreateRule2()); |
209 | 210 |
210 error = registry->AddRules(kExtensionId, rules); | 211 error = registry->AddRules(kExtensionId, rules); |
211 EXPECT_EQ("", error); | 212 EXPECT_EQ("", error); |
212 | 213 |
213 std::set<WebRequestRule::GlobalRuleId> matches; | 214 std::set<WebRequestRule::GlobalRuleId> matches; |
214 | 215 |
215 GURL http_url("http://www.example.com"); | 216 GURL http_url("http://www.example.com"); |
216 TestURLRequestContext context; | 217 TestURLRequestContext context; |
217 TestURLRequest http_request(http_url, NULL, &context); | 218 TestURLRequest http_request(http_url, NULL, &context); |
218 matches = registry->GetMatches(&http_request, ON_BEFORE_REQUEST); | 219 matches = registry->GetMatches( |
220 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | |
219 EXPECT_EQ(2u, matches.size()); | 221 EXPECT_EQ(2u, matches.size()); |
220 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != | 222 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != |
221 matches.end()); | 223 matches.end()); |
222 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 224 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != |
223 matches.end()); | 225 matches.end()); |
224 | 226 |
225 GURL foobar_url("http://www.foobar.com"); | 227 GURL foobar_url("http://www.foobar.com"); |
226 TestURLRequest foobar_request(foobar_url, NULL, &context); | 228 TestURLRequest foobar_request(foobar_url, NULL, &context); |
227 matches = registry->GetMatches(&foobar_request, ON_BEFORE_REQUEST); | 229 matches = registry->GetMatches( |
230 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); | |
228 EXPECT_EQ(1u, matches.size()); | 231 EXPECT_EQ(1u, matches.size()); |
229 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 232 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != |
230 matches.end()); | 233 matches.end()); |
231 } | 234 } |
232 | 235 |
233 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 236 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
234 scoped_refptr<WebRequestRulesRegistry> registry( | 237 scoped_refptr<WebRequestRulesRegistry> registry( |
235 new TestWebRequestRulesRegistry()); | 238 new TestWebRequestRulesRegistry()); |
236 std::string error; | 239 std::string error; |
237 | 240 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 | 334 |
332 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 335 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
333 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 336 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
334 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 337 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
335 EXPECT_EQ("", error); | 338 EXPECT_EQ("", error); |
336 | 339 |
337 GURL url("http://www.google.com"); | 340 GURL url("http://www.google.com"); |
338 TestURLRequestContext context; | 341 TestURLRequestContext context; |
339 TestURLRequest request(url, NULL, &context); | 342 TestURLRequest request(url, NULL, &context); |
340 std::list<LinkedPtrEventResponseDelta> deltas = | 343 std::list<LinkedPtrEventResponseDelta> deltas = |
341 registry->CreateDeltas(NULL, &request, false, ON_BEFORE_REQUEST, | 344 registry->CreateDeltas( |
342 WebRequestRule::OptionalRequestData()); | 345 NULL, |
346 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST, NULL), | |
battre
2012/08/03 10:06:43
nit: no need to pass NULL here.
Yoyo Zhou
2012/08/03 10:12:19
Oh right, this was before I realized there were en
| |
347 false); | |
343 | 348 |
344 // The second extension is installed later and will win for this reason | 349 // The second extension is installed later and will win for this reason |
345 // in conflict resolution. | 350 // in conflict resolution. |
346 ASSERT_EQ(2u, deltas.size()); | 351 ASSERT_EQ(2u, deltas.size()); |
347 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 352 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
348 | 353 |
349 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 354 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
350 LinkedPtrEventResponseDelta winner = *i++; | 355 LinkedPtrEventResponseDelta winner = *i++; |
351 LinkedPtrEventResponseDelta loser = *i; | 356 LinkedPtrEventResponseDelta loser = *i; |
352 | 357 |
(...skipping 26 matching lines...) Expand all Loading... | |
379 | 384 |
380 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 385 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
381 rules_to_add_3[0] = CreateIgnoreRule(); | 386 rules_to_add_3[0] = CreateIgnoreRule(); |
382 error = registry->AddRules(kExtensionId, rules_to_add_3); | 387 error = registry->AddRules(kExtensionId, rules_to_add_3); |
383 EXPECT_EQ("", error); | 388 EXPECT_EQ("", error); |
384 | 389 |
385 GURL url("http://www.google.com/index.html"); | 390 GURL url("http://www.google.com/index.html"); |
386 TestURLRequestContext context; | 391 TestURLRequestContext context; |
387 TestURLRequest request(url, NULL, &context); | 392 TestURLRequest request(url, NULL, &context); |
388 std::list<LinkedPtrEventResponseDelta> deltas = | 393 std::list<LinkedPtrEventResponseDelta> deltas = |
389 registry->CreateDeltas(NULL, &request, false, ON_BEFORE_REQUEST, | 394 registry->CreateDeltas( |
390 WebRequestRule::OptionalRequestData()); | 395 NULL, |
396 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST, NULL), | |
battre
2012/08/03 10:06:43
nit: no need to pass NULL here.
| |
397 false); | |
391 | 398 |
392 // The redirect by the first extension is ignored due to the ignore rule. | 399 // The redirect by the first extension is ignored due to the ignore rule. |
393 ASSERT_EQ(1u, deltas.size()); | 400 ASSERT_EQ(1u, deltas.size()); |
394 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 401 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
395 | 402 |
396 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 403 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
397 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 404 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
398 effective_rule->extension_install_time); | 405 effective_rule->extension_install_time); |
399 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 406 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
400 } | 407 } |
401 } // namespace extensions | 408 } // namespace extensions |
OLD | NEW |