Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 #include <cups/cups.h> | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "printing/backend/cups_ipp_util.h" | |
| 11 #include "printing/backend/cups_printer.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 class MockCupsOptionProvider : public printing::CupsOptionProvider { | |
| 15 public: | |
| 16 ~MockCupsOptionProvider() override {} | |
| 17 | |
| 18 ipp_attribute_t* GetSupportedOptionValues( | |
| 19 base::StringPiece option_name) const override { | |
| 20 const auto attr = supported_attributes_.find(option_name); | |
| 21 return attr != supported_attributes_.end() ? attr->second : nullptr; | |
| 22 } | |
| 23 | |
| 24 std::vector<base::StringPiece> GetSupportedOptionValueStrings( | |
| 25 base::StringPiece option_name) const override { | |
| 26 ipp_attribute_t* attr = GetSupportedOptionValues(option_name); | |
| 27 if (!attr) | |
| 28 return std::vector<base::StringPiece>(); | |
| 29 | |
| 30 std::vector<base::StringPiece> strings; | |
| 31 int size = ippGetCount(attr); | |
| 32 for (int i = 0; i < size; ++i) { | |
| 33 strings.emplace_back(ippGetString(attr, i, NULL)); | |
|
Lei Zhang
2016/07/21 00:46:19
nullptr, more below. :-P
skau
2016/07/21 20:07:31
I'll break this habit soon!
| |
| 34 } | |
| 35 | |
| 36 return strings; | |
| 37 } | |
| 38 | |
| 39 ipp_attribute_t* GetDefaultOptionValue( | |
| 40 base::StringPiece option_name) const override { | |
| 41 const auto attr = default_attributes_.find(option_name); | |
| 42 return attr != default_attributes_.end() ? attr->second : nullptr; | |
| 43 } | |
| 44 | |
| 45 bool CheckOptionSupported(base::StringPiece name, | |
| 46 base::StringPiece value) const override { | |
| 47 NOTREACHED(); | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 void SetSupportedOptions(base::StringPiece name, ipp_attribute_t* attribute) { | |
| 52 supported_attributes_[name] = attribute; | |
| 53 } | |
| 54 | |
| 55 void SetOptionDefault(base::StringPiece name, ipp_attribute_t* attribute) { | |
| 56 default_attributes_[name] = attribute; | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 std::map<base::StringPiece, ipp_attribute_t*> supported_attributes_; | |
| 61 std::map<base::StringPiece, ipp_attribute_t*> default_attributes_; | |
| 62 }; | |
| 63 | |
| 64 class PrintBackendCupsIppUtilTest : public ::testing::Test { | |
| 65 protected: | |
| 66 void SetUp() override { | |
| 67 ipp_ = ippNew(); | |
| 68 printer_ = new MockCupsOptionProvider(); | |
| 69 } | |
| 70 | |
| 71 void TearDown() override { | |
| 72 ippDelete(ipp_); | |
| 73 delete printer_; | |
|
Lei Zhang
2016/07/21 00:46:19
unique_ptr, reset() here.
skau
2016/07/21 20:07:31
Done.
| |
| 74 } | |
| 75 | |
| 76 ipp_t* ipp_; | |
| 77 MockCupsOptionProvider* printer_; | |
| 78 }; | |
| 79 | |
| 80 ipp_attribute_t* MakeRange(ipp_t* ipp, int lower_bound, int upper_bound) { | |
| 81 return ippAddRange(ipp, IPP_TAG_PRINTER, "TEST_DATA", lower_bound, | |
| 82 upper_bound); | |
| 83 } | |
| 84 | |
| 85 ipp_attribute_t* MakeString(ipp_t* ipp, const char* value) { | |
| 86 return ippAddString(ipp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "TEST_DATA", NULL, | |
| 87 value); | |
| 88 } | |
| 89 | |
| 90 ipp_attribute_t* MakeStringCollection(ipp_t* ipp, | |
| 91 std::vector<const char*> strings) { | |
| 92 return ippAddStrings(ipp, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "TEST_DATA", | |
| 93 strings.size(), NULL, strings.data()); | |
| 94 } | |
| 95 | |
| 96 TEST_F(PrintBackendCupsIppUtilTest, CopiesCapable) { | |
| 97 printer_->SetSupportedOptions("copies", MakeRange(ipp_, 1, 2)); | |
| 98 | |
| 99 printing::PrinterSemanticCapsAndDefaults caps; | |
|
Lei Zhang
2016/07/21 00:46:19
Put the entire thing in namespace printing and sav
skau
2016/07/21 20:07:31
Done.
| |
| 100 | |
|
Lei Zhang
2016/07/21 00:46:19
too
many
blank spaces.
skau
2016/07/21 20:07:31
lol
| |
| 101 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 102 | |
| 103 EXPECT_TRUE(caps.copies_capable); | |
| 104 } | |
| 105 | |
| 106 TEST_F(PrintBackendCupsIppUtilTest, CopiesNotCapable) { | |
| 107 // copies missing, no setup | |
| 108 | |
| 109 printing::PrinterSemanticCapsAndDefaults caps; | |
| 110 | |
| 111 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 112 | |
| 113 EXPECT_FALSE(caps.copies_capable); | |
| 114 } | |
| 115 | |
| 116 TEST_F(PrintBackendCupsIppUtilTest, ColorPrinter) { | |
| 117 printer_->SetSupportedOptions( | |
| 118 "print-color-mode", MakeStringCollection(ipp_, {"color", "monochrome"})); | |
| 119 printer_->SetOptionDefault("print-color-mode", MakeString(ipp_, "color")); | |
| 120 | |
| 121 printing::PrinterSemanticCapsAndDefaults caps; | |
| 122 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 123 | |
| 124 EXPECT_TRUE(caps.color_changeable); | |
| 125 EXPECT_TRUE(caps.color_default); | |
| 126 } | |
| 127 | |
| 128 TEST_F(PrintBackendCupsIppUtilTest, BWPrinter) { | |
| 129 printer_->SetSupportedOptions("print-color-mode", | |
| 130 MakeStringCollection(ipp_, {"monochrome"})); | |
| 131 printer_->SetOptionDefault("print-color-mode", | |
| 132 MakeString(ipp_, "monochrome")); | |
| 133 | |
| 134 printing::PrinterSemanticCapsAndDefaults caps; | |
| 135 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 136 | |
| 137 EXPECT_FALSE(caps.color_changeable); | |
| 138 EXPECT_FALSE(caps.color_default); | |
| 139 } | |
| 140 | |
| 141 TEST_F(PrintBackendCupsIppUtilTest, DuplexSupported) { | |
| 142 printer_->SetSupportedOptions( | |
| 143 "sides", | |
| 144 MakeStringCollection(ipp_, {"two-sided-long-edge", "one-sided"})); | |
| 145 printer_->SetOptionDefault("sides", MakeString(ipp_, "one-sided")); | |
| 146 | |
| 147 printing::PrinterSemanticCapsAndDefaults caps; | |
| 148 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 149 | |
| 150 EXPECT_TRUE(caps.duplex_capable); | |
| 151 EXPECT_FALSE(caps.duplex_default); | |
| 152 } | |
| 153 | |
| 154 TEST_F(PrintBackendCupsIppUtilTest, DuplexNotSupported) { | |
| 155 printer_->SetSupportedOptions("sides", | |
| 156 MakeStringCollection(ipp_, {"one-sided"})); | |
| 157 printer_->SetOptionDefault("sides", MakeString(ipp_, "one-sided")); | |
| 158 | |
| 159 printing::PrinterSemanticCapsAndDefaults caps; | |
| 160 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 161 | |
| 162 EXPECT_FALSE(caps.duplex_capable); | |
| 163 EXPECT_FALSE(caps.duplex_default); | |
| 164 } | |
| 165 | |
| 166 TEST_F(PrintBackendCupsIppUtilTest, A4PaperSupported) { | |
| 167 printer_->SetSupportedOptions( | |
| 168 "media", MakeStringCollection(ipp_, {"iso_a4_210x297mm"})); | |
| 169 | |
| 170 printing::PrinterSemanticCapsAndDefaults caps; | |
| 171 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 172 | |
| 173 printing::PrinterSemanticCapsAndDefaults::Paper paper = caps.papers[0]; | |
| 174 | |
| 175 EXPECT_EQ(paper.display_name, "iso a4"); | |
|
Lei Zhang
2016/07/21 00:46:20
EXPECT_EQ(expected_value, actual_value);
skau
2016/07/21 20:07:32
Done.
| |
| 176 EXPECT_EQ(paper.vendor_id, "iso_a4_210x297mm"); | |
| 177 EXPECT_EQ(paper.size_um.width(), 210000); | |
| 178 EXPECT_EQ(paper.size_um.height(), 297000); | |
| 179 } | |
| 180 | |
| 181 TEST_F(PrintBackendCupsIppUtilTest, LegalPaperDefault) { | |
| 182 printer_->SetOptionDefault("media", MakeString(ipp_, "na_legal_8.5x14in")); | |
| 183 | |
| 184 printing::PrinterSemanticCapsAndDefaults caps; | |
| 185 printing::CapsAndDefaultsFromPrinter(*printer_, &caps); | |
| 186 | |
| 187 EXPECT_EQ(caps.default_paper.display_name, "na legal"); | |
| 188 EXPECT_EQ(caps.default_paper.vendor_id, "na_legal_8.5x14in"); | |
| 189 EXPECT_EQ(caps.default_paper.size_um.width(), 215900); | |
| 190 EXPECT_EQ(caps.default_paper.size_um.height(), 355600); | |
| 191 } | |
| OLD | NEW |