forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathback_button_unittest.cc
206 lines (164 loc) · 7.67 KB
/
back_button_unittest.cc
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2017 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 "ash/shelf/back_button.h"
#include <memory>
#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/app_list/test/app_list_test_helper.h"
#include "ash/app_list/views/app_list_view.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_navigation_widget.h"
#include "ash/shelf/shelf_view.h"
#include "ash/shelf/shelf_view_test_api.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/test_accelerator_target.h"
#include "ui/events/test/event_generator.h"
namespace ash {
class BackButtonTest : public AshTestBase,
public testing::WithParamInterface<bool> {
public:
BackButtonTest() = default;
~BackButtonTest() override = default;
BackButton* back_button() {
return test_api_->shelf_view()
->shelf_widget()
->navigation_widget()
->GetBackButton();
}
ShelfViewTestAPI* test_api() { return test_api_.get(); }
void SetUp() override {
if (GetParam()) {
scoped_feature_list_.InitAndEnableFeature(
chromeos::features::kShelfHotseat);
} else {
scoped_feature_list_.InitAndDisableFeature(
chromeos::features::kShelfHotseat);
}
AshTestBase::SetUp();
test_api_ = std::make_unique<ShelfViewTestAPI>(
GetPrimaryShelf()->GetShelfViewForTesting());
// Finish all setup tasks. In particular we want to finish the
// GetSwitchStates post task in (Fake)PowerManagerClient which is triggered
// by TabletModeController otherwise this will cause tablet mode to exit
// while we wait for animations in the test.
base::RunLoop().RunUntilIdle();
}
bool IsBackButtonVisible() const {
return ShelfNavigationWidget::TestApi(
test_api_->shelf_view()->shelf_widget()->navigation_widget())
.IsBackButtonVisible();
}
protected:
std::unique_ptr<ShelfViewTestAPI> test_api_;
private:
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(BackButtonTest);
};
// The parameter indicates whether the kShelfHotseat feature is enabled.
INSTANTIATE_TEST_SUITE_P(All, BackButtonTest, testing::Bool());
// Verify that the back button is visible in tablet mode.
TEST_P(BackButtonTest, Visibility) {
EXPECT_FALSE(back_button());
EXPECT_FALSE(IsBackButtonVisible());
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
test_api()->RunMessageLoopUntilAnimationsDone();
// Ensure the back button is not yet visible when hotseat is enabled.
EXPECT_EQ(!GetParam(), IsBackButtonVisible());
ASSERT_EQ(GetParam(), !back_button());
if (GetParam()) {
// When hotseat is enabled, the back button is only usable in in-app shelf.
std::unique_ptr<views::Widget> widget = CreateTestWidget();
test_api()->RunMessageLoopUntilAnimationsDone();
}
EXPECT_TRUE(IsBackButtonVisible());
EXPECT_EQ(1.f, back_button()->layer()->opacity());
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
test_api()->RunMessageLoopUntilAnimationsDone();
EXPECT_FALSE(IsBackButtonVisible());
}
// Verify that the back button is visible in tablet mode, if the initial shelf
// alignment is on the left or right.
TEST_P(BackButtonTest, VisibilityWithVerticalShelf) {
test_api()->shelf_view()->shelf()->SetAlignment(ShelfAlignment::kLeft);
EXPECT_FALSE(back_button());
EXPECT_FALSE(IsBackButtonVisible());
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
// When hotseat is enabled, the back button is only usable in in-app shelf.
if (GetParam())
std::unique_ptr<views::Widget> widget = CreateTestWidget();
test_api()->RunMessageLoopUntilAnimationsDone();
EXPECT_TRUE(back_button());
EXPECT_TRUE(IsBackButtonVisible());
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
test_api()->RunMessageLoopUntilAnimationsDone();
EXPECT_FALSE(back_button());
EXPECT_FALSE(IsBackButtonVisible());
}
TEST_P(BackButtonTest, BackKeySequenceGenerated) {
// Enter tablet mode; the back button is not visible in non tablet mode.
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
// When hotseat is enabled, the back button is only usable in in-app shelf.
if (GetParam())
std::unique_ptr<views::Widget> widget = CreateTestWidget();
ShelfNavigationWidget::TestApi navigation_widget_test_api(
GetPrimaryShelf()->shelf_widget()->navigation_widget());
// Wait for the navigation widget's animation.
test_api()->RunMessageLoopUntilAnimationsDone(
navigation_widget_test_api.GetBoundsAnimator());
AcceleratorControllerImpl* controller =
Shell::Get()->accelerator_controller();
// Register an accelerator that looks for back presses. Note there is already
// an accelerator on AppListView, which will handle the accelerator since it
// is targeted before AcceleratorController (switching to tablet mode with no
// other windows activates the app list). First remove that accelerator. In
// release, there's only the AppList's accelerator, so it's always hit when
// the app list is active. (ash/accelerators.cc has VKEY_BROWSER_BACK, but it
// also needs Ctrl pressed).
GetAppListTestHelper()->GetAppListView()->ResetAccelerators();
ui::Accelerator accelerator_back_press(ui::VKEY_BROWSER_BACK, ui::EF_NONE);
accelerator_back_press.set_key_state(ui::Accelerator::KeyState::PRESSED);
ui::TestAcceleratorTarget target_back_press;
controller->Register({accelerator_back_press}, &target_back_press);
// Register an accelerator that looks for back releases.
ui::Accelerator accelerator_back_release(ui::VKEY_BROWSER_BACK, ui::EF_NONE);
accelerator_back_release.set_key_state(ui::Accelerator::KeyState::RELEASED);
ui::TestAcceleratorTarget target_back_release;
controller->Register({accelerator_back_release}, &target_back_release);
// Verify that by pressing the back button no event is generated on the press,
// but there is one generated on the release.
ui::test::EventGenerator* generator = GetEventGenerator();
generator->MoveMouseTo(back_button()->GetBoundsInScreen().CenterPoint());
generator->PressLeftButton();
EXPECT_EQ(0, target_back_press.accelerator_count());
EXPECT_EQ(0, target_back_release.accelerator_count());
generator->ReleaseLeftButton();
EXPECT_EQ(1, target_back_press.accelerator_count());
EXPECT_EQ(1, target_back_release.accelerator_count());
}
// Tests that the back button does not show a context menu.
TEST_P(BackButtonTest, NoContextMenuOnBackButton) {
ui::test::EventGenerator* generator = GetEventGenerator();
// Enable tablet mode to show the back button. Wait for tablet mode animations
// to finish in order for the back button to move out from under the
// home button.
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
// When hotseat is enabled, the back button is only usable in in-app shelf.
if (GetParam())
std::unique_ptr<views::Widget> widget = CreateTestWidget();
// Wait for the navigation widget's animation.
ShelfNavigationWidget::TestApi navigation_widget_test_api(
GetPrimaryShelf()->shelf_widget()->navigation_widget());
test_api()->RunMessageLoopUntilAnimationsDone(
navigation_widget_test_api.GetBoundsAnimator());
generator->MoveMouseTo(back_button()->GetBoundsInScreen().CenterPoint());
generator->PressRightButton();
EXPECT_FALSE(test_api_->CloseMenu());
}
} // namespace ash