File: browser_printing_context_factory_for_test.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (190 lines) | stat: -rw-r--r-- 5,779 bytes parent folder | download | duplicates (3)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// 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.

#include "chrome/browser/printing/browser_printing_context_factory_for_test.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/types/optional_util.h"
#include "build/build_config.h"
#include "chrome/browser/printing/print_test_utils.h"
#include "printing/buildflags/buildflags.h"
#include "printing/test_printing_context.h"

namespace printing {

BrowserPrintingContextFactoryForTest::BrowserPrintingContextFactoryForTest() =
    default;

BrowserPrintingContextFactoryForTest::~BrowserPrintingContextFactoryForTest() =
    default;

std::unique_ptr<PrintingContext>
BrowserPrintingContextFactoryForTest::CreatePrintingContext(
    PrintingContext::Delegate* delegate,
    PrintingContext::OutOfProcessBehavior out_of_process_behavior) {
  auto context = MakeDefaultTestPrintingContext(
      delegate, out_of_process_behavior, printer_name_);

  if (new_document_job_id_.has_value()) {
    context->SetNewDocumentJobId(new_document_job_id_.value());
  }

  if (failed_error_for_update_printer_settings_) {
    context->SetUpdatePrinterSettingsFails();
  }
  if (cancels_in_new_document_) {
    context->SetNewDocumentCancels();
  }
  if (failed_error_for_new_document_) {
    context->SetNewDocumentFails();
  }
  if (access_denied_errors_for_new_document_) {
    context->SetNewDocumentBlockedByPermissions();
  }
#if BUILDFLAG(IS_WIN)
  if (access_denied_errors_for_render_page_) {
    context->SetOnRenderPageBlockedByPermissions();
  }
  if (failed_error_for_render_page_number_) {
    context->SetOnRenderPageFailsForPage(failed_error_for_render_page_number_);
  }
#endif
  if (access_denied_errors_for_render_document_) {
    context->SetOnRenderDocumentBlockedByPermissions();
  }
  if (access_denied_errors_for_document_done_) {
    context->SetDocumentDoneBlockedByPermissions();
  }

  if (fail_on_use_default_settings_) {
    context->SetUseDefaultSettingsFails();
  }
#if BUILDFLAG(ENABLE_BASIC_PRINT_DIALOG)
  if (cancel_on_ask_user_for_settings_) {
    context->SetAskUserForSettingsCanceled();
  }

  if (fail_on_ask_user_for_settings_) {
    context->SetAskUserForSettingsFails();
  }
#endif

  context->SetUserSettings(*test::MakeUserModifiedPrintSettings(
      printer_name_, base::OptionalToPtr(page_ranges_)));

  context->SetOnNewDocumentCallback(on_new_document_callback_);

  return context;
}

void BrowserPrintingContextFactoryForTest::SetPrinterNameForSubsequentContexts(
    const std::string& printer_name) {
  printer_name_ = printer_name;
}

#if BUILDFLAG(IS_WIN)
void BrowserPrintingContextFactoryForTest::
    SetPrinterLanguageTypeForSubsequentContexts(
        mojom::PrinterLanguageType printer_language_type) {
  printer_language_type_ = printer_language_type;
}
#endif

void BrowserPrintingContextFactoryForTest::
    SetUserSettingsPageRangesForSubsequentContext(
        const PageRanges& page_ranges) {
  page_ranges_ = page_ranges;
}

void BrowserPrintingContextFactoryForTest::
    SetFailedErrorOnUpdatePrinterSettings() {
  failed_error_for_update_printer_settings_ = true;
}

void BrowserPrintingContextFactoryForTest::SetCancelErrorOnNewDocument(
    bool cause_errors) {
  cancels_in_new_document_ = cause_errors;
}

void BrowserPrintingContextFactoryForTest::SetFailedErrorOnNewDocument(
    bool cause_errors) {
  failed_error_for_new_document_ = cause_errors;
}

void BrowserPrintingContextFactoryForTest::SetJobIdOnNewDocument(int job_id) {
  new_document_job_id_ = job_id;
}

void BrowserPrintingContextFactoryForTest::SetAccessDeniedErrorOnNewDocument(
    bool cause_errors) {
  access_denied_errors_for_new_document_ = cause_errors;
}

#if BUILDFLAG(IS_WIN)
void BrowserPrintingContextFactoryForTest::SetAccessDeniedErrorOnRenderPage(
    bool cause_errors) {
  access_denied_errors_for_render_page_ = cause_errors;
}

void BrowserPrintingContextFactoryForTest::SetFailedErrorForRenderPage(
    uint32_t page_number) {
  failed_error_for_render_page_number_ = page_number;
}
#endif

void BrowserPrintingContextFactoryForTest::SetAccessDeniedErrorOnRenderDocument(
    bool cause_errors) {
  access_denied_errors_for_render_document_ = cause_errors;
}

void BrowserPrintingContextFactoryForTest::SetAccessDeniedErrorOnDocumentDone(
    bool cause_errors) {
  access_denied_errors_for_document_done_ = cause_errors;
}

void BrowserPrintingContextFactoryForTest::SetFailErrorOnUseDefaultSettings() {
  fail_on_use_default_settings_ = true;
}

#if BUILDFLAG(ENABLE_BASIC_PRINT_DIALOG)
void BrowserPrintingContextFactoryForTest::
    SetCancelErrorOnAskUserForSettings() {
  cancel_on_ask_user_for_settings_ = true;
}

void BrowserPrintingContextFactoryForTest::SetFailErrorOnAskUserForSettings() {
  fail_on_ask_user_for_settings_ = true;
}
#endif

void BrowserPrintingContextFactoryForTest::SetOnNewDocumentCallback(
    TestPrintingContext::OnNewDocumentCallback callback) {
  on_new_document_callback_ = std::move(callback);
}

std::unique_ptr<TestPrintingContext>
BrowserPrintingContextFactoryForTest::MakeDefaultTestPrintingContext(
    PrintingContext::Delegate* delegate,
    PrintingContext::OutOfProcessBehavior out_of_process_behavior,
    const std::string& printer_name) {
  auto context =
      std::make_unique<TestPrintingContext>(delegate, out_of_process_behavior);

  std::unique_ptr<PrintSettings> settings =
      test::MakeDefaultPrintSettings(printer_name);

#if BUILDFLAG(IS_WIN)
  if (printer_language_type_.has_value()) {
    settings->set_printer_language_type(printer_language_type_.value());
  }
#endif

  context->SetDeviceSettings(printer_name, std::move(settings));
  return context;
}

}  // namespace printing