File: first_run_internal_posix_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,900 bytes parent folder | download | duplicates (6)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <signal.h>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/path_service.h"
#include "base/task/sequenced_task_runner.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/first_run/first_run_dialog.h"
#include "chrome/browser/first_run/first_run_internal.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"

namespace first_run {

class FirstRunInternalPosixTest : public InProcessBrowserTest {
 protected:
  FirstRunInternalPosixTest() = default;

  FirstRunInternalPosixTest(const FirstRunInternalPosixTest&) = delete;
  FirstRunInternalPosixTest& operator=(const FirstRunInternalPosixTest&) =
      delete;

  // InProcessBrowserTest:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(switches::kForceFirstRun);
  }

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  // For Chrome, the presence of a Local State file should not influence whether
  // the first run dialog is shown. See crbug.com/1221483.
  bool SetUpUserDataDirectory() override {
    base::FilePath user_data_dir;
    base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
    const base::FilePath local_state_file =
        user_data_dir.Append(chrome::kLocalStateFilename);
    const std::string empty_prefs = "{}";
    base::WriteFile(local_state_file, empty_prefs.data());
    return true;
  }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

  void SetUpInProcessBrowserTestFixture() override {
    InProcessBrowserTest::SetUpInProcessBrowserTestFixture();

#if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
    // While Chromium uses ForcedShowDialogState::kForceShown, Chrome uses the
    // default, ForcedShowDialogState::kNotForced, to exercise Chrome-specific
    // behavior. See ShouldShowFirstRunDialog().
    internal::ForceFirstRunDialogShownForTesting(true);
#endif  // !BUILDFLAG(GOOGLE_CHROME_BRANDING)

    // The modal dialog will spawn and spin a nested RunLoop when
    // content::BrowserTestBase::SetUp() invokes content::ContentMain().
    // BrowserTestBase sets ContentMainParams::ui_task before this, but the
    // ui_task isn't actually Run() until after the dialog is spawned in
    // ChromeBrowserMainParts::PreMainMessageLoopRunImpl(). Instead, try to
    // inspect state by posting a task to run in that nested RunLoop. There's no
    // MessageLoop to enqueue a task on yet or anything else sensible that would
    // allow us to hook in a task. So use a testing-only Closure.
    GetBeforeShowFirstRunDialogHookForTesting() = base::BindOnce(
        &FirstRunInternalPosixTest::SetupNestedTask, base::Unretained(this));
    EXPECT_FALSE(inspected_state_);
  }

  void TearDownInProcessBrowserTestFixture() override {
    EXPECT_TRUE(inspected_state_);
    // The test closure should have run. But clear the global in case it hasn't.
    EXPECT_FALSE(GetBeforeShowFirstRunDialogHookForTesting());
    GetBeforeShowFirstRunDialogHookForTesting().Reset();
    InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
  }

 private:
  // A task run immediately before first_run::DoPostImportPlatformSpecificTasks
  // shows the first-run dialog.
  void SetupNestedTask() {
    EXPECT_TRUE(base::SequencedTaskRunner::GetCurrentDefault());
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(&FirstRunInternalPosixTest::InspectState,
                                  base::Unretained(this)));
  }

  // A task queued up to run once the first-run dialog starts pumping messages
  // in its modal loop.
  void InspectState() {
    // Send a signal to myself. This should post a task for the next run loop
    // iteration to set browser_shutdown::IsTryingToQuit(), and interrupt the
    // RunLoop.
    raise(SIGINT);
    inspected_state_ = true;
  }

  bool inspected_state_ = false;
};

// Test the first run flow for showing the modal dialog that surfaces the first
// run dialog. Ensure browser startup safely handles a signal while the modal
// RunLoop is running.
// TODO(crbug.com/338037494): Flaky on Linux ASan.
#if BUILDFLAG(IS_LINUX) && defined(ADDRESS_SANITIZER)
#define MAYBE_HandleSigint DISABLED_HandleSigint
#else
#define MAYBE_HandleSigint HandleSigint
#endif  //  BUILDFLAG(IS_LINUX) && defined(ADDRESS_SANITIZER)
IN_PROC_BROWSER_TEST_F(FirstRunInternalPosixTest, MAYBE_HandleSigint) {
  // Never reached. The above SIGINT should prevent the main message loop
  // (and the browser test hooking it) from running.
  ADD_FAILURE() << "Should never be called";
}

}  // namespace first_run