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