Commit c4e94b8c authored by Nicholas Verne's avatar Nicholas Verne Committed by Commit Bot

Browser tests for CrostiniRecoveryView

These tests are an promised add on to
https://chromium-review.googlesource.com/c/chromium/src/+/2084213

Bug: 1057899
Change-Id: I92f6f7a308df197a02e0b935c6d0627509b60761
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2087879
Commit-Queue: Nicholas Verne <nverne@chromium.org>
Auto-Submit: Nicholas Verne <nverne@chromium.org>
Reviewed-by: default avatarNic Hollingum <hollingum@google.com>
Cr-Commit-Position: refs/heads/master@{#747146}
parent 55016d4e
......@@ -828,6 +828,10 @@ bool CrostiniManager::IsUncleanStartup() const {
return is_unclean_startup_;
}
void CrostiniManager::SetUncleanStartupForTesting(bool is_unclean_startup) {
is_unclean_startup_ = is_unclean_startup;
}
base::Optional<ContainerInfo> CrostiniManager::GetContainerInfo(
std::string vm_name,
std::string container_name) {
......
......@@ -606,6 +606,7 @@ class CrostiniManager : public KeyedService,
void UpgradePromptShown(const ContainerId& container_id);
void EnsureVmRunning(const ContainerId& key, CrostiniResultCallback callback);
bool IsUncleanStartup() const;
void SetUncleanStartupForTesting(bool is_unclean_startup);
private:
class CrostiniRestarter;
......
......@@ -35,10 +35,15 @@ bool crostini::ShowCrostiniRecoveryView(
const std::string& app_id,
int64_t display_id,
crostini::LaunchCrostiniAppCallback callback) {
base::UmaHistogramEnumeration(kCrostiniRecoverySourceHistogram, ui_surface,
crostini::CrostiniUISurface::kCount);
return CrostiniRecoveryView::Show(profile, app_id, display_id,
std::move(callback));
bool allow_app_launch = CrostiniRecoveryView::Show(
profile, app_id, display_id, std::move(callback));
if (!allow_app_launch) {
// App launches are prevented by the view's can_launch_apps_. In this case,
// we want to sample the Show call.
base::UmaHistogramEnumeration(kCrostiniRecoverySourceHistogram, ui_surface,
crostini::CrostiniUISurface::kCount);
}
return allow_app_launch;
}
bool CrostiniRecoveryView::Show(Profile* profile,
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/crostini/crostini_recovery_view.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_base.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/chromeos/crostini/crostini_manager.h"
#include "chrome/browser/chromeos/crostini/crostini_registry_service.h"
#include "chrome/browser/chromeos/crostini/crostini_registry_service_factory.h"
#include "chrome/browser/chromeos/crostini/crostini_test_helper.h"
#include "chrome/browser/chromeos/crostini/crostini_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/crostini/crostini_browser_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
constexpr crostini::CrostiniUISurface kUiSurface =
crostini::CrostiniUISurface::kAppList;
constexpr char kDesktopFileId[] = "test_app";
constexpr int kDisplayId = 0;
class CrostiniRecoveryViewBrowserTest : public CrostiniDialogBrowserTest {
public:
CrostiniRecoveryViewBrowserTest()
: CrostiniDialogBrowserTest(true /*register_termina*/),
app_id_(crostini::CrostiniTestHelper::GenerateAppId(kDesktopFileId)) {}
// DialogBrowserTest:
void ShowUi(const std::string& name) override {
ShowCrostiniRecoveryView(browser()->profile(), kUiSurface, app_id(),
kDisplayId, base::DoNothing());
}
void SetUncleanStartup() {
crostini::CrostiniManager::GetForProfile(browser()->profile())
->SetUncleanStartupForTesting(true);
}
CrostiniRecoveryView* ActiveView() {
return CrostiniRecoveryView::GetActiveViewForTesting();
}
bool HasAcceptButton() { return ActiveView()->GetOkButton() != nullptr; }
bool HasCancelButton() { return ActiveView()->GetCancelButton() != nullptr; }
void WaitForViewDestroyed() {
base::RunLoop().RunUntilIdle();
ExpectNoView();
}
void ExpectView() {
// A new Widget was created in ShowUi() or since the last VerifyUi().
EXPECT_TRUE(VerifyUi());
// There is one view, and it's ours.
EXPECT_NE(nullptr, ActiveView());
EXPECT_EQ(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL,
ActiveView()->GetDialogButtons());
EXPECT_TRUE(HasAcceptButton());
EXPECT_TRUE(HasCancelButton());
}
void ExpectNoView() {
// No new Widget was created in ShowUi() or since the last VerifyUi().
EXPECT_FALSE(VerifyUi());
// Our view has really been deleted.
EXPECT_EQ(nullptr, ActiveView());
}
bool IsUncleanStartup() {
return crostini::CrostiniManager::GetForProfile(browser()->profile())
->IsUncleanStartup();
}
void RegisterApp() {
vm_tools::apps::ApplicationList app_list =
crostini::CrostiniTestHelper::BasicAppList(
kDesktopFileId, crostini::kCrostiniDefaultVmName,
crostini::kCrostiniDefaultContainerName);
crostini::CrostiniRegistryServiceFactory::GetForProfile(
browser()->profile())
->UpdateApplicationList(app_list);
}
const std::string& app_id() const { return app_id_; }
private:
std::string app_id_;
DISALLOW_COPY_AND_ASSIGN(CrostiniRecoveryViewBrowserTest);
};
// Test the dialog is actually launched.
IN_PROC_BROWSER_TEST_F(CrostiniRecoveryViewBrowserTest, InvokeUi_default) {
ShowAndVerifyUi();
}
IN_PROC_BROWSER_TEST_F(CrostiniRecoveryViewBrowserTest, NoViewOnNormalStartup) {
base::HistogramTester histogram_tester;
RegisterApp();
crostini::LaunchCrostiniApp(browser()->profile(), app_id(), kDisplayId);
ExpectNoView();
}
IN_PROC_BROWSER_TEST_F(CrostiniRecoveryViewBrowserTest, ShowsOnUncleanStart) {
base::HistogramTester histogram_tester;
SetUncleanStartup();
RegisterApp();
crostini::LaunchCrostiniApp(browser()->profile(), app_id(), kDisplayId);
ExpectView();
ActiveView()->CancelDialog();
WaitForViewDestroyed();
// Canceling means we aren't restarting the VM.
EXPECT_TRUE(IsUncleanStartup());
histogram_tester.ExpectUniqueSample(
"Crostini.RecoverySource",
static_cast<base::HistogramBase::Sample>(kUiSurface), 1);
}
IN_PROC_BROWSER_TEST_F(CrostiniRecoveryViewBrowserTest,
ReshowViewIfRestartNotSelected) {
base::HistogramTester histogram_tester;
SetUncleanStartup();
RegisterApp();
crostini::LaunchCrostiniApp(browser()->profile(), app_id(), kDisplayId);
ExpectView();
ActiveView()->CancelDialog();
WaitForViewDestroyed();
crostini::LaunchCrostiniApp(browser()->profile(), app_id(), kDisplayId);
ExpectView();
ActiveView()->CancelDialog();
WaitForViewDestroyed();
EXPECT_TRUE(IsUncleanStartup());
histogram_tester.ExpectUniqueSample(
"Crostini.RecoverySource",
static_cast<base::HistogramBase::Sample>(kUiSurface), 2);
}
IN_PROC_BROWSER_TEST_F(CrostiniRecoveryViewBrowserTest, NoViewAfterRestart) {
base::HistogramTester histogram_tester;
SetUncleanStartup();
RegisterApp();
crostini::LaunchCrostiniApp(browser()->profile(), app_id(), kDisplayId);
ExpectView();
ActiveView()->AcceptDialog();
WaitForViewDestroyed();
EXPECT_FALSE(IsUncleanStartup());
crostini::LaunchCrostiniApp(browser()->profile(), app_id(), kDisplayId);
ExpectNoView();
histogram_tester.ExpectUniqueSample(
"Crostini.RecoverySource",
static_cast<base::HistogramBase::Sample>(kUiSurface), 1);
}
......@@ -2463,6 +2463,7 @@ if (!is_android) {
"../browser/ui/views/crostini/crostini_browser_test_util.h",
"../browser/ui/views/crostini/crostini_force_close_view_browsertest.cc",
"../browser/ui/views/crostini/crostini_package_install_failure_view_browsertest.cc",
"../browser/ui/views/crostini/crostini_recovery_view_browsertest.cc",
"../browser/ui/views/crostini/crostini_uninstaller_view_browsertest.cc",
"../browser/ui/views/crostini/crostini_update_component_view_browsertest.cc",
"../browser/ui/views/crostini/crostini_update_filesystem_view_browsertest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment