OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cups/cups.h> | 5 #include <cups/cups.h> |
6 #include <cups/ppd.h> | 6 #include <cups/ppd.h> |
7 | 7 |
8 #include <cstring> | 8 #include <cstring> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/message_loop.h" |
14 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
15 #include "chrome/browser/printing/print_system_task_proxy.h" | 16 #include "chrome/browser/printing/print_system_task_proxy.h" |
| 17 #include "content/public/test/test_browser_thread.h" |
16 #include "printing/backend/print_backend.h" | 18 #include "printing/backend/print_backend.h" |
17 #include "printing/print_job_constants.h" | 19 #include "printing/print_job_constants.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
| 22 namespace { |
| 23 |
20 #if !defined(OS_MACOSX) | 24 #if !defined(OS_MACOSX) |
21 namespace { | |
22 | 25 |
23 // TestEntry stores the printer name and the expected number of options. | 26 // TestEntry stores the printer name and the expected number of options. |
24 struct TestEntry { | 27 struct TestEntry { |
25 TestEntry(std::string name, int count) | 28 TestEntry(std::string name, int count) |
26 : printer_name(name), | 29 : printer_name(name), |
27 expected_option_count(count) {} | 30 expected_option_count(count) {} |
28 | 31 |
29 std::string printer_name; | 32 std::string printer_name; |
30 int expected_option_count; | 33 int expected_option_count; |
31 }; | 34 }; |
32 | 35 |
33 // Verify the option marked in |ppd|. | 36 // Verify the option marked in |ppd|. |
34 void verifyOptionValue(ppd_file_t* ppd, | 37 void verifyOptionValue(ppd_file_t* ppd, |
35 const std::string& option_name, | 38 const std::string& option_name, |
36 const std::string& expected_choice_value) { | 39 const std::string& expected_choice_value) { |
37 ppd_choice_t* option_choice = ppdFindMarkedChoice(ppd, option_name.c_str()); | 40 ppd_choice_t* option_choice = ppdFindMarkedChoice(ppd, option_name.c_str()); |
38 if (option_choice == NULL) { | 41 if (option_choice == NULL) { |
39 ppd_option_t* option = ppdFindOption(ppd, option_name.c_str()); | 42 ppd_option_t* option = ppdFindOption(ppd, option_name.c_str()); |
40 if (option != NULL) | 43 if (option != NULL) |
41 option_choice = ppdFindChoice(option, option->defchoice); | 44 option_choice = ppdFindChoice(option, option->defchoice); |
42 } | 45 } |
43 ASSERT_TRUE(option_choice); | 46 ASSERT_TRUE(option_choice); |
44 EXPECT_EQ(strcmp(option_choice->choice, expected_choice_value.c_str()), 0); | 47 EXPECT_EQ(strcmp(option_choice->choice, expected_choice_value.c_str()), 0); |
45 } | 48 } |
46 | 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 |
47 } // namespace | 69 } // namespace |
48 | 70 |
| 71 #if !defined(OS_MACOSX) |
| 72 |
49 using printing_internal::parse_lpoptions; | 73 using printing_internal::parse_lpoptions; |
50 | 74 |
| 75 |
| 76 |
51 // Test to verify that lpoption custom settings are marked on the ppd file. | 77 // Test to verify that lpoption custom settings are marked on the ppd file. |
52 TEST(PrintSystemTaskProxyTest, MarkLpoptionsInPPD) { | 78 TEST_F(PrintSystemTaskProxyTest, MarkLpoptionsInPPD) { |
53 const std::string kColorModel = "ColorModel"; | 79 const std::string kColorModel = "ColorModel"; |
54 const std::string kBlack = "Black"; | 80 const std::string kBlack = "Black"; |
55 const std::string kGray = "Gray"; | 81 const std::string kGray = "Gray"; |
56 | 82 |
57 const std::string kDuplex = "Duplex"; | 83 const std::string kDuplex = "Duplex"; |
58 const std::string kDuplexNone = "None"; | 84 const std::string kDuplexNone = "None"; |
59 const std::string kDuplexNoTumble = "DuplexNoTumble"; | 85 const std::string kDuplexNoTumble = "DuplexNoTumble"; |
60 const std::string kDuplexTumble = "DuplexTumble"; | 86 const std::string kDuplexTumble = "DuplexTumble"; |
61 const std::string kTestPrinterName = "printerE"; | 87 const std::string kTestPrinterName = "printerE"; |
62 | 88 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 cupsFreeOptions(num_options, options); | 175 cupsFreeOptions(num_options, options); |
150 | 176 |
151 // Verify that the settings are updated as per user lpoptions. Make sure | 177 // Verify that the settings are updated as per user lpoptions. Make sure |
152 // duplex setting is updated but the color setting remains the same. | 178 // duplex setting is updated but the color setting remains the same. |
153 verifyOptionValue(ppd, kDuplex, kDuplexNoTumble); | 179 verifyOptionValue(ppd, kDuplex, kDuplexNoTumble); |
154 verifyOptionValue(ppd, kColorModel, kBlack); | 180 verifyOptionValue(ppd, kColorModel, kBlack); |
155 ppdClose(ppd); | 181 ppdClose(ppd); |
156 } | 182 } |
157 | 183 |
158 // Test the lpoption parsing code. | 184 // Test the lpoption parsing code. |
159 TEST(PrintSystemTaskProxyTest, ParseLpoptionData) { | 185 TEST_F(PrintSystemTaskProxyTest, ParseLpoptionData) { |
160 // Specifies the user lpoption data. | 186 // Specifies the user lpoption data. |
161 std::string user_lpoptions; | 187 std::string user_lpoptions; |
162 | 188 |
163 // Printer A default printer settings. | 189 // Printer A default printer settings. |
164 user_lpoptions.append("Default printerA Duplex=None landscape=true "); | 190 user_lpoptions.append("Default printerA Duplex=None landscape=true "); |
165 user_lpoptions.append("media=A4 Collate=True sides=two-sided-long-edge "); | 191 user_lpoptions.append("media=A4 Collate=True sides=two-sided-long-edge "); |
166 user_lpoptions.append("ColorModel=Color nUp=7\n"); | 192 user_lpoptions.append("ColorModel=Color nUp=7\n"); |
167 | 193 |
168 // PrinterB custom settings. | 194 // PrinterB custom settings. |
169 user_lpoptions.append("Dest printerB Duplex=None scaling=98 "); | 195 user_lpoptions.append("Dest printerB Duplex=None scaling=98 "); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 printing_internal::parse_lpoptions(userLpOptionsFile, it->printer_name, | 246 printing_internal::parse_lpoptions(userLpOptionsFile, it->printer_name, |
221 &num_options, &options); | 247 &num_options, &options); |
222 ASSERT_EQ(num_options, it->expected_option_count); | 248 ASSERT_EQ(num_options, it->expected_option_count); |
223 EXPECT_EQ(num_options != 0, options != NULL); | 249 EXPECT_EQ(num_options != 0, options != NULL); |
224 cupsFreeOptions(num_options, options); | 250 cupsFreeOptions(num_options, options); |
225 } | 251 } |
226 } | 252 } |
227 #endif // !defined(OS_MACOSX) | 253 #endif // !defined(OS_MACOSX) |
228 | 254 |
229 // Test duplex detection code, which regressed in http://crbug.com/103999. | 255 // Test duplex detection code, which regressed in http://crbug.com/103999. |
230 TEST(PrintSystemTaskProxyTest, DetectDuplexModeCUPS) { | 256 TEST_F(PrintSystemTaskProxyTest, DetectDuplexModeCUPS) { |
231 // Specifies the test ppd data. | 257 // Specifies the test ppd data. |
232 printing::PrinterCapsAndDefaults printer_info; | 258 printing::PrinterCapsAndDefaults printer_info; |
233 printer_info.printer_capabilities.append( | 259 printer_info.printer_capabilities.append( |
234 "*PPD-Adobe: \"4.3\"\n\n" | 260 "*PPD-Adobe: \"4.3\"\n\n" |
235 "*OpenGroup: General/General\n\n" | 261 "*OpenGroup: General/General\n\n" |
236 "*OpenUI *Duplex/Double-Sided Printing: PickOne\n" | 262 "*OpenUI *Duplex/Double-Sided Printing: PickOne\n" |
237 "*DefaultDuplex: None\n" | 263 "*DefaultDuplex: None\n" |
238 "*Duplex None/Off: " | 264 "*Duplex None/Off: " |
239 "\"<</Duplex false>>setpagedevice\"\n" | 265 "\"<</Duplex false>>setpagedevice\"\n" |
240 "*Duplex DuplexNoTumble/Long Edge (Standard): " | 266 "*Duplex DuplexNoTumble/Long Edge (Standard): " |
241 "\"<</Duplex true/Tumble false>>setpagedevice\"\n" | 267 "\"<</Duplex true/Tumble false>>setpagedevice\"\n" |
242 "*Duplex DuplexTumble/Short Edge (Flip): " | 268 "*Duplex DuplexTumble/Short Edge (Flip): " |
243 "\"<</Duplex true/Tumble true>>setpagedevice\"\n" | 269 "\"<</Duplex true/Tumble true>>setpagedevice\"\n" |
244 "*CloseUI: *Duplex\n\n" | 270 "*CloseUI: *Duplex\n\n" |
245 "*CloseGroup: General\n"); | 271 "*CloseGroup: General\n"); |
246 | 272 |
247 bool set_color_as_default = false; | 273 bool set_color_as_default = false; |
248 bool set_duplex_as_default = false; | 274 bool set_duplex_as_default = false; |
249 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; | 275 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; |
250 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; | 276 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; |
251 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; | 277 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; |
252 | 278 |
253 scoped_refptr<PrintSystemTaskProxy> proxy( | 279 scoped_refptr<PrintSystemTaskProxy> proxy( |
254 new PrintSystemTaskProxy(base::WeakPtr<PrintPreviewHandler>(), NULL, | 280 new PrintSystemTaskProxy(base::WeakPtr<PrintPreviewHandler>(), NULL, |
255 false)); | 281 false)); |
256 | |
257 ASSERT_TRUE(proxy->ParsePrinterCapabilities( | 282 ASSERT_TRUE(proxy->ParsePrinterCapabilities( |
258 printer_info, | 283 printer_info, |
259 "InvalidPrinter", | 284 "InvalidPrinter", |
260 &set_color_as_default, | 285 &set_color_as_default, |
261 &printer_color_space_for_color, | 286 &printer_color_space_for_color, |
262 &printer_color_space_for_black, | 287 &printer_color_space_for_black, |
263 &set_duplex_as_default, | 288 &set_duplex_as_default, |
264 &default_duplex_setting_value)); | 289 &default_duplex_setting_value)); |
265 EXPECT_FALSE(set_duplex_as_default); | 290 EXPECT_FALSE(set_duplex_as_default); |
266 EXPECT_EQ(printing::SIMPLEX, default_duplex_setting_value); | 291 EXPECT_EQ(printing::SIMPLEX, default_duplex_setting_value); |
267 } | 292 } |
268 | 293 |
269 TEST(PrintSystemTaskProxyTest, DetectNoDuplexModeCUPS) { | 294 TEST_F(PrintSystemTaskProxyTest, DetectNoDuplexModeCUPS) { |
270 // Specifies the test ppd data. | 295 // Specifies the test ppd data. |
271 printing::PrinterCapsAndDefaults printer_info; | 296 printing::PrinterCapsAndDefaults printer_info; |
272 printer_info.printer_capabilities.append( | 297 printer_info.printer_capabilities.append( |
273 "*PPD-Adobe: \"4.3\"\n\n" | 298 "*PPD-Adobe: \"4.3\"\n\n" |
274 "*OpenGroup: General/General\n\n" | 299 "*OpenGroup: General/General\n\n" |
275 "*CloseGroup: General\n"); | 300 "*CloseGroup: General\n"); |
276 | 301 |
277 bool set_color_as_default = false; | 302 bool set_color_as_default = false; |
278 bool set_duplex_as_default = false; | 303 bool set_duplex_as_default = false; |
279 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; | 304 int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL; |
280 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; | 305 int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL; |
281 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; | 306 int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE; |
282 | 307 |
283 scoped_refptr<PrintSystemTaskProxy> proxy( | 308 scoped_refptr<PrintSystemTaskProxy> proxy( |
284 new PrintSystemTaskProxy(base::WeakPtr<PrintPreviewHandler>(), NULL, | 309 new PrintSystemTaskProxy(base::WeakPtr<PrintPreviewHandler>(), NULL, |
285 false)); | 310 false)); |
286 | |
287 ASSERT_TRUE(proxy->ParsePrinterCapabilities( | 311 ASSERT_TRUE(proxy->ParsePrinterCapabilities( |
288 printer_info, | 312 printer_info, |
289 "InvalidPrinter", | 313 "InvalidPrinter", |
290 &set_color_as_default, | 314 &set_color_as_default, |
291 &printer_color_space_for_color, | 315 &printer_color_space_for_color, |
292 &printer_color_space_for_black, | 316 &printer_color_space_for_black, |
293 &set_duplex_as_default, | 317 &set_duplex_as_default, |
294 &default_duplex_setting_value)); | 318 &default_duplex_setting_value)); |
295 EXPECT_FALSE(set_duplex_as_default); | 319 EXPECT_FALSE(set_duplex_as_default); |
296 EXPECT_EQ(printing::UNKNOWN_DUPLEX_MODE, default_duplex_setting_value); | 320 EXPECT_EQ(printing::UNKNOWN_DUPLEX_MODE, default_duplex_setting_value); |
297 } | 321 } |
OLD | NEW |