Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Unified Diff: ui/app_list/views/search_result_list_view_unittest.cc

Issue 164193005: Refactors the auto-launch logic and adds tests for it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/app_list/views/search_result_list_view_unittest.cc
diff --git a/ui/app_list/views/search_result_list_view_unittest.cc b/ui/app_list/views/search_result_list_view_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..137ee7d60db7201b5a05ed6524b4db7debe679ff
--- /dev/null
+++ b/ui/app_list/views/search_result_list_view_unittest.cc
@@ -0,0 +1,180 @@
+// Copyright 2014 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_list_view.h"
+
+#include <map>
+
+#include "ui/app_list/app_list_model.h"
+#include "ui/app_list/search_result.h"
+#include "ui/app_list/test/app_list_test_view_delegate.h"
+#include "ui/app_list/views/search_result_list_view_delegate.h"
+#include "ui/views/test/views_test_base.h"
+
+namespace app_list {
+namespace test {
+
+namespace {
+int kDefaultSearchItems = 5;
+}
xiyuan 2014/02/14 04:55:55 nit: // namespace
Jun Mukai 2014/02/14 19:17:27 Done.
+
+class SearchResultListViewTest : public views::ViewsTestBase,
+ public SearchResultListViewDelegate {
+ public:
+ SearchResultListViewTest() {}
+ virtual ~SearchResultListViewTest() {}
+
+ // Overridden from testing::Test:
+ virtual void SetUp() OVERRIDE {
+ views::ViewsTestBase::SetUp();
+ view_.reset(new SearchResultListView(this, &view_delegate_));
+ view_->SetResults(view_delegate_.GetModel()->results());
+ view_->SetSelectedIndex(0);
+ }
+
+ protected:
+ SearchResultListView* view() { return view_.get(); }
+
+ void SetUpAutoLaunchTimeout() {
+ // Sets a long timeout to make sure the timeout actualy happening in the
+ // test case.
xiyuan 2014/02/14 04:55:55 nit: Update comment and function name as the other
Jun Mukai 2014/02/14 19:17:27 Done.
+ view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
+ }
+
+ void SetUpSearchResults() {
+ AppListModel::SearchResults* results = view_delegate_.GetModel()->results();
+ for (int i = 0; i < kDefaultSearchItems; ++i)
+ results->Add(new SearchResult());
+
+ // Adding results will schedule Update().
+ RunPendingMessages();
+ }
+
+ int GetOpenResultCountAndReset(int ranking) {
+ int result = open_result_counts_[ranking];
+ open_result_counts_.clear();
+ return result;
+ }
+
+ int GetSearchResults() {
+ return view_->last_visible_index_ + 1;
+ }
+
+ int GetSelectedIndex() {
+ return view_->selected_index_;
+ }
+
+ void ResetSelectedIndex() {
+ view_->SetSelectedIndex(0);
+ }
+
+ bool KeyPress(ui::KeyboardCode key_code) {
+ ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE, true);
+ return view_->OnKeyPressed(event);
+ }
+
+ bool IsAutoLaunching() {
+ return view_->auto_launch_animation_;
+ }
+
+ void ForceAutoLaunch() {
+ view_->ForceAutoLaunchForTest();
+ }
+
+ private:
+ // Overridden from SearchResultListViewDelegate:
+ virtual void OpenResult(SearchResult* result, int event_flags) OVERRIDE {
+ const AppListModel::SearchResults* results =
+ view_delegate_.GetModel()->results();
+ for (size_t i = 0; i < results->item_count(); ++i) {
+ if (results->GetItemAt(i) == result) {
+ open_result_counts_[i]++;
+ break;
+ }
+ }
+ }
+ virtual void InvokeResultAction(SearchResult* result,
+ int action_index,
+ int event_flags) OVERRIDE {}
+ virtual void OnResultInstalled(SearchResult* result) OVERRIDE {}
+ virtual void OnResultUninstalled(SearchResult* result) OVERRIDE {}
+
+ AppListTestViewDelegate view_delegate_;
+ scoped_ptr<SearchResultListView> view_;
+ std::map<int, int> open_result_counts_;
+
+ DISALLOW_COPY_AND_ASSIGN(SearchResultListViewTest);
+};
+
+TEST_F(SearchResultListViewTest, Basic) {
+ SetUpSearchResults();
+
+ const int results = GetSearchResults();
+ EXPECT_EQ(kDefaultSearchItems, results);
+ EXPECT_EQ(0, GetSelectedIndex());
+ EXPECT_FALSE(IsAutoLaunching());
+
+ EXPECT_TRUE(KeyPress(ui::VKEY_RETURN));
+ EXPECT_EQ(1, GetOpenResultCountAndReset(0));
+
+ for (int i = 1; i < results; ++i) {
+ EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
+ EXPECT_EQ(i, GetSelectedIndex());
+ }
+ // Doesn't rotate.
+ EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
+ EXPECT_EQ(results - 1, GetSelectedIndex());
+
+ for (int i = 1; i < results; ++i) {
+ EXPECT_TRUE(KeyPress(ui::VKEY_UP));
+ EXPECT_EQ(results - i - 1, GetSelectedIndex());
+ }
+ // Doesn't rotate.
+ EXPECT_TRUE(KeyPress(ui::VKEY_UP));
+ EXPECT_EQ(0, GetSelectedIndex());
+ ResetSelectedIndex();
+
+ for (int i = 1; i < results; ++i) {
+ EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
+ EXPECT_EQ(i, GetSelectedIndex());
+ }
+ // Doesn't rotate.
+ EXPECT_TRUE(KeyPress(ui::VKEY_TAB));
+ EXPECT_EQ(results - 1, GetSelectedIndex());
+}
+
+TEST_F(SearchResultListViewTest, AutoLaunch) {
+ SetUpAutoLaunchTimeout();
+ SetUpSearchResults();
+
+ EXPECT_TRUE(IsAutoLaunching());
+ ForceAutoLaunch();
+
+ EXPECT_FALSE(IsAutoLaunching());
+ EXPECT_EQ(1, GetOpenResultCountAndReset(0));
+}
+
+TEST_F(SearchResultListViewTest, CancelAutoLaunch) {
+ SetUpAutoLaunchTimeout();
+ SetUpSearchResults();
+
+ EXPECT_TRUE(IsAutoLaunching());
+
+ EXPECT_TRUE(KeyPress(ui::VKEY_DOWN));
+ EXPECT_FALSE(IsAutoLaunching());
+
+ SetUpAutoLaunchTimeout();
+ view()->UpdateAutoLaunchState();
+ EXPECT_TRUE(IsAutoLaunching());
+
+ view()->SetVisible(false);
+ EXPECT_FALSE(IsAutoLaunching());
+
+ SetUpAutoLaunchTimeout();
+ view()->SetVisible(true);
+ EXPECT_TRUE(IsAutoLaunching());
+}
+
+} // namespace test
+} // namespace app_list
« ui/app_list/views/search_result_list_view.h ('K') | « ui/app_list/views/search_result_list_view.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698