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" |
(...skipping 24 matching lines...) Expand all Loading... | |
35 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { | 35 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { |
36 public: | 36 public: |
37 TestWebRequestRulesRegistry() | 37 TestWebRequestRulesRegistry() |
38 : WebRequestRulesRegistry(NULL, NULL), | 38 : WebRequestRulesRegistry(NULL, NULL), |
39 num_clear_cache_calls_(0) {} | 39 num_clear_cache_calls_(0) {} |
40 | 40 |
41 // Returns how often the in-memory caches of the renderers were instructed | 41 // Returns how often the in-memory caches of the renderers were instructed |
42 // to be cleared. | 42 // to be cleared. |
43 int num_clear_cache_calls() const { return num_clear_cache_calls_; } | 43 int num_clear_cache_calls() const { return num_clear_cache_calls_; } |
44 | 44 |
45 // How many rules are there which have some conditions not triggered by URL | |
46 // matches. | |
47 size_t RulesWithoutTriggers() const { | |
48 return rules_with_untriggered_conditions_for_test().size(); | |
49 } | |
50 | |
45 protected: | 51 protected: |
46 virtual ~TestWebRequestRulesRegistry() {} | 52 virtual ~TestWebRequestRulesRegistry() {} |
47 | 53 |
48 virtual base::Time GetExtensionInstallationTime( | 54 virtual base::Time GetExtensionInstallationTime( |
49 const std::string& extension_id) const { | 55 const std::string& extension_id) const { |
50 if (extension_id == kExtensionId) | 56 if (extension_id == kExtensionId) |
51 return base::Time() + base::TimeDelta::FromDays(1); | 57 return base::Time() + base::TimeDelta::FromDays(1); |
52 else if (extension_id == kExtensionId2) | 58 else if (extension_id == kExtensionId2) |
53 return base::Time() + base::TimeDelta::FromDays(2); | 59 return base::Time() + base::TimeDelta::FromDays(2); |
54 else | 60 else |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 std::string error; | 223 std::string error; |
218 | 224 |
219 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 225 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
220 rules.push_back(CreateRule1()); | 226 rules.push_back(CreateRule1()); |
221 rules.push_back(CreateRule2()); | 227 rules.push_back(CreateRule2()); |
222 | 228 |
223 error = registry->AddRules(kExtensionId, rules); | 229 error = registry->AddRules(kExtensionId, rules); |
224 EXPECT_EQ("", error); | 230 EXPECT_EQ("", error); |
225 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 231 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
226 | 232 |
227 std::set<WebRequestRule::GlobalRuleId> matches; | 233 std::set<const WebRequestRule*> matches; |
228 | 234 |
229 GURL http_url("http://www.example.com"); | 235 GURL http_url("http://www.example.com"); |
230 net::TestURLRequestContext context; | 236 net::TestURLRequestContext context; |
231 net::TestURLRequest http_request(http_url, NULL, &context); | 237 net::TestURLRequest http_request(http_url, NULL, &context); |
232 matches = registry->GetMatches( | 238 matches = registry->GetMatches( |
233 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); | 239 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); |
234 EXPECT_EQ(2u, matches.size()); | 240 EXPECT_EQ(2u, matches.size()); |
235 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != | 241 |
236 matches.end()); | 242 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
237 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 243 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
238 matches.end()); | 244 it != matches.end(); ++it) |
245 matches_ids.insert((*it)->id()); | |
246 EXPECT_TRUE(matches_ids.find(std::make_pair(kExtensionId, kRuleId1)) != | |
Jeffrey Yasskin
2012/12/18 21:56:02
Use ContainsKey for this?
vabr (Chromium)
2012/12/19 08:38:19
Done.
| |
247 matches_ids.end()); | |
248 EXPECT_TRUE(matches_ids.find(std::make_pair(kExtensionId, kRuleId2)) != | |
249 matches_ids.end()); | |
239 | 250 |
240 GURL foobar_url("http://www.foobar.com"); | 251 GURL foobar_url("http://www.foobar.com"); |
241 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 252 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
242 matches = registry->GetMatches( | 253 matches = registry->GetMatches( |
243 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); | 254 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); |
244 EXPECT_EQ(1u, matches.size()); | 255 EXPECT_EQ(1u, matches.size()); |
245 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 256 WebRequestRule::GlobalRuleId expected_pair = |
246 matches.end()); | 257 std::make_pair(kExtensionId, kRuleId2); |
258 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | |
247 } | 259 } |
248 | 260 |
249 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 261 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
250 scoped_refptr<TestWebRequestRulesRegistry> registry( | 262 scoped_refptr<TestWebRequestRulesRegistry> registry( |
251 new TestWebRequestRulesRegistry()); | 263 new TestWebRequestRulesRegistry()); |
252 std::string error; | 264 std::string error; |
253 | 265 |
254 // Setup RulesRegistry to contain two rules. | 266 // Setup RulesRegistry to contain two rules. |
255 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; | 267 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; |
256 rules_to_add.push_back(CreateRule1()); | 268 rules_to_add.push_back(CreateRule1()); |
257 rules_to_add.push_back(CreateRule2()); | 269 rules_to_add.push_back(CreateRule2()); |
258 error = registry->AddRules(kExtensionId, rules_to_add); | 270 error = registry->AddRules(kExtensionId, rules_to_add); |
259 EXPECT_EQ("", error); | 271 EXPECT_EQ("", error); |
260 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 272 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
261 | 273 |
262 // Verify initial state. | 274 // Verify initial state. |
263 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 275 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
264 registry->GetAllRules(kExtensionId, ®istered_rules); | 276 registry->GetAllRules(kExtensionId, ®istered_rules); |
265 EXPECT_EQ(2u, registered_rules.size()); | 277 EXPECT_EQ(2u, registered_rules.size()); |
278 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | |
266 | 279 |
267 // Remove first rule. | 280 // Remove first rule. |
268 std::vector<std::string> rules_to_remove; | 281 std::vector<std::string> rules_to_remove; |
269 rules_to_remove.push_back(kRuleId1); | 282 rules_to_remove.push_back(kRuleId1); |
270 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 283 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
271 EXPECT_EQ("", error); | 284 EXPECT_EQ("", error); |
272 EXPECT_EQ(2, registry->num_clear_cache_calls()); | 285 EXPECT_EQ(2, registry->num_clear_cache_calls()); |
273 | 286 |
274 // Verify that only one rule is left. | 287 // Verify that only one rule is left. |
275 registered_rules.clear(); | 288 registered_rules.clear(); |
276 registry->GetAllRules(kExtensionId, ®istered_rules); | 289 registry->GetAllRules(kExtensionId, ®istered_rules); |
277 EXPECT_EQ(1u, registered_rules.size()); | 290 EXPECT_EQ(1u, registered_rules.size()); |
291 EXPECT_EQ(1u, registry->RulesWithoutTriggers()); | |
278 | 292 |
279 // Now rules_to_remove contains both rules, i.e. one that does not exist in | 293 // Now rules_to_remove contains both rules, i.e. one that does not exist in |
280 // the rules registry anymore. Effectively we only remove the second rule. | 294 // the rules registry anymore. Effectively we only remove the second rule. |
281 rules_to_remove.push_back(kRuleId2); | 295 rules_to_remove.push_back(kRuleId2); |
282 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 296 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
283 EXPECT_EQ("", error); | 297 EXPECT_EQ("", error); |
284 EXPECT_EQ(3, registry->num_clear_cache_calls()); | 298 EXPECT_EQ(3, registry->num_clear_cache_calls()); |
285 | 299 |
286 // Verify that everything is gone. | 300 // Verify that everything is gone. |
287 registered_rules.clear(); | 301 registered_rules.clear(); |
288 registry->GetAllRules(kExtensionId, ®istered_rules); | 302 registry->GetAllRules(kExtensionId, ®istered_rules); |
289 EXPECT_EQ(0u, registered_rules.size()); | 303 EXPECT_EQ(0u, registered_rules.size()); |
304 EXPECT_EQ(0u, registry->RulesWithoutTriggers()); | |
290 | 305 |
291 EXPECT_TRUE(registry->IsEmpty()); | 306 EXPECT_TRUE(registry->IsEmpty()); |
292 } | 307 } |
293 | 308 |
294 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { | 309 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { |
295 scoped_refptr<TestWebRequestRulesRegistry> registry( | 310 scoped_refptr<TestWebRequestRulesRegistry> registry( |
296 new TestWebRequestRulesRegistry()); | 311 new TestWebRequestRulesRegistry()); |
297 std::string error; | 312 std::string error; |
298 | 313 |
299 // Setup RulesRegistry to contain two rules, one for each extension. | 314 // Setup RulesRegistry to contain two rules, one for each extension. |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 // The redirect by the first extension is ignored due to the ignore rule. | 435 // The redirect by the first extension is ignored due to the ignore rule. |
421 ASSERT_EQ(1u, deltas.size()); | 436 ASSERT_EQ(1u, deltas.size()); |
422 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 437 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
423 | 438 |
424 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 439 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
425 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 440 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
426 effective_rule->extension_install_time); | 441 effective_rule->extension_install_time); |
427 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 442 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
428 } | 443 } |
429 } // namespace extensions | 444 } // namespace extensions |
OLD | NEW |