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 "win8/delegate_execute/delegate_execute_util.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 static const char kSomeSwitch
[] = "some-switch";
21 TEST(DelegateExecuteUtil
, CommandLineFromParametersTest
) {
22 base::CommandLine
cl(base::CommandLine::NO_PROGRAM
);
24 // Empty parameters means empty command-line string.
25 cl
= delegate_execute::CommandLineFromParameters(NULL
);
26 EXPECT_EQ(std::wstring(), cl
.GetProgram().value());
27 EXPECT_EQ(base::CommandLine::StringType(), cl
.GetCommandLineString());
29 // Parameters with a switch are parsed properly.
30 cl
= delegate_execute::CommandLineFromParameters(
31 base::StringPrintf(L
"--%ls",
32 base::ASCIIToUTF16(kSomeSwitch
).c_str()).c_str());
33 EXPECT_EQ(std::wstring(), cl
.GetProgram().value());
34 EXPECT_TRUE(cl
.HasSwitch(kSomeSwitch
));
37 TEST(DelegateExecuteUtil
, MakeChromeCommandLineTest
) {
38 static const wchar_t kSomeArgument
[] = L
"http://some.url/";
39 static const wchar_t kOtherArgument
[] = L
"http://some.other.url/";
40 const base::FilePath
this_exe(
41 base::CommandLine::ForCurrentProcess()->GetProgram());
43 base::CommandLine
cl(base::CommandLine::NO_PROGRAM
);
45 // Empty params and argument contains only the exe.
46 cl
= delegate_execute::MakeChromeCommandLine(
48 delegate_execute::CommandLineFromParameters(NULL
),
50 EXPECT_EQ(1, cl
.argv().size());
51 EXPECT_EQ(this_exe
.value(), cl
.GetProgram().value());
53 // Empty params with arg contains the arg.
54 cl
= delegate_execute::MakeChromeCommandLine(
55 this_exe
, delegate_execute::CommandLineFromParameters(NULL
),
56 base::string16(kSomeArgument
));
57 EXPECT_EQ(2, cl
.argv().size());
58 EXPECT_EQ(this_exe
.value(), cl
.GetProgram().value());
59 EXPECT_EQ(1, cl
.GetArgs().size());
60 EXPECT_EQ(base::string16(kSomeArgument
), cl
.GetArgs()[0]);
62 // Params with switchs and args plus arg contains the arg.
63 cl
= delegate_execute::MakeChromeCommandLine(
64 this_exe
, delegate_execute::CommandLineFromParameters(
65 base::StringPrintf(L
"--%ls -- %ls",
66 base::ASCIIToUTF16(kSomeSwitch
).c_str(),
67 kOtherArgument
).c_str()),
68 base::string16(kSomeArgument
));
69 EXPECT_EQ(5, cl
.argv().size());
70 EXPECT_EQ(this_exe
.value(), cl
.GetProgram().value());
71 EXPECT_TRUE(cl
.HasSwitch(kSomeSwitch
));
72 base::CommandLine::StringVector
args(cl
.GetArgs());
73 EXPECT_EQ(2, args
.size());
76 std::find(args
.begin(), args
.end(), base::string16(kOtherArgument
)));
78 std::find(args
.begin(), args
.end(), base::string16(kSomeArgument
)));
81 TEST(DelegateExecuteUtil
, ParametersFromSwitchTest
) {
82 EXPECT_EQ(base::string16(), delegate_execute::ParametersFromSwitch(NULL
));
83 EXPECT_EQ(base::string16(L
"--some-switch"),
84 delegate_execute::ParametersFromSwitch(kSomeSwitch
));