File: print_test_utils.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 (160 lines) | stat: -rw-r--r-- 6,114 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
// Copyright 2018 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/print_test_utils.h"

#include <string>
#include <utility>

#include "base/json/json_writer.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/printing/print_view_manager_common.h"
#include "printing/buildflags/buildflags.h"
#include "printing/mojom/print.mojom.h"
#include "printing/print_job_constants.h"

#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
#include "chrome/browser/printing/oop_features.h"
#endif

namespace printing::test {

const char kPrinterName[] = "DefaultPrinter";

const PrinterSemanticCapsAndDefaults::Paper kPaperLetter{
    /*display_name=*/"Letter", /*vendor_id=*/"45",
    /*size_um=*/gfx::Size(215900, 279400),
    /*printable_area_um=*/gfx::Rect(1764, 1764, 212372, 275872)};
const PrinterSemanticCapsAndDefaults::Paper kPaperLegal{
    /*display_name=*/"Legal", /*vendor_id=*/"46",
    /*size_um=*/gfx::Size(215900, 355600),
    /*printable_area_um=*/gfx::Rect(1764, 1764, 212372, 352072)};

const std::vector<gfx::Size> kPrinterCapabilitiesDefaultDpis{
    kPrinterCapabilitiesDpi};
const PrinterBasicInfoOptions kPrintInfoOptions{{"opt1", "123"},
                                                {"opt2", "456"}};

base::Value::Dict GetPrintTicket(mojom::PrinterType type) {
  base::Value::Dict ticket;

  // Letter
  base::Value::Dict media_size;
  media_size.Set(kSettingMediaSizeIsDefault, true);
  media_size.Set(kSettingMediaSizeWidthMicrons, 215900);
  media_size.Set(kSettingMediaSizeHeightMicrons, 279400);
  ticket.Set(kSettingMediaSize, std::move(media_size));

  ticket.Set(kSettingPreviewPageCount, 1);
  ticket.Set(kSettingLandscape, false);
  ticket.Set(kSettingColor, 2);  // color printing
  ticket.Set(kSettingHeaderFooterEnabled, false);
  ticket.Set(kSettingMarginsType, 0);  // default margins
  ticket.Set(kSettingDuplexMode,
             static_cast<int>(mojom::DuplexMode::kLongEdge));
  ticket.Set(kSettingCopies, 1);
  ticket.Set(kSettingCollate, true);
  ticket.Set(kSettingShouldPrintBackgrounds, false);
  ticket.Set(kSettingShouldPrintSelectionOnly, false);
  ticket.Set(kSettingPreviewModifiable, true);
  ticket.Set(kSettingPrinterType, static_cast<int>(type));
  ticket.Set(kSettingRasterizePdf, false);
  ticket.Set(kSettingScaleFactor, 100);
  ticket.Set(kSettingScalingType, FIT_TO_PAGE);
  ticket.Set(kSettingPagesPerSheet, 1);
  ticket.Set(kSettingDpiHorizontal, kPrinterDpi);
  ticket.Set(kSettingDpiVertical, kPrinterDpi);
  ticket.Set(kSettingDeviceName, kPrinterName);
  ticket.Set(kSettingPageWidth, 215900);
  ticket.Set(kSettingPageHeight, 279400);
  ticket.Set(kSettingShowSystemDialog, false);

  if (type == mojom::PrinterType::kExtension) {
    base::Value::Dict capabilities;
    capabilities.Set("duplex", true);  // non-empty
    std::string caps_string;
    base::JSONWriter::Write(capabilities, &caps_string);
    ticket.Set(kSettingCapabilities, caps_string);
    base::Value::Dict print_ticket;
    print_ticket.Set("version", "1.0");
    print_ticket.Set("print", base::Value());
    std::string ticket_string;
    base::JSONWriter::Write(print_ticket, &ticket_string);
    ticket.Set(kSettingTicket, ticket_string);
  }

  return ticket;
}

std::unique_ptr<PrintSettings> MakeDefaultPrintSettings(
    const std::string& printer_name) {
  // Setup a sample page setup, which is needed to pass checks in
  // `PrintRenderFrameHelper` that the print params are valid.
  constexpr gfx::Size kPhysicalSize = gfx::Size(200, 200);
  constexpr gfx::Rect kPrintableArea = gfx::Rect(0, 0, 200, 200);
  const PageMargins kRequestedMargins(0, 0, 5, 5, 5, 5);
  const PageSetup kPageSetup(kPhysicalSize, kPrintableArea, kRequestedMargins,
                             /*forced_margins=*/false,
                             /*text_height=*/0);

  auto settings = std::make_unique<PrintSettings>();
  settings->set_copies(kPrintSettingsCopies);
  settings->set_dpi(kPrinterDefaultRenderDpi);
  settings->set_page_setup_device_units(kPageSetup);
  settings->set_device_name(base::ASCIIToUTF16(printer_name));
  settings->set_duplex_mode(mojom::DuplexMode::kSimplex);
  settings->set_color(mojom::ColorModel::kGray);
  return settings;
}

std::unique_ptr<PrintSettings> MakeUserModifiedPrintSettings(
    const std::string& printer_name,
    const PageRanges* page_ranges) {
  std::unique_ptr<PrintSettings> settings =
      MakeDefaultPrintSettings(printer_name);
  settings->set_copies(kPrintSettingsCopies + 1);
  if (page_ranges) {
    settings->set_ranges(*page_ranges);
  }
#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
  if (ShouldPrintJobOop()) {
    // Supply fake data to mimic what might be collected from the system print
    // dialog.  Platform-specific since the fake data still has to be able to
    // pass mojom data validation.
    base::Value::Dict data;

#if BUILDFLAG(IS_MAC)
    data.Set(kMacSystemPrintDialogDataDestinationType, 2);
    data.Set(kMacSystemPrintDialogDataPageFormat,
             base::Value::BlobStorage({0xF1}));
    data.Set(kMacSystemPrintDialogDataPrintSettings,
             base::Value::BlobStorage({0xB2}));

#elif BUILDFLAG(IS_LINUX)
    data.Set(kLinuxSystemPrintDialogDataPrinter, printer_name);
    data.Set(kLinuxSystemPrintDialogDataPrintSettings, "print-settings");
    data.Set(kLinuxSystemPrintDialogDataPageSetup, "page-setup");

#else
#error "Missing fake system print dialog data for this platform."
#endif

    settings->set_system_print_dialog_data(std::move(data));
  }
#endif  // BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
  return settings;
}

void StartPrint(content::WebContents* contents) {
  printing::StartPrint(contents,
#if BUILDFLAG(IS_CHROMEOS)
                       /*print_renderer=*/mojo::NullAssociatedRemote(),
#endif
                       /*print_preview_disabled=*/false,
                       /*has_selection=*/false);
}

}  // namespace printing::test