1 // Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "include/cef_command_line.h"
6 #include "tests/gtest/include/gtest/gtest.h"
7
8 namespace {
9
VerifyCommandLine(CefRefPtr<CefCommandLine> command_line,const char * expected_arg1="arg1",const char * expected_arg2="arg 2")10 void VerifyCommandLine(CefRefPtr<CefCommandLine> command_line,
11 const char* expected_arg1 = "arg1",
12 const char* expected_arg2 = "arg 2") {
13 std::string program = command_line->GetProgram();
14 EXPECT_EQ("test.exe", program);
15
16 EXPECT_TRUE(command_line->HasSwitches());
17
18 EXPECT_TRUE(command_line->HasSwitch("switch1"));
19 std::string switch1 = command_line->GetSwitchValue("switch1");
20 EXPECT_EQ("", switch1);
21 // Switch names are converted to lowercase but values are left unchanged.
22 EXPECT_TRUE(command_line->HasSwitch("SWITCH2"));
23 std::string switch2 = command_line->GetSwitchValue("SWITCH2");
24 EXPECT_EQ("VAL2", switch2);
25 EXPECT_TRUE(command_line->HasSwitch("switch3"));
26 std::string switch3 = command_line->GetSwitchValue("switch3");
27 EXPECT_EQ("val3", switch3);
28 EXPECT_TRUE(command_line->HasSwitch("switch4"));
29 std::string switch4 = command_line->GetSwitchValue("switch4");
30 EXPECT_EQ("val 4", switch4);
31 EXPECT_FALSE(command_line->HasSwitch("switchnoexist"));
32
33 CefCommandLine::SwitchMap switches;
34 command_line->GetSwitches(switches);
35 EXPECT_EQ((size_t)4, switches.size());
36
37 bool has1 = false, has2 = false, has3 = false, has4 = false;
38
39 CefCommandLine::SwitchMap::const_iterator it = switches.begin();
40 for (; it != switches.end(); ++it) {
41 std::string name = it->first;
42 std::string val = it->second;
43
44 if (name == "switch1") {
45 has1 = true;
46 EXPECT_EQ("", val);
47 } else if (name == "switch2") {
48 has2 = true;
49 EXPECT_EQ("VAL2", val);
50 } else if (name == "switch3") {
51 has3 = true;
52 EXPECT_EQ("val3", val);
53 } else if (name == "switch4") {
54 has4 = true;
55 EXPECT_EQ("val 4", val);
56 }
57 }
58
59 EXPECT_TRUE(has1);
60 EXPECT_TRUE(has2);
61 EXPECT_TRUE(has3);
62 EXPECT_TRUE(has4);
63
64 EXPECT_TRUE(command_line->HasArguments());
65
66 CefCommandLine::ArgumentList args;
67 command_line->GetArguments(args);
68 EXPECT_EQ((size_t)2, args.size());
69 std::string arg0 = args[0];
70 EXPECT_EQ(expected_arg1, arg0);
71 std::string arg1 = args[1];
72 EXPECT_EQ(expected_arg2, arg1);
73
74 command_line->Reset();
75 EXPECT_FALSE(command_line->HasSwitches());
76 EXPECT_FALSE(command_line->HasArguments());
77 std::string cur_program = command_line->GetProgram();
78 EXPECT_EQ(program, cur_program);
79 }
80
81 } // namespace
82
83 // Test creating a command line from argc/argv or string.
TEST(CommandLineTest,Init)84 TEST(CommandLineTest, Init) {
85 CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
86 EXPECT_TRUE(command_line.get() != nullptr);
87
88 #if defined(OS_WIN)
89 command_line->InitFromString(
90 "test.exe --switch1 -switch2=VAL2 /switch3=val3 "
91 "-switch4=\"val 4\" arg1 \"arg 2\"");
92 #else
93 const char* args[] = {"test.exe", "--switch1", "-switch2=VAL2",
94 "-switch3=val3", "-switch4=val 4", "arg1",
95 "arg 2"};
96 command_line->InitFromArgv(sizeof(args) / sizeof(char*), args);
97 #endif
98
99 VerifyCommandLine(command_line);
100 }
101
102 // Test creating a command line using set and append methods.
TEST(CommandLineTest,Manual)103 TEST(CommandLineTest, Manual) {
104 CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
105 EXPECT_TRUE(command_line.get() != nullptr);
106
107 command_line->SetProgram("test.exe");
108 command_line->AppendSwitch("switch1");
109 command_line->AppendSwitchWithValue("switch2", "VAL2");
110 command_line->AppendSwitchWithValue("switch3", "val3");
111 command_line->AppendSwitchWithValue("switch4", "val 4");
112 command_line->AppendArgument("arg1");
113 command_line->AppendArgument("arg 2");
114
115 VerifyCommandLine(command_line);
116 }
117
118 // Test that any prefixes included with the switches are ignored.
TEST(CommandLineTest,IgnorePrefixes)119 TEST(CommandLineTest, IgnorePrefixes) {
120 CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
121 EXPECT_TRUE(command_line.get() != nullptr);
122
123 command_line->SetProgram("test.exe");
124 command_line->AppendSwitch("-switch1");
125 command_line->AppendSwitchWithValue("--switch2", "VAL2");
126 command_line->AppendSwitchWithValue("-switch3", "val3");
127 command_line->AppendSwitchWithValue("-switch4", "val 4");
128
129 // Prefixes will not be removed from arguments.
130 const char arg1[] = "-arg1";
131 const char arg2[] = "--arg 2";
132 command_line->AppendArgument(arg1);
133 command_line->AppendArgument(arg2);
134
135 VerifyCommandLine(command_line, arg1, arg2);
136 }
137
138 // Test that command line switch names are converted to lowercase ASCII.
TEST(CommandLineTest,IgnoreCase)139 TEST(CommandLineTest, IgnoreCase) {
140 CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
141 EXPECT_TRUE(command_line.get() != nullptr);
142
143 command_line->SetProgram("test.exe");
144 command_line->AppendSwitch("-Switch1");
145 command_line->AppendSwitchWithValue("-SWITCH2", "VAL2");
146 command_line->AppendSwitchWithValue("-switch3", "val3");
147 command_line->AppendSwitchWithValue("-switch4", "val 4");
148 command_line->AppendArgument("arg1");
149 command_line->AppendArgument("arg 2");
150
151 VerifyCommandLine(command_line);
152 }
153