Move PDF test classes out of findtext_unittest.cc. (try 2)
Make them available to other unit tests. This time, add build dependencies for the "pdf_test_utils" source_set. Change-Id: I75a1665c61e77032f218686f272e3788cb059474 Reviewed-on: https://chromium-review.googlesource.com/1246682 Reviewed-by: Henrique Nakashima <hnakashima@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org> Cr-Commit-Position: refs/heads/master@{#594412}
This commit is contained in:
18
pdf/BUILD.gn
18
pdf/BUILD.gn
@ -112,6 +112,21 @@ if (enable_pdf) {
|
||||
}
|
||||
}
|
||||
|
||||
source_set("pdf_test_utils") {
|
||||
testonly = true
|
||||
sources = [
|
||||
"test/test_client.cc",
|
||||
"test/test_client.h",
|
||||
"test/test_document_loader.cc",
|
||||
"test/test_document_loader.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
":pdf",
|
||||
"//base",
|
||||
]
|
||||
}
|
||||
|
||||
test("pdf_unittests") {
|
||||
testonly = true
|
||||
configs += [ ":pdf_common_config" ]
|
||||
@ -125,6 +140,7 @@ if (enable_pdf) {
|
||||
|
||||
deps = [
|
||||
":pdf",
|
||||
":pdf_test_utils",
|
||||
"//base",
|
||||
"//base/test:test_support",
|
||||
"//ppapi/c",
|
||||
@ -144,6 +160,8 @@ if (enable_pdf) {
|
||||
"pdfium/findtext_unittest.cc",
|
||||
"pdfium/pdfium_engine_exports_unittest.cc",
|
||||
"pdfium/pdfium_print_unittest.cc",
|
||||
"pdfium/pdfium_test_base.cc",
|
||||
"pdfium/pdfium_test_base.h",
|
||||
]
|
||||
|
||||
if (v8_use_external_startup_data) {
|
||||
|
@ -2,14 +2,11 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/optional.h"
|
||||
#include "base/path_service.h"
|
||||
#include "pdf/document_loader.h"
|
||||
#include "pdf/pdfium/pdfium_engine.h"
|
||||
#include "pdf/url_loader_wrapper.h"
|
||||
#include "pdf/pdfium/pdfium_test_base.h"
|
||||
#include "pdf/test/test_client.h"
|
||||
#include "testing/gmock/include/gmock/gmock.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
using testing::InSequence;
|
||||
using testing::_;
|
||||
@ -18,82 +15,15 @@ namespace chrome_pdf {
|
||||
|
||||
namespace {
|
||||
|
||||
class TestDocumentLoader : public DocumentLoader {
|
||||
class FindTextTestClient : public TestClient {
|
||||
public:
|
||||
TestDocumentLoader(Client* client, const base::FilePath::StringType& pdf_name)
|
||||
: client_(client) {
|
||||
base::FilePath pdf_path;
|
||||
CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &pdf_path));
|
||||
pdf_path = pdf_path.Append(FILE_PATH_LITERAL("pdf"))
|
||||
.Append(FILE_PATH_LITERAL("test"))
|
||||
.Append(FILE_PATH_LITERAL("data"))
|
||||
.Append(pdf_name);
|
||||
CHECK(base::ReadFileToString(pdf_path, &pdf_data_));
|
||||
}
|
||||
~TestDocumentLoader() override = default;
|
||||
|
||||
// DocumentLoader:
|
||||
bool Init(std::unique_ptr<URLLoaderWrapper> loader,
|
||||
const std::string& url) override {
|
||||
NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GetBlock(uint32_t position, uint32_t size, void* buf) const override {
|
||||
if (!IsDataAvailable(position, size))
|
||||
return false;
|
||||
|
||||
memcpy(buf, pdf_data_.data() + position, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsDataAvailable(uint32_t position, uint32_t size) const override {
|
||||
return position < pdf_data_.size() && size <= pdf_data_.size() &&
|
||||
position + size <= pdf_data_.size();
|
||||
}
|
||||
|
||||
void RequestData(uint32_t position, uint32_t size) override {
|
||||
client_->OnDocumentComplete();
|
||||
}
|
||||
|
||||
bool IsDocumentComplete() const override { return true; }
|
||||
|
||||
uint32_t GetDocumentSize() const override { return pdf_data_.size(); }
|
||||
|
||||
uint32_t BytesReceived() const override { return pdf_data_.size(); }
|
||||
|
||||
private:
|
||||
Client* const client_;
|
||||
std::string pdf_data_;
|
||||
};
|
||||
|
||||
const base::FilePath::CharType* g_test_pdf_name;
|
||||
|
||||
std::unique_ptr<DocumentLoader> CreateTestDocumentLoader(
|
||||
DocumentLoader::Client* client) {
|
||||
return std::make_unique<TestDocumentLoader>(client, g_test_pdf_name);
|
||||
}
|
||||
|
||||
class TestClient : public PDFEngine::Client {
|
||||
public:
|
||||
TestClient() = default;
|
||||
~TestClient() override = default;
|
||||
FindTextTestClient() = default;
|
||||
~FindTextTestClient() override = default;
|
||||
|
||||
// PDFEngine::Client:
|
||||
MOCK_METHOD2(NotifyNumberOfFindResultsChanged, void(int, bool));
|
||||
MOCK_METHOD1(NotifySelectedFindResultChanged, void((int)));
|
||||
|
||||
bool Confirm(const std::string& message) override { return false; }
|
||||
|
||||
std::string Prompt(const std::string& question,
|
||||
const std::string& default_answer) override {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string GetURL() override { return std::string(); }
|
||||
|
||||
pp::URLLoader CreateURLLoader() override { return pp::URLLoader(); }
|
||||
|
||||
std::vector<SearchStringResult> SearchString(const base::char16* string,
|
||||
const base::char16* term,
|
||||
bool case_sensitive) override {
|
||||
@ -118,59 +48,18 @@ class TestClient : public PDFEngine::Client {
|
||||
return results;
|
||||
}
|
||||
|
||||
pp::Instance* GetPluginInstance() override { return nullptr; }
|
||||
|
||||
bool IsPrintPreview() override { return false; }
|
||||
|
||||
uint32_t GetBackgroundColor() override { return 0; }
|
||||
|
||||
float GetToolbarHeightInScreenCoords() override { return 0; }
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(TestClient);
|
||||
DISALLOW_COPY_AND_ASSIGN(FindTextTestClient);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
class FindTextTest : public testing::Test {
|
||||
public:
|
||||
FindTextTest() = default;
|
||||
~FindTextTest() override = default;
|
||||
|
||||
protected:
|
||||
void SetUp() override {
|
||||
InitializePDFium();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
PDFiumEngine::SetCreateDocumentLoaderFunctionForTesting(nullptr);
|
||||
g_test_pdf_name = nullptr;
|
||||
FPDF_DestroyLibrary();
|
||||
}
|
||||
|
||||
void InitializePDFium() {
|
||||
FPDF_LIBRARY_CONFIG config;
|
||||
config.version = 2;
|
||||
config.m_pUserFontPaths = nullptr;
|
||||
config.m_pIsolate = nullptr;
|
||||
config.m_v8EmbedderSlot = 0;
|
||||
FPDF_InitLibraryWithConfig(&config);
|
||||
}
|
||||
|
||||
void SetDocumentForTest(const base::FilePath::CharType* test_pdf_name) {
|
||||
g_test_pdf_name = test_pdf_name;
|
||||
PDFiumEngine::SetCreateDocumentLoaderFunctionForTesting(
|
||||
&CreateTestDocumentLoader);
|
||||
}
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(FindTextTest);
|
||||
};
|
||||
using FindTextTest = PDFiumTestBase;
|
||||
|
||||
TEST_F(FindTextTest, FindText) {
|
||||
SetDocumentForTest(FILE_PATH_LITERAL("hello_world2.pdf"));
|
||||
pp::URLLoader dummy_loader;
|
||||
TestClient client;
|
||||
FindTextTestClient client;
|
||||
PDFiumEngine engine(&client, true);
|
||||
ASSERT_TRUE(engine.New("https://chromium.org/dummy.pdf", ""));
|
||||
ASSERT_TRUE(engine.HandleDocumentLoad(dummy_loader));
|
||||
@ -191,7 +80,7 @@ TEST_F(FindTextTest, FindText) {
|
||||
TEST_F(FindTextTest, FindHyphenatedText) {
|
||||
SetDocumentForTest(FILE_PATH_LITERAL("spanner.pdf"));
|
||||
pp::URLLoader dummy_loader;
|
||||
TestClient client;
|
||||
FindTextTestClient client;
|
||||
PDFiumEngine engine(&client, true);
|
||||
ASSERT_TRUE(engine.New("https://chromium.org/dummy.pdf", ""));
|
||||
ASSERT_TRUE(engine.HandleDocumentLoad(dummy_loader));
|
||||
@ -208,4 +97,5 @@ TEST_F(FindTextTest, FindHyphenatedText) {
|
||||
|
||||
engine.StartFind("application", /*case_sensitive=*/true);
|
||||
}
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
56
pdf/pdfium/pdfium_test_base.cc
Normal file
56
pdf/pdfium/pdfium_test_base.cc
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright 2018 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/pdfium/pdfium_test_base.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "pdf/pdfium/pdfium_engine.h"
|
||||
#include "pdf/test/test_document_loader.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
namespace {
|
||||
|
||||
const base::FilePath::CharType* g_test_pdf_name;
|
||||
|
||||
std::unique_ptr<DocumentLoader> CreateTestDocumentLoader(
|
||||
DocumentLoader::Client* client) {
|
||||
return std::make_unique<TestDocumentLoader>(client, g_test_pdf_name);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
PDFiumTestBase::PDFiumTestBase() = default;
|
||||
|
||||
PDFiumTestBase::~PDFiumTestBase() = default;
|
||||
|
||||
void PDFiumTestBase::SetUp() {
|
||||
InitializePDFium();
|
||||
}
|
||||
|
||||
void PDFiumTestBase::TearDown() {
|
||||
PDFiumEngine::SetCreateDocumentLoaderFunctionForTesting(nullptr);
|
||||
g_test_pdf_name = nullptr;
|
||||
FPDF_DestroyLibrary();
|
||||
}
|
||||
|
||||
void PDFiumTestBase::SetDocumentForTest(
|
||||
const base::FilePath::CharType* pdf_name) {
|
||||
DCHECK(!g_test_pdf_name);
|
||||
g_test_pdf_name = pdf_name;
|
||||
PDFiumEngine::SetCreateDocumentLoaderFunctionForTesting(
|
||||
&CreateTestDocumentLoader);
|
||||
}
|
||||
|
||||
void PDFiumTestBase::InitializePDFium() {
|
||||
FPDF_LIBRARY_CONFIG config;
|
||||
config.version = 2;
|
||||
config.m_pUserFontPaths = nullptr;
|
||||
config.m_pIsolate = nullptr;
|
||||
config.m_v8EmbedderSlot = 0;
|
||||
FPDF_InitLibraryWithConfig(&config);
|
||||
}
|
||||
|
||||
} // namespace chrome_pdf
|
36
pdf/pdfium/pdfium_test_base.h
Normal file
36
pdf/pdfium/pdfium_test_base.h
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright 2018 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_PDFIUM_PDFIUM_TEST_BASE_H_
|
||||
#define PDF_PDFIUM_PDFIUM_TEST_BASE_H_
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/macros.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
class PDFiumTestBase : public testing::Test {
|
||||
public:
|
||||
PDFiumTestBase();
|
||||
~PDFiumTestBase() override;
|
||||
|
||||
protected:
|
||||
// testing::Test:
|
||||
void SetUp() override;
|
||||
void TearDown() override;
|
||||
|
||||
// Sets the PDF to load for a test. This must be called for tests that use
|
||||
// TestDocumentLoader. See TestDocumentLoader for more info.
|
||||
void SetDocumentForTest(const base::FilePath::CharType* pdf_name);
|
||||
|
||||
private:
|
||||
void InitializePDFium();
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(PDFiumTestBase);
|
||||
};
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
||||
#endif // PDF_PDFIUM_PDFIUM_TEST_BASE_H_
|
46
pdf/test/test_client.cc
Normal file
46
pdf/test/test_client.cc
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2018 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/test_client.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
TestClient::TestClient() = default;
|
||||
|
||||
TestClient::~TestClient() = default;
|
||||
|
||||
bool TestClient::Confirm(const std::string& message) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string TestClient::Prompt(const std::string& question,
|
||||
const std::string& default_answer) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string TestClient::GetURL() {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
pp::URLLoader TestClient::CreateURLLoader() {
|
||||
return pp::URLLoader();
|
||||
}
|
||||
|
||||
pp::Instance* TestClient::GetPluginInstance() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool TestClient::IsPrintPreview() {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t TestClient::GetBackgroundColor() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float TestClient::GetToolbarHeightInScreenCoords() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace chrome_pdf
|
36
pdf/test/test_client.h
Normal file
36
pdf/test/test_client.h
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright 2018 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_TEST_CLIENT_H_
|
||||
#define PDF_TEST_TEST_CLIENT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "pdf/pdf_engine.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
class TestClient : public PDFEngine::Client {
|
||||
public:
|
||||
TestClient();
|
||||
~TestClient() override;
|
||||
|
||||
// PDFEngine::Client:
|
||||
bool Confirm(const std::string& message) override;
|
||||
std::string Prompt(const std::string& question,
|
||||
const std::string& default_answer) override;
|
||||
std::string GetURL() override;
|
||||
pp::URLLoader CreateURLLoader() override;
|
||||
pp::Instance* GetPluginInstance() override;
|
||||
bool IsPrintPreview() override;
|
||||
uint32_t GetBackgroundColor() override;
|
||||
float GetToolbarHeightInScreenCoords() override;
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(TestClient);
|
||||
};
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
||||
#endif // PDF_TEST_TEST_CLIENT_H_
|
68
pdf/test/test_document_loader.cc
Normal file
68
pdf/test/test_document_loader.cc
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright 2018 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/test_document_loader.h"
|
||||
|
||||
#include "base/base_paths.h"
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/path_service.h"
|
||||
#include "pdf/url_loader_wrapper.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
TestDocumentLoader::TestDocumentLoader(
|
||||
Client* client,
|
||||
const base::FilePath::StringType& pdf_name)
|
||||
: client_(client) {
|
||||
base::FilePath pdf_path;
|
||||
CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &pdf_path));
|
||||
pdf_path = pdf_path.Append(FILE_PATH_LITERAL("pdf"))
|
||||
.Append(FILE_PATH_LITERAL("test"))
|
||||
.Append(FILE_PATH_LITERAL("data"))
|
||||
.Append(pdf_name);
|
||||
CHECK(base::ReadFileToString(pdf_path, &pdf_data_));
|
||||
}
|
||||
|
||||
TestDocumentLoader::~TestDocumentLoader() = default;
|
||||
|
||||
bool TestDocumentLoader::Init(std::unique_ptr<URLLoaderWrapper> loader,
|
||||
const std::string& url) {
|
||||
NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TestDocumentLoader::GetBlock(uint32_t position,
|
||||
uint32_t size,
|
||||
void* buf) const {
|
||||
if (!IsDataAvailable(position, size))
|
||||
return false;
|
||||
|
||||
memcpy(buf, pdf_data_.data() + position, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TestDocumentLoader::IsDataAvailable(uint32_t position,
|
||||
uint32_t size) const {
|
||||
return position < pdf_data_.size() && size <= pdf_data_.size() &&
|
||||
position + size <= pdf_data_.size();
|
||||
}
|
||||
|
||||
void TestDocumentLoader::RequestData(uint32_t position, uint32_t size) {
|
||||
client_->OnDocumentComplete();
|
||||
}
|
||||
|
||||
bool TestDocumentLoader::IsDocumentComplete() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t TestDocumentLoader::GetDocumentSize() const {
|
||||
return pdf_data_.size();
|
||||
}
|
||||
|
||||
uint32_t TestDocumentLoader::BytesReceived() const {
|
||||
return pdf_data_.size();
|
||||
}
|
||||
|
||||
} // namespace chrome_pdf
|
41
pdf/test/test_document_loader.h
Normal file
41
pdf/test/test_document_loader.h
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2018 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_TEST_DOCUMENT_LOADER_H_
|
||||
#define PDF_TEST_TEST_DOCUMENT_LOADER_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "pdf/document_loader.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
// Loads test PDFs from pdf/test/data.
|
||||
class TestDocumentLoader : public DocumentLoader {
|
||||
public:
|
||||
// |pdf_name| is the base name for a PDF file.
|
||||
TestDocumentLoader(Client* client,
|
||||
const base::FilePath::StringType& pdf_name);
|
||||
~TestDocumentLoader() override;
|
||||
|
||||
// DocumentLoader:
|
||||
bool Init(std::unique_ptr<URLLoaderWrapper> loader,
|
||||
const std::string& url) override;
|
||||
bool GetBlock(uint32_t position, uint32_t size, void* buf) const override;
|
||||
bool IsDataAvailable(uint32_t position, uint32_t size) const override;
|
||||
void RequestData(uint32_t position, uint32_t size) override;
|
||||
bool IsDocumentComplete() const override;
|
||||
uint32_t GetDocumentSize() const override;
|
||||
uint32_t BytesReceived() const override;
|
||||
|
||||
private:
|
||||
Client* const client_;
|
||||
std::string pdf_data_;
|
||||
};
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
||||
#endif // PDF_TEST_TEST_DOCUMENT_LOADER_H_
|
Reference in New Issue
Block a user