summaryrefslogtreecommitdiffstats
path: root/win8/delegate_execute/delegate_execute_util_unittest.cc
blob: 63a5075bf6af8f0e03bb011a367f00459a7a4b7f (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
// 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 "win8/delegate_execute/delegate_execute_util.h"

#include <algorithm>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

static const char kSomeSwitch[] = "some-switch";

}  // namespace

TEST(DelegateExecuteUtil, CommandLineFromParametersTest) {
  base::CommandLine cl(base::CommandLine::NO_PROGRAM);

  // Empty parameters means empty command-line string.
  cl = delegate_execute::CommandLineFromParameters(NULL);
  EXPECT_EQ(std::wstring(), cl.GetProgram().value());
  EXPECT_EQ(base::CommandLine::StringType(), cl.GetCommandLineString());

  // Parameters with a switch are parsed properly.
  cl = delegate_execute::CommandLineFromParameters(
      base::StringPrintf(L"--%ls",
                         base::ASCIIToUTF16(kSomeSwitch).c_str()).c_str());
  EXPECT_EQ(std::wstring(), cl.GetProgram().value());
  EXPECT_TRUE(cl.HasSwitch(kSomeSwitch));
}

TEST(DelegateExecuteUtil, MakeChromeCommandLineTest) {
  static const wchar_t kSomeArgument[] = L"http://some.url/";
  static const wchar_t kOtherArgument[] = L"http://some.other.url/";
  const base::FilePath this_exe(
      base::CommandLine::ForCurrentProcess()->GetProgram());

  base::CommandLine cl(base::CommandLine::NO_PROGRAM);

  // Empty params and argument contains only the exe.
  cl = delegate_execute::MakeChromeCommandLine(
      this_exe,
      delegate_execute::CommandLineFromParameters(NULL),
      base::string16());
  EXPECT_EQ(1, cl.argv().size());
  EXPECT_EQ(this_exe.value(), cl.GetProgram().value());

  // Empty params with arg contains the arg.
  cl = delegate_execute::MakeChromeCommandLine(
      this_exe, delegate_execute::CommandLineFromParameters(NULL),
      base::string16(kSomeArgument));
  EXPECT_EQ(2, cl.argv().size());
  EXPECT_EQ(this_exe.value(), cl.GetProgram().value());
  EXPECT_EQ(1, cl.GetArgs().size());
  EXPECT_EQ(base::string16(kSomeArgument), cl.GetArgs()[0]);

  // Params with switchs and args plus arg contains the arg.
  cl = delegate_execute::MakeChromeCommandLine(
      this_exe, delegate_execute::CommandLineFromParameters(
          base::StringPrintf(L"--%ls -- %ls",
                             base::ASCIIToUTF16(kSomeSwitch).c_str(),
                             kOtherArgument).c_str()),
      base::string16(kSomeArgument));
  EXPECT_EQ(5, cl.argv().size());
  EXPECT_EQ(this_exe.value(), cl.GetProgram().value());
  EXPECT_TRUE(cl.HasSwitch(kSomeSwitch));
  base::CommandLine::StringVector args(cl.GetArgs());
  EXPECT_EQ(2, args.size());
  EXPECT_NE(
      args.end(),
      std::find(args.begin(), args.end(), base::string16(kOtherArgument)));
  EXPECT_NE(args.end(),
            std::find(args.begin(), args.end(), base::string16(kSomeArgument)));
}

TEST(DelegateExecuteUtil, ParametersFromSwitchTest) {
  EXPECT_EQ(base::string16(), delegate_execute::ParametersFromSwitch(NULL));
  EXPECT_EQ(base::string16(L"--some-switch"),
            delegate_execute::ParametersFromSwitch(kSomeSwitch));
}