forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_backend_cups_unittest.cc
131 lines (109 loc) · 4.28 KB
/
print_backend_cups_unittest.cc
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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/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 {
bool IsDestTypeEligible(int dest_type) {
cups_dest_t* dest = nullptr;
int num_dests = 0;
num_dests =
cupsAddDest(/*name=*/"test_dest", /*instance=*/nullptr, num_dests, &dest);
if (num_dests != 1)
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);
cupsFreeDests(num_dests, dest);
return result_code == mojom::ResultCode::kSuccess;
}
} // namespace
TEST(PrintBackendCupsTest, PrinterBasicInfoFromCUPS) {
constexpr char kName[] = "printer";
constexpr char kDescription[] = "description";
cups_dest_t* printer = nullptr;
ASSERT_EQ(
1, cupsAddDest(kName, /*instance=*/nullptr, /*num_dests=*/0, &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);
cupsFreeDests(/*num_dests=*/1, printer);
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, PrinterDriverInfoFromCUPS) {
constexpr char kName[] = "test-printer-name";
constexpr char kDescription[] = "A test printer";
cups_dest_t* printer = nullptr;
ASSERT_EQ(
1, cupsAddDest(kName, /*instance=*/nullptr, /*num_dests=*/0, &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));
cupsFreeDests(/*num_dests=*/1, 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