summaryrefslogtreecommitdiffstats
path: root/chrome/browser/printing/print_system_task_proxy_unittest.cc
blob: 9876583b0c2f949abec29e715d6c21747e335296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cups/cups.h>
#include <cups/ppd.h>

#include <cstring>
#include <string>
#include <vector>

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/scoped_temp_dir.h"
#include "chrome/browser/printing/print_system_task_proxy.h"
#include "printing/backend/print_backend.h"
#include "printing/print_job_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

#if !defined(OS_MACOSX)
namespace {

// TestEntry stores the printer name and the expected number of options.
struct TestEntry {
  TestEntry(std::string name, int count)
      : printer_name(name),
        expected_option_count(count) {}

  std::string printer_name;
  int expected_option_count;
};

// Verify the option marked in |ppd|.
void verifyOptionValue(ppd_file_t* ppd,
                       const std::string& option_name,
                       const std::string& expected_choice_value) {
  ppd_choice_t* option_choice = ppdFindMarkedChoice(ppd, option_name.c_str());
  if (option_choice == NULL) {
    ppd_option_t* option = ppdFindOption(ppd, option_name.c_str());
    if (option != NULL)
      option_choice = ppdFindChoice(option, option->defchoice);
  }
  ASSERT_TRUE(option_choice);
  EXPECT_EQ(strcmp(option_choice->choice, expected_choice_value.c_str()), 0);
}

}  // namespace

using printing_internal::parse_lpoptions;

// Test to verify that lpoption custom settings are marked on the ppd file.
TEST(PrintSystemTaskProxyTest, MarkLpoptionsInPPD) {
  const std::string kColorModel = "ColorModel";
  const std::string kBlack = "Black";
  const std::string kGray = "Gray";

  const std::string kDuplex = "Duplex";
  const std::string kDuplexNone = "None";
  const std::string kDuplexNoTumble = "DuplexNoTumble";
  const std::string kDuplexTumble = "DuplexTumble";
  const std::string kTestPrinterName = "printerE";

  ScopedTempDir temp_directory;
  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());

  std::string system_lpoptions;  // Specifies the system lpoption data.
  system_lpoptions.append("Dest printerE ColorModel=Black Duplex=");
  system_lpoptions.append(kDuplexNone+" ");

  // Create and write the system lpoptions to a temp file.
  FilePath system_lp_options_file;
  ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
                                                  &system_lp_options_file));
  ASSERT_TRUE(file_util::WriteFile(system_lp_options_file,
                                   system_lpoptions.c_str(),
                                   system_lpoptions.size()));

  // Specifies the user lpoption data.
  std::string user_lpoptions;
  user_lpoptions.append("Dest printerE          Duplex="+kDuplexNoTumble+"\n");

  // Create and write the user lpoptions to a temp file.
  FilePath user_lp_options_file;
  ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
                                                  &user_lp_options_file));
  ASSERT_TRUE(file_util::WriteFile(user_lp_options_file, user_lpoptions.c_str(),
                                   user_lpoptions.size()));
  // Specifies the test ppd data.
  std::string test_ppd_data;
  test_ppd_data.append("*PPD-Adobe: \"4.3\"\n\n"
                       "*OpenGroup: General/General\n\n"
                       "*OpenUI *ColorModel/Color Model: PickOne\n"
                       "*DefaultColorModel: Gray\n"
                       "*ColorModel Gray/Grayscale: \""
                       "<</cupsColorSpace 0/cupsColorOrder 0>>"
                       "setpagedevice\"\n"
                       "*ColorModel Black/Inverted Grayscale: \""
                       "<</cupsColorSpace 3/cupsColorOrder 0>>"
                       "setpagedevice\"\n"
                       "*CloseUI: *ColorModel\n"
                       "*OpenUI *Duplex/2-Sided Printing: PickOne\n"
                       "*DefaultDuplex: DuplexTumble\n"
                       "*Duplex None/Off: \"<</Duplex false>>"
                       "setpagedevice\"\n"
                       "*Duplex DuplexNoTumble/LongEdge: \""
                       "<</Duplex true/Tumble false>>setpagedevice\"\n"
                       "*Duplex DuplexTumble/ShortEdge: \""
                       "<</Duplex true/Tumble true>>setpagedevice\"\n"
                       "*CloseUI: *Duplex\n\n"
                       "*CloseGroup: General\n");

  // Create a test ppd file.
  FilePath ppd_file_path;
  ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
                                                  &ppd_file_path));
  ASSERT_TRUE(file_util::WriteFile(ppd_file_path, test_ppd_data.c_str(),
                                   test_ppd_data.size()));

  ppd_file_t* ppd = ppdOpenFile(ppd_file_path.value().c_str());
  ASSERT_TRUE(ppd);
  ppdMarkDefaults(ppd);

  // Verify the default settings.
  verifyOptionValue(ppd, kDuplex, kDuplexTumble);
  verifyOptionValue(ppd, kColorModel, kGray);

  // Parse the system lpoptions data.
  int num_options = 0;
  cups_option_t* options = NULL;
  parse_lpoptions(system_lp_options_file, kTestPrinterName, &num_options,
                  &options);
  ASSERT_EQ(num_options, 2);
  EXPECT_EQ(num_options != 0, options != NULL);
  cupsMarkOptions(ppd, num_options, options);
  cupsFreeOptions(num_options, options);

  // Verify that the settings are updated as per system lpoptions.
  verifyOptionValue(ppd, kDuplex, kDuplexNone);
  verifyOptionValue(ppd, kColorModel, kBlack);

  // Parse the user lpoptions data.
  num_options = 0;
  options = NULL;
  parse_lpoptions(user_lp_options_file, kTestPrinterName, &num_options,
                  &options);
  ASSERT_EQ(num_options, 1);
  EXPECT_EQ(num_options != 0, options != NULL);
  cupsMarkOptions(ppd, num_options, options);
  cupsFreeOptions(num_options, options);

  // Verify that the settings are updated as per user lpoptions. Make sure
  // duplex setting is updated but the color setting remains the same.
  verifyOptionValue(ppd, kDuplex, kDuplexNoTumble);
  verifyOptionValue(ppd, kColorModel, kBlack);
  ppdClose(ppd);
}

// Test the lpoption parsing code.
TEST(PrintSystemTaskProxyTest, ParseLpoptionData) {
  // Specifies the user lpoption data.
  std::string user_lpoptions;

  // Printer A default printer settings.
  user_lpoptions.append("Default printerA Duplex=None landscape=true ");
  user_lpoptions.append("media=A4 Collate=True sides=two-sided-long-edge ");
  user_lpoptions.append("ColorModel=Color nUp=7\n");

  // PrinterB custom settings.
  user_lpoptions.append("Dest printerB Duplex=None scaling=98 ");
  user_lpoptions.append("landscape=true media=A4 collate=True\n");

  // PrinterC has a invalid key and value but the format is valid.
  user_lpoptions.append("Dest printerC invalidKey1=invalidValue1 ");
  user_lpoptions.append("invalidKey2=invalidValue2 ");
  user_lpoptions.append("invalidKey3=invalidValue3 \n");

  // PrinterA instance custom settings. These settings will not override
  // PrinterA settings.
  user_lpoptions.append("Dest printerA/instanceA ");
  user_lpoptions.append("scaling=33 Duplex=DuplexTumble landscape=true\n");

  // PrinterD custom settings but the format is invalid because of the tab key
  // delimiter.
  user_lpoptions.append("Dest printerD\tDuplex=DuplexNoTumble\n");

  // PrinterE custom settings.
  user_lpoptions.append("Dest printerE          Duplex=DuplexNoTumble\n");

  ScopedTempDir temp_directory;
  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());

  // Create and write the user lpoptions to a temp file.
  FilePath userLpOptionsFile;
  ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
                                                  &userLpOptionsFile));
  ASSERT_TRUE(file_util::WriteFile(userLpOptionsFile, user_lpoptions.c_str(),
                                   user_lpoptions.size()));
  std::vector<TestEntry> test_cases;
  test_cases.push_back(
      TestEntry("printerA", 7));  // Parse generic printer.
  test_cases.push_back(
      TestEntry("printerB", 5));  // Valid printer info.
  test_cases.push_back(
      TestEntry("printerC", 3));  // Invalid settings found.
  test_cases.push_back(
      TestEntry("printerD", 0));  // Tab key delimiter used.
  test_cases.push_back(
      TestEntry("printerE", 1));  // user specified custom settings.
  test_cases.push_back(
      TestEntry("printerF", 0));  // Custom settings not found.

  // Parse the lpoptions for each printer. Parse the system file followed by the
  // user file. Ordering is important.
  int num_options;
  cups_option_t* options;
  for (std::vector<TestEntry>::iterator it = test_cases.begin();
       it != test_cases.end(); ++it) {
    num_options = 0;
    options = NULL;
    printing_internal::parse_lpoptions(userLpOptionsFile, it->printer_name,
                                       &num_options, &options);
    ASSERT_EQ(num_options, it->expected_option_count);
    EXPECT_EQ(num_options != 0, options != NULL);
    cupsFreeOptions(num_options, options);
  }
}
#endif  // !defined(OS_MACOSX)

// Test duplex detection code, which regressed in http://crbug.com/103999.
TEST(PrintSystemTaskProxyTest, DetectDuplexModeCUPS) {
  // Specifies the test ppd data.
  printing::PrinterCapsAndDefaults printer_info;
  printer_info.printer_capabilities.append(
      "*PPD-Adobe: \"4.3\"\n\n"
      "*OpenGroup: General/General\n\n"
      "*OpenUI *Duplex/Double-Sided Printing: PickOne\n"
      "*DefaultDuplex: None\n"
      "*Duplex None/Off: "
      "\"<</Duplex false>>setpagedevice\"\n"
      "*Duplex DuplexNoTumble/Long Edge (Standard): "
      "\"<</Duplex true/Tumble false>>setpagedevice\"\n"
      "*Duplex DuplexTumble/Short Edge (Flip): "
      "\"<</Duplex true/Tumble true>>setpagedevice\"\n"
      "*CloseUI: *Duplex\n\n"
      "*CloseGroup: General\n");

  bool set_color_as_default = false;
  bool set_duplex_as_default = false;
  int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL;
  int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL;
  int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE;

  bool res = PrintSystemTaskProxy::GetPrinterCapabilitiesCUPS(
      printer_info,
      "InvalidPrinter",
      &set_color_as_default,
      &printer_color_space_for_color,
      &printer_color_space_for_black,
      &set_duplex_as_default,
      &default_duplex_setting_value);
  ASSERT_TRUE(res);
  EXPECT_FALSE(set_duplex_as_default);
  EXPECT_EQ(printing::SIMPLEX, default_duplex_setting_value);
}

TEST(PrintSystemTaskProxyTest, DetectNoDuplexModeCUPS) {
  // Specifies the test ppd data.
  printing::PrinterCapsAndDefaults printer_info;
  printer_info.printer_capabilities.append(
      "*PPD-Adobe: \"4.3\"\n\n"
      "*OpenGroup: General/General\n\n"
      "*CloseGroup: General\n");

  bool set_color_as_default = false;
  bool set_duplex_as_default = false;
  int printer_color_space_for_color = printing::UNKNOWN_COLOR_MODEL;
  int printer_color_space_for_black = printing::UNKNOWN_COLOR_MODEL;
  int default_duplex_setting_value = printing::UNKNOWN_DUPLEX_MODE;

  bool res = PrintSystemTaskProxy::GetPrinterCapabilitiesCUPS(
      printer_info,
      "InvalidPrinter",
      &set_color_as_default,
      &printer_color_space_for_color,
      &printer_color_space_for_black,
      &set_duplex_as_default,
      &default_duplex_setting_value);
  ASSERT_TRUE(res);
  EXPECT_FALSE(set_duplex_as_default);
  EXPECT_EQ(printing::UNKNOWN_DUPLEX_MODE, default_duplex_setting_value);
}