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..05ece0048cfeb053a1adc3744a67a7771b84bf9f |
| --- /dev/null |
| +++ b/chrome/browser/autofill/autocheckout/whitelist_manager_unittest.cc |
| @@ -0,0 +1,164 @@ |
| +// 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 autocheckout { |
| + |
| +class WhitelistManagerTest : public testing::Test { |
| + public: |
| + WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {} |
| + |
| + virtual void SetUp() { |
| + io_thread_.StartIOThread(); |
| + profile_.CreateRequestContext(); |
| + } |
| + |
| + virtual void TearDown() { |
| + WhitelistManager::RemoveFromBrowserContext(&profile_); |
| + profile_.ResetRequestContext(); |
| + io_thread_.Stop(); |
| + } |
| + |
| + protected: |
| + 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.
|
| + // Create and register factory. |
| + net::TestURLFetcherFactory factory; |
| + |
| + bool url_fetched = WhitelistManager::GetForBrowserContext(&profile_)-> |
| + DownloadWhitelist(); |
| + if (url_fetched) { |
| + net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| + 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.
|
| + fetcher->set_response_code(response_code); |
| + fetcher->SetResponseString(response); |
| + fetcher->delegate()->OnURLFetchComplete(fetcher); |
| + } |
| + return url_fetched; |
| + } |
| + |
| + void ResetBackOff() { |
| + WhitelistManager::GetForBrowserContext(&profile_)->next_query_request_ = |
| + base::Time::Now(); |
| + } |
| + |
| + const std::vector<std::string> & GetUrlPrefixes() { |
|
ahutter
2013/01/19 02:07:16
No space before &
benquan
2013/01/23 23:50:53
Done.
|
| + return WhitelistManager::GetForBrowserContext(&profile_)->url_prefixes_; |
| + } |
| + |
| + protected: |
| + TestingProfile profile_; |
| + |
| + private: |
| + // 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); |
| + 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.
|
| + ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); |
| + ASSERT_EQ("https://www.merchant1.com/checkout/", |
| + GetUrlPrefixes()[0]); |
| + ASSERT_EQ("https://cart.merchant2.com/", |
| + GetUrlPrefixes()[1]); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenSwitchIsOff) { |
| + ASSERT_FALSE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, DoNotDownloadWhitelistWhenBackOff) { |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalFormFilling); |
| + // First attempt should call URLFetcher. |
| + ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); |
| + // Second attempt should not call URLFetcher as it is in back off mode. |
| + ASSERT_FALSE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); |
| + // It should call URLFetcher when not in back off mode. |
| + ResetBackOff(); |
| + ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, DownloadWhitelistFailed) { |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalFormFilling); |
| + ASSERT_TRUE(DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, |
| + kDownloadWhitelistResponse)); |
| + ASSERT_EQ((size_t)0, GetUrlPrefixes().size()); |
| + |
| + ResetBackOff(); |
| + ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); |
| + ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); |
| + |
| + ResetBackOff(); |
| + ASSERT_TRUE(DownloadWhitelist(net::HTTP_INTERNAL_SERVER_ERROR, |
| + kDownloadWhitelistResponse)); |
| + ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); |
| +} |
| + |
| +TEST_F(WhitelistManagerTest, IsAutocheckoutEnabled) { |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableExperimentalFormFilling); |
| + ASSERT_TRUE(DownloadWhitelist(net::HTTP_OK, kDownloadWhitelistResponse)); |
| + ASSERT_EQ((size_t)2, GetUrlPrefixes().size()); |
| + WhitelistManager* whitelist_manager = |
| + WhitelistManager::GetForBrowserContext(&profile_); |
| + // Positive tests. |
| + ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout/"))); |
| + ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout/Shipping"))); |
| + ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout/?a=b&c=d"))); |
| + ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://cart.merchant2.com/"))); |
| + ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://cart.merchant2.com/ShippingInfo"))); |
| + ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d"))); |
| + |
| + // Negative tests. |
| + ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/checkout"))); |
| + ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/"))); |
| + ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/Building"))); |
| + ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant2.com/cart"))); |
| + ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("a random string"))); |
| + |
| + // Test different cases in host name and path. |
| + ASSERT_TRUE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.Merchant1.com/checkout/"))); |
| + ASSERT_FALSE(whitelist_manager->IsAutocheckoutEnabled( |
| + GURL("https://www.merchant1.com/CheckOut/"))); |
| +} |
| + |
| +} // namespace autocheckout |
| + |