blob: d761ff2a7c7d78885743e01396e055abbe136c88 [file] [log] [blame]
James Cookb263b512018-03-07 22:30:331// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/accessibility/accessibility_panel_layout_manager.h"
6
7#include <memory>
8
Matthew Mourgos75327562019-09-09 21:23:579#include "ash/public/cpp/shelf_config.h"
James Cookb263b512018-03-07 22:30:3310#include "ash/shell.h"
11#include "ash/test/ash_test_base.h"
12#include "ui/display/display.h"
13#include "ui/display/screen.h"
14#include "ui/views/widget/widget.h"
15
16namespace ash {
17namespace {
18
19// Shorten the name for better line wrapping.
Zach Helfinsteinc62558512018-08-16 18:05:4520constexpr int kDefaultPanelHeight =
21 AccessibilityPanelLayoutManager::kDefaultPanelHeight;
James Cookb263b512018-03-07 22:30:3322
23AccessibilityPanelLayoutManager* GetLayoutManager() {
24 aura::Window* container =
25 Shell::GetContainer(Shell::GetPrimaryRootWindow(),
26 kShellWindowId_AccessibilityPanelContainer);
27 return static_cast<AccessibilityPanelLayoutManager*>(
28 container->layout_manager());
29}
30
31// Simulates Chrome creating the ChromeVoxPanel widget.
32std::unique_ptr<views::Widget> CreateChromeVoxPanel() {
33 std::unique_ptr<views::Widget> widget = std::make_unique<views::Widget>();
34 views::Widget::InitParams params(
35 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
36 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
37 aura::Window* root_window = Shell::GetPrimaryRootWindow();
38 params.parent = Shell::GetContainer(
39 root_window, kShellWindowId_AccessibilityPanelContainer);
40 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
41 params.bounds = gfx::Rect(0, 0, root_window->bounds().width(),
42 root_window->bounds().height());
Ahmed Fakhry32f3c452019-08-01 16:36:3443 widget->Init(std::move(params));
James Cookb263b512018-03-07 22:30:3344 return widget;
45}
46
47using AccessibilityPanelLayoutManagerTest = AshTestBase;
48
49TEST_F(AccessibilityPanelLayoutManagerTest, Basics) {
50 AccessibilityPanelLayoutManager* layout_manager = GetLayoutManager();
51 ASSERT_TRUE(layout_manager);
52
53 // The layout manager doesn't track anything at startup.
54 EXPECT_FALSE(layout_manager->panel_window_for_test());
55
56 // Simulate chrome creating the ChromeVox widget. The layout manager starts
57 // managing it.
58 std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel();
59 widget->Show();
60 EXPECT_EQ(widget->GetNativeWindow(), layout_manager->panel_window_for_test());
61
62 // The layout manager doesn't track anything after the widget closes.
63 widget.reset();
64 EXPECT_FALSE(layout_manager->panel_window_for_test());
65}
66
67TEST_F(AccessibilityPanelLayoutManagerTest, Shutdown) {
68 // Simulate chrome creating the ChromeVox widget.
69 std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel();
70 widget->Show();
71
72 // Don't close the window.
73 widget.release();
74
75 // Ash should not crash if the window is still open at shutdown.
76}
77
James Cookb263b512018-03-07 22:30:3378TEST_F(AccessibilityPanelLayoutManagerTest, PanelFullscreen) {
79 AccessibilityPanelLayoutManager* layout_manager = GetLayoutManager();
80 display::Screen* screen = display::Screen::GetScreen();
81
82 std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel();
83 widget->Show();
84
Zach Helfinsteinc62558512018-08-16 18:05:4585 layout_manager->SetPanelBounds(gfx::Rect(0, 0, 0, kDefaultPanelHeight),
Mike Wassermand13e95a2019-06-28 23:29:4886 AccessibilityPanelState::FULL_WIDTH);
Zach Helfinsteinc62558512018-08-16 18:05:4587
James Cookb263b512018-03-07 22:30:3388 gfx::Rect expected_work_area = screen->GetPrimaryDisplay().work_area();
89
Anastasia Helfinsteinb855e3c2018-12-13 23:02:4090 // When the panel is fullscreen it fills the display and clears the
James Cookb263b512018-03-07 22:30:3391 // work area.
Zach Helfinsteinc62558512018-08-16 18:05:4592 layout_manager->SetPanelBounds(gfx::Rect(),
Mike Wassermand13e95a2019-06-28 23:29:4893 AccessibilityPanelState::FULLSCREEN);
James Cookb263b512018-03-07 22:30:3394 EXPECT_EQ(widget->GetNativeWindow()->bounds(),
95 screen->GetPrimaryDisplay().bounds());
Anastasia Helfinsteinb855e3c2018-12-13 23:02:4096 EXPECT_EQ(screen->GetPrimaryDisplay().work_area().y(), 0);
James Cookb263b512018-03-07 22:30:3397
Anastasia Helfinsteinb855e3c2018-12-13 23:02:4098 // Restoring the panel to default size restores the bounds and sets
James Cookb263b512018-03-07 22:30:3399 // the work area.
Zach Helfinsteinc62558512018-08-16 18:05:45100 layout_manager->SetPanelBounds(gfx::Rect(0, 0, 0, kDefaultPanelHeight),
Mike Wassermand13e95a2019-06-28 23:29:48101 AccessibilityPanelState::FULL_WIDTH);
James Cookb263b512018-03-07 22:30:33102 gfx::Rect expected_bounds(0, 0, screen->GetPrimaryDisplay().bounds().width(),
Zach Helfinsteinc62558512018-08-16 18:05:45103 kDefaultPanelHeight);
James Cookb263b512018-03-07 22:30:33104 EXPECT_EQ(widget->GetNativeWindow()->bounds(), expected_bounds);
105 EXPECT_EQ(screen->GetPrimaryDisplay().work_area(), expected_work_area);
106}
107
Zach Helfinsteinc62558512018-08-16 18:05:45108TEST_F(AccessibilityPanelLayoutManagerTest, SetBounds) {
109 std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel();
110 widget->Show();
111
112 gfx::Rect bounds(0, 0, 100, 100);
Mike Wassermand13e95a2019-06-28 23:29:48113 GetLayoutManager()->SetPanelBounds(bounds, AccessibilityPanelState::BOUNDED);
Zach Helfinsteinc62558512018-08-16 18:05:45114 EXPECT_EQ(widget->GetNativeWindow()->bounds(), bounds);
115}
116
James Cookb263b512018-03-07 22:30:33117TEST_F(AccessibilityPanelLayoutManagerTest, DisplayBoundsChange) {
118 std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel();
119 widget->Show();
Mike Wassermand13e95a2019-06-28 23:29:48120 GetLayoutManager()->SetPanelBounds(gfx::Rect(0, 0, 0, kDefaultPanelHeight),
121 AccessibilityPanelState::FULL_WIDTH);
James Cookb263b512018-03-07 22:30:33122
123 // When the display resolution changes the panel still sits at the top of the
124 // screen.
125 UpdateDisplay("1234,567");
126 display::Screen* screen = display::Screen::GetScreen();
127 gfx::Rect expected_bounds(0, 0, screen->GetPrimaryDisplay().bounds().width(),
Zach Helfinsteinc62558512018-08-16 18:05:45128 kDefaultPanelHeight);
James Cookb263b512018-03-07 22:30:33129 EXPECT_EQ(widget->GetNativeWindow()->bounds(), expected_bounds);
130
131 gfx::Rect expected_work_area = screen->GetPrimaryDisplay().bounds();
Manu Corneta76b6b02018-08-30 21:02:23132 expected_work_area.Inset(0, kDefaultPanelHeight, 0,
Matthew Mourgos75327562019-09-09 21:23:57133 ShelfConfig::Get()->shelf_size());
James Cookb263b512018-03-07 22:30:33134 EXPECT_EQ(screen->GetPrimaryDisplay().work_area(), expected_work_area);
135}
136
137} // namespace
138} // namespace ash