| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/app_list/views/search_box_view.h" | 5 #include "ui/app_list/views/search_box_view.h" |
| 6 | 6 |
| 7 #include <cctype> | 7 #include <cctype> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/test/scoped_feature_list.h" |
| 13 #include "base/time/time.h" |
| 14 #include "ui/app_list/app_list_features.h" |
| 12 #include "ui/app_list/test/app_list_test_view_delegate.h" | 15 #include "ui/app_list/test/app_list_test_view_delegate.h" |
| 13 #include "ui/app_list/views/search_box_view_delegate.h" | 16 #include "ui/app_list/views/search_box_view_delegate.h" |
| 17 #include "ui/events/event.h" |
| 18 #include "ui/gfx/canvas.h" |
| 14 #include "ui/views/controls/textfield/textfield.h" | 19 #include "ui/views/controls/textfield/textfield.h" |
| 15 #include "ui/views/test/widget_test.h" | 20 #include "ui/views/test/widget_test.h" |
| 16 | 21 |
| 17 namespace app_list { | 22 namespace app_list { |
| 18 namespace test { | 23 namespace test { |
| 19 | 24 |
| 25 constexpr SkColor kDefaultSearchboxColor = |
| 26 SkColorSetARGBMacro(0xDE, 0x00, 0x00, 0x00); |
| 27 |
| 28 // Color used for placeholder text in zero query state. |
| 29 constexpr SkColor kZeroQuerySearchboxColor = |
| 30 SkColorSetARGBMacro(0x8A, 0x00, 0x00, 0x00); |
| 31 |
| 20 class KeyPressCounterView : public views::View { | 32 class KeyPressCounterView : public views::View { |
| 21 public: | 33 public: |
| 22 KeyPressCounterView() : count_(0) {} | 34 KeyPressCounterView() : count_(0) {} |
| 23 ~KeyPressCounterView() override {} | 35 ~KeyPressCounterView() override {} |
| 24 | 36 |
| 25 int GetCountAndReset() { | 37 int GetCountAndReset() { |
| 26 int count = count_; | 38 int count = count_; |
| 27 count_ = 0; | 39 count_ = 0; |
| 28 return count; | 40 return count; |
| 29 } | 41 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 57 widget_->GetContentsView()->AddChildView(view_); | 69 widget_->GetContentsView()->AddChildView(view_); |
| 58 widget_->GetContentsView()->AddChildView(counter_view_); | 70 widget_->GetContentsView()->AddChildView(counter_view_); |
| 59 view_->set_contents_view(counter_view_); | 71 view_->set_contents_view(counter_view_); |
| 60 } | 72 } |
| 61 | 73 |
| 62 void TearDown() override { | 74 void TearDown() override { |
| 63 widget_->CloseNow(); | 75 widget_->CloseNow(); |
| 64 views::test::WidgetTest::TearDown(); | 76 views::test::WidgetTest::TearDown(); |
| 65 } | 77 } |
| 66 | 78 |
| 79 void EnableFullscreenAppList() { |
| 80 scoped_feature_list_.InitAndEnableFeature( |
| 81 app_list::features::kEnableFullscreenAppList); |
| 82 } |
| 83 |
| 67 protected: | 84 protected: |
| 68 SearchBoxView* view() { return view_; } | 85 SearchBoxView* view() { return view_; } |
| 69 | 86 |
| 70 void SetLongAutoLaunchTimeout() { | 87 void SetLongAutoLaunchTimeout() { |
| 71 // Sets a long timeout that lasts longer than the test run. | 88 // Sets a long timeout that lasts longer than the test run. |
| 72 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1)); | 89 view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1)); |
| 73 } | 90 } |
| 74 | 91 |
| 75 base::TimeDelta GetAutoLaunchTimeout() { | 92 base::TimeDelta GetAutoLaunchTimeout() { |
| 76 return view_delegate_.GetAutoLaunchTimeout(); | 93 return view_delegate_.GetAutoLaunchTimeout(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 132 |
| 116 void BackButtonPressed() override {} | 133 void BackButtonPressed() override {} |
| 117 | 134 |
| 118 void SetSearchResultSelection(bool select) override {} | 135 void SetSearchResultSelection(bool select) override {} |
| 119 | 136 |
| 120 AppListTestViewDelegate view_delegate_; | 137 AppListTestViewDelegate view_delegate_; |
| 121 views::Widget* widget_; | 138 views::Widget* widget_; |
| 122 SearchBoxView* view_; | 139 SearchBoxView* view_; |
| 123 KeyPressCounterView* counter_view_; | 140 KeyPressCounterView* counter_view_; |
| 124 base::string16 last_query_; | 141 base::string16 last_query_; |
| 142 base::test::ScopedFeatureList scoped_feature_list_; |
| 125 int query_changed_count_; | 143 int query_changed_count_; |
| 126 | 144 |
| 127 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest); | 145 DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest); |
| 128 }; | 146 }; |
| 129 | 147 |
| 130 TEST_F(SearchBoxViewTest, Basic) { | 148 TEST_F(SearchBoxViewTest, Basic) { |
| 131 KeyPress(ui::VKEY_A); | 149 KeyPress(ui::VKEY_A); |
| 132 EXPECT_EQ("a", GetLastQueryAndReset()); | 150 EXPECT_EQ("a", GetLastQueryAndReset()); |
| 133 EXPECT_EQ(1, GetQueryChangedCountAndReset()); | 151 EXPECT_EQ(1, GetQueryChangedCountAndReset()); |
| 134 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset()); | 152 EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 157 KeyPress(ui::VKEY_DOWN); | 175 KeyPress(ui::VKEY_DOWN); |
| 158 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); | 176 EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); |
| 159 ResetAutoLaunchTimeout(); | 177 ResetAutoLaunchTimeout(); |
| 160 | 178 |
| 161 // Clearing search box also cancels. | 179 // Clearing search box also cancels. |
| 162 SetLongAutoLaunchTimeout(); | 180 SetLongAutoLaunchTimeout(); |
| 163 view()->ClearSearch(); | 181 view()->ClearSearch(); |
| 164 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); | 182 EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); |
| 165 } | 183 } |
| 166 | 184 |
| 185 // Tests that the placeholder text and cursor are changed properly upon click. |
| 186 TEST_F(SearchBoxViewTest, PlaceHolderTextTest) { |
| 187 EnableFullscreenAppList(); |
| 188 // Test that with no input the cursor is hidden and the text is center |
| 189 // aligned. |
| 190 views::Textfield* search_box_text_field = view()->search_box(); |
| 191 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(), |
| 192 gfx::Canvas::TEXT_ALIGN_CENTER); |
| 193 EXPECT_EQ(search_box_text_field->placeholder_text_color(), |
| 194 kDefaultSearchboxColor); |
| 195 EXPECT_FALSE(view()->is_cursor_enabled()); |
| 196 |
| 197 // click search box, text changes and aligns left but is still there |
| 198 gfx::Point search_box_point = |
| 199 search_box_text_field->GetBoundsInScreen().CenterPoint(); |
| 200 ui::MouseEvent mouse_event = |
| 201 ui::MouseEvent(ui::ET_MOUSE_PRESSED, search_box_point, search_box_point, |
| 202 base::TimeTicks::Now(), 0, 0); |
| 203 EXPECT_TRUE(view()->PassMouseEventForTesting(mouse_event)); |
| 204 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(), |
| 205 gfx::Canvas::TEXT_ALIGN_LEFT); |
| 206 EXPECT_EQ(search_box_text_field->placeholder_text_color(), |
| 207 kZeroQuerySearchboxColor); |
| 208 EXPECT_TRUE(view()->is_cursor_enabled()); |
| 209 // Todo(newcomer): Add an EventGenerator for SearchBoxViewTest so we can pass |
| 210 // gestures through the Textfield to SearchBoxView::HandleGestureEvent() |
| 211 // crbug.com/735240 |
| 212 } |
| 213 |
| 214 // Tests that a keypress enables the cursor and that an empty searchbox disables |
| 215 // the cursor. |
| 216 TEST_F(SearchBoxViewTest, KeyPressEnablesCursor) { |
| 217 EnableFullscreenAppList(); |
| 218 views::Textfield* search_box_text_field = view()->search_box(); |
| 219 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(), |
| 220 gfx::Canvas::TEXT_ALIGN_CENTER); |
| 221 EXPECT_EQ(search_box_text_field->placeholder_text_color(), |
| 222 kDefaultSearchboxColor); |
| 223 EXPECT_FALSE(view()->is_cursor_enabled()); |
| 224 |
| 225 // Press any key, the search box cursor should enable and the placeholder text |
| 226 // should be aligned left |
| 227 KeyPress(ui::VKEY_0); |
| 228 EXPECT_TRUE(view()->PassMouseEventForTesting(mouse_event)); |
| 229 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(), |
| 230 gfx::Canvas::TEXT_ALIGN_LEFT); |
| 231 EXPECT_EQ(search_box_text_field->placeholder_text_color(), |
| 232 kZeroQuerySearchboxColor); |
| 233 EXPECT_TRUE(view()->is_cursor_enabled()); |
| 234 |
| 235 // Delete the text, the cursor should return to its previous state with the |
| 236 // placeholder text back in place. |
| 237 KeyPress(ui::VKEY_BACK); |
| 238 EXPECT_EQ(search_box_text_field->placeholder_text_draw_flags(), |
| 239 gfx::Canvas::TEXT_ALIGN_CENTER); |
| 240 EXPECT_EQ(search_box_text_field->placeholder_text_color(), |
| 241 kDefaultSearchboxColor); |
| 242 EXPECT_FALSE(view()->is_cursor_enabled()); |
| 243 } |
| 244 |
| 167 } // namespace test | 245 } // namespace test |
| 168 } // namespace app_list | 246 } // namespace app_list |
| OLD | NEW |