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 EXPECT_TRUE(command_line->HasSwitch("switch2"));
22 std::string switch2 = command_line->GetSwitchValue("switch2");
23 EXPECT_EQ("val2", switch2);
24 EXPECT_TRUE(command_line->HasSwitch("switch3"));
25 std::string switch3 = command_line->GetSwitchValue("switch3");
26 EXPECT_EQ("val3", switch3);
27 EXPECT_TRUE(command_line->HasSwitch("switch4"));
28 std::string switch4 = command_line->GetSwitchValue("switch4");
29 EXPECT_EQ("val 4", switch4);
30 EXPECT_FALSE(command_line->HasSwitch("switchnoexist"));
31
32 CefCommandLine::SwitchMap switches;
33 command_line->GetSwitches(switches);
34 EXPECT_EQ((size_t)4, switches.size());
35
36 bool has1 = false, has2 = false, has3 = false, has4 = false;
37
38 CefCommandLine::SwitchMap::const_iterator it = switches.begin();
39 for (; it != switches.end(); ++it) {
40 std::string name = it->first;
41 std::string val = it->second;
42
43 if (name == "switch1") {
44 has1 = true;
45 EXPECT_EQ("", val);
46 } else if (name == "switch2") {
47 has2 = true;
48 EXPECT_EQ("val2", val);
49 } else if (name == "switch3") {
50 has3 = true;
51 EXPECT_EQ("val3", val);
52 } else if (name == "switch4") {
53 has4 = true;
54 EXPECT_EQ("val 4", val);
55 }
56 }
57
58 EXPECT_TRUE(has1);
59 EXPECT_TRUE(has2);
60 EXPECT_TRUE(has3);
61 EXPECT_TRUE(has4);
62
63 EXPECT_TRUE(command_line->HasArguments());
64
65 CefCommandLine::ArgumentList args;
66 command_line->GetArguments(args);
67 EXPECT_EQ((size_t)2, args.size());
68 std::string arg0 = args[0];
69 EXPECT_EQ(expected_arg1, arg0);
70 std::string arg1 = args[1];
71 EXPECT_EQ(expected_arg2, arg1);
72
73 command_line->Reset();
74 EXPECT_FALSE(command_line->HasSwitches());
75 EXPECT_FALSE(command_line->HasArguments());
76 std::string cur_program = command_line->GetProgram();
77 EXPECT_EQ(program, cur_program);
78 }
79
80 } // namespace
81
82 // Test creating a command line from argc/argv or string.
TEST(CommandLineTest,Init)83 TEST(CommandLineTest, Init) {
84 CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
85 EXPECT_TRUE(command_line.get() != nullptr);
86
87 #if defined(OS_WIN)
88 command_line->InitFromString(
89 "test.exe --switch1 -switch2=val2 /switch3=val3 "
90 "-switch4=\"val 4\" arg1 \"arg 2\"");
91 #else
92 const char* args[] = {"test.exe", "--switch1", "-switch2=val2",
93 "-switch3=val3", "-switch4=val 4", "arg1",
94 "arg 2"};
95 command_line->InitFromArgv(sizeof(args) / sizeof(char*), args);
96 #endif
97
98 VerifyCommandLine(command_line);
99 }
100
101 // Test creating a command line using set and append methods.
TEST(CommandLineTest,Manual)102 TEST(CommandLineTest, Manual) {
103 CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
104 EXPECT_TRUE(command_line.get() != nullptr);
105
106 command_line->SetProgram("test.exe");
107 command_line->AppendSwitch("switch1");
108 command_line->AppendSwitchWithValue("switch2", "val2");
109 command_line->AppendSwitchWithValue("switch3", "val3");
110 command_line->AppendSwitchWithValue("switch4", "val 4");
111 command_line->AppendArgument("arg1");
112 command_line->AppendArgument("arg 2");
113
114 VerifyCommandLine(command_line);
115 }
116
117 // Test that any prefixes included with the switches are ignored.
TEST(CommandLineTest,IgnorePrefixes)118 TEST(CommandLineTest, IgnorePrefixes) {
119 CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
120 EXPECT_TRUE(command_line.get() != nullptr);
121
122 command_line->SetProgram("test.exe");
123 command_line->AppendSwitch("-switch1");
124 command_line->AppendSwitchWithValue("--switch2", "val2");
125 command_line->AppendSwitchWithValue("-switch3", "val3");
126 command_line->AppendSwitchWithValue("-switch4", "val 4");
127
128 // Prefixes will not be removed from arguments.
129 const char arg1[] = "-arg1";
130 const char arg2[] = "--arg 2";
131 command_line->AppendArgument(arg1);
132 command_line->AppendArgument(arg2);
133
134 VerifyCommandLine(command_line, arg1, arg2);
135 }
136