0

[pdf] Initialize resource bundle for pdf_unittests

Instead of using a `MockResourceBundleDelegate` for each test that needs
localization resources, load a resource bundle when initializing
pdf_unittests.

This is better because now we can test with the actual strings, which is
scalable if more tests require the resource bundle.

Also load the //ui/ test pak data, since it's needed for setting the
default font description on CrOS.

Fixed: 1184524
Change-Id: I9b7a8547003b52c22d33d00862c1a4c4bdcf3b64
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2990530
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Auto-Submit: Daniel Hosseinian <dhoss@chromium.org>
Cr-Commit-Position: refs/heads/master@{#896269}
This commit is contained in:
Daniel Hosseinian
2021-06-26 00:12:50 +00:00
committed by Chromium LUCI CQ
parent 0d8a9422d3
commit cced0fe459
4 changed files with 37 additions and 53 deletions

@ -8,6 +8,7 @@ import("//pdf/features.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("//testing/test.gni")
import("//third_party/pdfium/pdfium.gni")
import("//tools/grit/repack.gni")
import("//v8/gni/v8.gni")
# Generate a buildflag header for compile-time checking of PDF support.
@ -474,7 +475,6 @@ if (enable_pdf) {
"//base:i18n",
"//base/test:test_support",
"//cc:test_support",
"//components/strings",
"//gin",
"//mojo/core/embedder",
"//mojo/public/cpp/bindings",
@ -489,7 +489,7 @@ if (enable_pdf) {
"//third_party/blink/public:test_support",
"//third_party/blink/public/common:headers",
"//third_party/pdfium",
"//ui/base:test_support",
"//ui/base",
"//ui/gfx:test_support",
"//ui/gfx/geometry",
"//ui/gfx/range",
@ -502,6 +502,18 @@ if (enable_pdf) {
"//v8:external_startup_data",
]
}
data_deps = [
":pdf_unittests_pak",
"//ui/resources:ui_test_pak_data",
]
}
repack("pdf_unittests_pak") {
sources =
[ "$root_gen_dir/components/strings/components_strings_en-US.pak" ]
output = "$root_out_dir/pdf_tests_resources.pak"
deps = [ "//components/strings" ]
}
fuzzer_test("pdf_dates_fuzzer") {

@ -4,17 +4,23 @@
#include <memory>
#include "base/base_paths.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/task_environment.h"
#include "base/test/test_suite.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/public/cpp/bindings/binder_map.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
#include "third_party/blink/public/web/blink.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
#include "v8/include/v8.h"
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
@ -71,14 +77,31 @@ class PdfTestSuite final : public base::TestSuite {
mojo::BinderMap binders;
blink::Initialize(platform_.get(), &binders,
platform_->GetMainThreadScheduler());
InitializeResourceBundle();
}
void Shutdown() override {
platform_.reset();
ui::ResourceBundle::CleanupSharedInstance();
base::TestSuite::Shutdown();
}
private:
void InitializeResourceBundle() {
ui::RegisterPathProvider();
base::FilePath ui_test_pak_path;
ASSERT_TRUE(base::PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
base::FilePath pdf_tests_pak_path;
ASSERT_TRUE(base::PathService::Get(base::DIR_MODULE, &pdf_tests_pak_path));
pdf_tests_pak_path =
pdf_tests_pak_path.AppendASCII("pdf_tests_resources.pak");
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
pdf_tests_pak_path, ui::SCALE_FACTOR_NONE);
}
std::unique_ptr<BlinkPlatformForTesting> platform_;
};

@ -3,9 +3,3 @@ include_rules = [
"+third_party/icu/source/i18n/unicode/ulocdata.h",
"+ui/base/l10n/l10n_util.h",
]
specific_include_rules = {
"document_properties_unittest.cc": [
"+ui/base/resource",
],
}

@ -8,64 +8,22 @@
#include "base/i18n/number_formatting.h"
#include "base/i18n/rtl.h"
#include "components/strings/grit/components_strings.h"
#include "pdf/document_metadata.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/base/resource/mock_resource_bundle_delegate.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/geometry/size.h"
namespace chrome_pdf {
namespace {
using ::testing::Invoke;
using ::testing::IsEmpty;
using ::testing::NiceMock;
bool GetPageSizeString(int message_id, std::u16string* value) {
switch (message_id) {
case IDS_PDF_PROPERTIES_PAGE_SIZE_VALUE_INCH:
*value = u"$1 × $2 in ($3)";
break;
case IDS_PDF_PROPERTIES_PAGE_SIZE_VALUE_MM:
*value = u"$1 × $2 mm ($3)";
break;
case IDS_PDF_PROPERTIES_PAGE_SIZE_PORTRAIT:
*value = u"portrait";
break;
case IDS_PDF_PROPERTIES_PAGE_SIZE_LANDSCAPE:
*value = u"landscape";
break;
case IDS_PDF_PROPERTIES_PAGE_SIZE_VARIABLE:
*value = u"Varies";
break;
default:
return false;
}
return true;
}
class FormatPageSizeTest : public testing::Test {
protected:
void SetUp() override {
// TODO(crbug.com/1184524): Consider initializing a resource bundle instance
// for all tests in `pdf_unittests`.
// `pdf_unittests` does not have a resource bundle that needs to be restored
// at the end of the test.
ASSERT_FALSE(ui::ResourceBundle::HasSharedInstance());
ON_CALL(mock_resource_delegate_, GetLocalizedString)
.WillByDefault(Invoke(GetPageSizeString));
const std::string locale(GetLocale());
ui::ResourceBundle::InitSharedInstanceWithLocale(
locale, &mock_resource_delegate_,
ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
if (!locale.empty()) {
base::i18n::SetICUDefaultLocale(locale);
base::ResetFormattersForTesting();
@ -73,8 +31,6 @@ class FormatPageSizeTest : public testing::Test {
}
void TearDown() override {
ASSERT_TRUE(ui::ResourceBundle::HasSharedInstance());
ui::ResourceBundle::CleanupSharedInstance();
base::i18n::SetICUDefaultLocale(default_locale_);
base::ResetFormattersForTesting();
}
@ -83,7 +39,6 @@ class FormatPageSizeTest : public testing::Test {
private:
std::string default_locale_{base::i18n::GetConfiguredLocale()};
NiceMock<ui::MockResourceBundleDelegate> mock_resource_delegate_;
};
} // namespace