File: printer_capabilities_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (403 lines) | stat: -rw-r--r-- 15,868 bytes parent folder | download | duplicates (6)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// 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/common/printing/printer_capabilities.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/common/printing/printing_buildflags.h"
#include "content/public/test/browser_task_environment.h"
#include "printing/backend/test_print_backend.h"
#include "printing/print_job_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"

namespace printing {

namespace {

const char kDpi[] = "dpi";

void GetSettingsDone(base::OnceClosure done_closure,
                     base::Value::Dict* out_settings,
                     base::Value::Dict settings) {
  *out_settings = std::move(settings);
  std::move(done_closure).Run();
}

void VerifyPaper(const base::Value& paper_dict,
                 const std::string& expected_name,
                 const std::string& expected_vendor,
                 const gfx::Size& expected_size) {
  ASSERT_TRUE(paper_dict.is_dict());
  const std::string* name =
      paper_dict.GetDict().FindString("custom_display_name");
  ASSERT_TRUE(name);
  EXPECT_EQ(expected_name, *name);
  const std::string* vendor = paper_dict.GetDict().FindString("vendor_id");
  ASSERT_TRUE(vendor);
  EXPECT_EQ(expected_vendor, *vendor);
  std::optional<int> width = paper_dict.GetDict().FindInt("width_microns");
  ASSERT_TRUE(width.has_value());
  EXPECT_EQ(expected_size.width(), width.value());
  std::optional<int> height = paper_dict.GetDict().FindInt("height_microns");
  ASSERT_TRUE(height.has_value());
  EXPECT_EQ(expected_size.height(), height.value());
}

#if BUILDFLAG(IS_CHROMEOS)
void VerifyMediaType(const base::Value& media_type_dict,
                     const std::string& expected_name,
                     const std::string& expected_vendor) {
  ASSERT_TRUE(media_type_dict.is_dict());
  const std::string* name =
      media_type_dict.GetDict().FindString("custom_display_name");
  ASSERT_TRUE(name);
  EXPECT_EQ(expected_name, *name);
  const std::string* vendor = media_type_dict.GetDict().FindString("vendor_id");
  ASSERT_TRUE(vendor);
  EXPECT_EQ(expected_vendor, *vendor);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace

class PrinterCapabilitiesTest : public testing::Test {
 public:
  PrinterCapabilitiesTest() = default;
  ~PrinterCapabilitiesTest() override = default;

 protected:
  void SetUp() override {
    test_backend_ = base::MakeRefCounted<TestPrintBackend>();
    PrintBackend::SetPrintBackendForTesting(test_backend_.get());
    blocking_task_runner_ =
        base::ThreadPool::CreateSingleThreadTaskRunner({base::MayBlock()});
    disallow_blocking_ = std::make_unique<base::ScopedDisallowBlocking>();
  }

  void TearDown() override { PrintBackend::SetPrintBackendForTesting(nullptr); }

  base::Value::Dict GetSettingsOnBlockingTaskRunnerAndWaitForResults(
      const std::string& printer_name,
      const PrinterBasicInfo& basic_info,
      PrinterSemanticCapsAndDefaults::Papers papers) {
    base::RunLoop run_loop;
    base::Value::Dict settings;

    blocking_task_runner_->PostTaskAndReplyWithResult(
        FROM_HERE,
        base::BindOnce(&GetSettingsOnBlockingTaskRunner, printer_name,
                       basic_info, std::move(papers), test_backend_),
        base::BindOnce(&GetSettingsDone, run_loop.QuitClosure(), &settings));

    run_loop.Run();
    return settings;
  }

  TestPrintBackend* print_backend() { return test_backend_.get(); }

 private:
  content::BrowserTaskEnvironment task_environment_;
  scoped_refptr<TestPrintBackend> test_backend_;
  scoped_refptr<base::TaskRunner> blocking_task_runner_;
  std::unique_ptr<base::ScopedDisallowBlocking> disallow_blocking_;
};

// Verify that we don't crash for a missing printer and a nullptr is never
// returned.
TEST_F(PrinterCapabilitiesTest, NonNullForMissingPrinter) {
  std::string printer_name = "missing_printer";
  PrinterBasicInfo basic_info;
  PrinterSemanticCapsAndDefaults::Papers no_user_defined_papers;

  base::Value::Dict settings_dictionary =
      GetSettingsOnBlockingTaskRunnerAndWaitForResults(
          printer_name, basic_info, std::move(no_user_defined_papers));

  ASSERT_FALSE(settings_dictionary.empty());
}

TEST_F(PrinterCapabilitiesTest, ProvidedCapabilitiesUsed) {
  std::string printer_name = "test_printer";
  PrinterBasicInfo basic_info;
  PrinterSemanticCapsAndDefaults::Papers no_user_defined_papers;

  // Set a capability and add a valid printer.
  auto caps = std::make_unique<PrinterSemanticCapsAndDefaults>();
  caps->dpis = {{600, 600}};
  print_backend()->AddValidPrinter(
      printer_name, std::move(caps),
      std::make_unique<printing::PrinterBasicInfo>(basic_info));

  base::Value::Dict settings_dictionary =
      GetSettingsOnBlockingTaskRunnerAndWaitForResults(
          printer_name, basic_info, std::move(no_user_defined_papers));

  // Verify settings were created.
  ASSERT_FALSE(settings_dictionary.empty());

  // Verify capabilities dict exists and has 2 entries. (printer and version)
  const base::Value::Dict* cdd =
      settings_dictionary.FindDict(kSettingCapabilities);
  ASSERT_TRUE(cdd);
  EXPECT_EQ(2U, cdd->size());

  // Read the CDD for the "dpi" attribute.
  const base::Value::Dict* caps_dict = cdd->FindDict(kPrinter);
  ASSERT_TRUE(caps_dict);
  EXPECT_TRUE(caps_dict->contains(kDpi));
}

// Ensure that the capabilities dictionary is not present if the backend
// doesn't return capabilities.
TEST_F(PrinterCapabilitiesTest, NullCapabilitiesExcluded) {
  std::string printer_name = "test_printer";
  PrinterBasicInfo basic_info;
  PrinterSemanticCapsAndDefaults::Papers no_user_defined_papers;

  // Return false when attempting to retrieve capabilities.
  print_backend()->AddValidPrinter(printer_name, /*caps=*/nullptr,
                                   /*info=*/nullptr);

  base::Value::Dict settings_dictionary =
      GetSettingsOnBlockingTaskRunnerAndWaitForResults(
          printer_name, basic_info, std::move(no_user_defined_papers));

  // Verify settings were created.
  ASSERT_FALSE(settings_dictionary.empty());

  // Verify that capabilities is not present.
  ASSERT_FALSE(settings_dictionary.contains(kSettingCapabilities));
}

TEST_F(PrinterCapabilitiesTest, UserDefinedPapers) {
  std::string printer_name = "test_printer";
  PrinterBasicInfo basic_info;

  // Set a capability and add a valid printer.
  auto caps = std::make_unique<PrinterSemanticCapsAndDefaults>();
  caps->papers.push_back({"printer_foo", "printer_vendor", {100, 234}});
  caps->dpis = {{600, 600}};
  print_backend()->AddValidPrinter(
      printer_name, std::move(caps),
      std::make_unique<printing::PrinterBasicInfo>(basic_info));

  // Add some more paper sizes.
  PrinterSemanticCapsAndDefaults::Papers user_defined_papers;
  user_defined_papers.push_back({"foo", "vendor", {200, 300}});
  user_defined_papers.push_back({"bar", "vendor", {600, 600}});

  base::Value::Dict settings_dictionary =
      GetSettingsOnBlockingTaskRunnerAndWaitForResults(
          printer_name, basic_info, std::move(user_defined_papers));

  // Verify settings were created.
  ASSERT_FALSE(settings_dictionary.empty());

  // Verify there is a CDD with a printer entry.
  const base::Value::Dict* cdd =
      settings_dictionary.FindDict(kSettingCapabilities);
  ASSERT_TRUE(cdd);
  const base::Value::Dict* printer = cdd->FindDict(kPrinter);
  ASSERT_TRUE(printer);

  // Verify there are 3 paper sizes.
  const base::Value::Dict* media_size = printer->FindDict("media_size");
  ASSERT_TRUE(media_size);
  const base::Value::List* media_option = media_size->FindList("option");
  ASSERT_TRUE(media_option);
  ASSERT_EQ(3U, media_option->size());

  // Verify the 3 paper sizes are the ones in |caps->papers|, followed by the
  // ones in |user_defined_papers|.
#if BUILDFLAG(PRINT_MEDIA_L10N_ENABLED)
  VerifyPaper((*media_option)[0], "0 x 0 mm", "om_100x234um_0x0mm", {100, 234});
#else
  VerifyPaper((*media_option)[0], "printer_foo", "printer_vendor", {100, 234});
#endif
  VerifyPaper((*media_option)[1], "foo", "vendor", {200, 300});
  VerifyPaper((*media_option)[2], "bar", "vendor", {600, 600});
}

#if BUILDFLAG(PRINT_MEDIA_L10N_ENABLED)
TEST_F(PrinterCapabilitiesTest, PaperLocalizationsApplied) {
  std::string printer_name = "test_printer";
  PrinterBasicInfo basic_info;

  // Set a capability and add a valid printer.
  auto caps = std::make_unique<PrinterSemanticCapsAndDefaults>();
  caps->papers.push_back(
      {"na square-photo", "oe_square-photo_5x5in", {127000, 127000}});
  caps->papers.push_back(
      {"om 21x40", "om_photo-21x40_215x400mm", {215000, 400000}});
  caps->papers.push_back({"na letter", "na_letter_8.5x11in", {215900, 279400}});
  caps->papers.push_back(
      {"na custom-2", "na_custom-2_299.6x405.3mm", {299600, 405300}});
  caps->papers.push_back(
      {"na custom-1", "na_custom-1_9x12.1in", {228600, 307340}});
  caps->papers.push_back(
      {"na index-4x6", "na_index-4x6_4x6in", {101600, 152400}});
  caps->papers.push_back(
      {"na square-photo", "oe_square-photo_4x4in", {101600, 101600}});
  caps->papers.push_back({"iso a4", "iso_a4_210x297mm", {210000, 297000}});
  caps->papers.push_back(
      {"om folio-sp", "om_folio-sp_215x315mm", {215000, 315000}});
  caps->papers.push_back({"oe photo-l", "oe_photo-l_3.5x5in", {88900, 127000}});
  caps->papers.push_back({"om folio", "om_folio_210x330mm", {210000, 330000}});
  caps->papers.push_back({"oe 2x3", "oe_2x3_2x3in", {50800, 76200}});
  caps->papers.push_back({"om 1x1", "om_1-x-1_1x1mm", {1000, 1000}});

  caps->dpis = {{600, 600}};
  print_backend()->AddValidPrinter(
      printer_name, std::move(caps),
      std::make_unique<printing::PrinterBasicInfo>(basic_info));

  // Add some more paper sizes.
  PrinterSemanticCapsAndDefaults::Papers user_defined_papers;
  user_defined_papers.push_back({"foo", "vendor", {200, 300}});
  user_defined_papers.push_back({"bar", "vendor", {600, 600}});

  base::Value::Dict settings_dictionary =
      GetSettingsOnBlockingTaskRunnerAndWaitForResults(
          printer_name, basic_info, std::move(user_defined_papers));

  // Verify settings were created.
  ASSERT_FALSE(settings_dictionary.empty());

  // Verify there is a CDD with a printer entry.
  const base::Value::Dict* cdd =
      settings_dictionary.FindDict(kSettingCapabilities);
  ASSERT_TRUE(cdd);
  const base::Value::Dict* printer = cdd->FindDict(kPrinter);
  ASSERT_TRUE(printer);

  // Verify there are 3 paper sizes.
  const base::Value::Dict* media_size = printer->FindDict("media_size");
  ASSERT_TRUE(media_size);
  const base::Value::List* media_option = media_size->FindList("option");
  ASSERT_TRUE(media_option);
  ASSERT_EQ(15U, media_option->size());

  // Verify the paper sizes are the ones in `caps->papers` in the correct
  // order.
  VerifyPaper((*media_option)[0], "2 x 3 in", "om_50800x76200um_50x76mm",
              {50800, 76200});
  VerifyPaper((*media_option)[1], "3.5 x 5 in", "oe_photo-l_3.5x5in",
              {88900, 127000});
  VerifyPaper((*media_option)[2], "4 x 4 in", "oe_square-photo_4x4in",
              {101600, 101600});
  VerifyPaper((*media_option)[3], "4 x 6 in", "na_index-4x6_4x6in",
              {101600, 152400});
  VerifyPaper((*media_option)[4], "5 x 5 in", "oe_square-photo_5x5in",
              {127000, 127000});
  VerifyPaper((*media_option)[5], "9 x 12.1 in", "om_228600x307340um_228x307mm",
              {228600, 307340});
  VerifyPaper((*media_option)[6], "1 x 1 mm", "om_1000x1000um_1x1mm",
              {1000, 1000});
  VerifyPaper((*media_option)[7], "210 x 330 mm", "om_folio_210x330mm",
              {210000, 330000});
  VerifyPaper((*media_option)[8], "215 x 315 mm", "om_folio-sp_215x315mm",
              {215000, 315000});
  VerifyPaper((*media_option)[9], "215 x 400 mm",
              "om_215000x400000um_215x400mm", {215000, 400000});
  VerifyPaper((*media_option)[10], "300 x 405 mm",
              "om_299600x405300um_299x405mm", {299600, 405300});
  VerifyPaper((*media_option)[11], "A4", "iso_a4_210x297mm", {210000, 297000});
  VerifyPaper((*media_option)[12], "Letter", "na_letter_8.5x11in",
              {215900, 279400});
  VerifyPaper((*media_option)[13], "foo", "vendor", {200, 300});
  VerifyPaper((*media_option)[14], "bar", "vendor", {600, 600});
}
#endif  // BUILDFLAG(PRINT_MEDIA_L10N_ENABLED)

#if BUILDFLAG(IS_CHROMEOS)
TEST_F(PrinterCapabilitiesTest, MediaTypeLocalizationsApplied) {
  std::string printer_name = "test_printer";
  PrinterBasicInfo basic_info;

  // Set a capability and add a valid printer.
  auto caps = std::make_unique<PrinterSemanticCapsAndDefaults>();
  caps->papers.push_back({"na letter", "na_letter_8.5x11in", {215900, 279400}});

  // Add some media types.
  caps->media_types.push_back({"stationery display name", "stationery"});
  caps->media_types.push_back({"custom 1 display name", "custom-1"});
  caps->media_types.push_back({"photo display name", "photographic"});
  caps->media_types.push_back({"custom 2 display name", "custom-2"});

  caps->dpis = {{600, 600}};
  print_backend()->AddValidPrinter(
      printer_name, std::move(caps),
      std::make_unique<printing::PrinterBasicInfo>(basic_info));

  base::Value::Dict settings_dictionary =
      GetSettingsOnBlockingTaskRunnerAndWaitForResults(printer_name, basic_info,
                                                       {});

  // Verify settings were created.
  ASSERT_FALSE(settings_dictionary.empty());

  // Verify there is a CDD with a printer entry.
  const base::Value::Dict* cdd =
      settings_dictionary.FindDict(kSettingCapabilities);
  ASSERT_TRUE(cdd);
  const base::Value::Dict* printer = cdd->FindDict(kPrinter);
  ASSERT_TRUE(printer);

  // Verify there are 4 media types.
  const base::Value::Dict* media_type = printer->FindDict("media_type");
  ASSERT_TRUE(media_type);
  const base::Value::List* media_type_option = media_type->FindList("option");
  ASSERT_TRUE(media_type_option);
  ASSERT_EQ(4U, media_type_option->size());

  VerifyMediaType((*media_type_option)[0], "Paper (Plain)", "stationery");
  VerifyMediaType((*media_type_option)[1], "custom 1 display name", "custom-1");
  VerifyMediaType((*media_type_option)[2], "Photo", "photographic");
  VerifyMediaType((*media_type_option)[3], "custom 2 display name", "custom-2");
}

TEST_F(PrinterCapabilitiesTest, HasNotSecureProtocol) {
  std::string printer_name = "test_printer";
  PrinterBasicInfo basic_info;
  PrinterSemanticCapsAndDefaults::Papers no_user_defined_papers;

  // Set a capability and add a valid printer.
  auto caps = std::make_unique<PrinterSemanticCapsAndDefaults>();
  caps->pin_supported = true;
  print_backend()->AddValidPrinter(
      printer_name, std::move(caps),
      std::make_unique<printing::PrinterBasicInfo>(basic_info));

  base::Value::Dict settings_dictionary =
      GetSettingsOnBlockingTaskRunnerAndWaitForResults(
          printer_name, basic_info, std::move(no_user_defined_papers));

  // Verify settings were created.
  ASSERT_FALSE(settings_dictionary.empty());

  // Verify there is a CDD with a printer entry.
  const base::Value::Dict* cdd =
      settings_dictionary.FindDict(kSettingCapabilities);
  ASSERT_TRUE(cdd);
  const base::Value::Dict* printer = cdd->FindDict(kPrinter);
  ASSERT_TRUE(printer);

  // Verify that pin is not supported.
  const base::Value::Dict* pin = printer->FindDict("pin");
  ASSERT_TRUE(pin);
  std::optional<bool> pin_supported = pin->FindBool("supported");
  ASSERT_TRUE(pin_supported.has_value());
  ASSERT_FALSE(pin_supported.value());
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace printing