Commit 4a0132fa authored by Alan Screen's avatar Alan Screen Committed by Chromium LUCI CQ

Improve PrintBackendServiceTestImpl usability

Move PrintBackendServiceTestImpl into its own files instead of being
bundled with PrintBackendBrowserTest.  This will enable it to be reused
by other forthcoming tests.

Add helper functions for launching the service.

Add ability to register a test service to be used in place of launching
out-of-process instances on-demand by regular production code.

This has no change to existing test coverage or behavior.

Bug: 809738
Change-Id: I74c596b3284e69d6eabd525eb77e5f6e8f8b0c05
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2631326Reviewed-by: default avatarDaniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: Alan Screen <awscreen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846393}
parent c70f317d
...@@ -5,5 +5,6 @@ include_rules = [ ...@@ -5,5 +5,6 @@ include_rules = [
specific_include_rules = { specific_include_rules = {
"print_backend_browsertest.cc": [ "print_backend_browsertest.cc": [
"+chrome/services/printing/print_backend_service_impl.h", "+chrome/services/printing/print_backend_service_impl.h",
"+chrome/services/printing/print_backend_service_test_impl.h",
] ]
} }
...@@ -14,12 +14,12 @@ ...@@ -14,12 +14,12 @@
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "chrome/services/printing/print_backend_service_impl.h" #include "chrome/services/printing/print_backend_service_test_impl.h"
#include "chrome/services/printing/public/mojom/print_backend_service.mojom.h" #include "chrome/services/printing/public/mojom/print_backend_service.mojom.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "mojo/public/cpp/bindings/pending_receiver.h" #include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "printing/backend/print_backend.h" #include "printing/backend/print_backend.h"
#include "printing/backend/test_print_backend.h" #include "printing/backend/test_print_backend.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -35,50 +35,20 @@ constexpr int32_t kCopiesMax = 123; ...@@ -35,50 +35,20 @@ constexpr int32_t kCopiesMax = 123;
} // namespace } // namespace
// PrintBackendServiceTestImpl uses a TestPrintBackend to enable testing of the
// PrintBackendService without relying upon the presence of real printer
// drivers.
class PrintBackendServiceTestImpl : public PrintBackendServiceImpl {
public:
explicit PrintBackendServiceTestImpl(
mojo::PendingReceiver<mojom::PrintBackendService> receiver)
: PrintBackendServiceImpl(std::move(receiver)) {}
PrintBackendServiceTestImpl(const PrintBackendServiceTestImpl&) = delete;
PrintBackendServiceTestImpl& operator=(const PrintBackendServiceTestImpl&) =
delete;
~PrintBackendServiceTestImpl() override = default;
// Overrides which need special handling for using `test_print_backend_`.
void Init(const std::string& locale) override {
test_print_backend_ = base::MakeRefCounted<TestPrintBackend>();
print_backend_ = test_print_backend_;
}
private:
friend class PrintBackendBrowserTest;
scoped_refptr<TestPrintBackend> test_print_backend_;
};
class PrintBackendBrowserTest : public InProcessBrowserTest { class PrintBackendBrowserTest : public InProcessBrowserTest {
public: public:
PrintBackendBrowserTest() = default; PrintBackendBrowserTest() = default;
~PrintBackendBrowserTest() override = default; ~PrintBackendBrowserTest() override = default;
void PreRunTestOnMainThread() override { void LaunchUninitialized() {
InProcessBrowserTest::PreRunTestOnMainThread();
// Launch the service, and bind the testing interface to it.
mojo::PendingReceiver<mojom::PrintBackendService> receiver =
mojo::PendingRemote<mojom::PrintBackendService>()
.InitWithNewPipeAndPassReceiver();
print_backend_service_ = print_backend_service_ =
std::make_unique<PrintBackendServiceTestImpl>(std::move(receiver)); PrintBackendServiceTestImpl::LaunchUninitialized(remote_);
} }
// Initialize and load the backend service with some test print drivers. // Initialize and load the backend service with some test print drivers.
void DoInitAndSetupTestData() { void DoInitAndSetupTestData() {
print_backend_service_->Init(/*locale=*/""); print_backend_service_ = PrintBackendServiceTestImpl::LaunchForTesting(
remote_, test_print_backend_);
auto printer_info = std::make_unique<PrinterBasicInfo>( auto printer_info = std::make_unique<PrinterBasicInfo>(
/*printer_name=*/kDefaultPrinterName, /*printer_name=*/kDefaultPrinterName,
...@@ -91,7 +61,7 @@ class PrintBackendBrowserTest : public InProcessBrowserTest { ...@@ -91,7 +61,7 @@ class PrintBackendBrowserTest : public InProcessBrowserTest {
// tests. // tests.
auto default_caps = std::make_unique<PrinterSemanticCapsAndDefaults>(); auto default_caps = std::make_unique<PrinterSemanticCapsAndDefaults>();
default_caps->copies_max = kCopiesMax; default_caps->copies_max = kCopiesMax;
print_backend_service_->test_print_backend_->AddValidPrinter( test_print_backend_->AddValidPrinter(
kDefaultPrinterName, std::move(default_caps), std::move(printer_info)); kDefaultPrinterName, std::move(default_caps), std::move(printer_info));
} }
...@@ -155,6 +125,9 @@ class PrintBackendBrowserTest : public InProcessBrowserTest { ...@@ -155,6 +125,9 @@ class PrintBackendBrowserTest : public InProcessBrowserTest {
bool received_message_ = false; bool received_message_ = false;
base::OnceClosure quit_callback_; base::OnceClosure quit_callback_;
mojo::Remote<mojom::PrintBackendService> remote_;
scoped_refptr<TestPrintBackend> test_print_backend_ =
base::MakeRefCounted<TestPrintBackend>();
std::unique_ptr<PrintBackendServiceTestImpl> print_backend_service_; std::unique_ptr<PrintBackendServiceTestImpl> print_backend_service_;
}; };
...@@ -165,6 +138,9 @@ IN_PROC_BROWSER_TEST_F(PrintBackendBrowserTest, FailWithoutInit) { ...@@ -165,6 +138,9 @@ IN_PROC_BROWSER_TEST_F(PrintBackendBrowserTest, FailWithoutInit) {
base::Optional<std::string> default_printer_name; base::Optional<std::string> default_printer_name;
base::Optional<PrinterSemanticCapsAndDefaults> printer_caps; base::Optional<PrinterSemanticCapsAndDefaults> printer_caps;
// Launch the service, but without initializing to desired locale.
LaunchUninitialized();
// Safe to use base::Unretained(this) since waiting locally on the callback // Safe to use base::Unretained(this) since waiting locally on the callback
// forces a shorter lifetime than `this`. // forces a shorter lifetime than `this`.
GetPrintBackendService()->GetDefaultPrinterName( GetPrintBackendService()->GetDefaultPrinterName(
......
...@@ -21,6 +21,10 @@ namespace { ...@@ -21,6 +21,10 @@ namespace {
constexpr base::TimeDelta kResetOnIdleTimeout = constexpr base::TimeDelta kResetOnIdleTimeout =
base::TimeDelta::FromSeconds(20); base::TimeDelta::FromSeconds(20);
// `PrintBackendService` override for testing.
mojo::Remote<printing::mojom::PrintBackendService>*
g_print_backend_service_for_test = nullptr;
} // namespace } // namespace
const mojo::Remote<printing::mojom::PrintBackendService>& const mojo::Remote<printing::mojom::PrintBackendService>&
...@@ -29,6 +33,10 @@ GetPrintBackendService(const std::string& locale, ...@@ -29,6 +33,10 @@ GetPrintBackendService(const std::string& locale,
static base::NoDestructor<base::flat_map< static base::NoDestructor<base::flat_map<
std::string, mojo::Remote<printing::mojom::PrintBackendService>>> std::string, mojo::Remote<printing::mojom::PrintBackendService>>>
remotes; remotes;
if (g_print_backend_service_for_test)
return *g_print_backend_service_for_test;
std::string remote_id; std::string remote_id;
#if defined(OS_WIN) #if defined(OS_WIN)
// Windows drivers are not thread safe. Use a process per driver to prevent // Windows drivers are not thread safe. Use a process per driver to prevent
...@@ -74,3 +82,8 @@ GetPrintBackendService(const std::string& locale, ...@@ -74,3 +82,8 @@ GetPrintBackendService(const std::string& locale,
return service; return service;
} }
void SetPrintBackendServiceForTesting(
mojo::Remote<printing::mojom::PrintBackendService>* remote) {
g_print_backend_service_for_test = remote;
}
...@@ -16,4 +16,9 @@ const mojo::Remote<printing::mojom::PrintBackendService>& ...@@ -16,4 +16,9 @@ const mojo::Remote<printing::mojom::PrintBackendService>&
GetPrintBackendService(const std::string& locale, GetPrintBackendService(const std::string& locale,
const std::string& printer_name); const std::string& printer_name);
// Test support to override the print backend service for testing. Caller
// retains ownership of `remote`.
void SetPrintBackendServiceForTesting(
mojo::Remote<printing::mojom::PrintBackendService>* remote);
#endif // CHROME_BROWSER_PRINTING_PRINT_BACKEND_SERVICE_H_ #endif // CHROME_BROWSER_PRINTING_PRINT_BACKEND_SERVICE_H_
...@@ -3,3 +3,9 @@ include_rules = [ ...@@ -3,3 +3,9 @@ include_rules = [
"+components/pwg_encoder", "+components/pwg_encoder",
"+pdf/pdf.h", "+pdf/pdf.h",
] ]
specific_include_rules = {
"print_backend_service_test_impl.cc": [
"+chrome/browser/printing/print_backend_service.h",
]
}
// 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 "chrome/services/printing/print_backend_service_test_impl.h"
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "chrome/browser/printing/print_backend_service.h"
#include "printing/backend/test_print_backend.h"
namespace printing {
PrintBackendServiceTestImpl::PrintBackendServiceTestImpl(
mojo::PendingReceiver<mojom::PrintBackendService> receiver)
: PrintBackendServiceImpl(std::move(receiver)) {}
PrintBackendServiceTestImpl::~PrintBackendServiceTestImpl() = default;
void PrintBackendServiceTestImpl::Init(const std::string& locale) {
DCHECK(test_print_backend_);
print_backend_ = test_print_backend_;
}
// static
std::unique_ptr<PrintBackendServiceTestImpl>
PrintBackendServiceTestImpl::LaunchUninitialized(
mojo::Remote<mojom::PrintBackendService>& remote) {
// Launch the service running locally in-process.
mojo::PendingReceiver<mojom::PrintBackendService> receiver =
remote.BindNewPipeAndPassReceiver();
return std::make_unique<PrintBackendServiceTestImpl>(std::move(receiver));
}
// static
std::unique_ptr<PrintBackendServiceTestImpl>
PrintBackendServiceTestImpl::LaunchForTesting(
mojo::Remote<mojom::PrintBackendService>& remote,
scoped_refptr<TestPrintBackend> backend) {
std::unique_ptr<PrintBackendServiceTestImpl> service =
LaunchUninitialized(remote);
// Do the common initialization using the testing print backend.
service->test_print_backend_ = backend;
service->Init(/*locale=*/std::string());
// Register this test version of print backend service to be used instead of
// launching instances out-of-process on-demand.
SetPrintBackendServiceForTesting(&remote);
return service;
}
} // namespace printing
// 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.
#ifndef CHROME_SERVICES_PRINTING_PRINT_BACKEND_SERVICE_TEST_IMPL_H_
#define CHROME_SERVICES_PRINTING_PRINT_BACKEND_SERVICE_TEST_IMPL_H_
#include <memory>
#include <string>
#include "base/memory/scoped_refptr.h"
#include "chrome/services/printing/print_backend_service_impl.h"
#include "chrome/services/printing/public/mojom/print_backend_service.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "printing/backend/test_print_backend.h"
namespace printing {
// `PrintBackendServiceTestImpl` uses a `TestPrintBackend` to enable testing
// of the `PrintBackendService` without relying upon the presence of real
// printer drivers.
class PrintBackendServiceTestImpl : public PrintBackendServiceImpl {
public:
explicit PrintBackendServiceTestImpl(
mojo::PendingReceiver<mojom::PrintBackendService> receiver);
PrintBackendServiceTestImpl(const PrintBackendServiceTestImpl&) = delete;
PrintBackendServiceTestImpl& operator=(const PrintBackendServiceTestImpl&) =
delete;
~PrintBackendServiceTestImpl() override;
// Overrides which need special handling for using `test_print_backend_`.
void Init(const std::string& locale) override;
// Launch the service in-process for testing using the provided backend.
static std::unique_ptr<PrintBackendServiceTestImpl> LaunchForTesting(
mojo::Remote<mojom::PrintBackendService>& remote,
scoped_refptr<TestPrintBackend> backend);
private:
friend class PrintBackendBrowserTest;
// Launch the service in-process for testing without initializing backend.
static std::unique_ptr<PrintBackendServiceTestImpl> LaunchUninitialized(
mojo::Remote<mojom::PrintBackendService>& remote);
scoped_refptr<TestPrintBackend> test_print_backend_;
};
} // namespace printing
#endif // CHROME_SERVICES_PRINTING_PRINT_BACKEND_SERVICE_TEST_IMPL_H_
...@@ -3087,7 +3087,11 @@ if (!is_android) { ...@@ -3087,7 +3087,11 @@ if (!is_android) {
} }
if (enable_basic_printing && if (enable_basic_printing &&
(is_win || is_mac || is_linux || is_chromeos)) { (is_win || is_mac || is_linux || is_chromeos)) {
sources += [ "../browser/printing/print_backend_browsertest.cc" ] sources += [
"../browser/printing/print_backend_browsertest.cc",
"../services/printing/print_backend_service_test_impl.cc",
"../services/printing/print_backend_service_test_impl.h",
]
deps += [ deps += [
"//chrome/services/printing:lib", "//chrome/services/printing:lib",
"//printing:test_support", "//printing:test_support",
......
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