Speculative fix for the white/stale tab issue on Windows.
[chromium-blink-merge.git] / base / command_line_unittest.cc
blob372af37a3d12c3d2274ad68b7db6ee84b3a81b1f
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.
5 #include <string>
6 #include <vector>
8 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/file_path.h"
11 #include "base/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // To test Windows quoting behavior, we use a string that has some backslashes
15 // and quotes.
16 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
17 // Here it is with C-style escapes.
18 static const CommandLine::StringType kTrickyQuoted =
19 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
20 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
21 // Here that is with C-style escapes.
22 static const CommandLine::StringType kTricky =
23 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
25 TEST(CommandLineTest, CommandLineConstructor) {
26 const CommandLine::CharType* argv[] = {
27 FILE_PATH_LITERAL("program"),
28 FILE_PATH_LITERAL("--foo="),
29 FILE_PATH_LITERAL("-bAr"),
30 FILE_PATH_LITERAL("-spaetzel=pierogi"),
31 FILE_PATH_LITERAL("-baz"),
32 FILE_PATH_LITERAL("flim"),
33 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
34 FILE_PATH_LITERAL("-spaetzle=Crepe"),
35 FILE_PATH_LITERAL("-=loosevalue"),
36 FILE_PATH_LITERAL("-"),
37 FILE_PATH_LITERAL("FLAN"),
38 FILE_PATH_LITERAL("a"),
39 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
40 FILE_PATH_LITERAL("--"),
41 FILE_PATH_LITERAL("--"),
42 FILE_PATH_LITERAL("--not-a-switch"),
43 FILE_PATH_LITERAL("\"in the time of submarines...\""),
44 FILE_PATH_LITERAL("unquoted arg-with-space")};
45 CommandLine cl(arraysize(argv), argv);
47 EXPECT_FALSE(cl.GetCommandLineString().empty());
48 EXPECT_FALSE(cl.HasSwitch("cruller"));
49 EXPECT_FALSE(cl.HasSwitch("flim"));
50 EXPECT_FALSE(cl.HasSwitch("program"));
51 EXPECT_FALSE(cl.HasSwitch("dog"));
52 EXPECT_FALSE(cl.HasSwitch("cat"));
53 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
54 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
55 EXPECT_FALSE(cl.HasSwitch("--"));
57 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
58 cl.GetProgram().value());
60 EXPECT_TRUE(cl.HasSwitch("foo"));
61 EXPECT_TRUE(cl.HasSwitch("bAr"));
62 EXPECT_TRUE(cl.HasSwitch("baz"));
63 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
64 #if defined(OS_WIN)
65 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
66 #endif
67 EXPECT_TRUE(cl.HasSwitch("other-switches"));
68 EXPECT_TRUE(cl.HasSwitch("input-translation"));
70 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
71 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
72 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
73 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
74 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
75 "other-switches"));
76 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
78 const CommandLine::StringVector& args = cl.GetArgs();
79 ASSERT_EQ(8U, args.size());
81 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
82 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
83 ++iter;
84 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
85 ++iter;
86 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
87 ++iter;
88 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
89 ++iter;
90 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
91 ++iter;
92 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
93 ++iter;
94 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
95 ++iter;
96 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
97 ++iter;
98 EXPECT_TRUE(iter == args.end());
101 TEST(CommandLineTest, CommandLineFromString) {
102 #if defined(OS_WIN)
103 CommandLine cl = CommandLine::FromString(
104 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
105 L"--other-switches=\"--dog=canine --cat=feline\" "
106 L"-spaetzle=Crepe -=loosevalue FLAN "
107 L"--input-translation=\"45\"--output-rotation "
108 L"--quotes=" + kTrickyQuoted + L" "
109 L"-- -- --not-a-switch "
110 L"\"in the time of submarines...\"");
112 EXPECT_FALSE(cl.GetCommandLineString().empty());
113 EXPECT_FALSE(cl.HasSwitch("cruller"));
114 EXPECT_FALSE(cl.HasSwitch("flim"));
115 EXPECT_FALSE(cl.HasSwitch("program"));
116 EXPECT_FALSE(cl.HasSwitch("dog"));
117 EXPECT_FALSE(cl.HasSwitch("cat"));
118 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
119 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
120 EXPECT_FALSE(cl.HasSwitch("--"));
122 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
123 cl.GetProgram().value());
125 EXPECT_TRUE(cl.HasSwitch("foo"));
126 EXPECT_TRUE(cl.HasSwitch("bar"));
127 EXPECT_TRUE(cl.HasSwitch("baz"));
128 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
129 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
130 EXPECT_TRUE(cl.HasSwitch("other-switches"));
131 EXPECT_TRUE(cl.HasSwitch("input-translation"));
132 EXPECT_TRUE(cl.HasSwitch("quotes"));
134 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
135 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
136 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
137 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
138 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
139 "other-switches"));
140 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
141 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
143 const CommandLine::StringVector& args = cl.GetArgs();
144 ASSERT_EQ(5U, args.size());
146 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
147 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
148 ++iter;
149 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
150 ++iter;
151 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
152 ++iter;
153 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
154 ++iter;
155 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
156 ++iter;
157 EXPECT_TRUE(iter == args.end());
159 // Check that a generated string produces an equivalent command line.
160 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
161 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
162 #endif
165 // Tests behavior with an empty input string.
166 TEST(CommandLineTest, EmptyString) {
167 #if defined(OS_WIN)
168 CommandLine cl_from_string = CommandLine::FromString(L"");
169 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
170 EXPECT_TRUE(cl_from_string.GetProgram().empty());
171 EXPECT_EQ(1U, cl_from_string.argv().size());
172 EXPECT_TRUE(cl_from_string.GetArgs().empty());
173 #endif
174 CommandLine cl_from_argv(0, NULL);
175 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
176 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
177 EXPECT_EQ(1U, cl_from_argv.argv().size());
178 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
181 TEST(CommandLineTest, GetArgumentsString) {
182 static const FilePath::CharType kPath1[] =
183 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
184 static const FilePath::CharType kPath2[] =
185 FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
187 static const char kFirstArgName[] = "first-arg";
188 static const char kSecondArgName[] = "arg2";
189 static const char kThirdArgName[] = "arg with space";
190 static const char kFourthArgName[] = "nospace";
192 CommandLine cl(CommandLine::NO_PROGRAM);
193 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
194 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
195 cl.AppendArg(kThirdArgName);
196 cl.AppendArg(kFourthArgName);
198 #if defined(OS_WIN)
199 CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName));
200 CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName));
201 CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName));
202 CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName));
203 #elif defined(OS_POSIX)
204 CommandLine::StringType expected_first_arg(kFirstArgName);
205 CommandLine::StringType expected_second_arg(kSecondArgName);
206 CommandLine::StringType expected_third_arg(kThirdArgName);
207 CommandLine::StringType expected_fourth_arg(kFourthArgName);
208 #endif
210 #if defined(OS_WIN)
211 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
212 #else
213 #define QUOTE_ON_WIN FILE_PATH_LITERAL("")
214 #endif // OS_WIN
216 CommandLine::StringType expected_str;
217 expected_str.append(FILE_PATH_LITERAL("--"))
218 .append(expected_first_arg)
219 .append(FILE_PATH_LITERAL("="))
220 .append(QUOTE_ON_WIN)
221 .append(kPath1)
222 .append(QUOTE_ON_WIN)
223 .append(FILE_PATH_LITERAL(" "))
224 .append(FILE_PATH_LITERAL("--"))
225 .append(expected_second_arg)
226 .append(FILE_PATH_LITERAL("="))
227 .append(QUOTE_ON_WIN)
228 .append(kPath2)
229 .append(QUOTE_ON_WIN)
230 .append(FILE_PATH_LITERAL(" "))
231 .append(QUOTE_ON_WIN)
232 .append(expected_third_arg)
233 .append(QUOTE_ON_WIN)
234 .append(FILE_PATH_LITERAL(" "))
235 .append(expected_fourth_arg);
236 EXPECT_EQ(expected_str, cl.GetArgumentsString());
239 // Test methods for appending switches to a command line.
240 TEST(CommandLineTest, AppendSwitches) {
241 std::string switch1 = "switch1";
242 std::string switch2 = "switch2";
243 std::string value2 = "value";
244 std::string switch3 = "switch3";
245 std::string value3 = "a value with spaces";
246 std::string switch4 = "switch4";
247 std::string value4 = "\"a value with quotes\"";
248 std::string switch5 = "quotes";
249 CommandLine::StringType value5 = kTricky;
251 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
253 cl.AppendSwitch(switch1);
254 cl.AppendSwitchASCII(switch2, value2);
255 cl.AppendSwitchASCII(switch3, value3);
256 cl.AppendSwitchASCII(switch4, value4);
257 cl.AppendSwitchNative(switch5, value5);
259 EXPECT_TRUE(cl.HasSwitch(switch1));
260 EXPECT_TRUE(cl.HasSwitch(switch2));
261 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
262 EXPECT_TRUE(cl.HasSwitch(switch3));
263 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
264 EXPECT_TRUE(cl.HasSwitch(switch4));
265 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
266 EXPECT_TRUE(cl.HasSwitch(switch5));
267 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
269 #if defined(OS_WIN)
270 EXPECT_EQ(L"Program "
271 L"--switch1 "
272 L"--switch2=value "
273 L"--switch3=\"a value with spaces\" "
274 L"--switch4=\"\\\"a value with quotes\\\"\" "
275 L"--quotes=\"" + kTrickyQuoted + L"\"",
276 cl.GetCommandLineString());
277 #endif
280 TEST(CommandLineTest, AppendSwitchesDashDash) {
281 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
282 FILE_PATH_LITERAL("--"),
283 FILE_PATH_LITERAL("--arg1") };
284 CommandLine cl(arraysize(raw_argv), raw_argv);
286 cl.AppendSwitch("switch1");
287 cl.AppendSwitchASCII("switch2", "foo");
289 cl.AppendArg("--arg2");
291 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
292 cl.GetCommandLineString());
293 CommandLine::StringVector cl_argv = cl.argv();
294 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
295 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
296 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
297 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
298 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
299 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
302 // Tests that when AppendArguments is called that the program is set correctly
303 // on the target CommandLine object and the switches from the source
304 // CommandLine are added to the target.
305 TEST(CommandLineTest, AppendArguments) {
306 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
307 cl1.AppendSwitch("switch1");
308 cl1.AppendSwitchASCII("switch2", "foo");
310 CommandLine cl2(CommandLine::NO_PROGRAM);
311 cl2.AppendArguments(cl1, true);
312 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
313 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
315 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
316 c1.AppendSwitch("switch1");
317 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
318 c2.AppendSwitch("switch2");
320 c1.AppendArguments(c2, true);
321 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
322 EXPECT_TRUE(c1.HasSwitch("switch1"));
323 EXPECT_TRUE(c1.HasSwitch("switch2"));
326 #if defined(OS_WIN)
327 // Make sure that the command line string program paths are quoted as necessary.
328 // This only makes sense on Windows and the test is basically here to guard
329 // against regressions.
330 TEST(CommandLineTest, ProgramQuotes) {
331 // Check that quotes are not added for paths without spaces.
332 const FilePath kProgram(L"Program");
333 CommandLine cl_program(kProgram);
334 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
335 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
337 const FilePath kProgramPath(L"Program Path");
339 // Check that quotes are not returned from GetProgram().
340 CommandLine cl_program_path(kProgramPath);
341 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
343 // Check that quotes are added to command line string paths containing spaces.
344 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
345 CommandLine::StringType program_string(cl_program_path.GetProgram().value());
346 EXPECT_EQ('"', cmd_string[0]);
347 EXPECT_EQ(program_string, cmd_string.substr(1, program_string.length()));
348 EXPECT_EQ('"', cmd_string[program_string.length() + 1]);
350 #endif
352 // Calling Init multiple times should not modify the previous CommandLine.
353 TEST(CommandLineTest, Init) {
354 CommandLine* initial = CommandLine::ForCurrentProcess();
355 EXPECT_FALSE(CommandLine::Init(0, NULL));
356 CommandLine* current = CommandLine::ForCurrentProcess();
357 EXPECT_EQ(initial, current);