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

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

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

Powered by Google App Engine
This is Rietveld 408576698