app_list_keyboard_controller_unittest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/app_list/views/app_list_keyboard_controller.h"
  5. #include "ash/app_list/test/app_list_test_helper.h"
  6. #include "ash/app_list/views/app_list_bubble_apps_page.h"
  7. #include "ash/app_list/views/app_list_item_view.h"
  8. #include "ash/app_list/views/app_list_toast_container_view.h"
  9. #include "ash/app_list/views/apps_container_view.h"
  10. #include "ash/app_list/views/apps_grid_view.h"
  11. #include "ash/app_list/views/paged_apps_grid_view.h"
  12. #include "ash/app_list/views/recent_apps_view.h"
  13. #include "ash/app_list/views/scrollable_apps_grid_view.h"
  14. #include "ash/app_list/views/search_box_view.h"
  15. #include "ash/public/cpp/app_list/app_list_controller.h"
  16. #include "ash/public/cpp/app_list/app_list_types.h"
  17. #include "ash/public/cpp/tablet_mode.h"
  18. #include "ash/shell.h"
  19. #include "ash/test/ash_test_base.h"
  20. #include "base/callback_forward.h"
  21. #include "ui/events/test/event_generator.h"
  22. #include "ui/views/controls/button/label_button.h"
  23. #include "ui/views/controls/textfield/textfield.h"
  24. namespace ash {
  25. class AppListKeyboardControllerTest : public AshTestBase,
  26. public testing::WithParamInterface<bool> {
  27. public:
  28. AppListKeyboardControllerTest() = default;
  29. ~AppListKeyboardControllerTest() override = default;
  30. void SetUp() override {
  31. AshTestBase::SetUp();
  32. app_list_test_helper_ = GetAppListTestHelper();
  33. app_list_test_helper_->AddRecentApps(5);
  34. app_list_test_helper_->AddAppItems(5);
  35. if (GetParam()) {
  36. TabletMode::Get()->SetEnabledForTest(true);
  37. // Engages keyboard navigation in tablet mode (otherwise `RecentAppsView`
  38. // and other views do not receive keyboard events).
  39. PressDown();
  40. } else {
  41. app_list_test_helper_->ShowAppList();
  42. }
  43. }
  44. void PressDown() {
  45. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  46. generator.PressAndReleaseKey(ui::KeyboardCode::VKEY_DOWN);
  47. }
  48. void PressUp() {
  49. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  50. generator.PressAndReleaseKey(ui::KeyboardCode::VKEY_UP);
  51. }
  52. AppsGridView* apps_grid_view() {
  53. if (GetParam())
  54. return app_list_test_helper_->GetRootPagedAppsGridView();
  55. return app_list_test_helper_->GetScrollableAppsGridView();
  56. }
  57. SearchBoxView* search_box_view() {
  58. return GetParam() ? app_list_test_helper_->GetSearchBoxView()
  59. : app_list_test_helper_->GetBubbleSearchBoxView();
  60. }
  61. RecentAppsView* recent_apps_view() {
  62. return GetParam() ? app_list_test_helper_->GetFullscreenRecentAppsView()
  63. : app_list_test_helper_->GetBubbleRecentAppsView();
  64. }
  65. AppListToastContainerView* toast_container() {
  66. return GetParam() ? app_list_test_helper_->GetAppsContainerView()
  67. ->toast_container()
  68. : app_list_test_helper_->GetBubbleAppsPage()
  69. ->toast_container_for_test();
  70. }
  71. private:
  72. AppListTestHelper* app_list_test_helper_ = nullptr;
  73. };
  74. INSTANTIATE_TEST_SUITE_P(IsInTabletMode,
  75. AppListKeyboardControllerTest,
  76. testing::Bool());
  77. TEST_P(AppListKeyboardControllerTest, MovesFocusUpFromRecents) {
  78. // Focus an arbitrary item from recent apps.
  79. recent_apps_view()->GetItemViewAt(2)->RequestFocus();
  80. // Should focus on the view one step in reverse from the first recent app.
  81. PressUp();
  82. EXPECT_TRUE(search_box_view()->search_box()->HasFocus());
  83. }
  84. TEST_P(AppListKeyboardControllerTest, MovesFocusBetweenRecentsAndAppsGrid) {
  85. // Focus an arbitrary item from recent apps.
  86. recent_apps_view()->GetItemViewAt(2)->RequestFocus();
  87. // Should move focus to the apps grid keeping the same index.
  88. PressDown();
  89. EXPECT_TRUE(apps_grid_view()->GetItemViewAt(2)->HasFocus());
  90. // Should move focus back to recent apps keeping the same index.
  91. PressUp();
  92. EXPECT_TRUE(recent_apps_view()->GetItemViewAt(2)->HasFocus());
  93. }
  94. TEST_P(AppListKeyboardControllerTest,
  95. MovesFocusBetweenRecentsAndAppsGridViaToast) {
  96. // Sort apps grid (this should show the "Undo" toast).
  97. ASSERT_FALSE(toast_container()->IsToastVisible());
  98. AppListController::Get()->UpdateAppListWithNewTemporarySortOrder(
  99. AppListSortOrder::kColor, false, base::OnceClosure());
  100. ASSERT_TRUE(toast_container()->IsToastVisible());
  101. // Focus an arbitrary item from recent apps.
  102. recent_apps_view()->GetItemViewAt(2)->RequestFocus();
  103. // Should move focus to the "Undo" button.
  104. PressDown();
  105. EXPECT_TRUE(toast_container()->GetToastButton()->HasFocus());
  106. // Should move focus to the apps grid keeping the same index.
  107. PressDown();
  108. EXPECT_TRUE(apps_grid_view()->GetItemViewAt(2)->HasFocus());
  109. // Should move focus back to the "Undo" button.
  110. PressUp();
  111. EXPECT_TRUE(toast_container()->GetToastButton()->HasFocus());
  112. // Should move focus back to recent apps keeping the same index.
  113. PressUp();
  114. EXPECT_TRUE(recent_apps_view()->GetItemViewAt(2)->HasFocus());
  115. }
  116. } // namespace ash