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

ash / accessibility / ui / accessibility_focus_ring_group_unittest.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ash/accessibility/ui/accessibility_focus_ring_group.h"

#include <memory>
#include <vector>

#include "ash/accessibility/ui/accessibility_focus_ring.h"
#include "ash/accessibility/ui/accessibility_focus_ring_layer.h"
#include "ash/accessibility/ui/accessibility_layer.h"
#include "ash/test/ash_test_base.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/geometry/rect.h"

namespace ash {

class TestableAccessibilityFocusRingGroup : public AccessibilityFocusRingGroup {
 public:
  TestableAccessibilityFocusRingGroup() : AccessibilityFocusRingGroup() {
    // By default use an easy round number for testing.
    margin_ = 10;
  }

  TestableAccessibilityFocusRingGroup(
      const TestableAccessibilityFocusRingGroup&) = delete;
  TestableAccessibilityFocusRingGroup& operator=(
      const TestableAccessibilityFocusRingGroup&) = delete;

  ~TestableAccessibilityFocusRingGroup() override = default;

  void RectsToRings(const std::vector<gfx::Rect>& rects,
                    std::vector<AccessibilityFocusRing>* rings) const {
    AccessibilityFocusRingGroup::RectsToRings(rects, rings);
  }

  int GetMargin() const override { return margin_; }

 private:
  int margin_;
};

class AccessibilityFocusRingGroupTest : public AshTestBase {
 public:
  AccessibilityFocusRingGroupTest() {
    AccessibilityFocusRing::set_screen_bounds_for_testing(
        gfx::Rect(0, 0, 1000, 500));
  }
  ~AccessibilityFocusRingGroupTest() override = default;

 protected:
  gfx::Rect AddMargin(gfx::Rect r) {
    r.Inset(-group_.GetMargin());
    return r;
  }

  TestableAccessibilityFocusRingGroup group_;
};

TEST_F(AccessibilityFocusRingGroupTest, ClipToBounds) {
  // Test the ClipToBounds function, which takes an offscreen rect and preserves
  // the dimensions that are within bounds, and snaps the dimensions that are
  // out of bounds to the edges. This lets the code to create points work
  // properly, while ensuring all sides of the focus ring are visible.
  int length = 100;
  gfx::Rect screen(0, 0, 1000, 1000);

  gfx::Rect offTop(100, -1000, length, length);
  gfx::Rect expected(100, 0, length, 0);
  AccessibilityFocusRing::ClipToBounds(&offTop, screen);
  ASSERT_EQ(expected, offTop);

  gfx::Rect offBottom(100, 5000, length, length);
  expected = gfx::Rect(100, 1000, length, 0);
  AccessibilityFocusRing::ClipToBounds(&offBottom, screen);
  ASSERT_EQ(expected, offBottom);

  gfx::Rect offLeft(-1000, 100, length, length);
  expected = gfx::Rect(0, 100, 0, length);
  AccessibilityFocusRing::ClipToBounds(&offLeft, screen);
  ASSERT_EQ(expected, offLeft);

  gfx::Rect offRight(5000, 100, length, length);
  expected = gfx::Rect(1000, 100, 0, length);
  AccessibilityFocusRing::ClipToBounds(&offRight, screen);
  ASSERT_EQ(expected, offRight);

  gfx::Rect offCorner(-1000, -1000, length, length);
  expected = gfx::Rect(0, 0, 0, 0);
  AccessibilityFocusRing::ClipToBounds(&offCorner, screen);
  ASSERT_EQ(expected, offCorner);
}

TEST_F(AccessibilityFocusRingGroupTest, RectsToRingsSimpleBoundsCheck) {
  // Easy confidence check. Given a single rectangle, make sure we get back
  // a focus ring with the same bounds.
  std::vector<gfx::Rect> rects;
  rects.push_back(gfx::Rect(20, 30, 70, 150));
  std::vector<AccessibilityFocusRing> rings;
  group_.RectsToRings(rects, &rings);
  ASSERT_EQ(1U, rings.size());
  ASSERT_EQ(AddMargin(rects[0]), rings[0].GetBounds());
}

TEST_F(AccessibilityFocusRingGroupTest, RectsToRingsTinyRect) {
  // A single, very small rect can lead to error conditions.
  // Test that it displays as expected.
  std::vector<gfx::Rect> rects;
  rects.push_back(gfx::Rect(100, 100, group_.GetMargin(), group_.GetMargin()));
  std::vector<AccessibilityFocusRing> rings;
  group_.RectsToRings(rects, &rings);
  ASSERT_EQ(1U, rings.size());
  ASSERT_EQ(AddMargin(rects[0]), rings[0].GetBounds());
}

TEST_F(AccessibilityFocusRingGroupTest, RectsToRingsSnapToScreen) {
  // Given a rectangle that goes all the way to the screen edges, or off the
  // edge, check that we snap the focus ring to the screen, rather than going
  // offscreen.
  int x = -10;  // offscreen
  int y = -20;  // offscreen
  int length = 50;
  int padding = AccessibilityFocusRing::GetScreenPaddingForTesting();
  std::vector<gfx::Rect> rects;

  rects.push_back(gfx::Rect(x, y, length, length));
  std::vector<AccessibilityFocusRing> rings;
  group_.RectsToRings(rects, &rings);
  gfx::Rect result(padding, padding, length + x + group_.GetMargin() - padding,
                   length + y + group_.GetMargin() - padding);

  ASSERT_EQ(1U, rings.size());
  ASSERT_EQ(result, rings[0].GetBounds());
}

TEST_F(AccessibilityFocusRingGroupTest, RectsToRingsVerticalStack) {
  // Given two rects, one on top of each other, we should get back a
  // focus ring that surrounds them both.
  std::vector<gfx::Rect> rects;
  rects.push_back(gfx::Rect(20, 20, 60, 30));
  rects.push_back(gfx::Rect(20, 50, 60, 30));
  std::vector<AccessibilityFocusRing> rings;
  group_.RectsToRings(rects, &rings);
  ASSERT_EQ(1U, rings.size());
  ASSERT_EQ(AddMargin(gfx::Rect(20, 20, 60, 60)), rings[0].GetBounds());
}

TEST_F(AccessibilityFocusRingGroupTest, RectsToRingsHorizontalStack) {
  // Given two rects, one next to the other horizontally, we should get back a
  // focus ring that surrounds them both.
  std::vector<gfx::Rect> rects;
  rects.push_back(gfx::Rect(20, 20, 60, 30));
  rects.push_back(gfx::Rect(80, 20, 60, 30));
  std::vector<AccessibilityFocusRing> rings;
  group_.RectsToRings(rects, &rings);
  ASSERT_EQ(1U, rings.size());
  ASSERT_EQ(AddMargin(gfx::Rect(20, 20, 120, 30)), rings[0].GetBounds());
}

TEST_F(AccessibilityFocusRingGroupTest, RectsToRingsParagraphShape) {
  // Given a simple paragraph shape, make sure we get something that
  // outlines it correctly.
  AccessibilityFocusRing::set_screen_bounds_for_testing(
      gfx::Rect(0, 0, 1000, 1000));
  std::vector<gfx::Rect> rects;
  rects.push_back(gfx::Rect(110, 110, 180, 80));
  rects.push_back(gfx::Rect(110, 210, 580, 280));
  rects.push_back(gfx::Rect(510, 510, 180, 80));
  std::vector<AccessibilityFocusRing> rings;
  group_.RectsToRings(rects, &rings);
  ASSERT_EQ(1U, rings.size());
  EXPECT_EQ(gfx::Rect(100, 100, 600, 500), rings[0].GetBounds());

  const gfx::Point* points = rings[0].points;
  EXPECT_EQ(gfx::Point(100, 190), points[0]);
  EXPECT_EQ(gfx::Point(100, 110), points[1]);
  EXPECT_EQ(gfx::Point(100, 100), points[2]);
  EXPECT_EQ(gfx::Point(110, 100), points[3]);
  EXPECT_EQ(gfx::Point(290, 100), points[4]);
  EXPECT_EQ(gfx::Point(300, 100), points[5]);
  EXPECT_EQ(gfx::Point(300, 110), points[6]);
  EXPECT_EQ(gfx::Point(300, 190), points[7]);
  EXPECT_EQ(gfx::Point(300, 200), points[8]);
  EXPECT_EQ(gfx::Point(310, 200), points[9]);
  EXPECT_EQ(gfx::Point(690, 200), points[10]);
  EXPECT_EQ(gfx::Point(700, 200), points[11]);
  EXPECT_EQ(gfx::Point(700, 210), points[12]);
  EXPECT_EQ(gfx::Point(700, 490), points[13]);
  EXPECT_EQ(gfx::Point(700, 500), points[14]);
  EXPECT_EQ(gfx::Point(700, 500), points[15]);
  EXPECT_EQ(gfx::Point(700, 500), points[16]);
  EXPECT_EQ(gfx::Point(700, 500), points[17]);
  EXPECT_EQ(gfx::Point(700, 510), points[18]);
  EXPECT_EQ(gfx::Point(700, 590), points[19]);
  EXPECT_EQ(gfx::Point(700, 600), points[20]);
  EXPECT_EQ(gfx::Point(690, 600), points[21]);
  EXPECT_EQ(gfx::Point(510, 600), points[22]);
  EXPECT_EQ(gfx::Point(500, 600), points[23]);
  EXPECT_EQ(gfx::Point(500, 590), points[24]);
  EXPECT_EQ(gfx::Point(500, 510), points[25]);
  EXPECT_EQ(gfx::Point(500, 500), points[26]);
  EXPECT_EQ(gfx::Point(490, 500), points[27]);
  EXPECT_EQ(gfx::Point(110, 500), points[28]);
  EXPECT_EQ(gfx::Point(100, 500), points[29]);
  EXPECT_EQ(gfx::Point(100, 490), points[30]);
  EXPECT_EQ(gfx::Point(100, 210), points[31]);
  EXPECT_EQ(gfx::Point(100, 200), points[32]);
  EXPECT_EQ(gfx::Point(100, 200), points[33]);
  EXPECT_EQ(gfx::Point(100, 200), points[34]);
  EXPECT_EQ(gfx::Point(100, 200), points[35]);
}

}  // namespace ash