File: pdf_printer_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (368 lines) | stat: -rw-r--r-- 13,675 bytes parent folder | download | duplicates (5)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2017 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/ui/webui/print_preview/pdf_printer_handler.h"

#include <optional>

#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/values_test_util.h"
#include "build/build_config.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/scoped_browser_locale.h"
#include "components/url_formatter/url_formatter.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/common/printing/printer_capabilities_mac.h"
#include "printing/backend/print_backend.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#endif

#define FPL(x) FILE_PATH_LITERAL(x)

namespace printing {

namespace {

const char kPdfDeviceName[] = "Save as PDF";

const char kPdfPrinterCapability[] =
    R"({
        "capabilities":{
          "printer":{
            "color":{
              "option":[
                {
                  "is_default":true,
                  "type":"STANDARD_COLOR",
                  "vendor_id":"2"
                }
              ]
            },
            "dpi": {
               "option": [ {
                  "horizontal_dpi": 300,
                  "is_default": true,
                  "vertical_dpi": 300
               } ]
            },
            "media_size":{
              "option":[
                {
                  "height_microns":1189000,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 841000,
                  "imageable_area_top_microns": 1189000,
                  "name":"ISO_A0",
                  "width_microns":841000,
                },
                {
                  "height_microns":841000,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 594000,
                  "imageable_area_top_microns": 841000,
                  "name":"ISO_A1",
                  "width_microns":594000,
                },
                {
                  "height_microns":594000,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 420000,
                  "imageable_area_top_microns": 594000,
                  "name":"ISO_A2",
                  "width_microns":420000,
                },
                {
                  "height_microns":420000,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 297000,
                  "imageable_area_top_microns": 420000,
                  "name":"ISO_A3",
                  "width_microns":297000,
                },
                {
                  "height_microns":297000,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 210000,
                  "imageable_area_top_microns": 297000,
                  "name":"ISO_A4",
                  "width_microns":210000,
                },
                {
                  "height_microns":210000,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 148000,
                  "imageable_area_top_microns": 210000,
                  "name":"ISO_A5",
                  "width_microns":148000,
                },
                {
                  "height_microns":355600,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 215900,
                  "imageable_area_top_microns": 355600,
                  "name":"NA_LEGAL",
                  "width_microns":215900,
                },
                {
                  "height_microns":279400,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 215900,
                  "imageable_area_top_microns": 279400,
                  "is_default":true,
                  "name":"NA_LETTER",
                  "width_microns":215900,
                },
                {
                  "height_microns":431800,
                  "imageable_area_bottom_microns": 0,
                  "imageable_area_left_microns": 0,
                  "imageable_area_right_microns": 279400,
                  "imageable_area_top_microns": 431800,
                  "name":"NA_LEDGER",
                  "width_microns":279400,
                }
              ]
            },
            "page_orientation":{
              "option":[
                {
                  "type":"PORTRAIT"
                },
                {
                  "type":"LANDSCAPE"
                },
                {
                  "is_default":true,
                  "type":"AUTO"
                }
              ]
            }
          },
          "version":"1.0"
        },
        "deviceName":"Save as PDF"
      })";

// Used as a callback to StartGetCapability() in tests.
// Records values returned by StartGetCapability().
void RecordCapability(base::OnceClosure done_closure,
                      base::Value::Dict* capability_out,
                      base::Value::Dict capability) {
  *capability_out = std::move(capability);
  std::move(done_closure).Run();
}

#if BUILDFLAG(IS_MAC)
base::Value::Dict GetValueFromCustomPaper(
    const PrinterSemanticCapsAndDefaults::Paper& paper) {
  base::Value::Dict paper_value;
  paper_value.Set("custom_display_name", paper.display_name());
  paper_value.Set("height_microns", paper.size_um().height());
  paper_value.Set("width_microns", paper.size_um().width());
  int imageable_area_left_microns = paper.printable_area_um().x();
  int imageable_area_bottom_microns = paper.printable_area_um().y();
  int imageable_area_right_microns =
      paper.printable_area_um().x() + paper.printable_area_um().width();
  int imageable_area_top_microns =
      paper.printable_area_um().y() + paper.printable_area_um().height();
  paper_value.Set("imageable_area_left_microns", imageable_area_left_microns);
  paper_value.Set("imageable_area_bottom_microns",
                  imageable_area_bottom_microns);
  paper_value.Set("imageable_area_right_microns", imageable_area_right_microns);
  paper_value.Set("imageable_area_top_microns", imageable_area_top_microns);
  return paper_value;
}
#endif

}  // namespace

using PdfPrinterHandlerTest = testing::Test;

class PdfPrinterHandlerGetCapabilityTest : public BrowserWithTestWindowTest {
 public:
  PdfPrinterHandlerGetCapabilityTest() = default;
  ~PdfPrinterHandlerGetCapabilityTest() override = default;

  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();

    // Set the locale to ensure NA_LETTER is the default paper size.
    scoped_browser_locale_ = std::make_unique<ScopedBrowserLocale>("en-US");

    // Create the PDF printer handler
    pdf_printer_handler_ = std::make_unique<PdfPrinterHandler>(
        profile(), browser()->tab_strip_model()->GetActiveWebContents(),
        /*sticky_settings=*/nullptr);
  }

 protected:
  base::Value::Dict StartGetCapabilityAndWaitForResults() {
    base::RunLoop run_loop;
    base::Value::Dict capability;
    pdf_printer_handler_->StartGetCapability(
        kPdfDeviceName,
        base::BindOnce(&RecordCapability, run_loop.QuitClosure(), &capability));
    run_loop.Run();

    return capability;
  }

 private:
  std::unique_ptr<ScopedBrowserLocale> scoped_browser_locale_;
  std::unique_ptr<PdfPrinterHandler> pdf_printer_handler_;
};

TEST_F(PdfPrinterHandlerTest, GetFileNameForPrintJobTitle) {
  static const struct {
    const char* input;
    const base::FilePath::CharType* expected_output;
  } kTestData[] = {
      {"Foo", FPL("Foo.pdf")},
      {"bar", FPL("bar.pdf")},
      {"qux.html", FPL("qux.html.pdf")},
      {"qux.pdf", FPL("qux.pdf")},
      {"Print Me", FPL("Print Me.pdf")},
      {"Print Me.html", FPL("Print Me.html.pdf")},
      {"1l!egal_F@L#(N)ame.html", FPL("1l!egal_F@L#(N)ame.html.pdf")},
      {"example.com", FPL("example.com.pdf")},
      {"data:text/html,foo", FPL("data_text_html,foo.pdf")},
      {"Baz.com Mail - this is e-mail - what. does it mean",
       FPL("Baz.com Mail - this is e-mail - what. does it mean.pdf")},
      {"Baz.com Mail - this is email - what. does. it. mean?",
       FPL("Baz.com Mail - this is email - what. does. it. mean_.pdf")},
      {"Baz.com Mail - This is email. What does it mean.",
       FPL("Baz.com Mail - This is email. What does it mean_.pdf")},
      {"Baz.com Mail - this is email what does it mean",
       FPL("Baz.com Mail - this is email what does it mean.pdf")},
  };

  for (const auto& data : kTestData) {
    SCOPED_TRACE(data.input);
    base::FilePath path = PdfPrinterHandler::GetFileNameForPrintJobTitle(
        base::ASCIIToUTF16(data.input));
    EXPECT_EQ(data.expected_output, path.value());
  }
}

TEST_F(PdfPrinterHandlerTest, GetFileNameForPrintJobURL) {
  static const struct {
    const char* input;
    const base::FilePath::CharType* expected_output;
  } kTestData[] = {
      {"http://example.com", FPL("example.com.pdf")},
      {"http://example.com/?foo", FPL("example.com.pdf")},
      {"https://example.com/foo.html", FPL("foo.pdf")},
      {"https://example.com/bar/qux.txt", FPL("qux.pdf")},
      {"https://example.com/bar/qux.pdf", FPL("qux.pdf")},
      {"data:text/html,foo", FPL("dataurl.pdf")},
  };

  for (const auto& data : kTestData) {
    SCOPED_TRACE(data.input);
    base::FilePath path =
        PdfPrinterHandler::GetFileNameForURL(GURL(data.input));
    EXPECT_EQ(data.expected_output, path.value());
  }
}

TEST_F(PdfPrinterHandlerTest, GetFileName) {
  static const struct {
    const char* url;
    const char* job_title;
    bool is_savable;
    const base::FilePath::CharType* expected_output;
  } kTestData[] = {
      {"http://example.com", "Example Website", true,
       FPL("Example Website.pdf")},
      {"http://example.com/foo.html", "Website", true, FPL("Website.pdf")},
      {"http://example.com/foo.html", "Print Me.html", true,
       FPL("Print Me.html.pdf")},
      {"http://mail.google.com/mail/u/0/#inbox/hash",
       "Baz.com Mail - This is email. What does it mean.", true,
       FPL("Baz.com Mail - This is email. What does it mean_.pdf")},
      {"data:text/html,foo", "data:text/html,foo", true, FPL("dataurl.pdf")},
      {"data:text/html,<title>someone@example.com", "someone@example.com", true,
       FPL("someone@example.com.pdf")},
      {"file:///tmp/test.png", "test.png (420x150)", false, FPL("test.pdf")},
      {"http://empty.com", "", true, FPL("empty.com.pdf")},
      {"http://empty.com/image", "", false, FPL("image.pdf")},
      {"http://empty.com/nomimetype", "", false, FPL("nomimetype.pdf")},
      {"http://empty.com/weird.extension", "", false, FPL("weird.pdf")},
      {"chrome-extension://foo/views/app.html", "demo.docx", true,
       FPL("demo.docx.pdf")},
  };

  for (const auto& data : kTestData) {
    SCOPED_TRACE(std::string(data.url) + " | " + data.job_title);
    GURL url(data.url);
    std::u16string job_title = base::ASCIIToUTF16(data.job_title);
    base::FilePath path =
        PdfPrinterHandler::GetFileName(url, job_title, data.is_savable);
    EXPECT_EQ(data.expected_output, path.value());
  }
}

TEST_F(PdfPrinterHandlerGetCapabilityTest, GetCapability) {
  base::Value expected_capability =
      base::test::ParseJson(kPdfPrinterCapability);
  ASSERT_TRUE(expected_capability.is_dict());
  base::Value::Dict capability = StartGetCapabilityAndWaitForResults();
  EXPECT_EQ(expected_capability.GetDict(), capability);
}

#if BUILDFLAG(IS_MAC)
TEST_F(PdfPrinterHandlerGetCapabilityTest,
       GetMacCustomPaperSizesInCapabilities) {
  constexpr char kPaperOptionPath[] = "capabilities.printer.media_size.option";
  // The first Paper has an arbitrary valid printable area. The rest have
  // printable area that match the size.
  static const PrinterSemanticCapsAndDefaults::Papers kTestPapers = {
      {"printer1", "", gfx::Size(101600, 127000),
       gfx::Rect(120, 120, 101480, 126880)},
      {"printer2", "", gfx::Size(76200, 152400),
       gfx::Rect(0, 0, 76200, 152400)},
      {"printer3", "", gfx::Size(330200, 863600),
       gfx::Rect(0, 0, 330200, 863600)},
      {"printer4", "", gfx::Size(101600, 50800),
       gfx::Rect(0, 0, 101600, 50800)},
  };

  base::Value expected_capability =
      base::test::ParseJson(kPdfPrinterCapability);
  ASSERT_TRUE(expected_capability.is_dict());

  base::Value::List* expected_paper_options =
      expected_capability.GetDict().FindListByDottedPath(kPaperOptionPath);
  ASSERT_TRUE(expected_paper_options);

  for (const PrinterSemanticCapsAndDefaults::Paper& paper : kTestPapers) {
    expected_paper_options->Append(GetValueFromCustomPaper(paper));
  }

  SetMacCustomPaperSizesForTesting(kTestPapers);

  base::Value::Dict capability = StartGetCapabilityAndWaitForResults();

  const base::Value::List* paper_options =
      capability.FindListByDottedPath(kPaperOptionPath);
  ASSERT_TRUE(paper_options);
  EXPECT_EQ(*expected_paper_options, *paper_options);
}
#endif

}  // namespace printing