OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 <cstring> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "base/file_util.h" | |
13 #include "base/scoped_temp_dir.h" | |
14 #include "chrome/browser/printing/print_system_task_proxy.h" | |
15 #include "printing/backend/print_backend.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace { | |
19 | |
20 // TestEntry stores the printer name and the expected number of options. | |
21 struct TestEntry { | |
22 TestEntry(std::string name, int count) | |
23 : printer_name(name), | |
24 expected_option_count(count) {} | |
25 | |
26 std::string printer_name; | |
27 int expected_option_count; | |
28 }; | |
29 | |
30 // Verify the option marked in |ppd|. | |
31 void verifyOptionValue(ppd_file_t* ppd, | |
32 const std::string& option_name, | |
33 const std::string& expected_choice_value) { | |
34 ppd_choice_t* option_choice = ppdFindMarkedChoice(ppd, option_name.c_str()); | |
35 if (option_choice == NULL) { | |
36 ppd_option_t* option = ppdFindOption(ppd, option_name.c_str()); | |
37 if (option != NULL) | |
38 option_choice = ppdFindChoice(option, option->defchoice); | |
39 } | |
40 ASSERT_TRUE(option_choice); | |
41 EXPECT_EQ(strcmp(option_choice->choice, expected_choice_value.c_str()), 0); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 using printing_internal::parse_lpoptions; | |
47 | |
48 // Test to verify that lpoption custom settings are marked on the ppd file. | |
49 TEST(PrintSystemTaskProxyTest, MarkLpoptionsInPPD) { | |
50 const std::string kColorModel = "ColorModel"; | |
51 const std::string kBlack = "Black"; | |
52 const std::string kGray = "Gray"; | |
53 | |
54 const std::string kDuplex = "Duplex"; | |
55 const std::string kDuplexNone = "None"; | |
56 const std::string kDuplexNoTumble = "DuplexNoTumble"; | |
57 const std::string kDuplexTumble = "DuplexTumble"; | |
58 const std::string kTestPrinterName = "printerE"; | |
59 | |
60 ScopedTempDir temp_directory; | |
61 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
62 | |
63 std::string system_lpoptions; // Specifies the system lpoption data. | |
64 system_lpoptions.append("Dest printerE ColorModel=Black Duplex="); | |
65 system_lpoptions.append(kDuplexNone+" "); | |
66 | |
67 // Create and write the system lpoptions to a temp file. | |
68 FilePath system_lp_options_file; | |
69 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
70 &system_lp_options_file)); | |
71 ASSERT_TRUE(file_util::WriteFile(system_lp_options_file, | |
72 system_lpoptions.c_str(), | |
73 system_lpoptions.size())); | |
74 | |
75 // Specifies the user lpoption data. | |
76 std::string user_lpoptions; | |
77 user_lpoptions.append("Dest printerE Duplex="+kDuplexNoTumble+"\n"); | |
78 | |
79 // Create and write the user lpoptions to a temp file. | |
80 FilePath user_lp_options_file; | |
81 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
82 &user_lp_options_file)); | |
83 ASSERT_TRUE(file_util::WriteFile(user_lp_options_file, user_lpoptions.c_str(), | |
84 user_lpoptions.size())); | |
85 // Specifies the test ppd data. | |
86 std::string test_ppd_data; | |
87 test_ppd_data.append("*PPD-Adobe: \"4.3\"\n"); | |
88 test_ppd_data.append("\n*OpenGroup: General/General\n\n"); | |
89 test_ppd_data.append("*OpenUI *ColorModel/Color Model: PickOne\n"); | |
90 test_ppd_data.append("*DefaultColorModel: Gray\n"); | |
91 test_ppd_data.append("*ColorModel Gray/Grayscale: \""); | |
92 test_ppd_data.append("<</cupsColorSpace 0/cupsColorOrder 0>>"); | |
93 test_ppd_data.append("setpagedevice\"\n"); | |
94 test_ppd_data.append("*ColorModel Black/Inverted Grayscale: \""); | |
95 test_ppd_data.append("<</cupsColorSpace 3/cupsColorOrder 0>>"); | |
96 test_ppd_data.append("setpagedevice\"\n"); | |
97 test_ppd_data.append("*CloseUI: *ColorModel\n"); | |
98 test_ppd_data.append("*OpenUI *Duplex/2-Sided Printing: PickOne\n"); | |
99 test_ppd_data.append("*DefaultDuplex: DuplexTumble\n"); | |
100 test_ppd_data.append("*Duplex None/Off: \"<</Duplex false>>"); | |
101 test_ppd_data.append("setpagedevice\"\n"); | |
102 test_ppd_data.append("*Duplex DuplexNoTumble/LongEdge: \""); | |
103 test_ppd_data.append("<</Duplex true/Tumble false>>setpagedevice\"\n"); | |
104 test_ppd_data.append("*Duplex DuplexTumble/ShortEdge: \""); | |
105 test_ppd_data.append("<</Duplex true/Tumble true>>setpagedevice\"\n"); | |
106 test_ppd_data.append("*CloseUI: *Duplex\n\n"); | |
107 test_ppd_data.append("*CloseGroup: General\n"); | |
108 | |
109 // Create a test ppd file. | |
110 FilePath ppd_file_path; | |
111 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
112 &ppd_file_path)); | |
113 ASSERT_TRUE(file_util::WriteFile(ppd_file_path, test_ppd_data.c_str(), | |
114 test_ppd_data.size())); | |
115 | |
116 ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str()); | |
117 ASSERT_TRUE(ppd); | |
118 ppdMarkDefaults(ppd); | |
119 | |
120 // Verify the default settings. | |
121 verifyOptionValue(ppd, kDuplex, kDuplexTumble); | |
122 verifyOptionValue(ppd, kColorModel, kGray); | |
123 | |
124 // Parse the system lpoptions data. | |
125 int num_options = 0; | |
126 cups_option_t* options = NULL; | |
127 parse_lpoptions(system_lp_options_file, kTestPrinterName, &num_options, | |
128 &options); | |
129 ASSERT_EQ(num_options, 2); | |
130 EXPECT_EQ(num_options != 0, options != NULL); | |
131 cupsMarkOptions(ppd, num_options, options); | |
132 cupsFreeOptions(num_options, options); | |
133 | |
134 // Verify that the settings are updated as per system lpoptions. | |
135 verifyOptionValue(ppd, kDuplex, kDuplexNone); | |
136 verifyOptionValue(ppd, kColorModel, kBlack); | |
137 | |
138 // Parse the user lpoptions data. | |
139 num_options = 0; | |
140 options = NULL; | |
141 parse_lpoptions(user_lp_options_file, kTestPrinterName, &num_options, | |
142 &options); | |
143 ASSERT_EQ(num_options, 1); | |
144 EXPECT_EQ(num_options != 0, options != NULL); | |
145 cupsMarkOptions(ppd, num_options, options); | |
146 cupsFreeOptions(num_options, options); | |
147 | |
148 // Verify that the settings are updated as per user lpoptions. Make sure | |
149 // duplex setting is updated but the color setting remains the same. | |
150 verifyOptionValue(ppd, kDuplex, kDuplexNoTumble); | |
151 verifyOptionValue(ppd, kColorModel, kBlack); | |
152 ppdClose(ppd); | |
153 } | |
154 | |
155 // Test the lpoption parsing code. | |
156 TEST(PrintSystemTaskProxyTest, ParseLpoptionData) { | |
157 // Specifies the user lpoption data. | |
158 std::string user_lpoptions; | |
159 | |
160 // Printer A default printer settings. | |
161 user_lpoptions.append("Default printerA Duplex=None landscape=true "); | |
162 user_lpoptions.append("media=A4 Collate=True sides=two-sided-long-edge "); | |
163 user_lpoptions.append("ColorModel=Color nUp=7\n"); | |
164 | |
165 // PrinterB custom settings. | |
166 user_lpoptions.append("Dest printerB Duplex=None scaling=98 "); | |
167 user_lpoptions.append("landscape=true media=A4 collate=True\n"); | |
168 | |
169 // PrinterC has a invalid key and value but the format is valid. | |
170 user_lpoptions.append("Dest printerC invalidKey1=invalidValue1 "); | |
171 user_lpoptions.append("invalidKey2=invalidValue2 "); | |
172 user_lpoptions.append("invalidKey3=invalidValue3 \n"); | |
173 | |
174 // PrinterA instance custom settings. These settings will not override | |
175 // PrinterA settings. | |
176 user_lpoptions.append("Dest printerA/instanceA "); | |
177 user_lpoptions.append("scaling=33 Duplex=DuplexTumble landscape=true\n"); | |
178 | |
179 // PrinterD custom settings but the format is invalid because of the tab key | |
180 // delimiter. | |
181 user_lpoptions.append("Dest printerD\tDuplex=DuplexNoTumble\n"); | |
182 | |
183 // PrinterE custom settings. | |
184 user_lpoptions.append("Dest printerE Duplex=DuplexNoTumble\n"); | |
185 | |
186 ScopedTempDir temp_directory; | |
187 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
188 | |
189 // Create and write the user lpoptions to a temp file. | |
190 FilePath userLpOptionsFile; | |
191 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(), | |
192 &userLpOptionsFile)); | |
193 ASSERT_TRUE(file_util::WriteFile(userLpOptionsFile, user_lpoptions.c_str(), | |
194 user_lpoptions.size())); | |
195 std::vector<TestEntry> test_cases; | |
196 test_cases.push_back( | |
197 TestEntry("printerA", 7)); // Parse generic printer. | |
198 test_cases.push_back( | |
199 TestEntry("printerB", 5)); // Valid printer info. | |
200 test_cases.push_back( | |
201 TestEntry("printerC", 3)); // Invalid settings found. | |
202 test_cases.push_back( | |
203 TestEntry("printerD", 0)); // Tab key delimiter used. | |
204 test_cases.push_back( | |
205 TestEntry("printerE", 1)); // user specified custom settings. | |
206 test_cases.push_back( | |
207 TestEntry("printerF", 0)); // Custom settings not found. | |
208 | |
209 // Parse the lpoptions for each printer. Parse the system file followed by the | |
210 // user file. Ordering is important. | |
211 int num_options; | |
212 cups_option_t* options; | |
213 for (std::vector<TestEntry>::iterator it = test_cases.begin(); | |
214 it != test_cases.end(); ++it) { | |
215 num_options = 0; | |
216 options = NULL; | |
217 printing_internal::parse_lpoptions(userLpOptionsFile, it->printer_name, | |
218 &num_options, &options); | |
219 ASSERT_EQ(num_options, it->expected_option_count); | |
220 EXPECT_EQ(num_options != 0, options != NULL); | |
221 cupsFreeOptions(num_options, options); | |
222 } | |
223 } | |
OLD | NEW |