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_ = NULL; |
| 85 profile_.ResetRequestContext(); |
| 86 io_thread_.Stop(); |
| 87 } |
| 88 |
| 89 protected: |
| 90 void EnsureCreateWhitelistManager() { |
| 91 if (!whitelist_manager_.get()) |
| 92 whitelist_manager_ = new TestWhitelistManager( |
| 93 profile_.GetRequestContext()); |
| 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 { |
| 116 return whitelist_manager_->url_prefixes(); |
| 117 } |
| 118 |
| 119 protected: |
| 120 TestingProfile profile_; |
| 121 scoped_refptr<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); |
| 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, IsAutocheckoutEnabled) { |
| 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_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL(std::string()))); |
| 187 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL())); |
| 188 |
| 189 // Positive tests. |
| 190 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| 191 GURL("https://www.merchant1.com/checkout/"))); |
| 192 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| 193 GURL("https://www.merchant1.com/checkout/Shipping"))); |
| 194 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| 195 GURL("https://www.merchant1.com/checkout/?a=b&c=d"))); |
| 196 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| 197 GURL("https://cart.merchant2.com/"))); |
| 198 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| 199 GURL("https://cart.merchant2.com/ShippingInfo"))); |
| 200 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| 201 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d"))); |
| 202 |
| 203 // Negative tests. |
| 204 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 205 GURL("https://www.merchant1.com/checkout"))); |
| 206 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 207 GURL("https://www.merchant1.com/"))); |
| 208 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 209 GURL("https://www.merchant1.com/Building"))); |
| 210 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 211 GURL("https://www.merchant2.com/cart"))); |
| 212 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 213 GURL("a random string"))); |
| 214 |
| 215 // Test different cases in schema, host and path. |
| 216 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 217 GURL("http://www.Merchant1.com/checkout/"))); |
| 218 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 219 GURL("www.Merchant1.com/checkout/"))); |
| 220 EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| 221 GURL("https://www.Merchant1.com/checkout/"))); |
| 222 EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| 223 GURL("https://www.merchant1.com/CheckOut/"))); |
| 224 } |
| 225 |
| 226 } // namespace autocheckout |
| 227 } // namespace autofill |
| 228 |
OLD | NEW |