1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_API_PRINTING_PRINTING_TEST_UTILS_H_
#define CHROME_BROWSER_EXTENSIONS_API_PRINTING_PRINTING_TEST_UTILS_H_
#include <memory>
#include <string>
#include "build/chromeos_buildflags.h"
#include "chrome/browser/printing/browser_printing_context_factory_for_test.h"
#include "printing/buildflags/buildflags.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "base/callback_list.h"
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(ENABLE_OOP_PRINTING)
#include "chrome/services/printing/public/mojom/print_backend_service.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#endif // BUILDFLAG(ENABLE_OOP_PRINTING)
class Profile;
#if BUILDFLAG(IS_CHROMEOS)
namespace ash {
class TestCupsPrintJobManager;
class FakeCupsPrintersManager;
} // namespace ash
#endif
namespace content {
class BrowserContext;
} // namespace content
namespace printing {
struct PrinterSemanticCapsAndDefaults;
class PrintBackendServiceTestImpl;
class TestPrintBackend;
} // namespace printing
namespace extensions {
class TestExtensionDir;
// Enum used to initialize the parameterized test with different types of
// extensions.
enum class ExtensionType {
kChromeApp,
kExtensionMV2,
kExtensionMV3,
};
// Manages various printing-related test infra classes. This class is supposed
// to be used on the main thread.
class PrintingBackendInfrastructureHelper {
public:
PrintingBackendInfrastructureHelper();
~PrintingBackendInfrastructureHelper();
printing::TestPrintBackend& test_print_backend() {
return *test_print_backend_;
}
printing::BrowserPrintingContextFactoryForTest&
test_printing_context_factory() {
return test_printing_context_factory_;
}
private:
#if BUILDFLAG(ENABLE_OOP_PRINTING)
mojo::Remote<printing::mojom::PrintBackendService> test_remote_;
std::unique_ptr<printing::PrintBackendServiceTestImpl> print_backend_service_;
#endif // BUILDFLAG(ENABLE_OOP_PRINTING)
scoped_refptr<printing::TestPrintBackend> test_print_backend_;
printing::BrowserPrintingContextFactoryForTest test_printing_context_factory_;
};
#if BUILDFLAG(IS_CHROMEOS)
class PrintingTestHelper {
public:
// BrowserContextDependencyManager subscriptions should be established before
// the profile becomes available; for this reason `Profile*` is not provided
// as a constructor parameter but rather passed in Init().
// Note that most methods of this class (other than the constructor) are
// supposed to be called from the main thread.
PrintingTestHelper();
~PrintingTestHelper();
// Does the necessary setup; intended to be used from SetUpOnMainThread().
void Init(Profile* profile);
// Adds a printer with the given `printer_id`, `printer_display_name` and
// `capabilities` to the printers manager and the test backend.
void AddAvailablePrinter(
const std::string& printer_id,
const std::string& printer_display_name,
std::unique_ptr<printing::PrinterSemanticCapsAndDefaults> capabilities);
PrintingBackendInfrastructureHelper& printing_infra_helper() {
return *printing_infra_helper_;
}
private:
// Creates test factories for ash::TestCupsPrintJobManager and
// ash::FakeCupsPrintersManager.
void SetUpBrowserContextKeyedServices(content::BrowserContext* context);
raw_ptr<Profile> profile_ = nullptr;
base::CallbackListSubscription create_services_subscription_;
std::unique_ptr<PrintingBackendInfrastructureHelper> printing_infra_helper_;
};
#endif
// Creates a printing extension with the correct manifest for the given `type`.
std::unique_ptr<TestExtensionDir> CreatePrintingExtension(ExtensionType type);
// Constructs a printer with some predefined capabilities.
std::unique_ptr<printing::PrinterSemanticCapsAndDefaults>
ConstructPrinterCapabilities();
// Constructs a response to LocalPrinter::GetPrinters() with a single printer.
std::vector<crosapi::mojom::LocalDestinationInfoPtr>
ConstructGetPrintersResponse(const std::string& printer_id,
const std::string& printer_name);
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_PRINTING_PRINTING_TEST_UTILS_H_
|