Commit 07c57907 authored by Nela Kaczmarek's avatar Nela Kaczmarek Committed by Commit Bot

Add Affiliation Fetcher interface and mock class.

Bug: 1108279
Change-Id: I0d50e041c78f871fa9167ff157aa301559c6ff91
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2341646
Commit-Queue: Nela Kaczmarek <nelakaczmarek@google.com>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796441}
parent 6c2ddda1
......@@ -44,6 +44,7 @@ static_library("browser") {
"android_affiliation/affiliation_fetcher.cc",
"android_affiliation/affiliation_fetcher.h",
"android_affiliation/affiliation_fetcher_delegate.h",
"android_affiliation/affiliation_fetcher_interface.h",
"android_affiliation/android_affiliation_service.cc",
"android_affiliation/android_affiliation_service.h",
"android_affiliation/facet_manager.cc",
......@@ -451,6 +452,8 @@ static_library("test_support") {
"android_affiliation/mock_affiliated_match_helper.h",
"android_affiliation/mock_affiliation_consumer.cc",
"android_affiliation/mock_affiliation_consumer.h",
"android_affiliation/mock_affiliation_fetcher.cc",
"android_affiliation/mock_affiliation_fetcher.h",
"fake_form_fetcher.cc",
"fake_form_fetcher.h",
"mock_bulk_leak_check_service.cc",
......
......@@ -256,9 +256,8 @@ bool AffiliationBackend::OnCanSendNetworkRequest() {
if (requested_facet_uris.empty())
return false;
fetcher_.reset(AffiliationFetcher::Create(url_loader_factory_,
requested_facet_uris, this));
fetcher_->StartRequest();
fetcher_.reset(AffiliationFetcher::Create(url_loader_factory_, this));
fetcher_->StartRequest(requested_facet_uris);
ReportStatistics(requested_facet_uris.size());
return true;
}
......
......@@ -44,10 +44,8 @@ static TestAffiliationFetcherFactory* g_testing_factory = nullptr;
AffiliationFetcher::AffiliationFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_uris,
AffiliationFetcherDelegate* delegate)
: url_loader_factory_(std::move(url_loader_factory)),
requested_facet_uris_(facet_uris),
delegate_(delegate) {
for (const FacetURI& uri : requested_facet_uris_) {
DCHECK(uri.is_valid());
......@@ -59,14 +57,12 @@ AffiliationFetcher::~AffiliationFetcher() = default;
// static
AffiliationFetcher* AffiliationFetcher::Create(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_uris,
AffiliationFetcherDelegate* delegate) {
if (g_testing_factory) {
return g_testing_factory->CreateInstance(std::move(url_loader_factory),
facet_uris, delegate);
}
return new AffiliationFetcher(std::move(url_loader_factory), facet_uris,
delegate);
}
return new AffiliationFetcher(std::move(url_loader_factory), delegate);
}
// static
......@@ -75,8 +71,9 @@ void AffiliationFetcher::SetFactoryForTesting(
g_testing_factory = factory;
}
void AffiliationFetcher::StartRequest() {
void AffiliationFetcher::StartRequest(const std::vector<FacetURI>& facet_uris) {
DCHECK(!simple_url_loader_);
requested_facet_uris_ = facet_uris;
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("affiliation_lookup", R"(
......@@ -125,6 +122,14 @@ void AffiliationFetcher::StartRequest() {
base::Unretained(this)));
}
const std::vector<FacetURI>& AffiliationFetcher::GetRequestedFacetURIs() const {
return requested_facet_uris_;
}
AffiliationFetcherDelegate* AffiliationFetcher::delegate() const {
return delegate_;
}
// static
GURL AffiliationFetcher::BuildQueryURL() {
return net::AppendQueryParameter(
......
......@@ -11,8 +11,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_delegate.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_utils.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_interface.h"
class GURL;
......@@ -31,16 +30,15 @@ class TestAffiliationFetcherFactory;
//
// An instance is good for exactly one fetch, and may be used from any thread
// that runs a message loop (i.e. not a worker pool thread).
class AffiliationFetcher {
class AffiliationFetcher : public AffiliationFetcherInterface {
public:
~AffiliationFetcher();
~AffiliationFetcher() override;
// Constructs a fetcher to retrieve affiliations for each facet in |facet_ids|
// using the specified |url_loader_factory|, and will provide the results
// to the |delegate| on the same thread that creates the instance.
// Constructs a fetcher using the specified |url_loader_factory|, and will
// provide the results to the |delegate| on the same thread that creates the
// instance.
static AffiliationFetcher* Create(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_uris,
AffiliationFetcherDelegate* delegate);
// Builds the URL for the Affiliation API's lookup method.
......@@ -53,25 +51,22 @@ class AffiliationFetcher {
// 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.
// Further details:
// Actually starts the request to retrieve affiliations for each facet in
// |facet_uris|, 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. Further details:
// * No cookies are sent/saved with the request.
// * In case of network/server errors, the request will not be retried.
// * Results are guaranteed to be always fresh and will never be cached.
void StartRequest();
void StartRequest(const std::vector<FacetURI>& facet_uris) override;
const std::vector<FacetURI>& requested_facet_uris() const {
return requested_facet_uris_;
}
const std::vector<FacetURI>& GetRequestedFacetURIs() const override;
AffiliationFetcherDelegate* delegate() const { return delegate_; }
AffiliationFetcherDelegate* delegate() const;
protected:
AffiliationFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_uris,
AffiliationFetcherDelegate* delegate);
private:
......@@ -91,7 +86,7 @@ class AffiliationFetcher {
void OnSimpleLoaderComplete(std::unique_ptr<std::string> response_body);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
const std::vector<FacetURI> requested_facet_uris_;
std::vector<FacetURI> requested_facet_uris_;
AffiliationFetcherDelegate* const delegate_;
std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
......
// Copyright 2020 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_ANDROID_AFFILIATION_AFFILIATION_FETCHER_INTERFACE_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_ANDROID_AFFILIATION_AFFILIATION_FETCHER_INTERFACE_H_
#include <vector>
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_delegate.h"
#include "components/password_manager/core/browser/android_affiliation/affiliation_utils.h"
namespace password_manager {
class AffiliationFetcherInterface {
public:
AffiliationFetcherInterface() = default;
virtual ~AffiliationFetcherInterface() = default;
// Not copyable or movable
AffiliationFetcherInterface(const AffiliationFetcherInterface&) = delete;
AffiliationFetcherInterface& operator=(const AffiliationFetcherInterface&) =
delete;
AffiliationFetcherInterface(AffiliationFetcherInterface&&) = delete;
AffiliationFetcherInterface& operator=(AffiliationFetcherInterface&&) =
delete;
// Starts the request to retrieve affiliations for each facet in
// |facet_uris|.
virtual void StartRequest(const std::vector<FacetURI>& facet_uris) = 0;
// Returns requested facet uris.
virtual const std::vector<FacetURI>& GetRequestedFacetURIs() const = 0;
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_ANDROID_AFFILIATION_AFFILIATION_FETCHER_INTERFACE_H_
......@@ -146,9 +146,9 @@ TEST_F(AffiliationFetcherTest, BasicReqestAndResponse) {
SetupSuccessfulResponse(test_response.SerializeAsString());
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchSucceededProxy());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), requested_uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(requested_uris);
WaitForResponse();
ASSERT_NO_FATAL_FAILURE(VerifyRequestPayload(requested_uris));
......@@ -185,9 +185,9 @@ TEST_F(AffiliationFetcherTest, AndroidBrandingInfoIsReturnedIfPresent) {
SetupSuccessfulResponse(test_response.SerializeAsString());
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchSucceededProxy());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), requested_uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(requested_uris);
WaitForResponse();
ASSERT_NO_FATAL_FAILURE(VerifyRequestPayload(requested_uris));
......@@ -216,9 +216,9 @@ TEST_F(AffiliationFetcherTest, MissingEquivalenceClassesAreCreated) {
SetupSuccessfulResponse(empty_test_response.SerializeAsString());
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchSucceededProxy());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), requested_uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(requested_uris);
WaitForResponse();
ASSERT_NO_FATAL_FAILURE(VerifyRequestPayload(requested_uris));
......@@ -247,9 +247,9 @@ TEST_F(AffiliationFetcherTest, DuplicateEquivalenceClassesAreIgnored) {
SetupSuccessfulResponse(test_response.SerializeAsString());
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchSucceededProxy());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), requested_uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(requested_uris);
WaitForResponse();
ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(&mock_delegate));
......@@ -275,9 +275,9 @@ TEST_F(AffiliationFetcherTest, EmptyEquivalenceClassesAreIgnored) {
SetupSuccessfulResponse(test_response.SerializeAsString());
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchSucceededProxy());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), requested_uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(requested_uris);
WaitForResponse();
ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(&mock_delegate));
......@@ -307,9 +307,9 @@ TEST_F(AffiliationFetcherTest, UnrecognizedFacetURIsAreIgnored) {
SetupSuccessfulResponse(test_response.SerializeAsString());
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchSucceededProxy());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), requested_uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(requested_uris);
WaitForResponse();
ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(&mock_delegate));
......@@ -331,9 +331,9 @@ TEST_F(AffiliationFetcherTest, FailureBecauseResponseIsNotAProtobuf) {
SetupSuccessfulResponse(kMalformedResponse);
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnMalformedResponse());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(uris);
WaitForResponse();
}
......@@ -355,9 +355,9 @@ TEST_F(AffiliationFetcherTest,
SetupSuccessfulResponse(test_response.SerializeAsString());
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnMalformedResponse());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(uris);
WaitForResponse();
}
......@@ -368,9 +368,9 @@ TEST_F(AffiliationFetcherTest, FailOnServerError) {
SetupServerErrorResponse();
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchFailed());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(uris);
WaitForResponse();
}
......@@ -381,9 +381,9 @@ TEST_F(AffiliationFetcherTest, FailOnNetworkError) {
SetupNetworkErrorResponse();
MockAffiliationFetcherDelegate mock_delegate;
EXPECT_CALL(mock_delegate, OnFetchFailed());
std::unique_ptr<AffiliationFetcher> fetcher(AffiliationFetcher::Create(
test_shared_loader_factory(), uris, &mock_delegate));
fetcher->StartRequest();
std::unique_ptr<AffiliationFetcher> fetcher(
AffiliationFetcher::Create(test_shared_loader_factory(), &mock_delegate));
fetcher->StartRequest(uris);
WaitForResponse();
}
......
......@@ -33,7 +33,7 @@ bool ScopedFakeAffiliationAPI::HasPendingRequest() {
std::vector<FacetURI> ScopedFakeAffiliationAPI::GetNextRequestedFacets() {
if (fake_fetcher_factory_.has_pending_fetchers())
return fake_fetcher_factory_.PeekNextFetcher()->requested_facet_uris();
return fake_fetcher_factory_.PeekNextFetcher()->GetRequestedFacetURIs();
return std::vector<FacetURI>();
}
......@@ -46,7 +46,7 @@ void ScopedFakeAffiliationAPI::ServeNextRequest() {
new AffiliationFetcherDelegate::Result);
for (const auto& preset_equivalence_class : preset_equivalence_relation_) {
bool had_intersection_with_request = false;
for (const auto& requested_facet_uri : fetcher->requested_facet_uris()) {
for (const auto& requested_facet_uri : fetcher->GetRequestedFacetURIs()) {
if (std::any_of(preset_equivalence_class.begin(),
preset_equivalence_class.end(),
[&requested_facet_uri](const Facet& facet) {
......
......@@ -11,9 +11,8 @@ namespace password_manager {
password_manager::FakeAffiliationFetcher::FakeAffiliationFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_ids,
AffiliationFetcherDelegate* delegate)
: AffiliationFetcher(std::move(url_loader_factory), facet_ids, delegate) {}
: AffiliationFetcher(std::move(url_loader_factory), delegate) {}
password_manager::FakeAffiliationFetcher::~FakeAffiliationFetcher() = default;
......@@ -50,10 +49,9 @@ FakeAffiliationFetcher* ScopedFakeAffiliationFetcherFactory::PeekNextFetcher() {
AffiliationFetcher* ScopedFakeAffiliationFetcherFactory::CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_ids,
AffiliationFetcherDelegate* delegate) {
FakeAffiliationFetcher* fetcher = new FakeAffiliationFetcher(
std::move(url_loader_factory), facet_ids, delegate);
FakeAffiliationFetcher* fetcher =
new FakeAffiliationFetcher(std::move(url_loader_factory), delegate);
pending_fetchers_.push(fetcher);
return fetcher;
}
......
......@@ -21,9 +21,8 @@ class FakeAffiliationFetcher : public AffiliationFetcher {
public:
FakeAffiliationFetcher(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_ids,
AffiliationFetcherDelegate* delegate);
~FakeAffiliationFetcher();
~FakeAffiliationFetcher() override;
// Simulates successful completion of the request with |fake_result|. Note
// that the consumer may choose to destroy |this| from within this call.
......@@ -66,7 +65,6 @@ class ScopedFakeAffiliationFetcherFactory
// AffiliationFetcherFactory:
AffiliationFetcher* CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_ids,
AffiliationFetcherDelegate* delegate) override;
private:
......
// Copyright 2020 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/android_affiliation/mock_affiliation_fetcher.h"
namespace password_manager {
MockAffiliationFetcher::MockAffiliationFetcher() = default;
MockAffiliationFetcher::~MockAffiliationFetcher() = default;
} // namespace password_manager
// Copyright 2020 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_ANDROID_AFFILIATION_MOCK_AFFILIATION_FETCHER_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_ANDROID_AFFILIATION_MOCK_AFFILIATION_FETCHER_H_
#include <string>
#include "components/password_manager/core/browser/android_affiliation/affiliation_fetcher_interface.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace password_manager {
class MockAffiliationFetcher : public AffiliationFetcherInterface {
public:
MockAffiliationFetcher();
~MockAffiliationFetcher() override;
MOCK_METHOD(void, StartRequest, (const std::vector<FacetURI>&), (override));
MOCK_METHOD(std::vector<FacetURI>&,
GetRequestedFacetURIs,
(),
(const, override));
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_ANDROID_AFFILIATION_MOCK_AFFILIATION_FETCHER_H_
......@@ -13,7 +13,6 @@ class SharedURLLoaderFactory;
namespace password_manager {
class FacetURI;
class AffiliationFetcherDelegate;
// Interface for a factory to be used by AffiliationFetcher::Create() in tests
......@@ -27,7 +26,6 @@ class TestAffiliationFetcherFactory {
// to the |delegate| on the same thread that creates the instance.
virtual AffiliationFetcher* CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::vector<FacetURI>& facet_ids,
AffiliationFetcherDelegate* delegate) = 0;
protected:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment