File: chrome_browser_main_win_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 4,886 bytes parent folder | download | duplicates (4)
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
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chrome_browser_main_win.h"

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/about_flags.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/webui/flags/feature_entry_macros.h"
#include "components/webui/flags/flags_ui_pref_names.h"
#include "components/webui/flags/flags_ui_switches.h"
#include "components/webui/flags/pref_service_flags_storage.h"
#include "testing/gtest/include/gtest/gtest.h"

// Test GetRestartCommandLine function behavior. It should remove the program at
// the beginning of the command line, and any non-switch args, since the
// intention is to restore the session, by adding --restore-last-session. It
// should also remove any flags within the flag sentinels, and the sentinels
// themselves.
TEST(ChromeBrowserMainWinTest, GetRestartCommand) {
  base::FilePath chrome_path(L"chrome.exe");
  base::CommandLine simple_command_line(chrome_path);

  // Simple command line with just the program.
  const base::CommandLine::StringType kNoArgsResult =
      L" --restore-last-session --restart";
  base::CommandLine restart_command_line =
      ChromeBrowserMainPartsWin::GetRestartCommandLine(simple_command_line);
  EXPECT_EQ(restart_command_line.GetCommandLineString(), kNoArgsResult);

  // Command line with a url argument - url should be removed.
  base::CommandLine url_command_line(chrome_path);
  url_command_line.AppendArg("https://www.example.com");
  restart_command_line =
      ChromeBrowserMainPartsWin::GetRestartCommandLine(url_command_line);
  EXPECT_EQ(restart_command_line.GetCommandLineString(), kNoArgsResult);

  // Command line with a retained switch.
  const std::string kRetainedSwitch = "--enable-sandbox-audio";
  const base::CommandLine::StringType kRetainedSwitchResult =
      L" --enable-sandbox-audio --restore-last-session --restart";
  base::CommandLine retained_switch_command_line(chrome_path);
  retained_switch_command_line.AppendSwitch(kRetainedSwitch);
  restart_command_line = ChromeBrowserMainPartsWin::GetRestartCommandLine(
      retained_switch_command_line);
  EXPECT_EQ(restart_command_line.GetCommandLineString(), kRetainedSwitchResult);

  // Command line with flag switches.
  base::CommandLine experiments_command_line(chrome_path);
  // Add an --enable-features flag outside the flag sentinels, as if the user or
  // admin had added it. It should be retained, because Chrome won't add it
  // automatically.
  experiments_command_line.AppendSwitchASCII(switches::kEnableFeatures, "Exp2");
  experiments_command_line.AppendSwitch("enable-foo");
  experiments_command_line.AppendSwitch("--enable-sandbox-audio");

  // Setup the feature infrastructure so that ConvertFlagsToSwitches works.
  TestingPrefServiceSimple prefs;
  flags_ui::PrefServiceFlagsStorage flags_storage(&prefs);
  prefs.registry()->RegisterListPref(flags_ui::prefs::kAboutFlagsEntries);
  prefs.registry()->RegisterDictionaryPref(
      flags_ui::prefs::kAboutFlagsOriginLists);
  const char kExperimentName[] = "exp-flag";
  about_flags::testing::ScopedFeatureEntries scoped_feature_entries(
      {{kExperimentName, "Exp", "description", static_cast<unsigned short>(-1),
        ORIGIN_LIST_VALUE_TYPE("flag-switch", "")}});
  about_flags::SetFeatureEntryEnabled(&flags_storage, kExperimentName,
                                      /*enable=*/true);
  about_flags::ConvertFlagsToSwitches(&flags_storage, &experiments_command_line,
                                      flags_ui::kAddSentinels);
  // Check that ConvertFlagsToSwitches added the start sentinel.
  ASSERT_TRUE(
      experiments_command_line.HasSwitch(::switches::kFlagSwitchesBegin));
  ASSERT_EQ(
      experiments_command_line.GetCommandLineString(),
      L"chrome.exe --enable-features=Exp2 --enable-foo --enable-sandbox-audio"
      L" --flag-switches-begin --flag-switch --flag-switches-end");
  // Check that the args and flag switches and sentinels are removed.
  restart_command_line = ChromeBrowserMainPartsWin::GetRestartCommandLine(
      experiments_command_line);
  EXPECT_EQ(restart_command_line.GetCommandLineString(),
            L" --enable-features=Exp2 --enable-foo"
            L" --enable-sandbox-audio"
            L" --restore-last-session --restart");
}

// Test RegisterApplicationRestart to make sure there are no crashes.
TEST(ChromeBrowserMainWinTest, RegisterRestart) {
  const base::CommandLine command_line = base::CommandLine::FromString(
      L"chrome.exe --enable-features=Exp2 --enable-foo -- "
      L"http://www.chromium.org");
  ChromeBrowserMainPartsWin::RegisterApplicationRestart(command_line);
}