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
175
176
177
178
179
180
ash / components / arc / compat_mode / arc_splash_screen_dialog_view_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/components/arc/compat_mode/arc_splash_screen_dialog_view.h"
#include <memory>
#include "ash/components/arc/compat_mode/test/compat_mode_test_base.h"
#include "ash/style/ash_color_provider.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/widget/widget.h"
namespace arc {
class ArcSplashScreenDialogViewTest : public CompatModeTestBase {
public:
ArcSplashScreenDialogViewTest() = default;
ArcSplashScreenDialogViewTest(const ArcSplashScreenDialogViewTest& other) =
delete;
ArcSplashScreenDialogViewTest& operator=(
const ArcSplashScreenDialogViewTest& other) = delete;
~ArcSplashScreenDialogViewTest() override = default;
// CompatModeTestBase:
void SetUp() override {
CompatModeTestBase::SetUp();
parent_widget_ = CreateWidget();
EnsureAnchor();
}
void TearDown() override {
parent_widget_->CloseNow();
parent_widget_.reset();
CompatModeTestBase::TearDown();
}
protected:
views::Widget* ShowAsBubble(
std::unique_ptr<ArcSplashScreenDialogView> dialog_view) {
auto* const widget =
views::BubbleDialogDelegateView::CreateBubble(std::move(dialog_view));
widget->Show();
return widget;
}
void EnsureAnchor() {
if (anchor_)
return;
anchor_ = parent_widget_->GetRootView()->AddChildView(
std::make_unique<views::View>());
}
void RemoveAnchor() {
parent_widget_->GetRootView()->RemoveChildViewT(anchor_.get());
anchor_ = nullptr;
}
views::View* anchor() { return anchor_; }
aura::Window* parent_window() { return parent_widget_->GetNativeView(); }
views::Widget* parent_widget() { return parent_widget_.get(); }
private:
ash::AshColorProvider ash_color_provider_;
std::unique_ptr<views::Widget> parent_widget_;
raw_ptr<views::View, DanglingUntriaged> anchor_{nullptr};
};
TEST_F(ArcSplashScreenDialogViewTest, TestCloseButton) {
for (const bool is_for_unresizable : {true, false}) {
bool on_close_callback_called = false;
auto dialog_view = std::make_unique<ArcSplashScreenDialogView>(
base::BindLambdaForTesting([&]() { on_close_callback_called = true; }),
parent_window(), anchor(), is_for_unresizable);
ArcSplashScreenDialogView::TestApi dialog_view_test(dialog_view.get());
ShowAsBubble(std::move(dialog_view));
EXPECT_TRUE(dialog_view_test.close_button()->GetVisible());
EXPECT_FALSE(on_close_callback_called);
LeftClickOnView(parent_widget(), dialog_view_test.close_button());
EXPECT_TRUE(on_close_callback_called);
}
}
TEST_F(ArcSplashScreenDialogViewTest, TestEscKey) {
for (const bool is_for_unresizable : {true, false}) {
bool on_close_callback_called = false;
auto dialog_view = std::make_unique<ArcSplashScreenDialogView>(
base::BindLambdaForTesting([&]() { on_close_callback_called = true; }),
parent_window(), anchor(), is_for_unresizable);
ArcSplashScreenDialogView::TestApi dialog_view_test(dialog_view.get());
auto* const bubble = ShowAsBubble(std::move(dialog_view));
EXPECT_FALSE(on_close_callback_called);
EXPECT_TRUE(
anchor()->GetIndexOf(dialog_view_test.highlight_border()).has_value());
// Simulates esc key event to close the dialog.
ui::KeyEvent event(ui::EventType::kKeyPressed, ui::VKEY_ESCAPE,
ui::EF_NONE);
bubble->OnKeyEvent(&event);
EXPECT_TRUE(on_close_callback_called);
EXPECT_FALSE(
anchor()->GetIndexOf(dialog_view_test.highlight_border()).has_value());
}
}
TEST_F(ArcSplashScreenDialogViewTest, TestAnchorHighlight) {
for (const bool is_for_unresizable : {true, false}) {
auto dialog_view = std::make_unique<ArcSplashScreenDialogView>(
base::DoNothing(), parent_window(), anchor(), is_for_unresizable);
ArcSplashScreenDialogView::TestApi dialog_view_test(dialog_view.get());
ShowAsBubble(std::move(dialog_view));
EXPECT_TRUE(
anchor()->GetIndexOf(dialog_view_test.highlight_border()).has_value());
LeftClickOnView(parent_widget(), dialog_view_test.close_button());
EXPECT_FALSE(
anchor()->GetIndexOf(dialog_view_test.highlight_border()).has_value());
}
}
TEST_F(ArcSplashScreenDialogViewTest, TestAnchorDestroy) {
for (const bool is_for_unresizable : {true, false}) {
EnsureAnchor();
auto dialog_view = std::make_unique<ArcSplashScreenDialogView>(
base::DoNothing(), parent_window(), anchor(), is_for_unresizable);
ArcSplashScreenDialogView::TestApi dialog_view_test(dialog_view.get());
ShowAsBubble(std::move(dialog_view));
// Removing the anchor from view hierarchy makes the anchor destroyed.
RemoveAnchor();
// Verify that clicking the button won't cause any crash even after
// destroying the anchor.
LeftClickOnView(parent_widget(), dialog_view_test.close_button());
}
}
TEST_F(ArcSplashScreenDialogViewTest,
TestSplashScreenInFullscreenOrMaximinzedWindow) {
for (const bool is_for_unresizable : {true, false}) {
for (const auto state : {ui::mojom::WindowShowState::kFullscreen,
ui::mojom::WindowShowState::kMaximized}) {
bool on_close_callback_called = false;
auto dialog_view = std::make_unique<ArcSplashScreenDialogView>(
base::BindLambdaForTesting(
[&]() { on_close_callback_called = true; }),
parent_window(), anchor(), is_for_unresizable);
ShowAsBubble(std::move(dialog_view));
EXPECT_FALSE(on_close_callback_called);
parent_window()->SetProperty(aura::client::kShowStateKey, state);
EXPECT_TRUE(on_close_callback_called);
}
}
}
// Test that the activation is forwarded to the bubble when the parent window is
// activated.
TEST_F(ArcSplashScreenDialogViewTest, TestForwardActivation) {
auto* const bubble = ShowAsBubble(std::make_unique<ArcSplashScreenDialogView>(
base::DoNothing(), parent_window(), anchor(),
/*is_for_unresizable=*/false));
EXPECT_TRUE(bubble->IsActive());
EXPECT_FALSE(parent_widget()->IsActive());
parent_widget()->Activate();
EXPECT_FALSE(bubble->IsActive());
EXPECT_TRUE(parent_widget()->IsActive());
RunPendingMessages();
EXPECT_TRUE(bubble->IsActive());
EXPECT_FALSE(parent_widget()->IsActive());
}
} // namespace arc