Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
David Tseng | b774097e | 2021-03-24 17:31:10 | [diff] [blame] | 5 | #include "ash/accessibility/ui/accessibility_panel_layout_manager.h" |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | |
Josiah Krutz | 1ccb7e92 | 2023-06-20 16:28:32 | [diff] [blame] | 9 | #include "ash/accessibility/magnifier/docked_magnifier_controller.h" |
Matthew Mourgos | 7532756 | 2019-09-09 21:23:57 | [diff] [blame] | 10 | #include "ash/public/cpp/shelf_config.h" |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 11 | #include "ash/shell.h" |
| 12 | #include "ash/test/ash_test_base.h" |
| 13 | #include "ui/display/display.h" |
| 14 | #include "ui/display/screen.h" |
| 15 | #include "ui/views/widget/widget.h" |
| 16 | |
| 17 | namespace ash { |
| 18 | namespace { |
| 19 | |
| 20 | // Shorten the name for better line wrapping. |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 21 | constexpr int kDefaultPanelHeight = |
| 22 | AccessibilityPanelLayoutManager::kDefaultPanelHeight; |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 23 | |
| 24 | AccessibilityPanelLayoutManager* GetLayoutManager() { |
| 25 | aura::Window* container = |
| 26 | Shell::GetContainer(Shell::GetPrimaryRootWindow(), |
| 27 | kShellWindowId_AccessibilityPanelContainer); |
| 28 | return static_cast<AccessibilityPanelLayoutManager*>( |
| 29 | container->layout_manager()); |
| 30 | } |
| 31 | |
| 32 | // Simulates Chrome creating the ChromeVoxPanel widget. |
| 33 | std::unique_ptr<views::Widget> CreateChromeVoxPanel() { |
| 34 | std::unique_ptr<views::Widget> widget = std::make_unique<views::Widget>(); |
| 35 | views::Widget::InitParams params( |
| 36 | views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 37 | params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 38 | aura::Window* root_window = Shell::GetPrimaryRootWindow(); |
| 39 | params.parent = Shell::GetContainer( |
| 40 | root_window, kShellWindowId_AccessibilityPanelContainer); |
Hwanseung Lee | 28dcc14 | 2021-04-15 10:15:45 | [diff] [blame] | 41 | params.activatable = views::Widget::InitParams::Activatable::kNo; |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 42 | params.bounds = gfx::Rect(0, 0, root_window->bounds().width(), |
| 43 | root_window->bounds().height()); |
Ahmed Fakhry | 32f3c45 | 2019-08-01 16:36:34 | [diff] [blame] | 44 | widget->Init(std::move(params)); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 45 | return widget; |
| 46 | } |
| 47 | |
| 48 | using AccessibilityPanelLayoutManagerTest = AshTestBase; |
| 49 | |
| 50 | TEST_F(AccessibilityPanelLayoutManagerTest, Basics) { |
| 51 | AccessibilityPanelLayoutManager* layout_manager = GetLayoutManager(); |
| 52 | ASSERT_TRUE(layout_manager); |
| 53 | |
| 54 | // The layout manager doesn't track anything at startup. |
| 55 | EXPECT_FALSE(layout_manager->panel_window_for_test()); |
| 56 | |
| 57 | // Simulate chrome creating the ChromeVox widget. The layout manager starts |
| 58 | // managing it. |
| 59 | std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel(); |
| 60 | widget->Show(); |
| 61 | EXPECT_EQ(widget->GetNativeWindow(), layout_manager->panel_window_for_test()); |
| 62 | |
| 63 | // The layout manager doesn't track anything after the widget closes. |
| 64 | widget.reset(); |
| 65 | EXPECT_FALSE(layout_manager->panel_window_for_test()); |
| 66 | } |
| 67 | |
| 68 | TEST_F(AccessibilityPanelLayoutManagerTest, Shutdown) { |
| 69 | // Simulate chrome creating the ChromeVox widget. |
| 70 | std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel(); |
| 71 | widget->Show(); |
| 72 | |
| 73 | // Don't close the window. |
| 74 | widget.release(); |
| 75 | |
| 76 | // Ash should not crash if the window is still open at shutdown. |
| 77 | } |
| 78 | |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 79 | TEST_F(AccessibilityPanelLayoutManagerTest, PanelFullscreen) { |
| 80 | AccessibilityPanelLayoutManager* layout_manager = GetLayoutManager(); |
| 81 | display::Screen* screen = display::Screen::GetScreen(); |
| 82 | |
| 83 | std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel(); |
| 84 | widget->Show(); |
| 85 | |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 86 | layout_manager->SetPanelBounds(gfx::Rect(0, 0, 0, kDefaultPanelHeight), |
Mike Wasserman | d13e95a | 2019-06-28 23:29:48 | [diff] [blame] | 87 | AccessibilityPanelState::FULL_WIDTH); |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 88 | |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 89 | gfx::Rect expected_work_area = screen->GetPrimaryDisplay().work_area(); |
| 90 | |
Anastasia Helfinstein | b855e3c | 2018-12-13 23:02:40 | [diff] [blame] | 91 | // When the panel is fullscreen it fills the display and clears the |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 92 | // work area. |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 93 | layout_manager->SetPanelBounds(gfx::Rect(), |
Mike Wasserman | d13e95a | 2019-06-28 23:29:48 | [diff] [blame] | 94 | AccessibilityPanelState::FULLSCREEN); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 95 | EXPECT_EQ(widget->GetNativeWindow()->bounds(), |
| 96 | screen->GetPrimaryDisplay().bounds()); |
Anastasia Helfinstein | b855e3c | 2018-12-13 23:02:40 | [diff] [blame] | 97 | EXPECT_EQ(screen->GetPrimaryDisplay().work_area().y(), 0); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 98 | |
Anastasia Helfinstein | b855e3c | 2018-12-13 23:02:40 | [diff] [blame] | 99 | // Restoring the panel to default size restores the bounds and sets |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 100 | // the work area. |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 101 | layout_manager->SetPanelBounds(gfx::Rect(0, 0, 0, kDefaultPanelHeight), |
Mike Wasserman | d13e95a | 2019-06-28 23:29:48 | [diff] [blame] | 102 | AccessibilityPanelState::FULL_WIDTH); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 103 | gfx::Rect expected_bounds(0, 0, screen->GetPrimaryDisplay().bounds().width(), |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 104 | kDefaultPanelHeight); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 105 | EXPECT_EQ(widget->GetNativeWindow()->bounds(), expected_bounds); |
| 106 | EXPECT_EQ(screen->GetPrimaryDisplay().work_area(), expected_work_area); |
| 107 | } |
| 108 | |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 109 | TEST_F(AccessibilityPanelLayoutManagerTest, SetBounds) { |
| 110 | std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel(); |
| 111 | widget->Show(); |
| 112 | |
| 113 | gfx::Rect bounds(0, 0, 100, 100); |
Mike Wasserman | d13e95a | 2019-06-28 23:29:48 | [diff] [blame] | 114 | GetLayoutManager()->SetPanelBounds(bounds, AccessibilityPanelState::BOUNDED); |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 115 | EXPECT_EQ(widget->GetNativeWindow()->bounds(), bounds); |
| 116 | } |
| 117 | |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 118 | TEST_F(AccessibilityPanelLayoutManagerTest, DisplayBoundsChange) { |
| 119 | std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel(); |
| 120 | widget->Show(); |
Mike Wasserman | d13e95a | 2019-06-28 23:29:48 | [diff] [blame] | 121 | GetLayoutManager()->SetPanelBounds(gfx::Rect(0, 0, 0, kDefaultPanelHeight), |
| 122 | AccessibilityPanelState::FULL_WIDTH); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 123 | |
| 124 | // When the display resolution changes the panel still sits at the top of the |
| 125 | // screen. |
Mitsuru Oshima | 3fe31cf | 2022-04-08 08:59:29 | [diff] [blame] | 126 | UpdateDisplay("1200x700,1300x800"); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 127 | display::Screen* screen = display::Screen::GetScreen(); |
| 128 | gfx::Rect expected_bounds(0, 0, screen->GetPrimaryDisplay().bounds().width(), |
Zach Helfinstein | c6255851 | 2018-08-16 18:05:45 | [diff] [blame] | 129 | kDefaultPanelHeight); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 130 | EXPECT_EQ(widget->GetNativeWindow()->bounds(), expected_bounds); |
| 131 | |
| 132 | gfx::Rect expected_work_area = screen->GetPrimaryDisplay().bounds(); |
Xianzhu Wang | 453ea8a | 2022-04-05 23:32:13 | [diff] [blame] | 133 | expected_work_area.Inset(gfx::Insets::TLBR( |
| 134 | kDefaultPanelHeight, 0, ShelfConfig::Get()->shelf_size(), 0)); |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 135 | EXPECT_EQ(screen->GetPrimaryDisplay().work_area(), expected_work_area); |
| 136 | } |
| 137 | |
Josiah Krutz | 1ccb7e92 | 2023-06-20 16:28:32 | [diff] [blame] | 138 | TEST_F(AccessibilityPanelLayoutManagerTest, DockedMagnifierEnabled) { |
| 139 | auto* docked_magnifier_controller = |
| 140 | Shell::Get()->docked_magnifier_controller(); |
| 141 | std::unique_ptr<views::Widget> widget = CreateChromeVoxPanel(); |
| 142 | widget->Show(); |
| 143 | GetLayoutManager()->SetPanelBounds(gfx::Rect(0, 0, 0, kDefaultPanelHeight), |
| 144 | AccessibilityPanelState::FULL_WIDTH); |
| 145 | |
| 146 | // When Docked Magnifier is enabled, panel sits right below, and work area |
| 147 | // sits below the panel. |
| 148 | docked_magnifier_controller->SetEnabled(true); |
| 149 | int magnifier_height = docked_magnifier_controller->GetTotalMagnifierHeight(); |
| 150 | |
| 151 | display::Screen* screen = display::Screen::GetScreen(); |
| 152 | gfx::Rect expected_bounds(0, magnifier_height, |
| 153 | screen->GetPrimaryDisplay().bounds().width(), |
| 154 | kDefaultPanelHeight); |
| 155 | EXPECT_EQ(widget->GetNativeWindow()->bounds(), expected_bounds); |
| 156 | |
| 157 | gfx::Rect expected_work_area = screen->GetPrimaryDisplay().bounds(); |
| 158 | expected_work_area.Inset( |
| 159 | gfx::Insets::TLBR(magnifier_height + kDefaultPanelHeight, 0, |
| 160 | ShelfConfig::Get()->shelf_size(), 0)); |
| 161 | EXPECT_EQ(screen->GetPrimaryDisplay().work_area(), expected_work_area); |
| 162 | } |
| 163 | |
James Cook | b263b51 | 2018-03-07 22:30:33 | [diff] [blame] | 164 | } // namespace |
| 165 | } // namespace ash |