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

ash / system / accessibility / switch_access / switch_access_menu_bubble_controller_unittest.cc [blame]

// Copyright 2020 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/accessibility/switch_access/switch_access_menu_bubble_controller.h"

#include "ash/accessibility/accessibility_controller.h"
#include "ash/shell.h"
#include "ash/system/accessibility/switch_access/switch_access_back_button_bubble_controller.h"
#include "ash/system/accessibility/switch_access/switch_access_back_button_view.h"
#include "ash/system/accessibility/switch_access/switch_access_menu_button.h"
#include "ash/system/accessibility/switch_access/switch_access_menu_view.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/test/ash_test_base.h"
#include "ui/views/accessibility/view_accessibility.h"

namespace ash {

class SwitchAccessMenuBubbleControllerTest : public AshTestBase {
 public:
  SwitchAccessMenuBubbleControllerTest() = default;
  ~SwitchAccessMenuBubbleControllerTest() override = default;

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

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();
    Shell::Get()->accessibility_controller()->switch_access().SetEnabled(true);
  }

  SwitchAccessMenuBubbleController* GetBubbleController() {
    return Shell::Get()
        ->accessibility_controller()
        ->GetSwitchAccessBubbleControllerForTest();
  }

  SwitchAccessMenuView* GetMenuView() {
    return GetBubbleController()->menu_view_;
  }

  std::vector<SwitchAccessMenuButton*> GetMenuButtons() {
    std::vector<SwitchAccessMenuButton*> buttons;
    for (views::View* button : GetMenuView()->children())
      buttons.push_back(static_cast<SwitchAccessMenuButton*>(button));
    return buttons;
  }

  gfx::Rect GetBackButtonBounds() {
    SwitchAccessMenuBubbleController* bubble_controller = GetBubbleController();
    if (bubble_controller && bubble_controller->back_button_controller_ &&
        bubble_controller->back_button_controller_->back_button_view_) {
      return bubble_controller->back_button_controller_->back_button_view_
          ->GetBoundsInScreen();
    }
    return gfx::Rect();
  }

  int GetExpectedButtonWidth() { return SwitchAccessMenuButton::kWidthDip; }
  int GetExpectedBubbleWidth() {
    return 288;  // From the Switch Access spec.
  }
};

// TODO(anastasi): Add more tests for closing and repositioning the button.
TEST_F(SwitchAccessMenuBubbleControllerTest, ShowBackButton) {
  gfx::Rect anchor_rect(100, 100, 0, 0);
  GetBubbleController()->ShowBackButton(anchor_rect);

  gfx::Rect bounds = GetBackButtonBounds();
  EXPECT_GT(bounds.width(), 36);
  EXPECT_GT(bounds.height(), 36);
}

TEST_F(SwitchAccessMenuBubbleControllerTest, ShowMenu) {
  gfx::Rect anchor_rect(10, 10, 0, 0);
  GetBubbleController()->ShowMenu(anchor_rect,
                                  {"select", "scrollDown", "settings"});
  EXPECT_TRUE(GetMenuView());

  for (SwitchAccessMenuButton* button : GetMenuButtons()) {
    EXPECT_TRUE(button);
    EXPECT_EQ(button->width(), GetExpectedButtonWidth());
  }

  EXPECT_EQ(GetMenuView()->width(), GetExpectedBubbleWidth());
}

TEST_F(SwitchAccessMenuBubbleControllerTest, SetActions) {
  gfx::Rect anchor_rect(10, 10, 0, 0);
  GetBubbleController()->ShowMenu(anchor_rect,
                                  {"select", "scrollDown", "settings"});
  EXPECT_TRUE(GetMenuView());

  std::vector<SwitchAccessMenuButton*> buttons = GetMenuButtons();
  EXPECT_EQ(3ul, buttons.size());

  GetBubbleController()->ShowMenu(
      anchor_rect,
      {"keyboard", "dictation", "increment", "decrement", "settings"});

  buttons = GetMenuButtons();
  EXPECT_EQ(5ul, buttons.size());
}

TEST_F(SwitchAccessMenuBubbleControllerTest, AvoidsShelfBubble) {
  const struct {
    session_manager::SessionState session_state;
    std::string name;
  } kTestCases[]{
      {session_manager::SessionState::OOBE, "OOBE"},
      {session_manager::SessionState::LOGIN_PRIMARY, "LOGIN_PRIMARY"},
      {session_manager::SessionState::ACTIVE, "ACTIVE"},
      {session_manager::SessionState::LOCKED, "LOCKED"},
  };

  for (auto test : kTestCases) {
    GetSessionControllerClient()->SetSessionState(test.session_state);
    // Set up the switch access menu and the shelf
    auto* system_tray = GetPrimaryUnifiedSystemTray();
    system_tray->ShowBubble();
    gfx::Rect tray_bubble_bounds = system_tray->GetBubbleBoundsInScreen();

    // Try to show the menu in the center of the tray bubble.
    gfx::Rect anchor(tray_bubble_bounds.CenterPoint(), gfx::Size());
    GetBubbleController()->ShowMenu(anchor,
                                    {"select", "scrollDown", "settings"});
    SwitchAccessMenuView* menu = GetMenuView();
    ASSERT_TRUE(menu) << test.name;
    gfx::Rect menu_bounds = menu->GetBoundsInScreen();

    // Expect that the menu is shown somewhere that doesn't overlap the system
    // tray bubble.
    EXPECT_FALSE(menu_bounds.Intersects(tray_bubble_bounds)) << test.name;
  }
}

TEST_F(SwitchAccessMenuBubbleControllerTest, AccessibleValueTest) {
  gfx::Rect anchor_rect(10, 10, 0, 0);
  GetBubbleController()->ShowMenu(anchor_rect,
                                  {"select", "scrollDown", "settings"});
  EXPECT_TRUE(GetMenuView());

  std::vector<SwitchAccessMenuButton*> buttons = GetMenuButtons();

  // kValue attribute gets initialised in SwitchAccessMenuButton constructor.
  ui::AXNodeData node_data = ui::AXNodeData();
  buttons[0]->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_EQ(std::string("select"),
            node_data.GetStringAttribute(ax::mojom::StringAttribute::kValue));
}

TEST_F(SwitchAccessMenuBubbleControllerTest,
       SwitchAccessMenuViewAccessibleProperties) {
  gfx::Rect anchor_rect(10, 10, 0, 0);
  GetBubbleController()->ShowMenu(anchor_rect,
                                  {"select", "scrollDown", "settings"});
  ui::AXNodeData node_data;

  GetMenuView()->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_EQ(ax::mojom::Role::kMenu, node_data.role);
}

}  // namespace ash