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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// Copyright 2022 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/lifetime/application_lifetime.h"
#include <optional>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime_chromeos.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/ash/components/dbus/session_manager/fake_session_manager_client.h"
#include "chromeos/ash/components/dbus/update_engine/fake_update_engine_client.h"
#include "chromeos/ash/components/dbus/update_engine/update_engine_client.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "components/keep_alive_registry/keep_alive_registry.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome {
class ApplicationLifetimeTest : public InProcessBrowserTest {
public:
void SetUpInProcessBrowserTestFixture() override {
InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
fake_update_engine_client_ =
ash::UpdateEngineClient::InitializeFakeForTest();
}
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
browser_did_close_subscription_ = browser()->RegisterBrowserDidClose(
base::BindRepeating(&ApplicationLifetimeTest::OnBrowserDidClose,
base::Unretained(this)));
}
void TearDownOnMainThread() override {
// Waits for the browser to close if it has not closed already.
if (browser_did_close_subscription_) {
quits_on_browser_closing_.emplace();
quits_on_browser_closing_->Run();
}
InProcessBrowserTest::TearDownOnMainThread();
}
protected:
void FakePendingUpdate() {
update_engine::StatusResult update_status;
update_status.set_current_operation(
update_engine::Operation::UPDATED_NEED_REBOOT);
fake_update_engine_client_->set_default_status(update_status);
// Shutdown code in termination_notification.cc is only run once.
// Exit Chrome after reboot after update would have been sent to
// update_engine. Otherwise the browsertest framework wont be able to stop
// Chrome gracefully.
fake_update_engine_client_->set_reboot_after_update_callback(
base::BindOnce(&ExitIgnoreUnloadHandlers));
}
bool RequestedRebootAfterUpdate() {
return fake_update_engine_client_->reboot_after_update_call_count() > 0;
}
private:
void OnBrowserDidClose(BrowserWindowInterface* browser_window_interface) {
browser_did_close_subscription_.reset();
if (quits_on_browser_closing_) {
quits_on_browser_closing_->Quit();
}
}
std::optional<base::RunLoop> quits_on_browser_closing_;
std::optional<base::CallbackListSubscription> browser_did_close_subscription_;
raw_ptr<ash::FakeUpdateEngineClient, DanglingUntriaged>
fake_update_engine_client_ = nullptr;
};
IN_PROC_BROWSER_TEST_F(ApplicationLifetimeTest,
AttemptRestartRestartsBrowserInPlace) {
AttemptRestart();
// Session Manager is not going to stop session.
EXPECT_FALSE(IsSendingStopRequestToSessionManager());
auto* fake_session_manager_client = ash::FakeSessionManagerClient::Get();
EXPECT_FALSE(fake_session_manager_client->session_stopped());
// No reboot requested.
auto* fake_power_manager_client = chromeos::FakePowerManagerClient::Get();
EXPECT_EQ(fake_power_manager_client->num_request_restart_calls(), 0);
// Restart flags are set.
PrefService* pref_service = g_browser_process->local_state();
EXPECT_TRUE(pref_service->GetBoolean(prefs::kWasRestarted));
EXPECT_TRUE(KeepAliveRegistry::GetInstance()->IsRestarting());
}
IN_PROC_BROWSER_TEST_F(ApplicationLifetimeTest,
AttemptRestartWithPendingUpdateReboots) {
FakePendingUpdate();
AttemptRestart();
// Session Manager is not going to stop session.
EXPECT_FALSE(IsSendingStopRequestToSessionManager());
auto* fake_session_manager_client = ash::FakeSessionManagerClient::Get();
EXPECT_FALSE(fake_session_manager_client->session_stopped());
// No reboot requested via power manager.
auto* fake_power_manager_client = chromeos::FakePowerManagerClient::Get();
EXPECT_EQ(fake_power_manager_client->num_request_restart_calls(), 0);
// Reboot requested via update engine client.
EXPECT_TRUE(RequestedRebootAfterUpdate());
// Restart flags are set.
PrefService* pref_service = g_browser_process->local_state();
EXPECT_TRUE(pref_service->GetBoolean(prefs::kWasRestarted));
EXPECT_TRUE(KeepAliveRegistry::GetInstance()->IsRestarting());
}
IN_PROC_BROWSER_TEST_F(ApplicationLifetimeTest, AttemptRelaunchRelaunchesOs) {
AttemptRelaunch();
// Session Manager is not going to stop session.
EXPECT_FALSE(IsSendingStopRequestToSessionManager());
auto* fake_session_manager_client = ash::FakeSessionManagerClient::Get();
EXPECT_FALSE(fake_session_manager_client->session_stopped());
// Reboot has been requested.
auto* fake_power_manager_client = chromeos::FakePowerManagerClient::Get();
EXPECT_GE(fake_power_manager_client->num_request_restart_calls(), 1);
// No restart flags set.
PrefService* pref_service = g_browser_process->local_state();
EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsRestarting());
}
IN_PROC_BROWSER_TEST_F(ApplicationLifetimeTest,
AttemptExitSendsStopRequestToSessionManager) {
AttemptExit();
// Session Manager has received stop session request.
EXPECT_TRUE(IsSendingStopRequestToSessionManager());
auto* fake_session_manager_client = ash::FakeSessionManagerClient::Get();
EXPECT_TRUE(fake_session_manager_client->session_stopped());
// No reboot requested.
auto* fake_power_manager_client = chromeos::FakePowerManagerClient::Get();
EXPECT_EQ(fake_power_manager_client->num_request_restart_calls(), 0);
// No restart flags set.
PrefService* pref_service = g_browser_process->local_state();
EXPECT_FALSE(pref_service->GetBoolean(prefs::kWasRestarted));
EXPECT_FALSE(KeepAliveRegistry::GetInstance()->IsRestarting());
}
IN_PROC_BROWSER_TEST_F(ApplicationLifetimeTest, RelaunchForUpdate) {
FakePendingUpdate();
RelaunchForUpdate();
// Reboot requested via update engine client.
EXPECT_TRUE(RequestedRebootAfterUpdate());
}
} // namespace chrome
|