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

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: revert code change for manual testing/:wq. Created 7 years, 11 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
Ilya Sherman 2013/01/24 22:01:55 nit: Please omit the second newline.
benquan 2013/01/25 00:55:31 Done.
20 namespace {
21
22 const char kDownloadWhitelistResponse[] =
23 "https://www.merchant1.com/checkout/\n"
24 "https://cart.merchant2.com/";
25
26 } // namespace
27
28 namespace autocheckout {
29
30 class WhitelistManagerTest : public testing::Test {
31 public:
32 WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {}
33
34 virtual void SetUp() {
35 io_thread_.StartIOThread();
36 profile_.CreateRequestContext();
37 }
38
39 virtual void TearDown() {
40 whitelist_manager_.reset();
41 profile_.ResetRequestContext();
42 io_thread_.Stop();
43 }
44
45 protected:
46 void EnsureCreateWhitelistManager() {
47 if (!whitelist_manager_.get())
48 whitelist_manager_.reset(new WhitelistManager::WhitelistManager(
49 profile_.GetRequestContext()));
50 }
Ilya Sherman 2013/01/24 22:01:55 Why not just do this in the constructor?
benquan 2013/01/25 00:55:31 WhitelistManager's constructor checks chrome_switc
Ilya Sherman 2013/01/25 01:22:40 Hmm, fair enough.
51
52 void DownloadWhitelist(int response_code, const std::string& response) {
53 // Create and register factory.
54 net::TestURLFetcherFactory factory;
55
56 EnsureCreateWhitelistManager();
57
58 whitelist_manager_->TriggerDownload();
59 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
60 ASSERT_TRUE(fetcher != NULL);
Ilya Sherman 2013/01/24 22:01:55 nit: You can drop the "!= NULL"
benquan 2013/01/25 00:55:31 Done.
61 fetcher->set_response_code(response_code);
62 fetcher->SetResponseString(response);
63 fetcher->delegate()->OnURLFetchComplete(fetcher);
64 }
65
66 void ResetBackOff() {
67 whitelist_manager_->download_timer_.Stop();
68 whitelist_manager_->callback_pending_ = false;
69 }
70
71 const std::vector<std::string>& GetUrlPrefixes() {
Ilya Sherman 2013/01/24 22:01:55 nit: const
benquan 2013/01/25 00:55:31 Done.
72 return whitelist_manager_->url_prefixes_;
73 }
74
75 protected:
76 TestingProfile profile_;
77 scoped_ptr<WhitelistManager> whitelist_manager_;
78
79 private:
80 MessageLoopForIO message_loop_;
81 // The profile's request context must be released on the IO thread.
82 content::TestBrowserThread io_thread_;
83 };
84
85 TEST_F(WhitelistManagerTest, DownloadWhitelist) {
86 CommandLine::ForCurrentProcess()->AppendSwitch(
87 switches::kEnableExperimentalFormFilling);
88 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
89 EXPECT_EQ((size_t)2, GetUrlPrefixes().size());
Ilya Sherman 2013/01/24 22:01:55 nit: Use C++-style casts rather than C-style casts
Ilya Sherman 2013/01/24 22:01:55 nit: ASSERT_EQ, since the test code below this wil
benquan 2013/01/25 00:55:31 Done.
benquan 2013/01/25 00:55:31 Done.
90 EXPECT_EQ("https://www.merchant1.com/checkout/",
91 GetUrlPrefixes()[0]);
92 EXPECT_EQ("https://cart.merchant2.com/",
93 GetUrlPrefixes()[1]);
94 }
95
96 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenSwitchIsOff) {
97 EnsureCreateWhitelistManager();
98 EXPECT_FALSE(whitelist_manager_->ScheduleDownload(3));
99 }
100
101 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenBackOff) {
102 CommandLine::ForCurrentProcess()->AppendSwitch(
103 switches::kEnableExperimentalFormFilling);
104 EnsureCreateWhitelistManager();
105 // First attempt should schedule a download.
106 EXPECT_TRUE(whitelist_manager_->ScheduleDownload(3));
107 // Second attempt should NOT schedule a download while there is already one.
108 EXPECT_FALSE(whitelist_manager_->ScheduleDownload(3));
109 // It should schedule a new download when not in backoff mode.
110 ResetBackOff();
111 EXPECT_TRUE(whitelist_manager_->ScheduleDownload(3));
112 }
113
114 TEST_F(WhitelistManagerTest, DownloadWhitelistFailed) {
115 CommandLine::ForCurrentProcess()->AppendSwitch(
116 switches::kEnableExperimentalFormFilling);
117 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR,
118 kDownloadWhitelistResponse);
119 EXPECT_EQ((size_t)0, GetUrlPrefixes().size());
120
121 ResetBackOff();
122 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
123 EXPECT_EQ((size_t)2, GetUrlPrefixes().size());
124
125 ResetBackOff();
126 DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR,
127 kDownloadWhitelistResponse);
128 EXPECT_EQ((size_t)2, GetUrlPrefixes().size());
129 }
130
131 TEST_F(WhitelistManagerTest, IsAutocheckoutEnabled) {
132 CommandLine::ForCurrentProcess()->AppendSwitch(
133 switches::kEnableExperimentalFormFilling);
134 DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse);
135 EXPECT_EQ((size_t)2, GetUrlPrefixes().size());
136
137 // Empty url.
138 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL("")));
Ilya Sherman 2013/01/24 22:01:55 nit: Prefer std::string() to ""
benquan 2013/01/25 00:55:31 Done.
139 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL()));
140
141 // Positive tests.
142 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
143 GURL("https://www.merchant1.com/checkout/")));
144 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
145 GURL("https://www.merchant1.com/checkout/Shipping")));
146 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
147 GURL("https://www.merchant1.com/checkout/?a=b&c=d")));
148 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
149 GURL("https://cart.merchant2.com/")));
150 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
151 GURL("https://cart.merchant2.com/ShippingInfo")));
152 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
153 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d")));
154
155 // Negative tests.
156 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
157 GURL("https://www.merchant1.com/checkout")));
158 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
159 GURL("https://www.merchant1.com/")));
160 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
161 GURL("https://www.merchant1.com/Building")));
162 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
163 GURL("https://www.merchant2.com/cart")));
164 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
165 GURL("a random string")));
Ilya Sherman 2013/01/24 22:01:55 Doesn't calling spec() on an invalid URL cause an
benquan 2013/01/25 00:55:31 No, it doesn't, spec() returns an empty string. On
Ilya Sherman 2013/01/25 01:22:40 In Debug mode, it will trigger a DCHECK: https://c
benquan 2013/01/26 02:05:53 Oh, in my WhitelistManager::IsAutocheckoutEnabled(
166
167 // Test different cases in host name and path.
168 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled(
169 GURL("https://www.Merchant1.com/checkout/")));
170 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(
171 GURL("https://www.merchant1.com/CheckOut/")));
172 }
Ilya Sherman 2013/01/24 22:01:55 Please add test cases for http://, and for no spec
benquan 2013/01/25 00:55:31 Done.
173
174 } // namespace autocheckout
175
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698