1 // Copyright 2014 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.
5 #include "ui/app_list/views/search_result_page_view.h"
9 #include "ui/app_list/app_list_model.h"
10 #include "ui/app_list/test/app_list_test_view_delegate.h"
11 #include "ui/app_list/test/test_search_result.h"
12 #include "ui/app_list/views/search_result_list_view.h"
13 #include "ui/app_list/views/search_result_list_view_delegate.h"
14 #include "ui/app_list/views/search_result_tile_item_list_view.h"
15 #include "ui/app_list/views/search_result_view.h"
16 #include "ui/views/controls/textfield/textfield.h"
17 #include "ui/views/test/views_test_base.h"
22 class SearchResultPageViewTest
: public views::ViewsTestBase
,
23 public SearchResultListViewDelegate
{
25 SearchResultPageViewTest() {}
26 ~SearchResultPageViewTest() override
{}
28 // Overridden from testing::Test:
29 void SetUp() override
{
30 views::ViewsTestBase::SetUp();
31 view_
.reset(new SearchResultPageView());
32 list_view_
= new SearchResultListView(this, &view_delegate_
);
33 view_
->AddSearchResultContainerView(GetResults(), list_view_
);
34 textfield_
.reset(new views::Textfield());
35 tile_list_view_
= new SearchResultTileItemListView(textfield_
.get());
36 view_
->AddSearchResultContainerView(GetResults(), tile_list_view_
);
40 SearchResultPageView
* view() { return view_
.get(); }
42 SearchResultListView
* list_view() { return list_view_
; }
43 SearchResultTileItemListView
* tile_list_view() { return tile_list_view_
; }
45 AppListModel::SearchResults
* GetResults() {
46 return view_delegate_
.GetModel()->results();
49 void SetUpSearchResults(
50 const std::map
<SearchResult::DisplayType
, int> result_types
) {
51 AppListModel::SearchResults
* results
= GetResults();
52 for (const auto& data
: result_types
) {
53 for (int i
= 0; i
< data
.second
; ++i
) {
54 TestSearchResult
* result
= new TestSearchResult();
55 result
->SetDisplayType(data
.first
);
60 // Adding results will schedule Update().
64 int GetSelectedIndex() { return view_
->selected_index(); }
66 bool KeyPress(ui::KeyboardCode key_code
) {
67 ui::KeyEvent
event(ui::ET_KEY_PRESSED
, key_code
, ui::EF_NONE
);
68 return view_
->OnKeyPressed(event
);
72 void OnResultInstalled(SearchResult
* result
) override
{}
74 SearchResultListView
* list_view_
;
75 SearchResultTileItemListView
* tile_list_view_
;
77 AppListTestViewDelegate view_delegate_
;
78 scoped_ptr
<SearchResultPageView
> view_
;
79 scoped_ptr
<views::Textfield
> textfield_
;
81 DISALLOW_COPY_AND_ASSIGN(SearchResultPageViewTest
);
84 TEST_F(SearchResultPageViewTest
, Basic
) {
85 std::map
<SearchResult::DisplayType
, int> result_types
;
86 const int kListResults
= 2;
87 const int kTileResults
= 1;
88 const int kNoneResults
= 3;
89 result_types
[SearchResult::DISPLAY_LIST
] = kListResults
;
90 result_types
[SearchResult::DISPLAY_TILE
] = kTileResults
;
91 result_types
[SearchResult::DISPLAY_NONE
] = kNoneResults
;
93 SetUpSearchResults(result_types
);
94 EXPECT_EQ(0, GetSelectedIndex());
95 EXPECT_EQ(0, list_view()->selected_index());
97 // Navigate to the second result in the list view.
98 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
99 EXPECT_EQ(0, GetSelectedIndex());
100 EXPECT_EQ(1, list_view()->selected_index());
102 // Navigate to the tile group.
103 EXPECT_TRUE(KeyPress(ui::VKEY_DOWN
));
104 EXPECT_EQ(1, GetSelectedIndex());
105 EXPECT_EQ(-1, list_view()->selected_index());
106 EXPECT_EQ(0, tile_list_view()->selected_index());
108 // Navigate off bottom of tile items.
109 EXPECT_FALSE(KeyPress(ui::VKEY_DOWN
));
110 EXPECT_EQ(1, GetSelectedIndex());
111 EXPECT_EQ(0, tile_list_view()->selected_index());
113 // Navigate back to the list group.
114 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
115 EXPECT_EQ(0, GetSelectedIndex());
116 EXPECT_EQ(1, list_view()->selected_index());
117 EXPECT_EQ(-1, tile_list_view()->selected_index());
119 // Navigate off top of list.
120 EXPECT_TRUE(KeyPress(ui::VKEY_UP
));
121 EXPECT_EQ(0, list_view()->selected_index());
122 EXPECT_FALSE(KeyPress(ui::VKEY_UP
));
123 EXPECT_EQ(0, list_view()->selected_index());
124 EXPECT_EQ(0, GetSelectedIndex());
128 } // namespace app_list