diff options
author | engedy <engedy@chromium.org> | 2015-01-22 07:50:39 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-22 15:52:04 +0000 |
commit | a6d3a968394fcfe080a6185819e725a698a9e7be (patch) | |
tree | 53dfbe285579da185a65bacdfbea94726b978b58 /components/password_manager/core | |
parent | 3453202ab27eb7b7368c082cd292dfdec3f8990a (diff) | |
download | chromium_src-a6d3a968394fcfe080a6185819e725a698a9e7be.zip chromium_src-a6d3a968394fcfe080a6185819e725a698a9e7be.tar.gz chromium_src-a6d3a968394fcfe080a6185819e725a698a9e7be.tar.bz2 |
Add FakeAffiliationFetcher and factory.
BUG=437865
Review URL: https://codereview.chromium.org/866733002
Cr-Commit-Position: refs/heads/master@{#312625}
Diffstat (limited to 'components/password_manager/core')
6 files changed, 201 insertions, 0 deletions
diff --git a/components/password_manager/core/browser/BUILD.gn b/components/password_manager/core/browser/BUILD.gn index 23d56b7..899dca9 100644 --- a/components/password_manager/core/browser/BUILD.gn +++ b/components/password_manager/core/browser/BUILD.gn @@ -67,6 +67,7 @@ static_library("browser") { "password_store_sync.h", "psl_matching_helper.cc", "psl_matching_helper.h", + "test_affiliation_fetcher_factory.h", "webdata/logins_table.cc", "webdata/logins_table.h", "webdata/logins_table_win.cc", @@ -118,6 +119,8 @@ proto_library("proto") { source_set("test_support") { testonly = true sources = [ + "fake_affiliation_fetcher.cc", + "fake_affiliation_fetcher.h", "mock_password_store.cc", "mock_password_store.h", "password_form_data.cc", diff --git a/components/password_manager/core/browser/affiliation_fetcher.cc b/components/password_manager/core/browser/affiliation_fetcher.cc index 89139e5..92af8c4 100644 --- a/components/password_manager/core/browser/affiliation_fetcher.cc +++ b/components/password_manager/core/browser/affiliation_fetcher.cc @@ -6,6 +6,7 @@ #include "components/password_manager/core/browser/affiliation_api.pb.h" #include "components/password_manager/core/browser/affiliation_utils.h" +#include "components/password_manager/core/browser/test_affiliation_fetcher_factory.h" #include "google_apis/google_api_keys.h" #include "net/base/load_flags.h" #include "net/base/url_util.h" @@ -16,6 +17,8 @@ namespace password_manager { +static TestAffiliationFetcherFactory* g_testing_factory = nullptr; + AffiliationFetcher::AffiliationFetcher( net::URLRequestContextGetter* request_context_getter, const std::vector<FacetURI>& facet_uris, @@ -36,9 +39,19 @@ AffiliationFetcher* AffiliationFetcher::Create( net::URLRequestContextGetter* context_getter, const std::vector<FacetURI>& facet_uris, AffiliationFetcherDelegate* delegate) { + if (g_testing_factory) { + return g_testing_factory->CreateInstance(context_getter, facet_uris, + delegate); + } return new AffiliationFetcher(context_getter, facet_uris, delegate); } +// static +void AffiliationFetcher::SetFactoryForTesting( + TestAffiliationFetcherFactory* factory) { + g_testing_factory = factory; +} + void AffiliationFetcher::StartRequest() { DCHECK(!fetcher_); diff --git a/components/password_manager/core/browser/affiliation_fetcher.h b/components/password_manager/core/browser/affiliation_fetcher.h index 5bda532..408a348 100644 --- a/components/password_manager/core/browser/affiliation_fetcher.h +++ b/components/password_manager/core/browser/affiliation_fetcher.h @@ -21,6 +21,8 @@ class URLRequestContextGetter; namespace password_manager { +class TestAffiliationFetcherFactory; + // Fetches authoritative information regarding which facets are affiliated with // each other, that is, which facets belong to the same logical application. // See affiliation_utils.h for a definition of what this means. @@ -39,6 +41,13 @@ class AffiliationFetcher : net::URLFetcherDelegate { const std::vector<FacetURI>& facet_uris, AffiliationFetcherDelegate* delegate); + // Sets the |factory| to be used by Create() to construct AffiliationFetcher + // instances. To be used only for testing. + // + // The caller must ensure that the |factory| outlives all potential Create() + // calls. The caller may pass in NULL to resume using the default factory. + static void SetFactoryForTesting(TestAffiliationFetcherFactory* factory); + // Actually starts the request, and will call the delegate with the results on // the same thread when done. If |this| is destroyed before completion, the // in-flight request is cancelled, and the delegate will not be called. diff --git a/components/password_manager/core/browser/fake_affiliation_fetcher.cc b/components/password_manager/core/browser/fake_affiliation_fetcher.cc new file mode 100644 index 0000000..0f78e1f --- /dev/null +++ b/components/password_manager/core/browser/fake_affiliation_fetcher.cc @@ -0,0 +1,59 @@ +// Copyright 2015 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 "components/password_manager/core/browser/fake_affiliation_fetcher.h" + +namespace password_manager { + +password_manager::FakeAffiliationFetcher::FakeAffiliationFetcher( + net::URLRequestContextGetter* request_context_getter, + const std::vector<FacetURI>& facet_ids, + AffiliationFetcherDelegate* delegate) + : AffiliationFetcher(request_context_getter, facet_ids, delegate) { +} + +password_manager::FakeAffiliationFetcher::~FakeAffiliationFetcher() { +} + +void password_manager::FakeAffiliationFetcher::SimulateSuccess( + scoped_ptr<AffiliationFetcherDelegate::Result> fake_result) { + delegate()->OnFetchSucceeded(fake_result.Pass()); +} + +void password_manager::FakeAffiliationFetcher::SimulateFailure() { + delegate()->OnFetchFailed(); +} + +void password_manager::FakeAffiliationFetcher::StartRequest() { + // Fake. Does nothing. +} + +password_manager::ScopedFakeAffiliationFetcherFactory:: + ScopedFakeAffiliationFetcherFactory() { + AffiliationFetcher::SetFactoryForTesting(this); +} + +password_manager::ScopedFakeAffiliationFetcherFactory:: + ~ScopedFakeAffiliationFetcherFactory() { + AffiliationFetcher::SetFactoryForTesting(nullptr); +} + +FakeAffiliationFetcher* ScopedFakeAffiliationFetcherFactory::GetNextFetcher() { + DCHECK(!pending_fetchers_.empty()); + FakeAffiliationFetcher* first = pending_fetchers_.front(); + pending_fetchers_.pop(); + return first; +} + +AffiliationFetcher* ScopedFakeAffiliationFetcherFactory::CreateInstance( + net::URLRequestContextGetter* request_context_getter, + const std::vector<FacetURI>& facet_ids, + AffiliationFetcherDelegate* delegate) { + FakeAffiliationFetcher* fetcher = + new FakeAffiliationFetcher(request_context_getter, facet_ids, delegate); + pending_fetchers_.push(fetcher); + return fetcher; +} + +} // namespace password_manager diff --git a/components/password_manager/core/browser/fake_affiliation_fetcher.h b/components/password_manager/core/browser/fake_affiliation_fetcher.h new file mode 100644 index 0000000..860d3a0 --- /dev/null +++ b/components/password_manager/core/browser/fake_affiliation_fetcher.h @@ -0,0 +1,78 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FAKE_AFFILIATION_FETCHER_H_ +#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FAKE_AFFILIATION_FETCHER_H_ + +#include <queue> + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" +#include "components/password_manager/core/browser/affiliation_fetcher.h" +#include "components/password_manager/core/browser/affiliation_fetcher_delegate.h" +#include "components/password_manager/core/browser/test_affiliation_fetcher_factory.h" + +namespace password_manager { + +// A fake AffiliationFetcher that can be used in tests to return fake API +// responses to users of AffiliationFetcher. +class FakeAffiliationFetcher : public AffiliationFetcher { + public: + FakeAffiliationFetcher(net::URLRequestContextGetter* request_context_getter, + const std::vector<FacetURI>& facet_ids, + AffiliationFetcherDelegate* delegate); + ~FakeAffiliationFetcher() override; + + // Simulates successful completion of the request with |fake_result|. Note + // that the consumer may choose to destroy |this| from within this call. + void SimulateSuccess( + scoped_ptr<AffiliationFetcherDelegate::Result> fake_result); + + // Simulates completion of the request with failure. Note that the consumer + // may choose to destroy |this| from within this call. + void SimulateFailure(); + + private: + void StartRequest() override; + + DISALLOW_COPY_AND_ASSIGN(FakeAffiliationFetcher); +}; + +// While this factory is in scope, calls to AffiliationFetcher::Create() will +// produce FakeAffiliationFetchers that can be used in tests to return fake API +// responses to users of AffiliationFetcher. Nesting is not supported. +class ScopedFakeAffiliationFetcherFactory + : public TestAffiliationFetcherFactory { + public: + ScopedFakeAffiliationFetcherFactory(); + ~ScopedFakeAffiliationFetcherFactory() override; + + // Returns the next FakeAffiliationFetcher instance previously produced, so + // that that the testing code can inject a response and simulate completion + // or failure of the request. + // + // Note that the factory does not retain ownership of the produced fetchers, + // so that the tests should ensure that the corresponding production code will + // not destroy them before they are accessed here. + FakeAffiliationFetcher* GetNextFetcher(); + + bool has_pending_fetchers() const { return !pending_fetchers_.empty(); } + + // AffiliationFetcherFactory: + AffiliationFetcher* CreateInstance( + net::URLRequestContextGetter* request_context_getter, + const std::vector<FacetURI>& facet_ids, + AffiliationFetcherDelegate* delegate) override; + + private: + // Fakes created by this factory. The elements are owned by the production + // code that normally owns the result of AffiliationFetcher::Create(). + std::queue<FakeAffiliationFetcher*> pending_fetchers_; + + DISALLOW_COPY_AND_ASSIGN(ScopedFakeAffiliationFetcherFactory); +}; + +} // namespace password_manager + +#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FAKE_AFFILIATION_FETCHER_H_ diff --git a/components/password_manager/core/browser/test_affiliation_fetcher_factory.h b/components/password_manager/core/browser/test_affiliation_fetcher_factory.h new file mode 100644 index 0000000..e313702 --- /dev/null +++ b/components/password_manager/core/browser/test_affiliation_fetcher_factory.h @@ -0,0 +1,39 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_TEST_AFFILIATION_FETCHER_FACTORY_H_ +#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_TEST_AFFILIATION_FETCHER_FACTORY_H_ + +#include <vector> + +namespace net { +class URLRequestContextGetter; +} // namespace net + +namespace password_manager { + +class FacetURI; +class AffiliationFetcherDelegate; + +// Interface for a factory to be used by AffiliationFetcher::Create() in tests +// to construct instances of test-specific AffiliationFetcher subclasses. +// +// The factory is registered with AffiliationFetcher::SetFactoryForTesting(). +class TestAffiliationFetcherFactory { + public: + // Constructs a fetcher to retrieve affiliations for each facet in |facet_ids| + // using the specified |request_context_getter|, and will provide the results + // to the |delegate| on the same thread that creates the instance. + virtual AffiliationFetcher* CreateInstance( + net::URLRequestContextGetter* request_context_getter, + const std::vector<FacetURI>& facet_ids, + AffiliationFetcherDelegate* delegate) = 0; + + protected: + virtual ~TestAffiliationFetcherFactory() {} +}; + +} // namespace password_manager + +#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_TEST_AFFILIATION_FETCHER_FACTORY_H_ |