OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "ui/app_list/views/search_result_tile_item_list_view.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/test/scoped_feature_list.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "ui/app_list/app_list_features.h" |
| 17 #include "ui/app_list/app_list_model.h" |
| 18 #include "ui/app_list/test/app_list_test_view_delegate.h" |
| 19 #include "ui/app_list/test/test_search_result.h" |
| 20 #include "ui/app_list/views/search_result_list_view_delegate.h" |
| 21 #include "ui/app_list/views/search_result_view.h" |
| 22 #include "ui/views/controls/textfield/textfield.h" |
| 23 #include "ui/views/test/views_test_base.h" |
| 24 |
| 25 namespace app_list { |
| 26 |
| 27 namespace { |
| 28 constexpr int kNumSearchResultTiles = 8; |
| 29 // Constants when the Play Store app search feature is enabled. |
| 30 constexpr int kMaxNumSearchResultTiles = 6; |
| 31 constexpr int kInstalledApps = 4; |
| 32 constexpr int kPlayStoreApps = 2; |
| 33 } // namespace |
| 34 |
| 35 class SearchResultTileItemListViewTest |
| 36 : public views::ViewsTestBase, |
| 37 public ::testing::WithParamInterface<bool> { |
| 38 public: |
| 39 SearchResultTileItemListViewTest() {} |
| 40 ~SearchResultTileItemListViewTest() override {} |
| 41 |
| 42 protected: |
| 43 void CreateSearchResultTileItemListView() { |
| 44 // Switches on/off the Play Store app search feature. |
| 45 if (IsPlayStoreAppSearchEnabled()) { |
| 46 scoped_feature_list_.InitAndEnableFeature( |
| 47 app_list::features::kEnablePlayStoreAppSearch); |
| 48 } else { |
| 49 scoped_feature_list_.InitAndDisableFeature( |
| 50 app_list::features::kEnablePlayStoreAppSearch); |
| 51 } |
| 52 EXPECT_EQ(IsPlayStoreAppSearchEnabled(), |
| 53 features::IsPlayStoreAppSearchEnabled()); |
| 54 |
| 55 // Sets up the views. |
| 56 textfield_ = base::MakeUnique<views::Textfield>(); |
| 57 view_ = base::MakeUnique<SearchResultTileItemListView>(textfield_.get(), |
| 58 &view_delegate_); |
| 59 view_->SetResults(view_delegate_.GetModel()->results()); |
| 60 } |
| 61 |
| 62 bool IsPlayStoreAppSearchEnabled() const { return GetParam(); } |
| 63 |
| 64 SearchResultTileItemListView* view() { return view_.get(); } |
| 65 |
| 66 AppListModel::SearchResults* GetResults() { |
| 67 return view_delegate_.GetModel()->results(); |
| 68 } |
| 69 |
| 70 void SetUpSearchResults() { |
| 71 AppListModel::SearchResults* results = GetResults(); |
| 72 |
| 73 // Populate results for installed applications. |
| 74 for (int i = 0; i < kInstalledApps; ++i) { |
| 75 std::unique_ptr<TestSearchResult> result = |
| 76 base::MakeUnique<TestSearchResult>(); |
| 77 result->set_display_type(SearchResult::DISPLAY_TILE); |
| 78 result->set_result_type(SearchResult::RESULT_INSTALLED_APP); |
| 79 result->set_title( |
| 80 base::UTF8ToUTF16(base::StringPrintf("InstalledApp %d", i))); |
| 81 results->Add(std::move(result)); |
| 82 } |
| 83 |
| 84 // Populate results for Play Store search applications. |
| 85 if (IsPlayStoreAppSearchEnabled()) { |
| 86 for (int i = 0; i < kPlayStoreApps; ++i) { |
| 87 std::unique_ptr<TestSearchResult> result = |
| 88 base::MakeUnique<TestSearchResult>(); |
| 89 result->set_display_type(SearchResult::DISPLAY_TILE); |
| 90 result->set_result_type(SearchResult::RESULT_PLAYSTORE_APP); |
| 91 result->set_title( |
| 92 base::UTF8ToUTF16(base::StringPrintf("PlayStoreApp %d", i))); |
| 93 results->Add(std::move(result)); |
| 94 } |
| 95 } |
| 96 |
| 97 // Adding results calls SearchResultContainerView::ScheduleUpdate(). |
| 98 // It will post a delayed task to update the results and relayout. |
| 99 RunPendingMessages(); |
| 100 view_->OnContainerSelected(false, false); |
| 101 } |
| 102 |
| 103 int GetOpenResultCount(int ranking) { |
| 104 int result = view_delegate_.open_search_result_counts()[ranking]; |
| 105 return result; |
| 106 } |
| 107 |
| 108 void ResetOpenResultCount() { |
| 109 view_delegate_.open_search_result_counts().clear(); |
| 110 } |
| 111 |
| 112 int GetResultCount() const { return view_->num_results(); } |
| 113 |
| 114 int GetSelectedIndex() const { return view_->selected_index(); } |
| 115 |
| 116 void ResetSelectedIndex() const { view_->SetSelectedIndex(0); } |
| 117 |
| 118 bool KeyPress(ui::KeyboardCode key_code) { |
| 119 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE); |
| 120 return view_->OnKeyPressed(event); |
| 121 } |
| 122 |
| 123 private: |
| 124 test::AppListTestViewDelegate view_delegate_; |
| 125 std::unique_ptr<SearchResultTileItemListView> view_; |
| 126 std::unique_ptr<views::Textfield> textfield_; |
| 127 base::test::ScopedFeatureList scoped_feature_list_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(SearchResultTileItemListViewTest); |
| 130 }; |
| 131 |
| 132 TEST_P(SearchResultTileItemListViewTest, Basic) { |
| 133 CreateSearchResultTileItemListView(); |
| 134 SetUpSearchResults(); |
| 135 |
| 136 const int results = GetResultCount(); |
| 137 const int expected_results = IsPlayStoreAppSearchEnabled() |
| 138 ? kInstalledApps + kPlayStoreApps |
| 139 : kInstalledApps; |
| 140 EXPECT_EQ(expected_results, results); |
| 141 // When the Play Store app search feature is enabled, for each results, |
| 142 // we added a separator for result type grouping. |
| 143 const int expected_child_count = IsPlayStoreAppSearchEnabled() |
| 144 ? kMaxNumSearchResultTiles * 2 |
| 145 : kNumSearchResultTiles; |
| 146 EXPECT_EQ(expected_child_count, view()->child_count()); |
| 147 |
| 148 // Tests item indexing by pressing TAB. |
| 149 for (int i = 1; i < results; ++i) { |
| 150 EXPECT_TRUE(KeyPress(ui::VKEY_TAB)); |
| 151 EXPECT_EQ(i, GetSelectedIndex()); |
| 152 } |
| 153 |
| 154 // Extra TAB events won't be handled by the view. |
| 155 EXPECT_FALSE(KeyPress(ui::VKEY_TAB)); |
| 156 EXPECT_EQ(results - 1, GetSelectedIndex()); |
| 157 |
| 158 // Tests app opening. |
| 159 ResetSelectedIndex(); |
| 160 ResetOpenResultCount(); |
| 161 for (int i = 1; i < results; ++i) { |
| 162 EXPECT_TRUE(KeyPress(ui::VKEY_TAB)); |
| 163 EXPECT_EQ(i, GetSelectedIndex()); |
| 164 for (int j = 0; j < i; j++) |
| 165 EXPECT_TRUE(KeyPress(ui::VKEY_RETURN)); |
| 166 EXPECT_EQ(i, GetOpenResultCount(i)); |
| 167 } |
| 168 } |
| 169 |
| 170 INSTANTIATE_TEST_CASE_P(, SearchResultTileItemListViewTest, testing::Bool()); |
| 171 |
| 172 } // namespace app_list |
OLD | NEW |