OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/memory/linked_ptr.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/values.h" | |
12 #include "content/test/test_browser_thread.h" | |
13 #include "net/url_request/url_request_test_util.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace { | |
17 const char kExtensionId[] = "ext1"; | |
18 const char kRuleId1[] = "rule1"; | |
19 const char kRuleId2[] = "rule2"; | |
20 | |
21 const char kCancelRequestType[] = "experimental.webRequest.CancelRequest"; | |
22 const char kRequestMatcher[] = "experimental.webRequest.RequestMatcher"; | |
23 const char kInstanceType[] = "instanceType"; | |
24 } | |
25 | |
26 namespace extensions { | |
27 namespace declarative_webrequest { | |
28 | |
29 class WebRequestRulesRegistryTest : public testing::Test { | |
30 public: | |
31 public: | |
32 WebRequestRulesRegistryTest() | |
33 : message_loop(MessageLoop::TYPE_IO), | |
34 ui(content::BrowserThread::UI, &message_loop), | |
35 io(content::BrowserThread::IO, &message_loop) {} | |
36 | |
37 virtual ~WebRequestRulesRegistryTest() {} | |
38 | |
39 virtual void TearDown() OVERRIDE { | |
40 // Make sure that deletion traits of all registries are executed. | |
41 message_loop.RunAllPending(); | |
42 } | |
43 | |
44 // Returns a rule that roughly matches http://*.example.com and | |
45 // https://www.example.com and cancels it | |
46 linked_ptr<RulesRegistry::Rule> CreateRule1() { | |
47 DictionaryValue http_condition_dict; | |
48 http_condition_dict.SetString("scheme", "http"); | |
49 http_condition_dict.SetString("host_suffix", "example.com"); | |
50 http_condition_dict.SetString(kInstanceType, kRequestMatcher); | |
51 | |
52 DictionaryValue https_condition_dict; | |
53 https_condition_dict.SetString("scheme", "https"); | |
54 https_condition_dict.SetString("host_suffix", "example.com"); | |
55 https_condition_dict.SetString("host_prefix", "www"); | |
56 https_condition_dict.SetString(kInstanceType, kRequestMatcher); | |
57 | |
58 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | |
59 new json_schema_compiler::any::Any); | |
60 condition1->Init(http_condition_dict); | |
61 | |
62 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | |
63 new json_schema_compiler::any::Any); | |
64 condition2->Init(https_condition_dict); | |
65 | |
66 DictionaryValue action_dict; | |
67 action_dict.SetString(kInstanceType, kCancelRequestType); | |
68 | |
69 // Test success. | |
Matt Perry
2012/03/22 23:07:25
not sure what this comment means
battre
2012/03/26 18:35:51
Copy and paste...
Done.
| |
70 linked_ptr<json_schema_compiler::any::Any> action1 = make_linked_ptr( | |
71 new json_schema_compiler::any::Any); | |
72 action1->Init(action_dict); | |
73 | |
74 linked_ptr<RulesRegistry::Rule> rule = | |
75 make_linked_ptr(new RulesRegistry::Rule); | |
76 rule->id.reset(new std::string(kRuleId1)); | |
77 rule->priority.reset(new int(100)); | |
78 rule->actions.push_back(action1); | |
79 rule->conditions.push_back(condition1); | |
80 rule->conditions.push_back(condition2); | |
81 return rule; | |
82 } | |
83 | |
84 // Returns a rule that matches anything and cancels it. | |
85 linked_ptr<RulesRegistry::Rule> CreateRule2() { | |
86 DictionaryValue condition_dict; | |
87 condition_dict.SetString(kInstanceType, kRequestMatcher); | |
88 | |
89 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | |
90 new json_schema_compiler::any::Any); | |
91 condition->Init(condition_dict); | |
92 | |
93 DictionaryValue action_dict; | |
94 action_dict.SetString(kInstanceType, kCancelRequestType); | |
95 | |
96 // Test success. | |
97 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | |
98 new json_schema_compiler::any::Any); | |
99 action->Init(action_dict); | |
100 | |
101 linked_ptr<RulesRegistry::Rule> rule = | |
102 make_linked_ptr(new RulesRegistry::Rule); | |
103 rule->id.reset(new std::string(kRuleId2)); | |
104 rule->priority.reset(new int(100)); | |
105 rule->actions.push_back(action); | |
106 rule->conditions.push_back(condition); | |
107 return rule; | |
108 } | |
109 | |
110 protected: | |
111 MessageLoop message_loop; | |
112 content::TestBrowserThread ui; | |
113 content::TestBrowserThread io; | |
114 }; | |
115 | |
116 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | |
117 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); | |
118 std::string error; | |
119 | |
120 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | |
121 rules.push_back(CreateRule1()); | |
122 rules.push_back(CreateRule2()); | |
123 | |
124 error = registry->AddRules(kExtensionId, rules); | |
125 EXPECT_TRUE(error.empty()); | |
126 | |
127 std::set<WebRequestRulesRegistry::GlobalRuleId> matches; | |
128 | |
129 GURL http_url("http://www.example.com"); | |
130 TestURLRequest http_request(http_url, NULL); | |
131 matches = registry->GetMatches(&http_request); | |
132 EXPECT_EQ(2u, matches.size()); | |
133 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != | |
134 matches.end()); | |
135 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | |
136 matches.end()); | |
137 | |
138 GURL foobar_url("http://www.foobar.com"); | |
139 TestURLRequest foobar_request(foobar_url, NULL); | |
140 matches = registry->GetMatches(&foobar_request); | |
141 EXPECT_EQ(1u, matches.size()); | |
142 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | |
143 matches.end()); | |
144 } | |
145 | |
146 } // namespace declarative_webrequest | |
147 } // namespace extensions | |
OLD | NEW |