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 | |
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 WhitelistManager::RemoveFromBrowserContext(&profile_); | |
41 profile_.ResetRequestContext(); | |
42 io_thread_.Stop(); | |
43 } | |
44 | |
45 protected: | |
46 bool DownloadWhitelist(int response_code, const char* response) { | |
ahutter
2013/01/19 02:07:16
Why not a const std::string reference?
benquan
2013/01/23 23:50:53
Done.
| |
47 // Create and register factory. | |
48 net::TestURLFetcherFactory factory; | |
49 | |
50 bool url_fetched = WhitelistManager::GetForBrowserContext(&profile_)-> | |
51 DownloadWhitelist(); | |
52 if (url_fetched) { | |
53 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
54 DCHECK(fetcher); | |
ahutter
2013/01/19 02:07:16
you should probably do an ASSERT_TRUE since you'll
benquan
2013/01/23 23:50:53
ah, fixed! BTW, this was copied from https://code.
| |
55 fetcher->set_response_code(response_code); | |
56 fetcher->SetResponseString(response); | |
57 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
58 } | |
59 return url_fetched; | |
60 } | |
61 | |
62 void ResetBackOff() { | |
63 WhitelistManager::GetForBrowserContext(&profile_)->next_query_request_ = | |
64 base::Time::Now(); | |
65 } | |
66 | |
67 const std::vector<std::string> & GetUrlPrefixes() { | |
ahutter
2013/01/19 02:07:16
No space before &
benquan
2013/01/23 23:50:53
Done.
| |
68 return WhitelistManager::GetForBrowserContext(&profile_)->url_prefixes_; | |
69 } | |
70 | |
71 protected: | |
72 TestingProfile profile_; | |
73 | |
74 private: | |
75 // The profile's request context must be released on the IO thread. | |
76 content::TestBrowserThread io_thread_; | |
77 }; | |
78 | |
79 TEST_F(WhitelistManagerTest, DownloadWhitelist) { | |
80 CommandLine::ForCurrentProcess()->AppendSwitch( | |
81 switches::kEnableExperimentalFormFilling); | |
82 ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); | |
ahutter
2013/01/19 02:07:16
EXPECT is preferred unless ASSERT is necessary.
benquan
2013/01/23 23:50:53
Done.
| |
83 ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); | |
84 ASSERT_EQ("https://www.merchant1.com/checkout/", | |
85 GetUrlPrefixes()[0]); | |
86 ASSERT_EQ("https://cart.merchant2.com/", | |
87 GetUrlPrefixes()[1]); | |
88 } | |
89 | |
90 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenSwitchIsOff) { | |
91 ASSERT_FALSE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); | |
92 } | |
93 | |
94 TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenBackOff) { | |
95 CommandLine::ForCurrentProcess()->AppendSwitch( | |
96 switches::kEnableExperimentalFormFilling); | |
97 // First attempt should call URLFetcher. | |
98 ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); | |
99 // Second attempt should not call URLFetcher as it is in back off mode. | |
100 ASSERT_FALSE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); | |
101 // It should call URLFetcher when not in back off mode. | |
102 ResetBackOff(); | |
103 ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); | |
104 } | |
105 | |
106 TEST_F(WhitelistManagerTest, DownloadWhitelistFailed) { | |
107 CommandLine::ForCurrentProcess()->AppendSwitch( | |
108 switches::kEnableExperimentalFormFilling); | |
109 ASSERT_TRUE(DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, | |
110 kDownloadWhitelistResponse)); | |
111 ASSERT_EQ((size_t)0, GetUrlPrefixes().size()); | |
112 | |
113 ResetBackOff(); | |
114 ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); | |
115 ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); | |
116 | |
117 ResetBackOff(); | |
118 ASSERT_TRUE(DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, | |
119 kDownloadWhitelistResponse)); | |
120 ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); | |
121 } | |
122 | |
123 TEST_F(WhitelistManagerTest, IsAutocheckoutEnabled) { | |
124 CommandLine::ForCurrentProcess()->AppendSwitch( | |
125 switches::kEnableExperimentalFormFilling); | |
126 ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); | |
127 ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); | |
128 WhitelistManager* whitelist_manager = | |
129 WhitelistManager::GetForBrowserContext(&profile_); | |
130 // Positive tests. | |
131 ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( | |
132 GURL("https://www.merchant1.com/checkout/"))); | |
133 ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( | |
134 GURL("https://www.merchant1.com/checkout/Shipping"))); | |
135 ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( | |
136 GURL("https://www.merchant1.com/checkout/?a=b&c=d"))); | |
137 ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( | |
138 GURL("https://cart.merchant2.com/"))); | |
139 ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( | |
140 GURL("https://cart.merchant2.com/ShippingInfo"))); | |
141 ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( | |
142 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d"))); | |
143 | |
144 // Negative tests. | |
145 ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( | |
146 GURL("https://www.merchant1.com/checkout"))); | |
147 ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( | |
148 GURL("https://www.merchant1.com/"))); | |
149 ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( | |
150 GURL("https://www.merchant1.com/Building"))); | |
151 ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( | |
152 GURL("https://www.merchant2.com/cart"))); | |
153 ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( | |
154 GURL("a random string"))); | |
155 | |
156 // Test different cases in host name and path. | |
157 ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( | |
158 GURL("https://www.Merchant1.com/checkout/"))); | |
159 ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( | |
160 GURL("https://www.merchant1.com/CheckOut/"))); | |
161 } | |
162 | |
163 } // namespace autocheckout | |
164 | |
OLD | NEW |