Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "ui/app_list/views/search_box_view.h" | |
| 6 | |
| 7 #include <cctype> | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "ui/app_list/test/app_list_test_view_delegate.h" | |
| 12 #include "ui/app_list/views/search_box_view_delegate.h" | |
| 13 #include "ui/views/controls/textfield/textfield.h" | |
| 14 #include "ui/views/test/widget_test.h" | |
| 15 | |
| 16 namespace app_list { | |
| 17 namespace test { | |
| 18 | |
| 19 class KeyPressCounterView : public views::View { | |
| 20 public: | |
| 21 KeyPressCounterView() : count_(0) {} | |
| 22 virtual ~KeyPressCounterView() {} | |
| 23 | |
| 24 int GetCountAndReset() { | |
| 25 int count = count_; | |
| 26 count_ = 0; | |
| 27 return count; | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 // Overridden from views::View: | |
| 32 virtual bool OnKeyPressed(const ui::KeyEvent& key_event) OVERRIDE { | |
| 33 if (!::isalnum(static_cast<int>(key_event.key_code()))) { | |
| 34 ++count_; | |
| 35 return true; | |
| 36 } | |
| 37 return false; | |
| 38 } | |
| 39 int count_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(KeyPressCounterView); | |
| 42 }; | |
| 43 | |
| 44 class SearchBoxViewTest : public views::test::WidgetTest, | |
| 45 public SearchBoxViewDelegate { | |
| 46 public: | |
| 47 SearchBoxViewTest() : query_changed_count_(0) {} | |
| 48 virtual ~SearchBoxViewTest() {} | |
| 49 | |
| 50 // Overridden from testing::Test: | |
| 51 virtual void SetUp() OVERRIDE { | |
| 52 views::test::WidgetTest::SetUp(); | |
| 53 widget_ = CreateTopLevelPlatformWidget(); | |
| 54 view_ = new SearchBoxView(this, &view_delegate_); | |
| 55 counter_view_ = new KeyPressCounterView(); | |
| 56 widget_->GetContentsView()->AddChildView(view_); | |
| 57 widget_->GetContentsView()->AddChildView(counter_view_); | |
| 58 view_->set_contents_view(counter_view_); | |
| 59 } | |
| 60 | |
| 61 virtual void TearDown() OVERRIDE { | |
| 62 widget_->CloseNow(); | |
| 63 views::test::WidgetTest::TearDown(); | |
| 64 } | |
| 65 | |
| 66 protected: | |
| 67 SearchBoxView* view() { return view_; } | |
| 68 | |
| 69 void SetUpAutoLaunchTimeout() { | |
| 70 // 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.
| |
| 71 // test case. | |
| 72 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1)); | |
| 73 } | |
| 74 | |
| 75 base::TimeDelta GetAutoLaunchTimeout() { | |
| 76 return view_delegate_.GetAutoLaunchTimeout(); | |
| 77 } | |
| 78 | |
| 79 void ResetAutoLaunchTimeout() { | |
| 80 view_delegate_.set_auto_launch_timeout(base::TimeDelta()); | |
| 81 } | |
| 82 | |
| 83 int GetContentsViewKeyPressCountAndReset() { | |
| 84 return counter_view_->GetCountAndReset(); | |
| 85 } | |
| 86 | |
| 87 void KeyPress(ui::KeyboardCode key_code) { | |
| 88 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE, true); | |
| 89 view_->search_box()->OnKeyPressed(event); | |
| 90 // Emulates the input method. | |
| 91 if (::isalnum(static_cast<int>(key_code))) { | |
| 92 base::char16 character = ::tolower(static_cast<int>(key_code)); | |
| 93 view_->search_box()->InsertText(base::string16(1, character)); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 std::string GetLastQueryAndReset() { | |
| 98 base::string16 query = last_query_; | |
| 99 last_query_.clear(); | |
| 100 return UTF16ToUTF8(query); | |
| 101 } | |
| 102 | |
| 103 int GetQueryChangedCountAndReset() { | |
| 104 int result = query_changed_count_; | |
| 105 query_changed_count_ = 0; | |
| 106 return result; | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 // Overridden from SearchBoxViewDelegate: | |
| 111 virtual void QueryChanged(SearchBoxView* sender) OVERRIDE { | |
| 112 ++query_changed_count_; | |
| 113 last_query_ = sender->search_box()->text(); | |
| 114 } | |
| 115 | |
| 116 AppListTestViewDelegate view_delegate_; | |
| 117 views::Widget* widget_; | |
| 118 SearchBoxView* view_; | |
| 119 KeyPressCounterView* counter_view_; | |
| 120 base::string16 last_query_; | |
| 121 int query_changed_count_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest); | |
| 124 }; | |
| 125 | |
| 126 TEST_F(SearchBoxViewTest, Basic) { | |
| 127 (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.
| |
| 128 EXPECT_EQ("a", GetLastQueryAndReset()); | |
| 129 EXPECT_EQ(1, GetQueryChangedCountAndReset()); | |
| 130 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset()); | |
| 131 | |
| 132 (KeyPress(ui::VKEY_DOWN)); | |
| 133 EXPECT_EQ(0, GetQueryChangedCountAndReset()); | |
| 134 EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset()); | |
| 135 | |
| 136 view()->ClearSearch(); | |
| 137 EXPECT_EQ(1, GetQueryChangedCountAndReset()); | |
| 138 EXPECT_TRUE(GetLastQueryAndReset().empty()); | |
| 139 } | |
| 140 | |
| 141 TEST_F(SearchBoxViewTest, CancelAutoLaunch) { | |
| 142 SetUpAutoLaunchTimeout(); | |
| 143 ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); | |
| 144 | |
| 145 // Normal key event cancels the timeout. | |
| 146 (KeyPress(ui::VKEY_A)); | |
| 147 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); | |
| 148 ResetAutoLaunchTimeout(); | |
| 149 | |
| 150 // Unusual key event doesn't cancel -- it will be canceled in | |
| 151 // SearchResultListView. | |
| 152 SetUpAutoLaunchTimeout(); | |
| 153 (KeyPress(ui::VKEY_DOWN)); | |
| 154 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); | |
| 155 ResetAutoLaunchTimeout(); | |
| 156 | |
| 157 // Clearing search box also cancels. | |
| 158 SetUpAutoLaunchTimeout(); | |
| 159 view()->ClearSearch(); | |
| 160 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); | |
| 161 } | |
| 162 | |
| 163 } // namespace test | |
| 164 } // namespace app_list | |
| OLD | NEW |