Chromium Code Reviews| Index: ui/app_list/views/search_result_tile_item_list_view_unittest.cc |
| diff --git a/ui/app_list/views/search_result_tile_item_list_view_unittest.cc b/ui/app_list/views/search_result_tile_item_list_view_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..be66402b0d88d58452007e256b89373a4d9693b1 |
| --- /dev/null |
| +++ b/ui/app_list/views/search_result_tile_item_list_view_unittest.cc |
| @@ -0,0 +1,151 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/app_list/views/search_result_tile_item_list_view.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/test/scoped_feature_list.h" |
| +#include "ui/app_list/app_list_features.h" |
| +#include "ui/app_list/app_list_model.h" |
| +#include "ui/app_list/test/app_list_test_view_delegate.h" |
| +#include "ui/app_list/test/test_search_result.h" |
| +#include "ui/app_list/views/search_result_list_view_delegate.h" |
| +#include "ui/app_list/views/search_result_view.h" |
| +#include "ui/views/controls/textfield/textfield.h" |
| +#include "ui/views/test/views_test_base.h" |
| + |
| +namespace app_list { |
| + |
| +namespace { |
| +constexpr int kMaxNumSearchResultTiles = 6; |
| +constexpr int kInstalledApps = 4; |
| +constexpr int kPlayStoreApps = 2; |
| +} // namespace |
| + |
| +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
|
| + public: |
| + SearchResultTileItemListViewTest() {} |
| + ~SearchResultTileItemListViewTest() override {} |
| + |
| + protected: |
| + void CreateSearchResultTileItemListView() { |
| + textfield_ = base::MakeUnique<views::Textfield>(); |
| + view_ = base::MakeUnique<SearchResultTileItemListView>(textfield_.get(), |
| + &view_delegate_); |
| + view_->SetResults(view_delegate_.GetModel()->results()); |
| + } |
| + |
| + void EnablePlayStoreAppSearch() { |
| + scoped_feature_list_.InitAndEnableFeature( |
| + app_list::features::kEnablePlayStoreAppSearch); |
| + } |
| + |
| + SearchResultTileItemListView* view() { return view_.get(); } |
| + |
| + AppListModel::SearchResults* GetResults() { |
| + return view_delegate_.GetModel()->results(); |
| + } |
| + |
| + void SetUpSearchResults() { |
| + AppListModel::SearchResults* results = GetResults(); |
| + |
| + // Populate results for installed applications. |
| + for (int i = 0; i < kInstalledApps; ++i) { |
| + std::unique_ptr<TestSearchResult> result = |
| + base::MakeUnique<TestSearchResult>(); |
| + result->set_display_type(SearchResult::DISPLAY_TILE); |
| + result->set_result_type(SearchResult::RESULT_INSTALLED_APP); |
| + result->set_title( |
| + base::UTF8ToUTF16(base::StringPrintf("InstalledApp %d", i))); |
| + results->Add(std::move(result)); |
| + } |
| + |
| + // Populate results for Play Store search applications. |
| + if (app_list::features::IsPlayStoreAppSearchEnabled()) { |
| + for (int i = 0; i < kPlayStoreApps; ++i) { |
| + std::unique_ptr<TestSearchResult> result = |
| + base::MakeUnique<TestSearchResult>(); |
| + result->set_display_type(SearchResult::DISPLAY_TILE); |
| + result->set_result_type(SearchResult::RESULT_PLAYSTORE_APP); |
| + result->set_title( |
| + base::UTF8ToUTF16(base::StringPrintf("PlayStoreApp %d", i))); |
| + results->Add(std::move(result)); |
| + } |
| + } |
| + |
| + // Adding results calls SearchResultContainerView::ScheduleUpdate(). |
| + // It will post a delayed task to update the results and relayout. |
| + RunPendingMessages(); |
| + view_->OnContainerSelected(false, false); |
| + } |
| + |
| + int GetOpenResultCount(int ranking) const { |
| + int result = view_delegate_.open_search_result_counts()[ranking]; |
| + return result; |
| + } |
| + |
| + void ResetOpenResultCount() { |
| + view_delegate_.open_search_result_counts().clear(); |
| + } |
| + |
| + int GetResultCount() const { return view_->num_results(); } |
| + |
| + int GetSelectedIndex() const { return view_->selected_index(); } |
| + |
| + void ResetSelectedIndex() const { view_->SetSelectedIndex(0); } |
| + |
| + bool KeyPress(ui::KeyboardCode key_code) { |
| + ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE); |
| + return view_->OnKeyPressed(event); |
| + } |
| + |
| + private: |
| + test::AppListTestViewDelegate view_delegate_; |
| + std::unique_ptr<SearchResultTileItemListView> view_; |
| + std::unique_ptr<views::Textfield> textfield_; |
| + base::test::ScopedFeatureList scoped_feature_list_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SearchResultTileItemListViewTest); |
| +}; |
| + |
| +TEST_F(SearchResultTileItemListViewTest, Basic) { |
| + EnablePlayStoreAppSearch(); |
| + CreateSearchResultTileItemListView(); |
| + SetUpSearchResults(); |
| + |
| + const int results = GetResultCount(); |
| + EXPECT_EQ(kInstalledApps + kPlayStoreApps, results); |
| + // For each results, we added a separator for result type grouping. |
| + EXPECT_EQ(kMaxNumSearchResultTiles * 2, view()->child_count()); |
| + |
| + // Tests item indexing by pressing TAB. |
| + for (int i = 1; i < results; ++i) { |
| + EXPECT_TRUE(KeyPress(ui::VKEY_TAB)); |
| + EXPECT_EQ(i, GetSelectedIndex()); |
| + } |
| + |
| + // Extra TAB events won't be handled by the view. |
| + EXPECT_FALSE(KeyPress(ui::VKEY_TAB)); |
| + EXPECT_EQ(results - 1, GetSelectedIndex()); |
| + |
| + // Tests app opening. |
| + ResetSelectedIndex(); |
| + ResetOpenResultCount(); |
| + for (int i = 1; i < results; ++i) { |
| + EXPECT_TRUE(KeyPress(ui::VKEY_TAB)); |
| + EXPECT_EQ(i, GetSelectedIndex()); |
| + for (int j = 0; j < i; j++) { |
| + EXPECT_TRUE(KeyPress(ui::VKEY_RETURN)); |
| + } |
| + EXPECT_EQ(i, GetOpenResultCount(i)); |
| + } |
| +} |
| + |
| +} // namespace app_list |