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

ash / style / combobox_unittest.cc [blame]

// Copyright 2024 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/style/combobox.h"

#include <array>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "ash/glanceables/classroom/glanceables_classroom_student_view.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/callback_helpers.h"
#include "base/json/values_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "ui/base/models/combobox_model.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {

class TestComboboxModel : public ui::ComboboxModel {
 public:
  // Overridden from ui::ComboboxModel:
  TestComboboxModel() = default;
  size_t GetItemCount() const override { return 10; }
  std::u16string GetItemAt(size_t index) const override {
    return u"Item " + base::NumberToString16(index);
  }
};

class ComboboxTest : public AshTestBase {
 public:
  ComboboxTest() = default;

  void SetUp() override {
    AshTestBase::SetUp();
    widget_ = CreateFramelessTestWidget();
    widget_->SetFullscreen(true);

    auto combobox_model_ = std::make_unique<TestComboboxModel>();
    combobox_view_ = widget_->SetContentsView(
        std::make_unique<Combobox>(std::move(combobox_model_)));
    widget_->Show();
  }

  void TearDown() override {
    combobox_view_ = nullptr;
    widget_.reset();
    AshTestBase::TearDown();
  }

  Combobox* GetComboboxView() { return combobox_view_; }

  void ShowComboBoxDropDownMenu() { GetComboboxView()->ShowDropDownMenu(); }

  void CloseComboBoxDropDownMenu() { GetComboboxView()->CloseDropDownMenu(); }

 protected:
  std::unique_ptr<views::Widget> widget_;
  raw_ptr<Combobox> combobox_view_;
};

TEST_F(ComboboxTest, AccessibleCheckedState) {
  ShowComboBoxDropDownMenu();

  Combobox* combobox_view = GetComboboxView();
  combobox_view->SetSelectedIndex(1);

  ui::AXNodeData data;
  views::View* selected_combobox_menu_option =
      combobox_view->MenuItemAtIndex(1);
  selected_combobox_menu_option->GetViewAccessibility().GetAccessibleNodeData(
      &data);
  EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kTrue);

  data = ui::AXNodeData();
  views::View* nonselected_combobox_menu_option =
      combobox_view->MenuItemAtIndex(0);
  nonselected_combobox_menu_option->GetViewAccessibility()
      .GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kFalse);

  combobox_view->SetSelectedIndex(std::nullopt);

  CloseComboBoxDropDownMenu();
}

TEST_F(ComboboxTest, AccessibleDefaultActionVerb) {
  auto* combobox = GetComboboxView();
  ui::AXNodeData data;

  combobox->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kOpen);

  combobox->SetEnabled(false);
  data = ui::AXNodeData();
  combobox->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kOpen);

  // ComboboxMenuOption default action verb test.
  combobox->SetEnabled(true);
  ShowComboBoxDropDownMenu();
  combobox->SetSelectedIndex(1);
  auto* menu_option = combobox->MenuItemAtIndex(1);
  data = ui::AXNodeData();
  ASSERT_TRUE(menu_option);
  menu_option->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kClick);

  menu_option->SetEnabled(false);
  data = ui::AXNodeData();
  menu_option->GetViewAccessibility().GetAccessibleNodeData(&data);
  EXPECT_EQ(data.GetDefaultActionVerb(), ax::mojom::DefaultActionVerb::kClick);

  CloseComboBoxDropDownMenu();
}

}  // namespace ash