Commit 58fababa authored by K. Moon's avatar K. Moon Committed by Commit Bot

Add unit tests for chrome_pdf::BlinkUrlLoader

Adds test fixture and some simple tests for BlinkUrlLoader. This class
is still being implemented, so these tests mainly serve as examples.

Also implements BlinkUrlLoader::GrantUniversalAccess(), to make the
tests a little more interesting.

BlinkUrlLoader::Client was changed to provide a WebAssociatedURLLoader
instead of a WebLocalFrame. The former is easier to fake in unit tests.

Bug: 1099022
Change-Id: I66a8132373cf61ab539321151253a20bbf7801b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2391928
Commit-Queue: K. Moon <kmoon@chromium.org>
Reviewed-by: default avatarDaniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804292}
parent 00a69502
......@@ -309,6 +309,7 @@ if (enable_pdf) {
"pdfium/pdfium_test_base.cc",
"pdfium/pdfium_test_base.h",
"ppapi_migration/geometry_conversions_unittest.cc",
"ppapi_migration/url_loader_unittest.cc",
"range_set_unittest.cc",
"test/run_all_unittests.cc",
"thumbnail_unittest.cc",
......
......@@ -25,7 +25,6 @@
#include "ppapi/cpp/var.h"
#include "third_party/blink/public/web/web_associated_url_loader.h"
#include "third_party/blink/public/web/web_associated_url_loader_options.h"
#include "third_party/blink/public/web/web_local_frame.h"
namespace chrome_pdf {
......@@ -52,7 +51,8 @@ BlinkUrlLoader::BlinkUrlLoader(base::WeakPtr<Client> client)
BlinkUrlLoader::~BlinkUrlLoader() = default;
void BlinkUrlLoader::GrantUniversalAccess() {
NOTIMPLEMENTED();
DCHECK(!blink_loader_);
grant_universal_access_ = true;
}
// Modeled on `content::PepperURLLoaderHost::OnHostMsgOpen()`.
......@@ -62,16 +62,14 @@ void BlinkUrlLoader::Open(const UrlRequest& request, ResultCallback callback) {
return;
}
blink::WebLocalFrame* frame = client_->GetFrame();
if (!frame) {
blink::WebAssociatedURLLoaderOptions options;
options.grant_universal_access = grant_universal_access_;
blink_loader_ = client_->CreateAssociatedURLLoader(options);
if (!blink_loader_) {
std::move(callback).Run(PP_ERROR_FAILED);
return;
}
blink::WebAssociatedURLLoaderOptions options;
blink_loader_.reset(frame->CreateAssociatedURLLoader(options));
DCHECK(blink_loader_);
NOTIMPLEMENTED();
}
......
......@@ -21,7 +21,7 @@
namespace blink {
class WebAssociatedURLLoader;
class WebLocalFrame;
struct WebAssociatedURLLoaderOptions;
} // namespace blink
namespace chrome_pdf {
......@@ -117,9 +117,11 @@ class BlinkUrlLoader final : public UrlLoader,
// client.
class Client {
public:
// Returns the current local frame. May return `nullptr` if the local frame
// no longer exists.
virtual blink::WebLocalFrame* GetFrame() = 0;
// Returns a new `blink::WebAssociatedURLLoader` from the current local
// frame. May return `nullptr` if the local frame no longer exists.
virtual std::unique_ptr<blink::WebAssociatedURLLoader>
CreateAssociatedURLLoader(
const blink::WebAssociatedURLLoaderOptions& options) = 0;
protected:
~Client() = default;
......@@ -156,6 +158,7 @@ class BlinkUrlLoader final : public UrlLoader,
~BlinkUrlLoader() override;
base::WeakPtr<Client> client_;
bool grant_universal_access_ = false;
std::unique_ptr<blink::WebAssociatedURLLoader> blink_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.
#include "pdf/ppapi_migration/url_loader.h"
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/test/mock_callback.h"
#include "pdf/ppapi_migration/callback.h"
#include "ppapi/c/pp_errors.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/web/web_associated_url_loader.h"
#include "third_party/blink/public/web/web_associated_url_loader_client.h"
#include "third_party/blink/public/web/web_associated_url_loader_options.h"
namespace chrome_pdf {
namespace {
using ::testing::_;
using ::testing::Invoke;
using ::testing::NiceMock;
using ::testing::ReturnNull;
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 MockBlinkUrlLoaderClient : public BlinkUrlLoader::Client {
public:
base::WeakPtr<MockBlinkUrlLoaderClient> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void InvalidateWeakPtrs() { weak_factory_.InvalidateWeakPtrs(); }
// BlinkUrlLoader::Client:
MOCK_METHOD(std::unique_ptr<blink::WebAssociatedURLLoader>,
CreateAssociatedURLLoader,
(const blink::WebAssociatedURLLoaderOptions&),
(override));
private:
base::WeakPtrFactory<MockBlinkUrlLoaderClient> weak_factory_{this};
};
class BlinkUrlLoaderTest : public testing::Test {
protected:
BlinkUrlLoaderTest() {
ON_CALL(mock_client_, CreateAssociatedURLLoader(_))
.WillByDefault(
Invoke(this, &BlinkUrlLoaderTest::FakeCreateAssociatedURLLoader));
loader_ = base::MakeRefCounted<BlinkUrlLoader>(mock_client_.GetWeakPtr());
}
std::unique_ptr<blink::WebAssociatedURLLoader> FakeCreateAssociatedURLLoader(
const blink::WebAssociatedURLLoaderOptions& options) {
EXPECT_TRUE(mock_url_loader_);
saved_options_ = options;
return std::move(mock_url_loader_);
}
NiceMock<MockBlinkUrlLoaderClient> mock_client_;
base::MockCallback<ResultCallback> mock_callback_;
scoped_refptr<BlinkUrlLoader> loader_;
std::unique_ptr<MockWebAssociatedURLLoader> mock_url_loader_ =
std::make_unique<MockWebAssociatedURLLoader>();
blink::WebAssociatedURLLoaderOptions saved_options_;
};
TEST_F(BlinkUrlLoaderTest, GrantUniversalAccess) {
loader_->GrantUniversalAccess();
loader_->Open(UrlRequest(), mock_callback_.Get());
EXPECT_TRUE(saved_options_.grant_universal_access);
}
TEST_F(BlinkUrlLoaderTest, Open) {
EXPECT_CALL(mock_client_, CreateAssociatedURLLoader(_));
EXPECT_CALL(mock_callback_, Run(_)).Times(0);
loader_->Open(UrlRequest(), mock_callback_.Get());
EXPECT_FALSE(saved_options_.grant_universal_access);
}
TEST_F(BlinkUrlLoaderTest, OpenWithInvalidatedClient) {
EXPECT_CALL(mock_client_, CreateAssociatedURLLoader(_)).Times(0);
EXPECT_CALL(mock_callback_, Run(PP_ERROR_FAILED));
mock_client_.InvalidateWeakPtrs();
loader_->Open(UrlRequest(), mock_callback_.Get());
}
TEST_F(BlinkUrlLoaderTest, OpenWithFailingCreateAssociatedURLLoader) {
EXPECT_CALL(mock_client_, CreateAssociatedURLLoader(_))
.WillOnce(ReturnNull());
EXPECT_CALL(mock_callback_, Run(PP_ERROR_FAILED));
loader_->Open(UrlRequest(), mock_callback_.Get());
}
} // namespace
} // namespace chrome_pdf
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