[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lei Zhang | db5696b | 2017-11-20 21:13:40 | [diff] [blame] | 5 | #include "chrome/browser/lifetime/switch_utils.h" |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 6 | |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 9 | #include "base/stl_util.h" |
avi | 2729e44 | 2015-12-26 05:27:45 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | TEST(SwitchUtilsTest, RemoveSwitches) { |
Lei Zhang | 4995661 | 2017-11-13 20:17:00 | [diff] [blame] | 14 | static const base::CommandLine::CharType* const argv[] = { |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 15 | 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 Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 21 | base::CommandLine cmd_line(base::size(argv), argv); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 22 | EXPECT_FALSE(cmd_line.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 23 | |
Jeremy Roman | 863386d | 2017-10-31 19:25:38 | [diff] [blame] | 24 | base::CommandLine::SwitchMap switches = cmd_line.GetSwitches(); |
[email protected] | 8c61902f | 2013-05-24 06:14:41 | [diff] [blame] | 25 | EXPECT_EQ(5U, switches.size()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 26 | |
| 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] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 33 | #if defined(OS_WIN) |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 34 | TEST(SwitchUtilsTest, RemoveSwitchesFromString) { |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 35 | // All these command line args (except foo and bar) will |
| 36 | // be removed after RemoveSwitchesForAutostart: |
avi | 79bf913 | 2014-12-25 17:48:05 | [diff] [blame] | 37 | base::CommandLine cmd_line = base::CommandLine::FromString( |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 38 | L"program" |
| 39 | L" --app=http://www.google.com/" |
[email protected] | 3f002a3 | 2013-01-02 17:52:38 | [diff] [blame] | 40 | L" --force-first-run" |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 41 | L" --make-default-browser" |
| 42 | L" --foo" |
| 43 | L" --bar"); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 44 | EXPECT_FALSE(cmd_line.GetCommandLineString().empty()); |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 45 | |
Jeremy Roman | 863386d | 2017-10-31 19:25:38 | [diff] [blame] | 46 | base::CommandLine::SwitchMap switches = cmd_line.GetSwitches(); |
[email protected] | 8c61902f | 2013-05-24 06:14:41 | [diff] [blame] | 47 | EXPECT_EQ(5U, switches.size()); |
[email protected] | 5c08f22 | 2010-09-22 09:37:21 | [diff] [blame] | 48 | |
| 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 | } |
fdoray | dd01ed2c | 2016-03-02 15:46:01 | [diff] [blame] | 54 | |
| 55 | TEST(SwitchUtilsTest, RemovePrefetchSwitch) { |
Lei Zhang | 4995661 | 2017-11-13 20:17:00 | [diff] [blame] | 56 | static const base::CommandLine::CharType* const argv[] = { |
fdoray | dd01ed2c | 2016-03-02 15:46:01 | [diff] [blame] | 57 | FILE_PATH_LITERAL("program"), |
| 58 | FILE_PATH_LITERAL("--foo"), |
| 59 | FILE_PATH_LITERAL("/prefetch:1"), |
| 60 | FILE_PATH_LITERAL("--bar")}; |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 61 | base::CommandLine cmd_line(base::size(argv), argv); |
fdoray | dd01ed2c | 2016-03-02 15:46:01 | [diff] [blame] | 62 | EXPECT_FALSE(cmd_line.GetCommandLineString().empty()); |
| 63 | |
Jeremy Roman | 863386d | 2017-10-31 19:25:38 | [diff] [blame] | 64 | base::CommandLine::SwitchMap switches = cmd_line.GetSwitches(); |
fdoray | dd01ed2c | 2016-03-02 15:46:01 | [diff] [blame] | 65 | 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 | |
| 73 | TEST(SwitchUtilsTest, RemovePrefetchSwitchAndNormalSwitch) { |
Lei Zhang | 4995661 | 2017-11-13 20:17:00 | [diff] [blame] | 74 | static const base::CommandLine::CharType* const argv[] = { |
fdoray | dd01ed2c | 2016-03-02 15:46:01 | [diff] [blame] | 75 | 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 Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 80 | base::CommandLine cmd_line(base::size(argv), argv); |
fdoray | dd01ed2c | 2016-03-02 15:46:01 | [diff] [blame] | 81 | EXPECT_FALSE(cmd_line.GetCommandLineString().empty()); |
| 82 | |
Jeremy Roman | 863386d | 2017-10-31 19:25:38 | [diff] [blame] | 83 | base::CommandLine::SwitchMap switches = cmd_line.GetSwitches(); |
fdoray | dd01ed2c | 2016-03-02 15:46:01 | [diff] [blame] | 84 | 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 Zhang | 4995661 | 2017-11-13 20:17:00 | [diff] [blame] | 91 | #endif // defined(OS_WIN) |