Chromium Code Reviews| Index: chrome/browser/autofill/autocheckout/whitelist_manager_unittest.cc |
| diff --git a/chrome/browser/autofill/autocheckout/whitelist_manager_unittest.cc b/chrome/browser/autofill/autocheckout/whitelist_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f8a542a32b303dedb6923e13b882779bbf1eb5c7 |
| --- /dev/null |
| +++ b/chrome/browser/autofill/autocheckout/whitelist_manager_unittest.cc |
| @@ -0,0 +1,211 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/command_line.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "chrome/browser/autofill/autocheckout/whitelist_manager.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/test_url_fetcher_factory.h" |
| +#include "net/url_request/url_fetcher_delegate.h" |
| +#include "net/url_request/url_request_status.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +const char kDownloadWhitelistResponse[] = |
| + "https://www.merchant1.com/checkout/\n" |
| + "https://cart.merchant2.com/"; |
| + |
| +} // namespace |
| + |
| +namespace autofill { |
| +namespace autocheckout { |
| + |
| +class WhitelistManagerTest; |
| + |
| +class TestWhitelistManager : public WhitelistManager { |
| + public: |
| + explicit TestWhitelistManager(net::URLRequestContextGetter* context_getter) |
| + : 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.
|
| + |
| + void ScheduleDownload(size_t interval_seconds) OVERRIDE { |
| + called_timer_start_ = false; |
| + return WhitelistManager::ScheduleDownload(interval_seconds); |
| + } |
| + |
| + void StartDownloadTimer(size_t interval_seconds) OVERRIDE { |
| + WhitelistManager::StartDownloadTimer(interval_seconds); |
| + called_timer_start_ = true; |
| + } |
| + |
| + 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.
|
| + return called_timer_start_; |
| + } |
|
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.
|
| + private: |
| + 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.
|
| + |
| + 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.
|
| + DISALLOW_COPY_AND_ASSIGN(TestWhitelistManager); |
| +}; |
| + |
| +class WhitelistManagerTest : public testing::Test { |
| + public: |
| + WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {} |
| + |
| + virtual void SetUp() { |
| + io_thread_.StartIOThread(); |
| + profile_.CreateRequestContext(); |
| + } |
| + |
| + virtual void TearDown() { |
| + whitelist_manager_ = NULL; |
| + profile_.ResetRequestContext(); |
| + io_thread_.Stop(); |
| + } |
| + |
| + protected: |
| + void EnsureCreateWhitelistManager() { |
| + if (!whitelist_manager_.get()) |
| + whitelist_manager_ = new TestWhitelistManager( |
| + profile_.GetRequestContext()); |
| + } |
| + |
| + void DownloadWhitelist(int response_code, const std::string& response) { |
| + // Create and register factory. |
| + net::TestURLFetcherFactory factory; |
| + |
| + EnsureCreateWhitelistManager(); |
| + |
| + whitelist_manager_->TriggerDownload(); |
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| + ASSERT_TRUE(fetcher); |
| + fetcher->set_response_code(response_code); |
| + fetcher->SetResponseString(response); |
| + fetcher->delegate()->OnURLFetchComplete(fetcher); |
| + } |
| + |
| + void ResetBackOff() { |
| + whitelist_manager_->download_timer().Stop(); |
| + whitelist_manager_->set_callback_is_pending(false); |
| + } |
| + |
| + const std::vector<std::string>& GetUrlPrefixes() const { |
| + return whitelist_manager_->url_prefixes(); |
| + } |
| + |
| + protected: |
| + TestingProfile profile_; |
| + scoped_refptr<TestWhitelistManager> whitelist_manager_; |
| + |
| + private: |
| + MessageLoopForIO message_loop_; |
| + // The profile's request context must be released on the IO thread. |
| + content::TestBrowserThread io_thread_; |
| +}; |
| + |
| +TEST_F(WhitelistManagerTest, DownloadWhitelist) { |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalFormFilling); |
| + DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse); |
| + ASSERT_EQ(2U, GetUrlPrefixes().size()); |
| + EXPECT_EQ("https://www.merchant1.com/checkout/", |
| + GetUrlPrefixes()[0]); |
| + EXPECT_EQ("https://cart.merchant2.com/", |
| + GetUrlPrefixes()[1]); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenSwitchIsOff) { |
| + EnsureCreateWhitelistManager(); |
| + whitelist_manager_->ScheduleDownload(3); |
| + EXPECT_FALSE(whitelist_manager_->CalledTimerStart()); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenBackOff) { |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalFormFilling); |
| + EnsureCreateWhitelistManager(); |
| + // First attempt should schedule a download. |
| + whitelist_manager_->ScheduleDownload(3); |
| + EXPECT_TRUE(whitelist_manager_->CalledTimerStart()); |
| + // Second attempt should NOT schedule a download while there is already one. |
| + whitelist_manager_->ScheduleDownload(3); |
| + EXPECT_FALSE(whitelist_manager_->CalledTimerStart()); |
| + // It should schedule a new download when not in backoff mode. |
| + ResetBackOff(); |
| + whitelist_manager_->ScheduleDownload(3); |
| + EXPECT_TRUE(whitelist_manager_->CalledTimerStart()); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, DownloadWhitelistFailed) { |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalFormFilling); |
| + DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, |
| + kDownloadWhitelistResponse); |
| + EXPECT_EQ(0U, GetUrlPrefixes().size()); |
| + |
| + ResetBackOff(); |
| + DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse); |
| + EXPECT_EQ(2U, GetUrlPrefixes().size()); |
| + |
| + ResetBackOff(); |
| + DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, |
| + kDownloadWhitelistResponse); |
| + EXPECT_EQ(2U, GetUrlPrefixes().size()); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, IsAutocheckoutEnabled) { |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalFormFilling); |
| + DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse); |
| + EXPECT_EQ(2U, GetUrlPrefixes().size()); |
| + |
| + // Empty url. |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL(std::string()))); |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled(GURL())); |
| + |
| + // Positive tests. |
| + EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout/"))); |
| + EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout/Shipping"))); |
| + EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout/?a=b&c=d"))); |
| + EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://cart.merchant2.com/"))); |
| + EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://cart.merchant2.com/ShippingInfo"))); |
| + EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d"))); |
| + |
| + // Negative tests. |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout"))); |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/"))); |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/Building"))); |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant2.com/cart"))); |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("a random string"))); |
| + |
| + // Test different cases in schema, host and path. |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("http://www.Merchant1.com/checkout/"))); |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("www.Merchant1.com/checkout/"))); |
| + EXPECT_TRUE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.Merchant1.com/checkout/"))); |
| + EXPECT_FALSE(whitelist_manager_->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/CheckOut/"))); |
| +} |
| + |
| +} // namespace autocheckout |
| +} // namespace autofill |
| + |