OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "base/command_line.h" | |
6 #include "base/memory/scoped_ptr.h" | |
7 #include "chrome/browser/autofill/autocheckout/whitelist_manager.h" | |
8 #include "chrome/common/chrome_switches.h" | |
9 #include "chrome/test/base/testing_profile.h" | |
10 #include "content/public/test/test_browser_thread.h" | |
11 #include "googleurl/src/gurl.h" | |
12 #include "net/base/net_errors.h" | |
13 #include "net/http/http_status_code.h" | |
14 #include "net/url_request/test_url_fetcher_factory.h" | |
15 #include "net/url_request/url_fetcher_delegate.h" | |
16 #include "net/url_request/url_request_status.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace { | |
20 | |
21 const char kDownloadWhitelistResponse[] = | |
22 "https://www.merchant1.com/checkout/\n" | |
23 "https://cart.merchant2.com/"; | |
24 | |
25 } // namespace | |
26 | |
27 namespace autofill { | |
28 namespace autocheckout { | |
29 | |
30 class WhitelistManagerTest; | |
31 | |
32 class TestWhitelistManager : public WhitelistManager { | |
33 public: | |
34 explicit TestWhitelistManager(net::URLRequestContextGetter* context_getter) | |
35 : WhitelistManager(context_getter), | |
36 did_start_download_timer_(false) {} | |
37 | |
38 void ScheduleDownload(size_t interval_seconds) OVERRIDE { | |
39 did_start_download_timer_ = false; | |
40 return WhitelistManager::ScheduleDownload(interval_seconds); | |
41 } | |
42 | |
43 void StartDownloadTimer(size_t interval_seconds) OVERRIDE { | |
44 WhitelistManager::StartDownloadTimer(interval_seconds); | |
45 did_start_download_timer_ = true; | |
46 } | |
47 | |
48 bool did_start_download_timer() const { | |
49 return did_start_download_timer_; | |
50 } | |
51 | |
52 void TriggerDownload() { | |
53 WhitelistManager::TriggerDownload(); | |
54 } | |
55 | |
56 void StopDownloadTimer() { | |
57 WhitelistManager::StopDownloadTimer(); | |
58 } | |
59 | |
60 void set_callback_is_pending(bool callback_is_pending) { | |
61 WhitelistManager::set_callback_is_pending(callback_is_pending); | |
62 } | |
63 | |
64 const std::vector<std::string>& url_prefixes() const { | |
65 return WhitelistManager::url_prefixes(); | |
66 } | |
67 | |
68 private: | |
69 bool did_start_download_timer_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(TestWhitelistManager); | |
72 }; | |
73 | |
74 class WhitelistManagerTest : public testing::Test { | |
75 public: | |
76 WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {} | |
77 | |
78 virtual void SetUp() { | |
79 io_thread_.StartIOThread(); | |
80 profile_.CreateRequestContext(); | |
81 } | |
82 | |
83 virtual void TearDown() { | |
84 whitelist_manager_.reset(); | |
Ilya Sherman
2013/01/31 05:01:29
nit: This doesn't seem like it ought to be needed.
benquan
2013/01/31 23:17:08
Done.
| |
85 profile_.ResetRequestContext(); | |
86 io_thread_.Stop(); | |
87 } | |
88 | |
89 protected: | |
90 void EnsureCreateWhitelistManager() { | |
Ilya Sherman
2013/01/31 05:01:29
nit: Omit "Create"
benquan
2013/01/31 23:17:08
Done.
| |
91 if (!whitelist_manager_.get()) | |
92 whitelist_manager_.reset(new TestWhitelistManager( | |
93 profile_.GetRequestContext())); | |
Ilya Sherman
2013/01/31 05:01:29
nit: Please add curly braces, since the body of th
benquan
2013/01/31 23:17:08
Done.
| |
94 } | |
95 | |
96 void DownloadWhitelist(int response_code, const std::string& response) { | |
97 // Create and register factory. | |
98 net::TestURLFetcherFactory factory; | |
99 | |
100 EnsureCreateWhitelistManager(); | |
101 | |
102 whitelist_manager_->TriggerDownload(); | |
103 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
104 ASSERT_TRUE(fetcher); | |
105 fetcher->set_response_code(response_code); | |
106 fetcher->SetResponseString(response); | |
107 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
108 } | |
109 | |
110 void ResetBackOff() { | |
111 whitelist_manager_->StopDownloadTimer(); | |
112 whitelist_manager_->set_callback_is_pending(false); | |
113 } | |
114 | |
115 const std::vector<std::string>& GetUrlPrefixes() const { | |
Ilya Sherman
2013/01/31 05:01:29
nit: Name this method get_url_prefixes()
benquan
2013/01/31 23:17:08
Done.
| |
116 return whitelist_manager_->url_prefixes(); | |
117 } | |
118 | |
119 protected: | |
120 TestingProfile profile_; | |
121 scoped_ptr<TestWhitelistManager> whitelist_manager_; | |
122 | |
123 private: | |
124 MessageLoopForIO message_loop_; | |
125 // The profile's request context must be released on the IO thread. | |
126 content::TestBrowserThread io_thread_; | |
127 }; | |
128 | |
129 TEST_F(WhitelistManagerTest, DownloadWhitelist) { | |
130 CommandLine::ForCurrentProcess()->AppendSwitch( | |
131 switches::kEnableExperimentalFormFilling); | |
132 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse); | |
133 ASSERT_EQ(2U, GetUrlPrefixes().size()); | |
134 EXPECT_EQ("https://www.merchant1.com/checkout/", | |
135 GetUrlPrefixes()[0]); | |
136 EXPECT_EQ("https://cart.merchant2.com/", | |
137 GetUrlPrefixes()[1]); | |
138 } | |
139 | |
140 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenSwitchIsOff) { | |
141 EnsureCreateWhitelistManager(); | |
142 whitelist_manager_->ScheduleDownload(3); | |
Ilya Sherman
2013/01/31 05:01:29
nit: Please define a named constant for "3".
benquan
2013/01/31 23:17:08
Done.
| |
143 EXPECT_FALSE(whitelist_manager_->did_start_download_timer()); | |
144 } | |
145 | |
146 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenBackOff) { | |
147 CommandLine::ForCurrentProcess()->AppendSwitch( | |
148 switches::kEnableExperimentalFormFilling); | |
149 EnsureCreateWhitelistManager(); | |
150 // First attempt should schedule a download. | |
151 whitelist_manager_->ScheduleDownload(3); | |
152 EXPECT_TRUE(whitelist_manager_->did_start_download_timer()); | |
153 // Second attempt should NOT schedule a download while there is already one. | |
154 whitelist_manager_->ScheduleDownload(3); | |
155 EXPECT_FALSE(whitelist_manager_->did_start_download_timer()); | |
156 // It should schedule a new download when not in backoff mode. | |
157 ResetBackOff(); | |
158 whitelist_manager_->ScheduleDownload(3); | |
159 EXPECT_TRUE(whitelist_manager_->did_start_download_timer()); | |
160 } | |
161 | |
162 TEST_F(WhitelistManagerTest, DownloadWhitelistFailed) { | |
163 CommandLine::ForCurrentProcess()->AppendSwitch( | |
164 switches::kEnableExperimentalFormFilling); | |
165 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, | |
166 kDownloadWhitelistResponse); | |
167 EXPECT_EQ(0U, GetUrlPrefixes().size()); | |
168 | |
169 ResetBackOff(); | |
170 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse); | |
171 EXPECT_EQ(2U, GetUrlPrefixes().size()); | |
172 | |
173 ResetBackOff(); | |
174 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, | |
175 kDownloadWhitelistResponse); | |
176 EXPECT_EQ(2U, GetUrlPrefixes().size()); | |
177 } | |
178 | |
179 TEST_F(WhitelistManagerTest, GetMatchedURLPrefix) { | |
180 CommandLine::ForCurrentProcess()->AppendSwitch( | |
181 switches::kEnableExperimentalFormFilling); | |
182 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse); | |
183 EXPECT_EQ(2U, GetUrlPrefixes().size()); | |
184 | |
185 // Empty url. | |
186 EXPECT_EQ(std::string(), | |
187 whitelist_manager_->GetMatchedURLPrefix(GURL(std::string()))); | |
188 EXPECT_EQ(std::string(), | |
189 whitelist_manager_->GetMatchedURLPrefix(GURL())); | |
190 | |
191 // Positive tests. | |
192 EXPECT_EQ("https://www.merchant1.com/checkout/", | |
193 whitelist_manager_->GetMatchedURLPrefix( | |
194 GURL("https://www.merchant1.com/checkout/"))); | |
195 EXPECT_EQ("https://www.merchant1.com/checkout/", | |
196 whitelist_manager_->GetMatchedURLPrefix( | |
197 GURL("https://www.merchant1.com/checkout/Shipping"))); | |
198 EXPECT_EQ("https://www.merchant1.com/checkout/", | |
199 whitelist_manager_->GetMatchedURLPrefix( | |
200 GURL("https://www.merchant1.com/checkout/?a=b&c=d"))); | |
201 EXPECT_EQ("https://cart.merchant2.com/", | |
202 whitelist_manager_->GetMatchedURLPrefix( | |
203 GURL("https://cart.merchant2.com/"))); | |
204 EXPECT_EQ("https://cart.merchant2.com/", | |
205 whitelist_manager_->GetMatchedURLPrefix( | |
206 GURL("https://cart.merchant2.com/ShippingInfo"))); | |
207 EXPECT_EQ("https://cart.merchant2.com/", | |
208 whitelist_manager_->GetMatchedURLPrefix( | |
209 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d"))); | |
210 | |
211 // Negative tests. | |
212 EXPECT_EQ(std::string(), | |
213 whitelist_manager_->GetMatchedURLPrefix( | |
214 GURL("https://www.merchant1.com/checkout"))); | |
215 EXPECT_EQ(std::string(), | |
216 whitelist_manager_->GetMatchedURLPrefix( | |
217 GURL("https://www.merchant1.com/"))); | |
218 EXPECT_EQ(std::string(), | |
219 whitelist_manager_->GetMatchedURLPrefix( | |
220 GURL("https://www.merchant1.com/Building"))); | |
221 EXPECT_EQ(std::string(), | |
222 whitelist_manager_->GetMatchedURLPrefix( | |
223 GURL("https://www.merchant2.com/cart"))); | |
224 EXPECT_EQ(std::string(), | |
225 whitelist_manager_->GetMatchedURLPrefix( | |
226 GURL("a random string"))); | |
227 | |
228 // Test different cases in schema, host and path. | |
229 EXPECT_EQ(std::string(), | |
230 whitelist_manager_->GetMatchedURLPrefix( | |
231 GURL("http://www.Merchant1.com/checkout/"))); | |
Ilya Sherman
2013/01/31 05:01:29
nit: Please use lowercase Merchant here, since wha
benquan
2013/01/31 23:17:08
Done
| |
232 EXPECT_EQ(std::string(), | |
233 whitelist_manager_->GetMatchedURLPrefix( | |
234 GURL("www.Merchant1.com/checkout/"))); | |
Ilya Sherman
2013/01/31 05:01:29
nit: Ditto.
benquan
2013/01/31 23:17:08
Ditto.
| |
235 EXPECT_EQ("https://www.merchant1.com/checkout/", | |
236 whitelist_manager_->GetMatchedURLPrefix( | |
237 GURL("https://www.Merchant1.com/checkout/"))); | |
238 EXPECT_EQ(std::string(), | |
239 whitelist_manager_->GetMatchedURLPrefix( | |
240 GURL("https://www.merchant1.com/CheckOut/"))); | |
241 } | |
242 | |
243 } // namespace autocheckout | |
244 } // namespace autofill | |
245 | |
OLD | NEW |