print_backend_cups_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "printing/backend/print_backend_cups.h"
  5. #include <cups/cups.h>
  6. #include "base/strings/string_number_conversions.h"
  7. #include "build/build_config.h"
  8. #include "printing/backend/print_backend.h"
  9. #include "printing/backend/print_backend_consts.h"
  10. #include "printing/mojom/print.mojom.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace printing {
  13. namespace {
  14. bool IsDestTypeEligible(int dest_type) {
  15. cups_dest_t* dest = nullptr;
  16. int num_dests = 0;
  17. num_dests =
  18. cupsAddDest(/*name=*/"test_dest", /*instance=*/nullptr, num_dests, &dest);
  19. if (num_dests != 1)
  20. return false;
  21. cups_option_t* options = nullptr;
  22. int num_options = 0;
  23. num_options = cupsAddOption(kCUPSOptPrinterType,
  24. base::NumberToString(dest_type).c_str(),
  25. num_options, &options);
  26. dest->num_options = num_options;
  27. dest->options = options;
  28. PrinterBasicInfo printer_info;
  29. const mojom::ResultCode result_code =
  30. PrintBackendCUPS::PrinterBasicInfoFromCUPS(*dest, &printer_info);
  31. cupsFreeDests(num_dests, dest);
  32. return result_code == mojom::ResultCode::kSuccess;
  33. }
  34. } // namespace
  35. TEST(PrintBackendCupsTest, PrinterBasicInfoFromCUPS) {
  36. constexpr char kName[] = "printer";
  37. constexpr char kDescription[] = "description";
  38. cups_dest_t* printer = nullptr;
  39. ASSERT_EQ(
  40. 1, cupsAddDest(kName, /*instance=*/nullptr, /*num_dests=*/0, &printer));
  41. int num_options = 0;
  42. cups_option_t* options = nullptr;
  43. #if BUILDFLAG(IS_MAC)
  44. constexpr char kInfo[] = "info";
  45. num_options =
  46. cupsAddOption(kCUPSOptPrinterInfo, kInfo, num_options, &options);
  47. num_options = cupsAddOption(kCUPSOptPrinterMakeAndModel, kDescription,
  48. num_options, &options);
  49. ASSERT_EQ(2, num_options);
  50. ASSERT_TRUE(options);
  51. #else
  52. num_options =
  53. cupsAddOption(kCUPSOptPrinterInfo, kDescription, num_options, &options);
  54. ASSERT_EQ(1, num_options);
  55. ASSERT_TRUE(options);
  56. #endif
  57. printer->num_options = num_options;
  58. printer->options = options;
  59. PrinterBasicInfo printer_info;
  60. EXPECT_EQ(PrintBackendCUPS::PrinterBasicInfoFromCUPS(*printer, &printer_info),
  61. mojom::ResultCode::kSuccess);
  62. cupsFreeDests(/*num_dests=*/1, printer);
  63. EXPECT_EQ(kName, printer_info.printer_name);
  64. #if BUILDFLAG(IS_MAC)
  65. EXPECT_EQ(kInfo, printer_info.display_name);
  66. #else
  67. EXPECT_EQ(kName, printer_info.display_name);
  68. #endif
  69. EXPECT_EQ(kDescription, printer_info.printer_description);
  70. // The option value of `kCUPSOptPrinterMakeAndModel` is used to set the value
  71. // for `kDriverInfoTagName`.
  72. auto driver = printer_info.options.find(kDriverInfoTagName);
  73. #if BUILDFLAG(IS_MAC)
  74. ASSERT_NE(driver, printer_info.options.end());
  75. EXPECT_EQ(kDescription, driver->second);
  76. #else
  77. // Didn't set option for `kCUPSOptPrinterMakeAndModel`.
  78. EXPECT_EQ(driver, printer_info.options.end());
  79. #endif
  80. }
  81. TEST(PrintBackendCupsTest, PrinterDriverInfoFromCUPS) {
  82. constexpr char kName[] = "test-printer-name";
  83. constexpr char kDescription[] = "A test printer";
  84. cups_dest_t* printer = nullptr;
  85. ASSERT_EQ(
  86. 1, cupsAddDest(kName, /*instance=*/nullptr, /*num_dests=*/0, &printer));
  87. int num_options = 0;
  88. cups_option_t* options = nullptr;
  89. num_options = cupsAddOption(kCUPSOptPrinterMakeAndModel, kDescription,
  90. num_options, &options);
  91. ASSERT_EQ(1, num_options);
  92. ASSERT_TRUE(options);
  93. printer->num_options = num_options;
  94. printer->options = options;
  95. EXPECT_EQ(kDescription,
  96. PrintBackendCUPS::PrinterDriverInfoFromCUPS(*printer));
  97. cupsFreeDests(/*num_dests=*/1, printer);
  98. }
  99. TEST(PrintBackendCupsTest, EligibleDestTypes) {
  100. EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_FAX));
  101. EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_SCANNER));
  102. EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_DISCOVERED));
  103. EXPECT_TRUE(IsDestTypeEligible(CUPS_PRINTER_LOCAL));
  104. // Try combos. `CUPS_PRINTER_LOCAL` has a value of 0, but keep these test
  105. // cases in the event that the constant values change in CUPS.
  106. EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_LOCAL | CUPS_PRINTER_FAX));
  107. EXPECT_FALSE(IsDestTypeEligible(CUPS_PRINTER_LOCAL | CUPS_PRINTER_SCANNER));
  108. }
  109. } // namespace printing