0

[pdf] Share MockWebAssociatedURLLoader

Consolidates blink::WebAssociatedURLLoader mocks into a single
implementation.

Bug: 1056817
Change-Id: I86f7e779f5b7feda82e7fe78c7776e5c8961bc21
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3709666
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: K. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1015601}
This commit is contained in:
K. Moon
2022-06-18 00:15:52 +00:00
committed by Chromium LUCI CQ
parent 4d785d27eb
commit 074b539cc1
6 changed files with 66 additions and 45 deletions

@ -275,11 +275,13 @@ if (enable_pdf) {
}
source_set("pdf_test_utils") {
visibility = [ ":*" ]
visibility = [ "//pdf/*" ]
testonly = true
sources = [
"test/mock_web_associated_url_loader.cc",
"test/mock_web_associated_url_loader.h",
"test/test_client.cc",
"test/test_client.h",
"test/test_document_loader.cc",
@ -297,7 +299,7 @@ if (enable_pdf) {
"//pdf/loader",
"//testing/gmock",
"//testing/gtest",
"//third_party/blink/public/common:headers",
"//third_party/blink/public:blink",
"//ui/gfx/range",
"//ui/latency:latency",
]

@ -71,6 +71,7 @@ source_set("unit_tests") {
"//base/test:test_support",
"//net",
"//pdf:features",
"//pdf:pdf_test_utils",
"//testing/gmock",
"//testing/gtest",
"//third_party/blink/public:blink",

@ -23,6 +23,7 @@
#include "net/base/net_errors.h"
#include "net/cookies/site_for_cookies.h"
#include "pdf/loader/result_codes.h"
#include "pdf/test/mock_web_associated_url_loader.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-shared.h"
@ -74,22 +75,6 @@ blink::WebURLError MakeWebURLError(int reason) {
return blink::WebURLError(reason, GURL());
}
class MockWebAssociatedURLLoader : public blink::WebAssociatedURLLoader {
public:
// blink::WebAssociatedURLLoader:
MOCK_METHOD(void,
LoadAsynchronously,
(const blink::WebURLRequest&,
blink::WebAssociatedURLLoaderClient*),
(override));
MOCK_METHOD(void, Cancel, (), (override));
MOCK_METHOD(void, SetDefersLoading, (bool), (override));
MOCK_METHOD(void,
SetLoadingTaskRunner,
(base::SingleThreadTaskRunner*),
(override));
};
class FakeUrlLoaderClient : public UrlLoader::Client {
public:
base::WeakPtr<FakeUrlLoaderClient> GetWeakPtr() {

@ -36,6 +36,7 @@
#include "pdf/paint_ready_rect.h"
#include "pdf/pdf_accessibility_data_handler.h"
#include "pdf/pdf_view_plugin_base.h"
#include "pdf/test/mock_web_associated_url_loader.h"
#include "pdf/test/test_helpers.h"
#include "pdf/test/test_pdfium_engine.h"
#include "printing/metafile_skia.h"
@ -174,32 +175,6 @@ class MockHeaderVisitor : public blink::WebHTTPHeaderVisitor {
(override));
};
class MockWebAssociatedURLLoader : public blink::WebAssociatedURLLoader {
public:
MockWebAssociatedURLLoader() {
ON_CALL(*this, LoadAsynchronously)
.WillByDefault([](const blink::WebURLRequest& /*request*/,
blink::WebAssociatedURLLoaderClient* client) {
// TODO(crbug.com/1322928): Must trigger callback to free `UrlLoader`.
client->DidReceiveResponse(blink::WebURLResponse());
client->DidFinishLoading();
});
}
// blink::WebAssociatedURLLoader:
MOCK_METHOD(void,
LoadAsynchronously,
(const blink::WebURLRequest&,
blink::WebAssociatedURLLoaderClient*),
(override));
MOCK_METHOD(void, Cancel, (), (override));
MOCK_METHOD(void, SetDefersLoading, (bool), (override));
MOCK_METHOD(void,
SetLoadingTaskRunner,
(base::SingleThreadTaskRunner*),
(override));
};
class MockPdfAccessibilityDataHandler : public PdfAccessibilityDataHandler {
public:
// PdfAccessibilityDataHandler:
@ -224,7 +199,17 @@ class FakePdfViewWebPluginClient : public PdfViewWebPlugin::Client {
public:
FakePdfViewWebPluginClient() {
ON_CALL(*this, CreateAssociatedURLLoader).WillByDefault([]() {
return std::make_unique<NiceMock<MockWebAssociatedURLLoader>>();
auto associated_loader =
std::make_unique<NiceMock<MockWebAssociatedURLLoader>>();
ON_CALL(*associated_loader, LoadAsynchronously)
.WillByDefault([](const blink::WebURLRequest& /*request*/,
blink::WebAssociatedURLLoaderClient* client) {
// TODO(crbug.com/1322928): Must trigger callback to free
// `UrlLoader`.
client->DidReceiveResponse(blink::WebURLResponse());
client->DidFinishLoading();
});
return associated_loader;
});
ON_CALL(*this, GetEmbedderOriginString)
.WillByDefault(

@ -0,0 +1,12 @@
// Copyright 2022 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 "pdf/test/mock_web_associated_url_loader.h"
namespace chrome_pdf {
MockWebAssociatedURLLoader::MockWebAssociatedURLLoader() = default;
MockWebAssociatedURLLoader::~MockWebAssociatedURLLoader() = default;
} // namespace chrome_pdf

@ -0,0 +1,36 @@
// Copyright 2022 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 PDF_TEST_MOCK_WEB_ASSOCIATED_URL_LOADER_H_
#define PDF_TEST_MOCK_WEB_ASSOCIATED_URL_LOADER_H_
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/web/web_associated_url_loader.h"
namespace chrome_pdf {
class MockWebAssociatedURLLoader : public blink::WebAssociatedURLLoader {
public:
MockWebAssociatedURLLoader();
MockWebAssociatedURLLoader(const MockWebAssociatedURLLoader&) = delete;
MockWebAssociatedURLLoader& operator=(const MockWebAssociatedURLLoader&) =
delete;
~MockWebAssociatedURLLoader() override;
MOCK_METHOD(void,
LoadAsynchronously,
(const blink::WebURLRequest&,
blink::WebAssociatedURLLoaderClient*),
(override));
MOCK_METHOD(void, Cancel, (), (override));
MOCK_METHOD(void, SetDefersLoading, (bool), (override));
MOCK_METHOD(void,
SetLoadingTaskRunner,
(base::SingleThreadTaskRunner*),
(override));
};
} // namespace chrome_pdf
#endif // PDF_TEST_MOCK_WEB_ASSOCIATED_URL_LOADER_H_