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

Unified Diff: ui/app_list/views/search_box_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_box_view_unittest.cc
diff --git a/ui/app_list/views/search_box_view_unittest.cc b/ui/app_list/views/search_box_view_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0227d8647f017dea5e3c8b2b5d9153707bf75460
--- /dev/null
+++ b/ui/app_list/views/search_box_view_unittest.cc
@@ -0,0 +1,164 @@
+// 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_box_view.h"
+
+#include <cctype>
+#include <map>
+
+#include "base/strings/utf_string_conversions.h"
+#include "ui/app_list/test/app_list_test_view_delegate.h"
+#include "ui/app_list/views/search_box_view_delegate.h"
+#include "ui/views/controls/textfield/textfield.h"
+#include "ui/views/test/widget_test.h"
+
+namespace app_list {
+namespace test {
+
+class KeyPressCounterView : public views::View {
+ public:
+ KeyPressCounterView() : count_(0) {}
+ virtual ~KeyPressCounterView() {}
+
+ int GetCountAndReset() {
+ int count = count_;
+ count_ = 0;
+ return count;
+ }
+
+ private:
+ // Overridden from views::View:
+ virtual bool OnKeyPressed(const ui::KeyEvent& key_event) OVERRIDE {
+ if (!::isalnum(static_cast<int>(key_event.key_code()))) {
+ ++count_;
+ return true;
+ }
+ return false;
+ }
+ int count_;
+
+ DISALLOW_COPY_AND_ASSIGN(KeyPressCounterView);
+};
+
+class SearchBoxViewTest : public views::test::WidgetTest,
+ public SearchBoxViewDelegate {
+ public:
+ SearchBoxViewTest() : query_changed_count_(0) {}
+ virtual ~SearchBoxViewTest() {}
+
+ // Overridden from testing::Test:
+ virtual void SetUp() OVERRIDE {
+ views::test::WidgetTest::SetUp();
+ widget_ = CreateTopLevelPlatformWidget();
+ view_ = new SearchBoxView(this, &view_delegate_);
+ counter_view_ = new KeyPressCounterView();
+ widget_->GetContentsView()->AddChildView(view_);
+ widget_->GetContentsView()->AddChildView(counter_view_);
+ view_->set_contents_view(counter_view_);
+ }
+
+ virtual void TearDown() OVERRIDE {
+ widget_->CloseNow();
+ views::test::WidgetTest::TearDown();
+ }
+
+ protected:
+ SearchBoxView* view() { return view_; }
+
+ void SetUpAutoLaunchTimeout() {
+ // Sets a long timeout to make sure the timeout actualy happening in the
xiyuan 2014/02/14 04:55:55 nit: Can we rephrase a bit? // Sets a long timeou
Jun Mukai 2014/02/14 19:17:27 Done.
+ // test case.
+ view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1));
+ }
+
+ base::TimeDelta GetAutoLaunchTimeout() {
+ return view_delegate_.GetAutoLaunchTimeout();
+ }
+
+ void ResetAutoLaunchTimeout() {
+ view_delegate_.set_auto_launch_timeout(base::TimeDelta());
+ }
+
+ int GetContentsViewKeyPressCountAndReset() {
+ return counter_view_->GetCountAndReset();
+ }
+
+ void KeyPress(ui::KeyboardCode key_code) {
+ ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE, true);
+ view_->search_box()->OnKeyPressed(event);
+ // Emulates the input method.
+ if (::isalnum(static_cast<int>(key_code))) {
+ base::char16 character = ::tolower(static_cast<int>(key_code));
+ view_->search_box()->InsertText(base::string16(1, character));
+ }
+ }
+
+ std::string GetLastQueryAndReset() {
+ base::string16 query = last_query_;
+ last_query_.clear();
+ return UTF16ToUTF8(query);
+ }
+
+ int GetQueryChangedCountAndReset() {
+ int result = query_changed_count_;
+ query_changed_count_ = 0;
+ return result;
+ }
+
+ private:
+ // Overridden from SearchBoxViewDelegate:
+ virtual void QueryChanged(SearchBoxView* sender) OVERRIDE {
+ ++query_changed_count_;
+ last_query_ = sender->search_box()->text();
+ }
+
+ AppListTestViewDelegate view_delegate_;
+ views::Widget* widget_;
+ SearchBoxView* view_;
+ KeyPressCounterView* counter_view_;
+ base::string16 last_query_;
+ int query_changed_count_;
+
+ DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest);
+};
+
+TEST_F(SearchBoxViewTest, Basic) {
+ (KeyPress(ui::VKEY_A));
xiyuan 2014/02/14 04:55:55 Why we need to wrap KeyPress in ()?
Jun Mukai 2014/02/14 19:17:27 Done.
+ EXPECT_EQ("a", GetLastQueryAndReset());
+ EXPECT_EQ(1, GetQueryChangedCountAndReset());
+ EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset());
+
+ (KeyPress(ui::VKEY_DOWN));
+ EXPECT_EQ(0, GetQueryChangedCountAndReset());
+ EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset());
+
+ view()->ClearSearch();
+ EXPECT_EQ(1, GetQueryChangedCountAndReset());
+ EXPECT_TRUE(GetLastQueryAndReset().empty());
+}
+
+TEST_F(SearchBoxViewTest, CancelAutoLaunch) {
+ SetUpAutoLaunchTimeout();
+ ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
+
+ // Normal key event cancels the timeout.
+ (KeyPress(ui::VKEY_A));
+ EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
+ ResetAutoLaunchTimeout();
+
+ // Unusual key event doesn't cancel -- it will be canceled in
+ // SearchResultListView.
+ SetUpAutoLaunchTimeout();
+ (KeyPress(ui::VKEY_DOWN));
+ EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout());
+ ResetAutoLaunchTimeout();
+
+ // Clearing search box also cancels.
+ SetUpAutoLaunchTimeout();
+ view()->ClearSearch();
+ EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout());
+}
+
+} // namespace test
+} // namespace app_list

Powered by Google App Engine
This is Rietveld 408576698