Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "components/autofill/browser/autocheckout/whitelist_manager.h" | 9 #include "components/autofill/browser/autocheckout/whitelist_manager.h" |
| 10 #include "components/autofill/browser/autofill_metrics.h" | |
| 10 #include "components/autofill/common/autofill_switches.h" | 11 #include "components/autofill/common/autofill_switches.h" |
| 11 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const size_t kTestDownloadInterval = 3; // 3 seconds | 24 const size_t kTestDownloadInterval = 3; // 3 seconds |
| 23 | 25 |
| 24 const char kDownloadWhitelistResponse[] = | 26 const char kDownloadWhitelistResponse[] = |
| 25 "https://www.merchant1.com/checkout/\n" | 27 "https://www.merchant1.com/checkout/\n" |
| 26 "https://cart.merchant2.com/"; | 28 "https://cart.merchant2.com/"; |
| 27 | 29 |
| 28 } // namespace | 30 } // namespace |
| 29 | 31 |
| 30 namespace autofill { | 32 namespace autofill { |
| 31 namespace autocheckout { | 33 namespace autocheckout { |
| 32 | 34 |
| 33 class WhitelistManagerTest; | 35 class WhitelistManagerTest; |
| 34 | 36 |
| 37 class MockAutofillMetrics : public autofill::AutofillMetrics { | |
| 38 public: | |
| 39 MockAutofillMetrics() {} | |
| 40 MOCK_CONST_METHOD2(LogAutocheckoutWhitelistDownloadDuration, | |
| 41 void(const base::TimeDelta& duration, | |
| 42 autofill::AutofillMetrics::WhitelistDownloadStatus)); | |
| 43 private: | |
| 44 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics); | |
| 45 }; | |
| 46 | |
| 35 class TestWhitelistManager : public WhitelistManager { | 47 class TestWhitelistManager : public WhitelistManager { |
| 36 public: | 48 public: |
| 37 TestWhitelistManager() | 49 TestWhitelistManager() |
| 38 : WhitelistManager(), | 50 : WhitelistManager(), |
| 39 did_start_download_timer_(false) {} | 51 did_start_download_timer_(false) {} |
| 40 | 52 |
| 41 virtual void ScheduleDownload(size_t interval_seconds) OVERRIDE { | 53 virtual void ScheduleDownload(size_t interval_seconds) OVERRIDE { |
| 42 did_start_download_timer_ = false; | 54 did_start_download_timer_ = false; |
| 43 return WhitelistManager::ScheduleDownload(interval_seconds); | 55 return WhitelistManager::ScheduleDownload(interval_seconds); |
| 44 } | 56 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 57 } | 69 } |
| 58 | 70 |
| 59 void StopDownloadTimer() { | 71 void StopDownloadTimer() { |
| 60 WhitelistManager::StopDownloadTimer(); | 72 WhitelistManager::StopDownloadTimer(); |
| 61 } | 73 } |
| 62 | 74 |
| 63 const std::vector<std::string>& url_prefixes() const { | 75 const std::vector<std::string>& url_prefixes() const { |
| 64 return WhitelistManager::url_prefixes(); | 76 return WhitelistManager::url_prefixes(); |
| 65 } | 77 } |
| 66 | 78 |
| 79 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { | |
| 80 return mock_metrics_logger_; | |
| 81 } | |
| 82 | |
| 67 private: | 83 private: |
| 68 bool did_start_download_timer_; | 84 bool did_start_download_timer_; |
| 69 | 85 |
| 86 MockAutofillMetrics mock_metrics_logger_; | |
| 87 | |
| 70 DISALLOW_COPY_AND_ASSIGN(TestWhitelistManager); | 88 DISALLOW_COPY_AND_ASSIGN(TestWhitelistManager); |
| 71 }; | 89 }; |
| 72 | 90 |
| 73 class WhitelistManagerTest : public testing::Test { | 91 class WhitelistManagerTest : public testing::Test { |
| 74 public: | 92 public: |
| 75 WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {} | 93 WhitelistManagerTest() : io_thread_(content::BrowserThread::IO) {} |
| 76 | 94 |
| 77 virtual void SetUp() { | 95 virtual void SetUp() { |
| 78 io_thread_.StartIOThread(); | 96 io_thread_.StartIOThread(); |
| 79 profile_.CreateRequestContext(); | 97 profile_.CreateRequestContext(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 90 whitelist_manager_.reset(new TestWhitelistManager()); | 108 whitelist_manager_.reset(new TestWhitelistManager()); |
| 91 } | 109 } |
| 92 } | 110 } |
| 93 | 111 |
| 94 void DownloadWhitelist(int response_code, const std::string& response) { | 112 void DownloadWhitelist(int response_code, const std::string& response) { |
| 95 // Create and register factory. | 113 // Create and register factory. |
| 96 net::TestURLFetcherFactory factory; | 114 net::TestURLFetcherFactory factory; |
| 97 | 115 |
| 98 CreateWhitelistManager(); | 116 CreateWhitelistManager(); |
| 99 | 117 |
| 118 EXPECT_CALL( | |
| 119 static_cast<const MockAutofillMetrics&>( | |
| 120 whitelist_manager_->GetMetricLogger()), | |
| 121 LogAutocheckoutWhitelistDownloadDuration( | |
| 122 testing::_, | |
| 123 response_code == net::HTTP_OK ? | |
| 124 autofill::AutofillMetrics::WHITELIST_DOWNLOAD_SUCCEEDED : | |
| 125 autofill::AutofillMetrics::WHITELIST_DOWNLOAD_FAILED)) | |
| 126 .Times(1); | |
|
Ilya Sherman
2013/04/17 00:18:01
nit: This is pretty hard to read. Could you break
benquan
2013/04/17 01:51:30
Done.
| |
| 127 | |
| 100 whitelist_manager_->TriggerDownload(); | 128 whitelist_manager_->TriggerDownload(); |
| 101 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 129 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 102 ASSERT_TRUE(fetcher); | 130 ASSERT_TRUE(fetcher); |
| 103 fetcher->set_response_code(response_code); | 131 fetcher->set_response_code(response_code); |
| 104 fetcher->SetResponseString(response); | 132 fetcher->SetResponseString(response); |
| 105 fetcher->delegate()->OnURLFetchComplete(fetcher); | 133 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 106 } | 134 } |
| 107 | 135 |
| 108 void ResetBackOff() { | 136 void ResetBackOff() { |
| 109 whitelist_manager_->StopDownloadTimer(); | 137 whitelist_manager_->StopDownloadTimer(); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d"))); | 285 GURL("https://cart.merchant2.com/ShippingInfo?a=b&c=d"))); |
| 258 // Bypass other urls. | 286 // Bypass other urls. |
| 259 EXPECT_EQ(std::string("https://bypass.me/"), | 287 EXPECT_EQ(std::string("https://bypass.me/"), |
| 260 whitelist_manager_->GetMatchedURLPrefix( | 288 whitelist_manager_->GetMatchedURLPrefix( |
| 261 GURL("https://bypass.me/"))); | 289 GURL("https://bypass.me/"))); |
| 262 } | 290 } |
| 263 | 291 |
| 264 } // namespace autocheckout | 292 } // namespace autocheckout |
| 265 } // namespace autofill | 293 } // namespace autofill |
| 266 | 294 |
| OLD | NEW |