OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include <cups/ppd.h> | |
7 | |
8 #include <cstring> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/file_path.h" | |
13 #include "base/file_util.h" | |
14 #include "base/message_loop.h" | |
15 #include "base/scoped_temp_dir.h" | |
16 #include "chrome/browser/printing/print_system_task_proxy.h" | |
17 #include "content/public/test/test_browser_thread.h" | |
18 #include "printing/backend/print_backend.h" | |
19 #include "printing/print_job_constants.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace { | |
23 | |
24 #if !defined(OS_MACOSX) | |
25 | |
26 // TestEntry stores the printer name and the expected number of options. | |
27 struct TestEntry { | |
28 TestEntry(std::string name, int count) | |
29 : printer_name(name), | |
30 expected_option_count(count) {} | |
31 | |
32 std::string printer_name; | |
33 int expected_option_count; | |
34 }; | |
35 | |
36 // Verify the option marked in |ppd|. | |
37 void verifyOptionValue(ppd_file_t* ppd, | |
38 const std::string& option_name, | |
39 const std::string& expected_choice_value) { | |
40 ppd_choice_t* option_choice = ppdFindMarkedChoice(ppd, option_name.c_str()); | |
41 if (option_choice == NULL) { | |
42 ppd_option_t* option = ppdFindOption(ppd, option_name.c_str()); | |
43 if (option != NULL) | |
44 option_choice = ppdFindChoice(option, option->defchoice); | |
45 } | |
46 ASSERT_TRUE(option_choice); | |
47 EXPECT_EQ(strcmp(option_choice->choice, expected_choice_value.c_str()), 0); | |
48 } | |
49 | |
50 #endif // !defined(OS_MACOSX) | |
51 | |
52 class PrintSystemTaskProxyTest : public testing::Test { | |
53 public: | |
54 PrintSystemTaskProxyTest() | |
55 : loop_(MessageLoop::TYPE_UI), | |
56 ui_thread_(content::BrowserThread::UI, &loop_) { | |
57 } | |
58 | |
59 protected: | |
60 virtual void TearDown() OVERRIDE { | |
61 MessageLoop::current()->RunAllPending(); | |
62 } | |
63 | |
64 private: | |
65 MessageLoop loop_; | |
66 content::TestBrowserThread ui_thread_; | |
67 }; | |
68 | |
69 } // namespace | |
70 | |
71 #if !defined(OS_MACOSX) | |
72 | |
73 using printing_internal::parse_lpoptions; | |
74 | |
75 | |
76 | |
77 // Test to verify that lpoption custom settings are marked on the ppd file. | |
78 TEST_F(PrintSystemTaskProxyTest, MarkLpoptionsInPPD) { | |
79 const std::string kColorModel = "ColorModel"; | |
80 const std::string kBlack = "Black"; | |
81 const std::string kGray = "Gray"; | |
82 | |
83 const std::string kDuplex = "Duplex"; | |
84 const std::string kDuplexNone = "None"; | |
85 const std::string kDuplexNoTumble = "DuplexNoTumble"; | |
86 const std::string kDuplexTumble = "DuplexTumble"; | |
87 const std::string kTestPrinterName = "printerE"; | |
88 | |
89 ScopedTempDir temp_directory; | |
90 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
91 | |
92 std::string system_lpoptions; // Specifies the system lpoption data. | |
93 system_lpoptions.append("Dest printerE ColorModel=Black Duplex="); | |
94 system_lpoptions.append(kDuplexNone+" "); | |
95 | |
96 // Create and write the system lpoptions to a temp file. | |
97 FilePath system_lp_options_file; | |
98 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
99 &system_lp_options_file)); | |
100 ASSERT_TRUE(file_util::WriteFile(system_lp_options_file, | |
101 system_lpoptions.c_str(), | |
102 system_lpoptions.size())); | |
103 | |
104 // Specifies the user lpoption data. | |
105 std::string user_lpoptions; | |
106 user_lpoptions.append("Dest printerE Duplex="+kDuplexNoTumble+"\n"); | |
107 | |
108 // Create and write the user lpoptions to a temp file. | |
109 FilePath user_lp_options_file; | |
110 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
111 &user_lp_options_file)); | |
112 ASSERT_TRUE(file_util::WriteFile(user_lp_options_file, user_lpoptions.c_str(), | |
113 user_lpoptions.size())); | |
114 // Specifies the test ppd data. | |
115 std::string test_ppd_data; | |
116 test_ppd_data.append("*PPD-Adobe: \"4.3\"\n\n" | |
117 "*OpenGroup: General/General\n\n" | |
118 "*OpenUI *ColorModel/Color Model: PickOne\n" | |
119 "*DefaultColorModel: Gray\n" | |
120 "*ColorModel Gray/Grayscale: \"" | |
121 "<</cupsColorSpace 0/cupsColorOrder 0>>" | |
122 "setpagedevice\"\n" | |
123 "*ColorModel Black/Inverted Grayscale: \"" | |
124 "<</cupsColorSpace 3/cupsColorOrder 0>>" | |
125 "setpagedevice\"\n" | |
126 "*CloseUI: *ColorModel\n" | |
127 "*OpenUI *Duplex/2-Sided Printing: PickOne\n" | |
128 "*DefaultDuplex: DuplexTumble\n" | |
129 "*Duplex None/Off: \"<</Duplex false>>" | |
130 "setpagedevice\"\n" | |
131 "*Duplex DuplexNoTumble/LongEdge: \"" | |
132 "<</Duplex true/Tumble false>>setpagedevice\"\n" | |
133 "*Duplex DuplexTumble/ShortEdge: \"" | |
134 "<</Duplex true/Tumble true>>setpagedevice\"\n" | |
135 "*CloseUI: *Duplex\n\n" | |
136 "*CloseGroup: General\n"); | |
137 | |
138 // Create a test ppd file. | |
139 FilePath ppd_file_path; | |
140 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
141 &ppd_file_path)); | |
142 ASSERT_TRUE(file_util::WriteFile(ppd_file_path, test_ppd_data.c_str(), | |
143 test_ppd_data.size())); | |
144 | |
145 ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str()); | |
146 ASSERT_TRUE(ppd); | |
147 ppdMarkDefaults(ppd); | |
148 | |
149 // Verify the default settings. | |
150 verifyOptionValue(ppd, kDuplex, kDuplexTumble); | |
151 verifyOptionValue(ppd, kColorModel, kGray); | |
152 | |
153 // Parse the system lpoptions data. | |
154 int num_options = 0; | |
155 cups_option_t* options = NULL; | |
156 parse_lpoptions(system_lp_options_file, kTestPrinterName, &num_options, | |
157 &options); | |
158 ASSERT_EQ(num_options, 2); | |
159 EXPECT_EQ(num_options != 0, options != NULL); | |
160 cupsMarkOptions(ppd, num_options, options); | |
161 cupsFreeOptions(num_options, options); | |
162 | |
163 // Verify that the settings are updated as per system lpoptions. | |
164 verifyOptionValue(ppd, kDuplex, kDuplexNone); | |
165 verifyOptionValue(ppd, kColorModel, kBlack); | |
166 | |
167 // Parse the user lpoptions data. | |
168 num_options = 0; | |
169 options = NULL; | |
170 parse_lpoptions(user_lp_options_file, kTestPrinterName, &num_options, | |
171 &options); | |
172 ASSERT_EQ(num_options, 1); | |
173 EXPECT_EQ(num_options != 0, options != NULL); | |
174 cupsMarkOptions(ppd, num_options, options); | |
175 cupsFreeOptions(num_options, options); | |
176 | |
177 // Verify that the settings are updated as per user lpoptions. Make sure | |
178 // duplex setting is updated but the color setting remains the same. | |
179 verifyOptionValue(ppd, kDuplex, kDuplexNoTumble); | |
180 verifyOptionValue(ppd, kColorModel, kBlack); | |
181 ppdClose(ppd); | |
182 } | |
183 | |
184 // Test the lpoption parsing code. | |
185 TEST_F(PrintSystemTaskProxyTest, ParseLpoptionData) { | |
186 // Specifies the user lpoption data. | |
187 std::string user_lpoptions; | |
188 | |
189 // Printer A default printer settings. | |
190 user_lpoptions.append("Default printerA Duplex=None landscape=true "); | |
191 user_lpoptions.append("media=A4 Collate=True sides=two-sided-long-edge "); | |
192 user_lpoptions.append("ColorModel=Color nUp=7\n"); | |
193 | |
194 // PrinterB custom settings. | |
195 user_lpoptions.append("Dest printerB Duplex=None scaling=98 "); | |
196 user_lpoptions.append("landscape=true media=A4 collate=True\n"); | |
197 | |
198 // PrinterC has a invalid key and value but the format is valid. | |
199 user_lpoptions.append("Dest printerC invalidKey1=invalidValue1 "); | |
200 user_lpoptions.append("invalidKey2=invalidValue2 "); | |
201 user_lpoptions.append("invalidKey3=invalidValue3 \n"); | |
202 | |
203 // PrinterA instance custom settings. These settings will not override | |
204 // PrinterA settings. | |
205 user_lpoptions.append("Dest printerA/instanceA "); | |
206 user_lpoptions.append("scaling=33 Duplex=DuplexTumble landscape=true\n"); | |
207 | |
208 // PrinterD custom settings but the format is invalid because of the tab key | |
209 // delimiter. | |
210 user_lpoptions.append("Dest printerD\tDuplex=DuplexNoTumble\n"); | |
211 | |
212 // PrinterE custom settings. | |
213 user_lpoptions.append("Dest printerE Duplex=DuplexNoTumble\n"); | |
214 | |
215 ScopedTempDir temp_directory; | |
216 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
217 | |
218 // Create and write the user lpoptions to a temp file. | |
219 FilePath userLpOptionsFile; | |
220 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
221 &userLpOptionsFile)); | |
222 ASSERT_TRUE(file_util::WriteFile(userLpOptionsFile, user_lpoptions.c_str(), | |
223 user_lpoptions.size())); | |
224 std::vector<TestEntry> test_cases; | |
225 test_cases.push_back( | |
226 TestEntry("printerA", 7)); // Parse generic printer. | |
227 test_cases.push_back( | |
228 TestEntry("printerB", 5)); // Valid printer info. | |
229 test_cases.push_back( | |
230 TestEntry("printerC", 3)); // Invalid settings found. | |
231 test_cases.push_back( | |
232 TestEntry("printerD", 0)); // Tab key delimiter used. | |
233 test_cases.push_back( | |
234 TestEntry("printerE", 1)); // user specified custom settings. | |
235 test_cases.push_back( | |
236 TestEntry("printerF", 0)); // Custom settings not found. | |
237 | |
238 // Parse the lpoptions for each printer. Parse the system file followed by the | |
239 // user file. Ordering is important. | |
240 int num_options; | |
241 cups_option_t* options; | |
242 for (std::vector<TestEntry>::iterator it = test_cases.begin(); | |
243 it != test_cases.end(); ++it) { | |
244 num_options = 0; | |
245 options = NULL; | |
246 printing_internal::parse_lpoptions(userLpOptionsFile, it->printer_name, | |
247 &num_options, &options); | |
248 ASSERT_EQ(num_options, it->expected_option_count); | |
249 EXPECT_EQ(num_options != 0, options != NULL); | |
250 cupsFreeOptions(num_options, options); | |
251 } | |
252 } | |
253 #endif // !defined(OS_MACOSX) | |
254 | |
255 // Test duplex detection code, which regressed in http://crbug.com/103999. | |
256 TEST_F(PrintSystemTaskProxyTest, DetectDuplexModeCUPS) { | |
257 // Specifies the test ppd data. | |
258 printing::PrinterCapsAndDefaults printer_info; | |
259 printer_info.printer_capabilities.append( | |
260 "*PPD-Adobe: \"4.3\"\n\n" | |
261 "*OpenGroup: General/General\n\n" | |
262 "*OpenUI *Duplex/Double-Sided Printing: PickOne\n" | |
263 "*DefaultDuplex: None\n" | |
264 "*Duplex None/Off: " | |
265 "\"<</Duplex false>>setpagedevice\"\n" | |
266 "*Duplex DuplexNoTumble/Long Edge (Standard): " | |
267 "\"<</Duplex true/Tumble false>>setpagedevice\"\n" | |
268 "*Duplex DuplexTumble/Short Edge (Flip): " | |
269 "\"<</Duplex true/Tumble true>>setpagedevice\"\n" | |
270 "*CloseUI: *Duplex\n\n" | |
271 "*CloseGroup: General\n"); | |
272 | |
273 bool set_color_as_default = false; | |
274 bool set_duplex_as_default = false; | |
275 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; | |
276 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; | |
277 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; | |
278 | |
279 scoped_refptr<PrintSystemTaskProxy> proxy( | |
280 new PrintSystemTaskProxy(base::WeakPtr<PrintPreviewHandler>(), NULL, | |
281 false)); | |
282 ASSERT_TRUE(proxy->ParsePrinterCapabilities( | |
283 printer_info, | |
284 "InvalidPrinter", | |
285 &set_color_as_default, | |
286 &printer_color_space_for_color, | |
287 &printer_color_space_for_black, | |
288 &set_duplex_as_default, | |
289 &default_duplex_setting_value)); | |
290 EXPECT_FALSE(set_duplex_as_default); | |
291 EXPECT_EQ(printing::SIMPLEX, default_duplex_setting_value); | |
292 } | |
293 | |
294 TEST_F(PrintSystemTaskProxyTest, DetectNoDuplexModeCUPS) { | |
295 // Specifies the test ppd data. | |
296 printing::PrinterCapsAndDefaults printer_info; | |
297 printer_info.printer_capabilities.append( | |
298 "*PPD-Adobe: \"4.3\"\n\n" | |
299 "*OpenGroup: General/General\n\n" | |
300 "*CloseGroup: General\n"); | |
301 | |
302 bool set_color_as_default = false; | |
303 bool set_duplex_as_default = false; | |
304 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; | |
305 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; | |
306 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; | |
307 | |
308 scoped_refptr<PrintSystemTaskProxy> proxy( | |
309 new PrintSystemTaskProxy(base::WeakPtr<PrintPreviewHandler>(), NULL, | |
310 false)); | |
311 ASSERT_TRUE(proxy->ParsePrinterCapabilities( | |
312 printer_info, | |
313 "InvalidPrinter", | |
314 &set_color_as_default, | |
315 &printer_color_space_for_color, | |
316 &printer_color_space_for_black, | |
317 &set_duplex_as_default, | |
318 &default_duplex_setting_value)); | |
319 EXPECT_FALSE(set_duplex_as_default); | |
320 EXPECT_EQ(printing::UNKNOWN_DUPLEX_MODE, default_duplex_setting_value); | |
321 } | |
OLD | NEW |