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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
// Copyright 2024 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/system/mahi/mahi_panel_widget.h"
#include "ash/keyboard/keyboard_controller_impl.h"
#include "ash/keyboard/virtual_keyboard_controller.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/mahi/fake_mahi_manager.h"
#include "ash/system/mahi/mahi_constants.h"
#include "ash/system/mahi/mahi_ui_controller.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/window_util.h"
#include "ash/wm/work_area_insets.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/components/mahi/public/cpp/mahi_manager.h"
#include "chromeos/constants/chromeos_features.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/base/ui_base_types.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/view.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
constexpr int kPanelDefaultWidth = 360;
constexpr int kPanelDefaultHeight = 492;
constexpr int kPanelBoundsShelfPadding = 8;
} // namespace
class MahiPanelWidgetTest : public AshTestBase {
public:
// AshTestBase:
void SetUp() override {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{chromeos::features::kMahi,
chromeos::features::kFeatureManagementMahi,
chromeos::features::kMahiPanelResizable},
/*disabled_features=*/{});
AshTestBase::SetUp();
fake_mahi_manager_ = std::make_unique<FakeMahiManager>();
scoped_setter_ = std::make_unique<chromeos::ScopedMahiManagerSetter>(
fake_mahi_manager_.get());
}
void TearDown() override {
scoped_setter_.reset();
fake_mahi_manager_.reset();
AshTestBase::TearDown();
}
protected:
MahiUiController ui_controller_;
private:
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<FakeMahiManager> fake_mahi_manager_;
std::unique_ptr<chromeos::ScopedMahiManagerSetter> scoped_setter_;
};
TEST_F(MahiPanelWidgetTest, DefaultWidgetBounds) {
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(10, 10, 300, 300), &ui_controller_);
// The mahi panel should have the same origin as the mahi_menu_bounds when
// there is enough space for it.
EXPECT_EQ(gfx::Rect(gfx::Point(10, 10),
gfx::Size(kPanelDefaultWidth, kPanelDefaultHeight)),
widget->GetRestoredBounds());
}
TEST_F(MahiPanelWidgetTest, AccessibleTitle) {
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(10, 10, 300, 300), &ui_controller_);
EXPECT_EQ(widget->GetRootView()->GetViewAccessibility().GetCachedName(),
l10n_util::GetStringUTF16(IDS_ASH_MAHI_PANEL_TITLE));
}
TEST_F(MahiPanelWidgetTest, WidgetPositionWithConstrainedBottomSpace) {
UpdateDisplay("800x700");
// Place the menu 200px above the screen's bottom to ensure there is not
// enough space for the panel to align with the top of the mahi menu.
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(100, 500, 300, 300), &ui_controller_);
// The panel's bottom should be `kPanelBoundsShelfPadding` pixels above the
// work_area's bottom.
EXPECT_EQ(
display::Screen::GetScreen()->GetPrimaryDisplay().work_area().bottom() -
kPanelBoundsShelfPadding,
widget->GetRestoredBounds().bottom());
}
TEST_F(MahiPanelWidgetTest, WidgetAfterResize) {
UpdateDisplay("800x700");
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(100, 100, 300, 300), &ui_controller_);
// Click on the top left of the panel and drag towards the top left of the
// screen to resize.
GetEventGenerator()->set_current_screen_location(
widget->GetWindowBoundsInScreen().origin());
constexpr gfx::Vector2d kDragOffset(-50, -70);
GetEventGenerator()->DragMouseBy(kDragOffset.x(), kDragOffset.y());
auto expected_bounds =
gfx::Rect(50, 30, kPanelDefaultWidth + 50, kPanelDefaultHeight + 70);
EXPECT_EQ(expected_bounds, widget->GetWindowBoundsInScreen());
}
TEST_F(MahiPanelWidgetTest, WidgetPositionAfterWorkAreaBoundsChange) {
auto default_work_area =
display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
// Create a widget that has the same size as the work area and show it at the
// bottom of the work area bounds.
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/
gfx::Rect(
gfx::Point(default_work_area.x(), default_work_area.bottom()),
gfx::Size(default_work_area.width(), default_work_area.height())),
&ui_controller_);
// Reduce the user work area bounds by force-showing the virtual keyboard.
Shell::Get()
->keyboard_controller()
->virtual_keyboard_controller()
->ForceShowKeyboard();
base::RunLoop().RunUntilIdle();
auto current_work_area = WorkAreaInsets::ForWindow(widget->GetNativeWindow())
->user_work_area_bounds();
EXPECT_LT(current_work_area.bottom(), default_work_area.bottom());
// The panel's bottom should be `kPanelBoundsShelfPadding` pixels above the
// work_area's bottom.
EXPECT_EQ(current_work_area.bottom() - kPanelBoundsShelfPadding,
widget->GetRestoredBounds().bottom());
// Hide the virtual keyboard. The work area bounds should return to their
// default value.
Shell::Get()->keyboard_controller()->HideKeyboard(HideReason::kSystem);
current_work_area = WorkAreaInsets::ForWindow(widget->GetNativeWindow())
->user_work_area_bounds();
EXPECT_EQ(current_work_area.bottom(), default_work_area.bottom());
// The panel's top should be `kPanelBoundsShelfPadding` pixels below the
// work_area's top.
EXPECT_EQ(current_work_area.y() + kPanelBoundsShelfPadding,
widget->GetRestoredBounds().y());
}
TEST_F(MahiPanelWidgetTest, WidgetPositionWithConstrainedRightSpace) {
UpdateDisplay("800x700");
// Place the menu at the right edge of the screen to ensure there is not
// enough space for the panel to align with the left edge of the mahi menu.
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(500, 100, 300, 300), &ui_controller_);
// The panel should be placed correctly within the work area.
EXPECT_EQ(
display::Screen::GetScreen()->GetPrimaryDisplay().work_area().right(),
widget->GetRestoredBounds().right());
}
TEST_F(MahiPanelWidgetTest, WidgetDestroyedDuringShowAnimation) {
// Enable animations.
ui::ScopedAnimationDurationScaleMode duration(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(100, 100, 200, 200), &ui_controller_);
ASSERT_TRUE(widget->GetContentsView()
->GetViewByID(mahi_constants::ViewId::kMahiPanelView)
->layer()
->GetAnimator()
->is_animating());
// Expect the widget to close gracefully without a crash while an animation
// is in progress.
widget->CloseNow();
}
TEST_F(MahiPanelWidgetTest, WidgetBoundsAfterRefreshBannerUpdate) {
views::UniqueWidgetPtr panel_widget =
MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(), /*mahi_menu_bounds=*/gfx::Rect(),
&ui_controller_);
// Set the widget bounds to be different to the default bounds, so that we can
// test that the panel location is preserved.
panel_widget->SetBounds(gfx::Rect(100, 200, 300, 200));
const gfx::Rect kInitialPanelWidgetBounds =
panel_widget->GetWindowBoundsInScreen();
views::View* panel_view = panel_widget->GetContentsView()->GetViewByID(
mahi_constants::ViewId::kMahiPanelView);
const gfx::Rect kInitialPanelViewBounds = panel_view->GetBoundsInScreen();
views::View* refresh_view = panel_widget->GetContentsView()->GetViewByID(
mahi_constants::ViewId::kRefreshView);
refresh_view->SetVisible(true);
// The widget bounds should now provide space for the refresh banner at the
// top, while preserving the panel view bounds.
EXPECT_EQ(panel_widget->GetWindowBoundsInScreen().InsetsFrom(
kInitialPanelWidgetBounds),
gfx::Insets::TLBR(refresh_view->height() -
mahi_constants::kRefreshBannerStackDepth,
0, 0, 0));
EXPECT_EQ(panel_view->GetBoundsInScreen(), kInitialPanelViewBounds);
refresh_view->SetVisible(false);
// The panel widget and view bounds should be restored to their values before
// the refresh banner was shown.
EXPECT_EQ(panel_widget->GetWindowBoundsInScreen(), kInitialPanelWidgetBounds);
EXPECT_EQ(panel_view->GetBoundsInScreen(), kInitialPanelViewBounds);
}
// Tests that the Mahi panel widget should not resize due to a screen size
// change e.g. due to using docked magnifier.
TEST_F(MahiPanelWidgetTest, WidgetDoesNotResize) {
// Set a window size that fits the panel and cache the widget size.
UpdateDisplay("800x700");
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(gfx::Point(10, 10), gfx::Size(300, 300)),
&ui_controller_);
const auto panel_widget_size = widget->GetSize();
// Reduce the screen size such that the panel widget would not entirely fit.
// It should keep its original size.
UpdateDisplay("200x180");
EXPECT_EQ(widget->GetSize(), panel_widget_size);
}
// Tests that the Mahi panel widget stays visible when another window goes
// full-screen.
TEST_F(MahiPanelWidgetTest, WidgetDoesNotHideOnFullScreen) {
// Create and show the panel widget. It should be visible.
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(gfx::Point(10, 10), gfx::Size(300, 300)),
&ui_controller_);
EXPECT_TRUE(widget->IsVisible());
// Create a fullscreen window. The panel widget should still be visible.
auto window = CreateTestWindow();
window->SetProperty(aura::client::kShowStateKey,
ui::mojom::WindowShowState::kFullscreen);
EXPECT_TRUE(widget->IsVisible());
// Expect the mahi panel widget to be in the top-most window compared to the
// regular window.
EXPECT_EQ(window_util::GetTopMostWindow(
{window->parent(), widget->GetNativeWindow()->parent()}),
widget->GetNativeWindow()->parent());
}
// Tests that the Mahi panel widget is activatable by selecting its textfield.
TEST_F(MahiPanelWidgetTest, WidgetIsActivatable) {
// Create and show the panel widget, it should be activatable.
auto widget = MahiPanelWidget::CreateAndShowPanelWidget(
GetPrimaryDisplay().id(),
/*mahi_menu_bounds=*/gfx::Rect(gfx::Point(10, 10), gfx::Size(300, 300)),
&ui_controller_);
EXPECT_TRUE(widget->CanActivate());
// Click on the textfield which should add focus to it, meaning that the
// widget is activatable.
auto* question_textfield = widget->GetContentsView()->GetViewByID(
mahi_constants::ViewId::kQuestionTextfield);
LeftClickOn(question_textfield);
EXPECT_TRUE(question_textfield->HasFocus());
}
} // namespace ash
|