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 2019 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/accessibility/ui/accessibility_focus_ring_controller_impl.h"
#include "ash/accessibility/ui/accessibility_cursor_ring_layer.h"
#include "ash/accessibility/ui/accessibility_highlight_layer.h"
#include "ash/public/cpp/accessibility_focus_ring_info.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/compositor/layer.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
namespace ash {
using AccessibilityFocusRingControllerTest = AshTestBase;
TEST_F(AccessibilityFocusRingControllerTest, CallingHideWhenEmpty) {
auto* controller = Shell::Get()->accessibility_focus_ring_controller();
// Ensure that calling hide does not crash the controller if there are
// no focus rings yet for a given ID.
controller->HideFocusRing("catsRCute");
}
// Disabled due to failure. http://crbug.com/1279278
TEST_F(AccessibilityFocusRingControllerTest, DISABLED_SetFocusRingCorrectRingGroup) {
auto* controller = Shell::Get()->accessibility_focus_ring_controller();
EXPECT_EQ(nullptr, controller->GetFocusRingGroupForTesting("catsRCute"));
SkColor cat_color = SkColorSetARGB(0xFF, 0x42, 0x42, 0x42);
auto cat_focus_ring = std::make_unique<AccessibilityFocusRingInfo>();
cat_focus_ring->color = cat_color;
cat_focus_ring->rects_in_screen.push_back(gfx::Rect(0, 0, 10, 10));
controller->SetFocusRing("catsRCute", std::move(cat_focus_ring));
// A focus ring group was created.
ASSERT_NE(nullptr, controller->GetFocusRingGroupForTesting("catsRCute"));
EXPECT_EQ(1u, controller->GetFocusRingGroupForTesting("catsRCute")
->focus_layers_for_testing()
.size());
EXPECT_EQ(nullptr, controller->GetFocusRingGroupForTesting("dogsRCool"));
auto dog_focus_ring = std::make_unique<AccessibilityFocusRingInfo>();
dog_focus_ring->rects_in_screen.push_back(gfx::Rect(10, 30, 70, 150));
// Set a background color to ensure that code path has no surprises.
dog_focus_ring->background_color = SK_ColorBLUE;
controller->SetFocusRing("dogsRCool", std::move(dog_focus_ring));
ASSERT_NE(nullptr, controller->GetFocusRingGroupForTesting("dogsRCool"));
int size = controller->GetFocusRingGroupForTesting("dogsRCool")
->focus_layers_for_testing()
.size();
EXPECT_EQ(1, size);
// The first focus ring group was not updated.
const std::vector<std::unique_ptr<AccessibilityFocusRingLayer>>& layers =
controller->GetFocusRingGroupForTesting("catsRCute")
->focus_layers_for_testing();
EXPECT_EQ(1u, layers.size());
EXPECT_EQ(cat_color, layers[0]->color_for_testing());
}
TEST_F(AccessibilityFocusRingControllerTest, CursorWorksOnMultipleDisplays) {
UpdateDisplay("500x400,500x400");
aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
ASSERT_EQ(2u, root_windows.size());
// Simulate a mouse event on the primary display.
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
gfx::Point location(90, 90);
controller->SetCursorRing(location);
AccessibilityCursorRingLayer* cursor_layer =
controller->cursor_layer_for_testing();
aura::Window* window0_container = Shell::GetContainer(
root_windows[0], kShellWindowId_AccessibilityBubbleContainer);
EXPECT_EQ(window0_container, cursor_layer->root_window());
EXPECT_LT(abs(cursor_layer->layer()->GetTargetBounds().x() - location.x()),
50);
EXPECT_LT(abs(cursor_layer->layer()->GetTargetBounds().y() - location.y()),
50);
// Simulate a mouse event at the same local location on the secondary display.
gfx::Point location_on_secondary = location;
location_on_secondary.Offset(500, 0);
controller->SetCursorRing(location_on_secondary);
cursor_layer = controller->cursor_layer_for_testing();
aura::Window* window1_container = Shell::GetContainer(
root_windows[1], kShellWindowId_AccessibilityBubbleContainer);
EXPECT_EQ(window1_container, cursor_layer->root_window());
EXPECT_LT(abs(cursor_layer->layer()->GetTargetBounds().x() - location.x()),
50);
EXPECT_LT(abs(cursor_layer->layer()->GetTargetBounds().y() - location.y()),
50);
}
// Disabled due to failure. http://crbug.com/1279278
TEST_F(AccessibilityFocusRingControllerTest, DISABLED_FocusRingWorksOnMultipleDisplays) {
UpdateDisplay("500x400,600x500");
aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
ASSERT_EQ(2u, root_windows.size());
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
auto focus_ring = std::make_unique<AccessibilityFocusRingInfo>();
focus_ring->color = SkColorSetRGB(0x33, 0x66, 0x99);
focus_ring->rects_in_screen.push_back(gfx::Rect(50, 50, 10, 10));
controller->SetFocusRing("catsRCute", std::move(focus_ring));
// A focus ring group was created.
ASSERT_NE(nullptr, controller->GetFocusRingGroupForTesting("catsRCute"));
const std::vector<std::unique_ptr<AccessibilityFocusRingLayer>>& layers =
controller->GetFocusRingGroupForTesting("catsRCute")
->focus_layers_for_testing();
EXPECT_EQ(1u, layers.size());
aura::Window* window0_container = Shell::GetContainer(
root_windows[0], kShellWindowId_AccessibilityBubbleContainer);
EXPECT_EQ(window0_container, layers[0]->root_window());
// The focus ring has some padding, so just check the center point is where
// we would expect it.
EXPECT_EQ(layers[0]->layer()->GetTargetBounds().CenterPoint(),
gfx::Rect(50, 50, 10, 10).CenterPoint());
// Move it to the secondary display.
auto moved = std::make_unique<AccessibilityFocusRingInfo>();
moved->color = SkColorSetRGB(0x33, 0x66, 0x99);
moved->rects_in_screen.push_back(gfx::Rect(600, 50, 10, 10));
controller->SetFocusRing("catsRCute", std::move(moved));
// Check it is correctly positioned on the secondary display.
const std::vector<std::unique_ptr<AccessibilityFocusRingLayer>>&
moved_layers = controller->GetFocusRingGroupForTesting("catsRCute")
->focus_layers_for_testing();
EXPECT_EQ(1u, moved_layers.size());
aura::Window* window1_container = Shell::GetContainer(
root_windows[1], kShellWindowId_AccessibilityBubbleContainer);
EXPECT_EQ(window1_container, moved_layers[0]->root_window());
EXPECT_EQ(moved_layers[0]->layer()->GetTargetBounds().CenterPoint(),
gfx::Rect(100, 50, 10, 10).CenterPoint());
}
TEST_F(AccessibilityFocusRingControllerTest, HighlightWorksOnMultipleDisplays) {
UpdateDisplay("500x400,600x500");
aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
ASSERT_EQ(2u, root_windows.size());
aura::Window* window0_container = Shell::GetContainer(
root_windows[0], kShellWindowId_AccessibilityPanelContainer);
aura::Window* window1_container = Shell::GetContainer(
root_windows[1], kShellWindowId_AccessibilityPanelContainer);
// Simulate highlighting on the primary display.
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
controller->SetHighlights(
{gfx::Rect(50, 50, 10, 10), gfx::Rect(50, 60, 10, 10)},
SkColorSetRGB(0x33, 0x66, 0x99));
AccessibilityHighlightLayer* highlight_layer =
controller->highlight_layer_for_testing();
EXPECT_EQ(window0_container, highlight_layer->root_window());
// The highlight bounds has some padding, so just check the center point is
// where we would expect it.
EXPECT_EQ(highlight_layer->layer()->GetTargetBounds().CenterPoint(),
gfx::Rect(50, 50, 10, 20).CenterPoint());
EXPECT_EQ(std::vector<gfx::Rect>(
{gfx::Rect(50, 50, 10, 10), gfx::Rect(50, 60, 10, 10)}),
highlight_layer->rects_for_test());
// Simulate highlighting on the secondary display.
controller->SetHighlights(
{gfx::Rect(550, 50, 10, 10), gfx::Rect(550, 60, 10, 10)},
SkColorSetRGB(0x33, 0x66, 0x99));
highlight_layer = controller->highlight_layer_for_testing();
EXPECT_EQ(window1_container, highlight_layer->root_window());
EXPECT_EQ(highlight_layer->layer()->GetTargetBounds().CenterPoint(),
gfx::Rect(50, 50, 10, 20).CenterPoint());
EXPECT_EQ(std::vector<gfx::Rect>(
{gfx::Rect(50, 50, 10, 10), gfx::Rect(50, 60, 10, 10)}),
highlight_layer->rects_for_test());
}
TEST_F(AccessibilityFocusRingControllerTest, HighlightColorCalculation) {
SkColor without_alpha = SkColorSetARGB(0xFF, 0x42, 0x42, 0x42);
SkColor with_alpha = SkColorSetARGB(0x3D, 0x14, 0x15, 0x92);
float default_opacity = 0.3f;
SkColor result_color = SK_ColorWHITE;
float result_opacity = 0.0f;
AccessibilityFocusRingControllerImpl::GetColorAndOpacityFromColor(
without_alpha, default_opacity, &result_color, &result_opacity);
EXPECT_EQ(default_opacity, result_opacity);
AccessibilityFocusRingControllerImpl::GetColorAndOpacityFromColor(
with_alpha, default_opacity, &result_color, &result_opacity);
EXPECT_NEAR(0.239f, result_opacity, .001);
}
} // namespace ash
|