1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef PRINTING_TEST_PRINTING_CONTEXT_H_
6 #define PRINTING_TEST_PRINTING_CONTEXT_H_
11 #include "base/containers/flat_map.h"
12 #include "base/functional/callback_forward.h"
13 #include "build/build_config.h"
14 #include "printing/mojom/print.mojom.h"
15 #include "printing/print_settings.h"
16 #include "printing/printing_context.h"
19 #include "third_party/abseil-cpp/absl/types/optional.h"
24 class TestPrintingContextDelegate : public PrintingContext::Delegate {
26 TestPrintingContextDelegate();
27 TestPrintingContextDelegate(const TestPrintingContextDelegate&) = delete;
28 TestPrintingContextDelegate& operator=(const TestPrintingContextDelegate&) =
30 ~TestPrintingContextDelegate() override;
32 // PrintingContext::Delegate overrides:
33 gfx::NativeView GetParentView() override;
34 std::string GetAppLocale() override;
37 // Note that TestPrintingContext uses a PrintBackend internally, so tests that
38 // want to avoid using a real PrintingContext will also want to use
39 // PrintBackend::SetPrintBackendForTesting() to avoid using a real PrintBackend.
40 class TestPrintingContext : public PrintingContext {
42 using OnNewDocumentCallback = base::RepeatingCallback<void(
44 bool destination_is_preview,
46 const PrintSettings&)>;
48 TestPrintingContext(Delegate* delegate, bool skip_system_calls);
49 TestPrintingContext(const TestPrintingContext&) = delete;
50 TestPrintingContext& operator=(const TestPrintingContext&) = delete;
51 ~TestPrintingContext() override;
53 // Methods for test setup:
55 // Provide settings that will be used as the current settings for the
57 void SetDeviceSettings(const std::string& device_name,
58 std::unique_ptr<PrintSettings> settings);
60 // Provide the settings which should be applied to mimic a user's choices
61 // during AskUserForSettings().
62 void SetUserSettings(const PrintSettings& settings);
64 // Enables tests to fail with an access-denied error.
65 void SetNewDocumentBlockedByPermissions() {
66 new_document_blocked_by_permissions_ = true;
69 void SetOnRenderPageBlockedByPermissions() {
70 render_page_blocked_by_permissions_ = true;
72 void SetOnRenderPageFailsForPage(uint32_t page_number) {
73 render_page_fail_for_page_number_ = page_number;
76 void SetOnRenderDocumentBlockedByPermissions() {
77 render_document_blocked_by_permissions_ = true;
79 void SetDocumentDoneBlockedByPermissions() {
80 document_done_blocked_by_permissions_ = true;
83 // Enables tests to fail with a failed error.
84 void SetNewDocumentFails() { new_document_fails_ = true; }
85 void SetUseDefaultSettingsFails() { use_default_settings_fails_ = true; }
87 // Enables tests to fail with a canceled error.
88 void SetNewDocumentCancels() { new_document_cancels_ = true; }
89 void SetAskUserForSettingsCanceled() { ask_user_for_settings_cancel_ = true; }
91 void SetOnNewDocumentCallback(OnNewDocumentCallback callback) {
92 on_new_document_callback_ = std::move(callback);
95 // PrintingContext overrides:
96 void AskUserForSettings(int max_pages,
99 PrintSettingsCallback callback) override;
100 mojom::ResultCode UseDefaultSettings() override;
101 gfx::Size GetPdfPaperSizeDeviceUnits() override;
102 mojom::ResultCode UpdatePrinterSettings(
103 const PrinterSettings& printer_settings) override;
104 mojom::ResultCode NewDocument(const std::u16string& document_name) override;
105 #if BUILDFLAG(IS_WIN)
106 mojom::ResultCode RenderPage(const PrintedPage& page,
107 const PageSetup& page_setup) override;
109 mojom::ResultCode PrintDocument(const MetafilePlayer& metafile,
110 const PrintSettings& settings,
111 uint32_t num_pages) override;
112 mojom::ResultCode DocumentDone() override;
113 void Cancel() override;
114 void ReleaseContext() override;
115 NativeDrawingContext context() const override;
116 #if BUILDFLAG(IS_WIN)
117 mojom::ResultCode InitWithSettingsForTest(
118 std::unique_ptr<PrintSettings> settings) override;
122 mojom::ResultCode AskUserForSettingsImpl(int max_pages,
126 // Simulation of platform drivers' default settings.
127 base::flat_map<std::string, std::unique_ptr<PrintSettings>> device_settings_;
129 // Settings to apply to mimic a user's choices in `AskUserForSettings()`.
130 absl::optional<PrintSettings> user_settings_;
132 // Platform implementations of `PrintingContext` apply PrintSettings to the
133 // respective device contexts. Once the upper printing layers call
134 // `TakeAndResetSettings()`, the values in `settings_` no longer reflect
135 // what the printer driver's device context are set for.
136 // Simulate this by capturing what the settings are whenever `settings_` is
137 // applied to a device context.
138 PrintSettings applied_settings_;
140 #if BUILDFLAG(IS_MAC)
141 // When printing a new document, Preview app is a special macOS destination.
142 // This member is used to track when this was indicated as the destination to
143 // use in `UpdatePrinterSettings()`.
144 bool destination_is_preview_ = false;
147 bool use_default_settings_fails_ = false;
148 bool ask_user_for_settings_cancel_ = false;
149 bool new_document_cancels_ = false;
150 bool new_document_fails_ = false;
151 bool new_document_blocked_by_permissions_ = false;
152 #if BUILDFLAG(IS_WIN)
153 bool render_page_blocked_by_permissions_ = false;
154 absl::optional<uint32_t> render_page_fail_for_page_number_;
156 bool render_document_blocked_by_permissions_ = false;
157 bool document_done_blocked_by_permissions_ = false;
159 // Called every time `NewDocument()` is called. Provides a copy of the
160 // effective device context settings.
161 OnNewDocumentCallback on_new_document_callback_;
164 } // namespace printing
166 #endif // PRINTING_TEST_PRINTING_CONTEXT_H_