blob: 7e3f32bf8c48e80e79ed9492304c7649fd91cba4 [file] [log] [blame]
[email protected]2edc2862011-04-04 18:04:371// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]5c08f222010-09-22 09:37:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lei Zhangdb5696b2017-11-20 21:13:405#include "chrome/browser/lifetime/switch_utils.h"
[email protected]5c08f222010-09-22 09:37:216
[email protected]5c08f222010-09-22 09:37:217#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
Avi Drissman5f0fb8c2018-12-25 23:20:499#include "base/stl_util.h"
avi2729e442015-12-26 05:27:4510#include "build/build_config.h"
[email protected]5c08f222010-09-22 09:37:2111#include "testing/gtest/include/gtest/gtest.h"
12
13TEST(SwitchUtilsTest, RemoveSwitches) {
Lei Zhang49956612017-11-13 20:17:0014 static const base::CommandLine::CharType* const argv[] = {
avi79bf9132014-12-25 17:48:0515 FILE_PATH_LITERAL("program"),
16 FILE_PATH_LITERAL("--app=http://www.google.com/"),
17 FILE_PATH_LITERAL("--force-first-run"),
18 FILE_PATH_LITERAL("--make-default-browser"),
19 FILE_PATH_LITERAL("--foo"),
20 FILE_PATH_LITERAL("--bar")};
Avi Drissman5f0fb8c2018-12-25 23:20:4921 base::CommandLine cmd_line(base::size(argv), argv);
[email protected]61a4c6f2011-07-20 04:54:5222 EXPECT_FALSE(cmd_line.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:2423
Jeremy Roman863386d2017-10-31 19:25:3824 base::CommandLine::SwitchMap switches = cmd_line.GetSwitches();
[email protected]8c61902f2013-05-24 06:14:4125 EXPECT_EQ(5U, switches.size());
[email protected]a40ca4302011-05-14 01:10:2426
27 switches::RemoveSwitchesForAutostart(&switches);
28 EXPECT_EQ(2U, switches.size());
29 EXPECT_TRUE(cmd_line.HasSwitch("foo"));
30 EXPECT_TRUE(cmd_line.HasSwitch("bar"));
31}
32
[email protected]5c08f222010-09-22 09:37:2133#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:2434TEST(SwitchUtilsTest, RemoveSwitchesFromString) {
[email protected]5c08f222010-09-22 09:37:2135 // All these command line args (except foo and bar) will
36 // be removed after RemoveSwitchesForAutostart:
avi79bf9132014-12-25 17:48:0537 base::CommandLine cmd_line = base::CommandLine::FromString(
[email protected]5c08f222010-09-22 09:37:2138 L"program"
39 L" --app=http://www.google.com/"
[email protected]3f002a32013-01-02 17:52:3840 L" --force-first-run"
[email protected]5c08f222010-09-22 09:37:2141 L" --make-default-browser"
42 L" --foo"
43 L" --bar");
[email protected]61a4c6f2011-07-20 04:54:5244 EXPECT_FALSE(cmd_line.GetCommandLineString().empty());
[email protected]5c08f222010-09-22 09:37:2145
Jeremy Roman863386d2017-10-31 19:25:3846 base::CommandLine::SwitchMap switches = cmd_line.GetSwitches();
[email protected]8c61902f2013-05-24 06:14:4147 EXPECT_EQ(5U, switches.size());
[email protected]5c08f222010-09-22 09:37:2148
49 switches::RemoveSwitchesForAutostart(&switches);
50 EXPECT_EQ(2U, switches.size());
51 EXPECT_TRUE(cmd_line.HasSwitch("foo"));
52 EXPECT_TRUE(cmd_line.HasSwitch("bar"));
53}
fdoraydd01ed2c2016-03-02 15:46:0154
55TEST(SwitchUtilsTest, RemovePrefetchSwitch) {
Lei Zhang49956612017-11-13 20:17:0056 static const base::CommandLine::CharType* const argv[] = {
fdoraydd01ed2c2016-03-02 15:46:0157 FILE_PATH_LITERAL("program"),
58 FILE_PATH_LITERAL("--foo"),
59 FILE_PATH_LITERAL("/prefetch:1"),
60 FILE_PATH_LITERAL("--bar")};
Avi Drissman5f0fb8c2018-12-25 23:20:4961 base::CommandLine cmd_line(base::size(argv), argv);
fdoraydd01ed2c2016-03-02 15:46:0162 EXPECT_FALSE(cmd_line.GetCommandLineString().empty());
63
Jeremy Roman863386d2017-10-31 19:25:3864 base::CommandLine::SwitchMap switches = cmd_line.GetSwitches();
fdoraydd01ed2c2016-03-02 15:46:0165 EXPECT_EQ(3U, switches.size());
66
67 switches::RemoveSwitchesForAutostart(&switches);
68 EXPECT_EQ(2U, switches.size());
69 EXPECT_TRUE(cmd_line.HasSwitch("foo"));
70 EXPECT_TRUE(cmd_line.HasSwitch("bar"));
71}
72
73TEST(SwitchUtilsTest, RemovePrefetchSwitchAndNormalSwitch) {
Lei Zhang49956612017-11-13 20:17:0074 static const base::CommandLine::CharType* const argv[] = {
fdoraydd01ed2c2016-03-02 15:46:0175 FILE_PATH_LITERAL("program"),
76 FILE_PATH_LITERAL("--foo"),
77 FILE_PATH_LITERAL("/prefetch:1"),
78 FILE_PATH_LITERAL("--force-first-run"),
79 FILE_PATH_LITERAL("--bar")};
Avi Drissman5f0fb8c2018-12-25 23:20:4980 base::CommandLine cmd_line(base::size(argv), argv);
fdoraydd01ed2c2016-03-02 15:46:0181 EXPECT_FALSE(cmd_line.GetCommandLineString().empty());
82
Jeremy Roman863386d2017-10-31 19:25:3883 base::CommandLine::SwitchMap switches = cmd_line.GetSwitches();
fdoraydd01ed2c2016-03-02 15:46:0184 EXPECT_EQ(4U, switches.size());
85
86 switches::RemoveSwitchesForAutostart(&switches);
87 EXPECT_EQ(2U, switches.size());
88 EXPECT_TRUE(cmd_line.HasSwitch("foo"));
89 EXPECT_TRUE(cmd_line.HasSwitch("bar"));
90}
Lei Zhang49956612017-11-13 20:17:0091#endif // defined(OS_WIN)