0

[unseasoned-pdf] Set up the unit test framework for PdfViewWebPlugin.

Set up the unit test framework for PdfViewWebPlugin and add a test for
PdfViewWebPlugin::UpdateGeometry().

Bug: 1199558
Change-Id: I2f1fe0e5fa0ba727000bc642588b30dca9312052
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2847989
Reviewed-by: K. Moon <kmoon@chromium.org>
Commit-Queue: Hui Yingst <nigi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#877265}
This commit is contained in:
Hui Yingst
2021-04-28 22:14:18 +00:00
committed by Chromium LUCI CQ
parent f9a2add6c4
commit d89a9c5da1
4 changed files with 121 additions and 0 deletions

@ -381,6 +381,7 @@ if (enable_pdf) {
"pdf_transform_unittest.cc",
"pdf_utils/dates_unittest.cc",
"pdf_view_plugin_base_unittest.cc",
"pdf_view_web_plugin_unittest.cc",
"pdfium/accessibility_unittest.cc",
"pdfium/findtext_unittest.cc",
"pdfium/pdfium_engine_exports_unittest.cc",

@ -145,6 +145,11 @@ bool PdfViewWebPlugin::Initialize(blink::WebPluginContainer* container) {
return InitializeCommon(std::make_unique<BlinkContainerWrapper>(container));
}
bool PdfViewWebPlugin::InitializeForTesting(
std::unique_ptr<ContainerWrapper> container_wrapper) {
return InitializeCommon(std::move(container_wrapper));
}
// Modeled on `OutOfProcessInstance::Init()`.
bool PdfViewWebPlugin::InitializeCommon(
std::unique_ptr<ContainerWrapper> container_wrapper) {

@ -118,6 +118,12 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
// SkiaGraphics::Client:
void UpdateSnapshot(sk_sp<SkImage> snapshot) override;
// Initializes the plugin using the `container_wrapper` provided by tests.
bool InitializeForTesting(
std::unique_ptr<ContainerWrapper> container_wrapper);
const gfx::Rect& GetPluginRectForTesting() const { return plugin_rect(); }
protected:
// PdfViewPluginBase:
base::WeakPtr<PdfViewPluginBase> GetWeakPtr() override;

@ -0,0 +1,109 @@
// Copyright 2021 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/pdf_view_web_plugin.h"
#include <memory>
#include <utility>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_plugin_params.h"
#include "ui/gfx/geometry/rect.h"
namespace chrome_pdf {
namespace {
class FakeContainerWrapper final : public PdfViewWebPlugin::ContainerWrapper {
public:
FakeContainerWrapper() = default;
FakeContainerWrapper(const FakeContainerWrapper&) = delete;
FakeContainerWrapper& operator=(const FakeContainerWrapper&) = delete;
~FakeContainerWrapper() override = default;
// PdfViewWebPlugin::ContainerWrapper:
void Invalidate() override {}
float DeviceScaleFactor() const override { return device_scale_; }
blink::WebPluginContainer* Container() override { return nullptr; }
void set_device_scale(float device_scale) { device_scale_ = device_scale; }
private:
float device_scale_ = 1.0f;
};
} // namespace
class PdfViewWebPluginTest : public testing::Test {
public:
// Custom deleter for `plugin_`. PdfViewWebPlugin must be destroyed by
// PdfViewWebPlugin::Destroy() instead of its destructor.
struct PluginDeleter {
void operator()(PdfViewWebPlugin* ptr) { ptr->Destroy(); }
};
PdfViewWebPluginTest() = default;
PdfViewWebPluginTest(const PdfViewWebPluginTest&) = delete;
PdfViewWebPluginTest& operator=(const PdfViewWebPluginTest&) = delete;
~PdfViewWebPluginTest() override = default;
void SetUp() override {
plugin_ = std::unique_ptr<PdfViewWebPlugin, PluginDeleter>(
new PdfViewWebPlugin(blink::WebPluginParams()));
auto wrapper = std::make_unique<FakeContainerWrapper>();
wrapper_ptr_ = wrapper.get();
plugin_->InitializeForTesting(std::move(wrapper));
}
void TearDown() override {
plugin_.reset();
wrapper_ptr_ = nullptr;
}
void TestUpdateGeometrySetsPluginRect(float device_scale,
const gfx::Rect& window_rect,
const gfx::Rect& expected_plugin_rect) {
// The plugin container's device scale must be set before calling
// UpdateGeometry().
ASSERT_TRUE(wrapper_ptr_);
wrapper_ptr_->set_device_scale(device_scale);
plugin_->UpdateGeometry(window_rect, window_rect, window_rect,
/*is_visible=*/true);
EXPECT_EQ(expected_plugin_rect, plugin_->GetPluginRectForTesting())
<< "Failure at device scale of " << device_scale << ", window rect of "
<< window_rect.ToString();
}
FakeContainerWrapper* wrapper_ptr_;
std::unique_ptr<PdfViewWebPlugin, PluginDeleter> plugin_;
};
TEST_F(PdfViewWebPluginTest, UpdateGeometrySetsPluginRect) {
struct UpdateGeometryParams {
// The plugin container's device scale.
float device_scale;
// The window area.
gfx::Rect window_rect;
// The expected plugin rect.
gfx::Rect expected_plugin_rect;
};
static constexpr UpdateGeometryParams kUpdateGeometryParams[] = {
{1.0f, gfx::Rect(3, 4, 5, 6), gfx::Rect(3, 4, 5, 6)},
{2.0f, gfx::Rect(5, 6, 7, 8), gfx::Rect(10, 12, 14, 16)},
};
for (const auto& params : kUpdateGeometryParams) {
TestUpdateGeometrySetsPluginRect(params.device_scale, params.window_rect,
params.expected_plugin_rect);
}
}
} // namespace chrome_pdf