File: print_backend_cups_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 (147 lines) | stat: -rw-r--r-- 4,828 bytes parent folder | download | duplicates (9)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/backend/print_backend_cups.h"

#include <cups/cups.h>

#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "printing/backend/cups_deleters.h"
#include "printing/backend/print_backend.h"
#include "printing/backend/print_backend_consts.h"
#include "printing/mojom/print.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace printing {

namespace {

ScopedDestination AddDest(const char* name) {
  cups_dest_t* dest = nullptr;
  if (cupsAddDest(name, /*instance=*/nullptr, /*num_dests=*/0, &dest) != 1) {
    return nullptr;
  }
  return ScopedDestination(dest);
}

bool IsDestTypeEligible(int dest_type) {
  ScopedDestination dest = AddDest(/*name=*/"test_dest");
  if (!dest) {
    return false;
  }

  cups_option_t* options = nullptr;
  int num_options = 0;
  num_options = cupsAddOption(kCUPSOptPrinterType,
                              base::NumberToString(dest_type).c_str(),
                              num_options, &options);
  dest->num_options = num_options;
  dest->options = options;

  PrinterBasicInfo printer_info;
  const mojom::ResultCode result_code =
      PrintBackendCUPS::PrinterBasicInfoFromCUPS(*dest, &printer_info);

  return result_code == mojom::ResultCode::kSuccess;
}

}  // namespace

TEST(PrintBackendCupsTest, PrinterBasicInfoFromCUPS) {
  constexpr char kName[] = "printer";
  constexpr char kDescription[] = "description";
  ScopedDestination printer = AddDest(kName);
  ASSERT_TRUE(printer);

  int num_options = 0;
  cups_option_t* options = nullptr;
#if BUILDFLAG(IS_MAC)
  constexpr char kInfo[] = "info";
  num_options =
      cupsAddOption(kCUPSOptPrinterInfo, kInfo, num_options, &options);
  num_options = cupsAddOption(kCUPSOptPrinterMakeAndModel, kDescription,
                              num_options, &options);
  ASSERT_EQ(2, num_options);
  ASSERT_TRUE(options);
#else
  num_options =
      cupsAddOption(kCUPSOptPrinterInfo, kDescription, num_options, &options);
  ASSERT_EQ(1, num_options);
  ASSERT_TRUE(options);
#endif
  printer->num_options = num_options;
  printer->options = options;

  PrinterBasicInfo printer_info;
  EXPECT_EQ(PrintBackendCUPS::PrinterBasicInfoFromCUPS(*printer, &printer_info),
            mojom::ResultCode::kSuccess);

  EXPECT_EQ(kName, printer_info.printer_name);
#if BUILDFLAG(IS_MAC)
  EXPECT_EQ(kInfo, printer_info.display_name);
#else
  EXPECT_EQ(kName, printer_info.display_name);
#endif
  EXPECT_EQ(kDescription, printer_info.printer_description);

  // The option value of `kCUPSOptPrinterMakeAndModel` is used to set the value
  // for `kDriverInfoTagName`.
  auto driver = printer_info.options.find(kDriverInfoTagName);
#if BUILDFLAG(IS_MAC)
  ASSERT_NE(driver, printer_info.options.end());
  EXPECT_EQ(kDescription, driver->second);
#else
  // Didn't set option for `kCUPSOptPrinterMakeAndModel`.
  EXPECT_EQ(driver, printer_info.options.end());
#endif
}

TEST(PrintBackendCupsTest, PrinterBasicInfoFromCUPSNoOptionsDisplayName) {
  constexpr char kName[] = "printer";
  ScopedDestination printer = AddDest(kName);
  ASSERT_TRUE(printer);

  PrinterBasicInfo printer_info;
  EXPECT_EQ(PrintBackendCUPS::PrinterBasicInfoFromCUPS(*printer, &printer_info),
            mojom::ResultCode::kSuccess);

  // Ensure that even if no options are specified that the display name is still
  // set.
  EXPECT_EQ(kName, printer_info.printer_name);
  EXPECT_EQ(kName, printer_info.display_name);
}

TEST(PrintBackendCupsTest, PrinterDriverInfoFromCUPS) {
  constexpr char kName[] = "test-printer-name";
  constexpr char kDescription[] = "A test printer";
  ScopedDestination printer = AddDest(kName);
  ASSERT_TRUE(printer);

  int num_options = 0;
  cups_option_t* options = nullptr;
  num_options = cupsAddOption(kCUPSOptPrinterMakeAndModel, kDescription,
                              num_options, &options);
  ASSERT_EQ(1, num_options);
  ASSERT_TRUE(options);
  printer->num_options = num_options;
  printer->options = options;

  EXPECT_EQ(kDescription,
            PrintBackendCUPS::PrinterDriverInfoFromCUPS(*printer));
}

TEST(PrintBackendCupsTest, EligibleDestTypes) {
  EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_FAX));
  EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_SCANNER));
  EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_DISCOVERED));
  EXPECT_TRUE(IsDestTypeEligible(CUPS_PRINTER_LOCAL));

  // Try combos. `CUPS_PRINTER_LOCAL` has a value of 0, but keep these test
  // cases in the event that the constant values change in CUPS.
  EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_LOCAL | CUPS_PRINTER_FAX));
  EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_LOCAL | CUPS_PRINTER_SCANNER));
}

}  // namespace printing