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 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 } |
| 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); |
| 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() { |
| 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()); |
| 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(""))); |
| 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"))); |
| 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 } |
| 173 |
| 174 } // namespace autocheckout |
| 175 |
OLD | NEW |