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 "ui/app_list/app_list_features.h" | |
16 #include "ui/app_list/app_list_model.h" | |
17 #include "ui/app_list/test/app_list_test_view_delegate.h" | |
18 #include "ui/app_list/test/test_search_result.h" | |
19 #include "ui/app_list/views/search_result_list_view_delegate.h" | |
20 #include "ui/app_list/views/search_result_view.h" | |
21 #include "ui/views/controls/textfield/textfield.h" | |
22 #include "ui/views/test/views_test_base.h" | |
23 | |
24 namespace app_list { | |
25 | |
26 namespace { | |
27 constexpr int kMaxNumSearchResultTiles = 6; | |
28 constexpr int kInstalledApps = 4; | |
29 constexpr int kPlayStoreApps = 2; | |
30 } // namespace | |
31 | |
32 class SearchResultTileItemListViewTest : public views::ViewsTestBase { | |
khmel
2017/07/17 17:16:12
It would be nice to have test with bool parameter.
Jiaquan He
2017/07/18 03:32:45
This makes sense. But I'm not sure what is a best
| |
33 public: | |
34 SearchResultTileItemListViewTest() {} | |
35 ~SearchResultTileItemListViewTest() override {} | |
36 | |
37 protected: | |
38 void CreateSearchResultTileItemListView() { | |
39 textfield_ = base::MakeUnique<views::Textfield>(); | |
40 view_ = base::MakeUnique<SearchResultTileItemListView>(textfield_.get(), | |
41 &view_delegate_); | |
42 view_->SetResults(view_delegate_.GetModel()->results()); | |
43 } | |
44 | |
45 void EnablePlayStoreAppSearch() { | |
46 scoped_feature_list_.InitAndEnableFeature( | |
47 app_list::features::kEnablePlayStoreAppSearch); | |
48 } | |
49 | |
50 SearchResultTileItemListView* view() { return view_.get(); } | |
51 | |
52 AppListModel::SearchResults* GetResults() { | |
53 return view_delegate_.GetModel()->results(); | |
54 } | |
55 | |
56 void SetUpSearchResults() { | |
57 AppListModel::SearchResults* results = GetResults(); | |
58 | |
59 // Populate results for installed applications. | |
60 for (int i = 0; i < kInstalledApps; ++i) { | |
61 std::unique_ptr<TestSearchResult> result = | |
62 base::MakeUnique<TestSearchResult>(); | |
63 result->set_display_type(SearchResult::DISPLAY_TILE); | |
64 result->set_result_type(SearchResult::RESULT_INSTALLED_APP); | |
65 result->set_title( | |
66 base::UTF8ToUTF16(base::StringPrintf("InstalledApp %d", i))); | |
67 results->Add(std::move(result)); | |
68 } | |
69 | |
70 // Populate results for Play Store search applications. | |
71 if (app_list::features::IsPlayStoreAppSearchEnabled()) { | |
72 for (int i = 0; i < kPlayStoreApps; ++i) { | |
73 std::unique_ptr<TestSearchResult> result = | |
74 base::MakeUnique<TestSearchResult>(); | |
75 result->set_display_type(SearchResult::DISPLAY_TILE); | |
76 result->set_result_type(SearchResult::RESULT_PLAYSTORE_APP); | |
77 result->set_title( | |
78 base::UTF8ToUTF16(base::StringPrintf("PlayStoreApp %d", i))); | |
79 results->Add(std::move(result)); | |
80 } | |
81 } | |
82 | |
83 // Adding results calls SearchResultContainerView::ScheduleUpdate(). | |
84 // It will post a delayed task to update the results and relayout. | |
85 RunPendingMessages(); | |
86 view_->OnContainerSelected(false, false); | |
87 } | |
88 | |
89 int GetOpenResultCount(int ranking) const { | |
90 int result = view_delegate_.open_search_result_counts()[ranking]; | |
91 return result; | |
92 } | |
93 | |
94 void ResetOpenResultCount() { | |
95 view_delegate_.open_search_result_counts().clear(); | |
96 } | |
97 | |
98 int GetResultCount() const { return view_->num_results(); } | |
99 | |
100 int GetSelectedIndex() const { return view_->selected_index(); } | |
101 | |
102 void ResetSelectedIndex() const { view_->SetSelectedIndex(0); } | |
103 | |
104 bool KeyPress(ui::KeyboardCode key_code) { | |
105 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE); | |
106 return view_->OnKeyPressed(event); | |
107 } | |
108 | |
109 private: | |
110 test::AppListTestViewDelegate view_delegate_; | |
111 std::unique_ptr<SearchResultTileItemListView> view_; | |
112 std::unique_ptr<views::Textfield> textfield_; | |
113 base::test::ScopedFeatureList scoped_feature_list_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(SearchResultTileItemListViewTest); | |
116 }; | |
117 | |
118 TEST_F(SearchResultTileItemListViewTest, Basic) { | |
119 EnablePlayStoreAppSearch(); | |
120 CreateSearchResultTileItemListView(); | |
121 SetUpSearchResults(); | |
122 | |
123 const int results = GetResultCount(); | |
124 EXPECT_EQ(kInstalledApps + kPlayStoreApps, results); | |
125 // For each results, we added a separator for result type grouping. | |
126 EXPECT_EQ(kMaxNumSearchResultTiles * 2, view()->child_count()); | |
127 | |
128 // Tests item indexing by pressing TAB. | |
129 for (int i = 1; i < results; ++i) { | |
130 EXPECT_TRUE(KeyPress(ui::VKEY_TAB)); | |
131 EXPECT_EQ(i, GetSelectedIndex()); | |
132 } | |
133 | |
134 // Extra TAB events won't be handled by the view. | |
135 EXPECT_FALSE(KeyPress(ui::VKEY_TAB)); | |
136 EXPECT_EQ(results - 1, GetSelectedIndex()); | |
137 | |
138 // Tests app opening. | |
139 ResetSelectedIndex(); | |
140 ResetOpenResultCount(); | |
141 for (int i = 1; i < results; ++i) { | |
142 EXPECT_TRUE(KeyPress(ui::VKEY_TAB)); | |
143 EXPECT_EQ(i, GetSelectedIndex()); | |
144 for (int j = 0; j < i; j++) { | |
145 EXPECT_TRUE(KeyPress(ui::VKEY_RETURN)); | |
146 } | |
147 EXPECT_EQ(i, GetOpenResultCount(i)); | |
148 } | |
149 } | |
150 | |
151 } // namespace app_list | |
OLD | NEW |