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

Side by Side Diff: chrome/browser/autofill/autocheckout/whitelist_manager_unittest.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix long lines Created 7 years, 9 months 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
(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/test/base/testing_profile.h"
9 #include "components/autofill/common/autofill_switches.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 size_t kTestDownloadInterval = 3; // 3 seconds
22
23 const char kDownloadWhitelistResponse[] =
24 "https://www.merchant1.com/checkout/\n"
25 "https://cart.merchant2.com/";
26
27 } // namespace
28
29 namespace autofill {
30 namespace autocheckout {
31
32 class WhitelistManagerTest;
33
34 class TestWhitelistManager : public WhitelistManager {
35 public:
36 explicit TestWhitelistManager(net::URLRequestContextGetter* context_getter)
37 : WhitelistManager(context_getter),
38 did_start_download_timer_(false) {}
39
40 virtual void ScheduleDownload(size_t interval_seconds) OVERRIDE {
41 did_start_download_timer_ = false;
42 return WhitelistManager::ScheduleDownload(interval_seconds);
43 }
44
45 virtual void StartDownloadTimer(size_t interval_seconds) OVERRIDE {
46 WhitelistManager::StartDownloadTimer(interval_seconds);
47 did_start_download_timer_ = true;
48 }
49
50 bool did_start_download_timer() const {
51 return did_start_download_timer_;
52 }
53
54 void TriggerDownload() {
55 WhitelistManager::TriggerDownload();
56 }
57
58 void StopDownloadTimer() {
59 WhitelistManager::StopDownloadTimer();
60 }
61
62 const std::vector<std::string>& url_prefixes() const {
63 return WhitelistManager::url_prefixes();
64 }
65
66 private:
67 bool did_start_download_timer_;
68
69 DISALLOW_COPY_AND_ASSIGN(TestWhitelistManager);
70 };
71
72 class WhitelistManagerTest : public testing::Test {
73 public:
74 WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {}
75
76 virtual void SetUp() {
77 io_thread_.StartIOThread();
78 profile_.CreateRequestContext();
79 }
80
81 virtual void TearDown() {
82 profile_.ResetRequestContext();
83 io_thread_.Stop();
84 }
85
86 protected:
87 void CreateWhitelistManager() {
88 if (!whitelist_manager_.get()) {
89 whitelist_manager_.reset(new TestWhitelistManager(
90 profile_.GetRequestContext()));
91 }
92 }
93
94 void DownloadWhitelist(int response_code, const std::string& response) {
95 // Create and register factory.
96 net::TestURLFetcherFactory factory;
97
98 CreateWhitelistManager();
99
100 whitelist_manager_->TriggerDownload();
101 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
102 ASSERT_TRUE(fetcher);
103 fetcher->set_response_code(response_code);
104 fetcher->SetResponseString(response);
105 fetcher->delegate()->OnURLFetchComplete(fetcher);
106 }
107
108 void ResetBackOff() {
109 whitelist_manager_->StopDownloadTimer();
110 }
111
112 const std::vector<std::string>& get_url_prefixes() const {
113 return whitelist_manager_->url_prefixes();
114 }
115
116 protected:
117 TestingProfile profile_;
118 scoped_ptr<TestWhitelistManager> whitelist_manager_;
119
120 private:
121 MessageLoopForIO message_loop_;
122 // The profile's request context must be released on the IO thread.
123 content::TestBrowserThread io_thread_;
124 };
125
126 TEST_F(WhitelistManagerTest, DownloadWhitelist) {
127 CommandLine::ForCurrentProcess()->AppendSwitch(
128 switches::kEnableExperimentalFormFilling);
129 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
130 ASSERT_EQ(2U, get_url_prefixes().size());
131 EXPECT_EQ("https://www.merchant1.com/checkout/",
132 get_url_prefixes()[0]);
133 EXPECT_EQ("https://cart.merchant2.com/",
134 get_url_prefixes()[1]);
135 }
136
137 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenSwitchIsOff) {
138 CreateWhitelistManager();
139 whitelist_manager_->ScheduleDownload(kTestDownloadInterval);
140 EXPECT_FALSE(whitelist_manager_->did_start_download_timer());
141 }
142
143 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenBackOff) {
144 CommandLine::ForCurrentProcess()->AppendSwitch(
145 switches::kEnableExperimentalFormFilling);
146 CreateWhitelistManager();
147 // First attempt should schedule a download.
148 whitelist_manager_->ScheduleDownload(kTestDownloadInterval);
149 EXPECT_TRUE(whitelist_manager_->did_start_download_timer());
150 // Second attempt should NOT schedule a download while there is already one.
151 whitelist_manager_->ScheduleDownload(kTestDownloadInterval);
152 EXPECT_FALSE(whitelist_manager_->did_start_download_timer());
153 // It should schedule a new download when not in backoff mode.
154 ResetBackOff();
155 whitelist_manager_->ScheduleDownload(kTestDownloadInterval);
156 EXPECT_TRUE(whitelist_manager_->did_start_download_timer());
157 }
158
159 TEST_F(WhitelistManagerTest, DownloadWhitelistFailed) {
160 CommandLine::ForCurrentProcess()->AppendSwitch(
161 switches::kEnableExperimentalFormFilling);
162 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR,
163 kDownloadWhitelistResponse);
164 EXPECT_EQ(0U, get_url_prefixes().size());
165
166 ResetBackOff();
167 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
168 EXPECT_EQ(2U, get_url_prefixes().size());
169
170 ResetBackOff();
171 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR,
172 kDownloadWhitelistResponse);
173 EXPECT_EQ(2U, get_url_prefixes().size());
174 }
175
176 TEST_F(WhitelistManagerTest, GetMatchedURLPrefix) {
177 CommandLine::ForCurrentProcess()->AppendSwitch(
178 switches::kEnableExperimentalFormFilling);
179 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
180 EXPECT_EQ(2U, get_url_prefixes().size());
181
182 // Empty url.
183 EXPECT_EQ(std::string(),
184 whitelist_manager_->GetMatchedURLPrefix(GURL(std::string())));
185 EXPECT_EQ(std::string(),
186 whitelist_manager_->GetMatchedURLPrefix(GURL()));
187
188 // Positive tests.
189 EXPECT_EQ("https://www.merchant1.com/checkout/",
190 whitelist_manager_->GetMatchedURLPrefix(
191 GURL("https://www.merchant1.com/checkout/")));
192 EXPECT_EQ("https://www.merchant1.com/checkout/",
193 whitelist_manager_->GetMatchedURLPrefix(
194 GURL("https://www.merchant1.com/checkout/Shipping")));
195 EXPECT_EQ("https://www.merchant1.com/checkout/",
196 whitelist_manager_->GetMatchedURLPrefix(
197 GURL("https://www.merchant1.com/checkout/?a=b&c=d")));
198 EXPECT_EQ("https://cart.merchant2.com/",
199 whitelist_manager_->GetMatchedURLPrefix(
200 GURL("https://cart.merchant2.com/")));
201 EXPECT_EQ("https://cart.merchant2.com/",
202 whitelist_manager_->GetMatchedURLPrefix(
203 GURL("https://cart.merchant2.com/ShippingInfo")));
204 EXPECT_EQ("https://cart.merchant2.com/",
205 whitelist_manager_->GetMatchedURLPrefix(
206 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d")));
207
208 // Negative tests.
209 EXPECT_EQ(std::string(),
210 whitelist_manager_->GetMatchedURLPrefix(
211 GURL("https://www.merchant1.com/checkout")));
212 EXPECT_EQ(std::string(),
213 whitelist_manager_->GetMatchedURLPrefix(
214 GURL("https://www.merchant1.com/")));
215 EXPECT_EQ(std::string(),
216 whitelist_manager_->GetMatchedURLPrefix(
217 GURL("https://www.merchant1.com/Building")));
218 EXPECT_EQ(std::string(),
219 whitelist_manager_->GetMatchedURLPrefix(
220 GURL("https://www.merchant2.com/cart")));
221 EXPECT_EQ(std::string(),
222 whitelist_manager_->GetMatchedURLPrefix(
223 GURL("a random string")));
224
225 // Test different cases in schema, host and path.
226 EXPECT_EQ(std::string(),
227 whitelist_manager_->GetMatchedURLPrefix(
228 GURL("http://www.merchant1.com/checkout/")));
229 EXPECT_EQ(std::string(),
230 whitelist_manager_->GetMatchedURLPrefix(
231 GURL("www.merchant1.com/checkout/")));
232 EXPECT_EQ("https://www.merchant1.com/checkout/",
233 whitelist_manager_->GetMatchedURLPrefix(
234 GURL("https://www.Merchant1.com/checkout/")));
235 EXPECT_EQ(std::string(),
236 whitelist_manager_->GetMatchedURLPrefix(
237 GURL("https://www.merchant1.com/CheckOut/")));
238 }
239
240 } // namespace autocheckout
241 } // namespace autofill
242
OLDNEW
« no previous file with comments | « chrome/browser/autofill/autocheckout/whitelist_manager.cc ('k') | chrome/browser/autofill/autocheckout_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698