[M120 Migration][VD] Remove accessing oom_score_adj in zygote process
[platform/framework/web/chromium-efl.git] / printing / test_printing_context.h
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.
4
5 #ifndef PRINTING_TEST_PRINTING_CONTEXT_H_
6 #define PRINTING_TEST_PRINTING_CONTEXT_H_
7
8 #include <memory>
9 #include <string>
10
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"
17
18 #if BUILDFLAG(IS_WIN)
19 #include "third_party/abseil-cpp/absl/types/optional.h"
20 #endif
21
22 namespace printing {
23
24 class TestPrintingContextDelegate : public PrintingContext::Delegate {
25  public:
26   TestPrintingContextDelegate();
27   TestPrintingContextDelegate(const TestPrintingContextDelegate&) = delete;
28   TestPrintingContextDelegate& operator=(const TestPrintingContextDelegate&) =
29       delete;
30   ~TestPrintingContextDelegate() override;
31
32   // PrintingContext::Delegate overrides:
33   gfx::NativeView GetParentView() override;
34   std::string GetAppLocale() override;
35 };
36
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 {
41  public:
42   using OnNewDocumentCallback = base::RepeatingCallback<void(
43 #if BUILDFLAG(IS_MAC)
44       bool destination_is_preview,
45 #endif
46       const PrintSettings&)>;
47
48   TestPrintingContext(Delegate* delegate, bool skip_system_calls);
49   TestPrintingContext(const TestPrintingContext&) = delete;
50   TestPrintingContext& operator=(const TestPrintingContext&) = delete;
51   ~TestPrintingContext() override;
52
53   // Methods for test setup:
54
55   // Provide settings that will be used as the current settings for the
56   // indicated device.
57   void SetDeviceSettings(const std::string& device_name,
58                          std::unique_ptr<PrintSettings> settings);
59
60   // Provide the settings which should be applied to mimic a user's choices
61   // during AskUserForSettings().
62   void SetUserSettings(const PrintSettings& settings);
63
64   // Enables tests to fail with an access-denied error.
65   void SetNewDocumentBlockedByPermissions() {
66     new_document_blocked_by_permissions_ = true;
67   }
68 #if BUILDFLAG(IS_WIN)
69   void SetOnRenderPageBlockedByPermissions() {
70     render_page_blocked_by_permissions_ = true;
71   }
72   void SetOnRenderPageFailsForPage(uint32_t page_number) {
73     render_page_fail_for_page_number_ = page_number;
74   }
75 #endif
76   void SetOnRenderDocumentBlockedByPermissions() {
77     render_document_blocked_by_permissions_ = true;
78   }
79   void SetDocumentDoneBlockedByPermissions() {
80     document_done_blocked_by_permissions_ = true;
81   }
82
83   // Enables tests to fail with a failed error.
84   void SetNewDocumentFails() { new_document_fails_ = true; }
85   void SetUseDefaultSettingsFails() { use_default_settings_fails_ = true; }
86
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; }
90
91   void SetOnNewDocumentCallback(OnNewDocumentCallback callback) {
92     on_new_document_callback_ = std::move(callback);
93   }
94
95   // PrintingContext overrides:
96   void AskUserForSettings(int max_pages,
97                           bool has_selection,
98                           bool is_scripted,
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;
108 #endif
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;
119 #endif
120
121  private:
122   mojom::ResultCode AskUserForSettingsImpl(int max_pages,
123                                            bool has_selection,
124                                            bool is_scripted);
125
126   // Simulation of platform drivers' default settings.
127   base::flat_map<std::string, std::unique_ptr<PrintSettings>> device_settings_;
128
129   // Settings to apply to mimic a user's choices in `AskUserForSettings()`.
130   absl::optional<PrintSettings> user_settings_;
131
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_;
139
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;
145 #endif
146
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_;
155 #endif
156   bool render_document_blocked_by_permissions_ = false;
157   bool document_done_blocked_by_permissions_ = false;
158
159   // Called every time `NewDocument()` is called.  Provides a copy of the
160   // effective device context settings.
161   OnNewDocumentCallback on_new_document_callback_;
162 };
163
164 }  // namespace printing
165
166 #endif  // PRINTING_TEST_PRINTING_CONTEXT_H_