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

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

Issue 11867025: Download autocheckout whitelist and enable autocheckout for whitelisted sites only. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix memory leak in AutofillMetricsTest. Created 7 years, 10 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/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), called_timer_start_(false) {}
Ilya Sherman 2013/01/28 23:21:39 nit: Format this as: explicit TestWhitelistMana
benquan 2013/01/29 03:28:30 Done.
36
37 void ScheduleDownload(size_t interval_seconds) OVERRIDE {
38 called_timer_start_ = false;
39 return WhitelistManager::ScheduleDownload(interval_seconds);
40 }
41
42 void StartDownloadTimer(size_t interval_seconds) OVERRIDE {
43 WhitelistManager::StartDownloadTimer(interval_seconds);
44 called_timer_start_ = true;
45 }
46
47 bool CalledTimerStart() const {
Ilya Sherman 2013/01/28 23:21:39 nit: Name this method called_timer_start(), since
benquan 2013/01/29 03:28:30 Done.
48 return called_timer_start_;
49 }
Ilya Sherman 2013/01/28 23:21:39 nit: Please leave a blank line after this one.
benquan 2013/01/29 03:28:30 Done.
50 private:
51 bool called_timer_start_;
Ilya Sherman 2013/01/28 23:21:39 Optional nit: Perhaps name this "did_start_downloa
benquan 2013/01/29 03:28:30 Done.
52
53 friend class WhitelistManagerTest;
Ilya Sherman 2013/01/28 23:21:39 nit: Don't use a friend declaration here.
benquan 2013/01/29 03:28:30 Done.
54 DISALLOW_COPY_AND_ASSIGN(TestWhitelistManager);
55 };
56
57 class WhitelistManagerTest : public testing::Test {
58 public:
59 WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {}
60
61 virtual void SetUp() {
62 io_thread_.StartIOThread();
63 profile_.CreateRequestContext();
64 }
65
66 virtual void TearDown() {
67 whitelist_manager_ = NULL;
68 profile_.ResetRequestContext();
69 io_thread_.Stop();
70 }
71
72 protected:
73 void EnsureCreateWhitelistManager() {
74 if (!whitelist_manager_.get())
75 whitelist_manager_ = new TestWhitelistManager(
76 profile_.GetRequestContext());
77 }
78
79 void DownloadWhitelist(int response_code, const std::string& response) {
80 // Create and register factory.
81 net::TestURLFetcherFactory factory;
82
83 EnsureCreateWhitelistManager();
84
85 whitelist_manager_->TriggerDownload();
86 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
87 ASSERT_TRUE(fetcher);
88 fetcher->set_response_code(response_code);
89 fetcher->SetResponseString(response);
90 fetcher->delegate()->OnURLFetchComplete(fetcher);
91 }
92
93 void ResetBackOff() {
94 whitelist_manager_->download_timer().Stop();
95 whitelist_manager_->set_callback_is_pending(false);
96 }
97
98 const std::vector<std::string>& GetUrlPrefixes() const {
99 return whitelist_manager_->url_prefixes();
100 }
101
102 protected:
103 TestingProfile profile_;
104 scoped_refptr<TestWhitelistManager> whitelist_manager_;
105
106 private:
107 MessageLoopForIO message_loop_;
108 // The profile's request context must be released on the IO thread.
109 content::TestBrowserThread io_thread_;
110 };
111
112 TEST_F(WhitelistManagerTest, DownloadWhitelist) {
113 CommandLine::ForCurrentProcess()->AppendSwitch(
114 switches::kEnableExperimentalFormFilling);
115 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
116 ASSERT_EQ(2U, GetUrlPrefixes().size());
117 EXPECT_EQ("https://www.merchant1.com/checkout/",
118 GetUrlPrefixes()[0]);
119 EXPECT_EQ("https://cart.merchant2.com/",
120 GetUrlPrefixes()[1]);
121 }
122
123 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenSwitchIsOff) {
124 EnsureCreateWhitelistManager();
125 whitelist_manager_->ScheduleDownload(3);
126 EXPECT_FALSE(whitelist_manager_->CalledTimerStart());
127 }
128
129 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenBackOff) {
130 CommandLine::ForCurrentProcess()->AppendSwitch(
131 switches::kEnableExperimentalFormFilling);
132 EnsureCreateWhitelistManager();
133 // First attempt should schedule a download.
134 whitelist_manager_->ScheduleDownload(3);
135 EXPECT_TRUE(whitelist_manager_->CalledTimerStart());
136 // Second attempt should NOT schedule a download while there is already one.
137 whitelist_manager_->ScheduleDownload(3);
138 EXPECT_FALSE(whitelist_manager_->CalledTimerStart());
139 // It should schedule a new download when not in backoff mode.
140 ResetBackOff();
141 whitelist_manager_->ScheduleDownload(3);
142 EXPECT_TRUE(whitelist_manager_->CalledTimerStart());
143 }
144
145 TEST_F(WhitelistManagerTest, DownloadWhitelistFailed) {
146 CommandLine::ForCurrentProcess()->AppendSwitch(
147 switches::kEnableExperimentalFormFilling);
148 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR,
149 kDownloadWhitelistResponse);
150 EXPECT_EQ(0U, GetUrlPrefixes().size());
151
152 ResetBackOff();
153 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
154 EXPECT_EQ(2U, GetUrlPrefixes().size());
155
156 ResetBackOff();
157 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR,
158 kDownloadWhitelistResponse);
159 EXPECT_EQ(2U, GetUrlPrefixes().size());
160 }
161
162 TEST_F(WhitelistManagerTest, IsAutocheckoutEnabled) {
163 CommandLine::ForCurrentProcess()->AppendSwitch(
164 switches::kEnableExperimentalFormFilling);
165 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
166 EXPECT_EQ(2U, GetUrlPrefixes().size());
167
168 // Empty url.
169 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL(std::string())));
170 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL()));
171
172 // Positive tests.
173 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
174 GURL("https://www.merchant1.com/checkout/")));
175 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
176 GURL("https://www.merchant1.com/checkout/Shipping")));
177 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
178 GURL("https://www.merchant1.com/checkout/?a=b&c=d")));
179 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
180 GURL("https://cart.merchant2.com/")));
181 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
182 GURL("https://cart.merchant2.com/ShippingInfo")));
183 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
184 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d")));
185
186 // Negative tests.
187 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
188 GURL("https://www.merchant1.com/checkout")));
189 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
190 GURL("https://www.merchant1.com/")));
191 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
192 GURL("https://www.merchant1.com/Building")));
193 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
194 GURL("https://www.merchant2.com/cart")));
195 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
196 GURL("a random string")));
197
198 // Test different cases in schema, host and path.
199 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
200 GURL("http://www.Merchant1.com/checkout/")));
201 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
202 GURL("www.Merchant1.com/checkout/")));
203 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
204 GURL("https://www.Merchant1.com/checkout/")));
205 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
206 GURL("https://www.merchant1.com/CheckOut/")));
207 }
208
209 } // namespace autocheckout
210 } // namespace autofill
211
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698