15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/app_list/views/search_box_view.h" 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <cctype> 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <map> 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/app_list/test/app_list_test_view_delegate.h" 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/app_list/views/search_box_view_delegate.h" 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/controls/textfield/textfield.h" 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/views/test/widget_test.h" 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace app_list { 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace test { 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class KeyPressCounterView : public views::View { 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) KeyPressCounterView() : count_(0) {} 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual ~KeyPressCounterView() {} 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int GetCountAndReset() { 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int count = count_; 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) count_ = 0; 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return count; 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Overridden from views::View: 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual bool OnKeyPressed(const ui::KeyEvent& key_event) OVERRIDE { 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (!::isalnum(static_cast<int>(key_event.key_code()))) { 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ++count_; 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return true; 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return false; 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int count_; 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(KeyPressCounterView); 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SearchBoxViewTest : public views::test::WidgetTest, 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public SearchBoxViewDelegate { 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SearchBoxViewTest() : query_changed_count_(0) {} 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual ~SearchBoxViewTest() {} 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Overridden from testing::Test: 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void SetUp() OVERRIDE { 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) views::test::WidgetTest::SetUp(); 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) widget_ = CreateTopLevelPlatformWidget(); 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view_ = new SearchBoxView(this, &view_delegate_); 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) counter_view_ = new KeyPressCounterView(); 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) widget_->GetContentsView()->AddChildView(view_); 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) widget_->GetContentsView()->AddChildView(counter_view_); 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view_->set_contents_view(counter_view_); 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void TearDown() OVERRIDE { 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) widget_->CloseNow(); 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) views::test::WidgetTest::TearDown(); 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected: 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SearchBoxView* view() { return view_; } 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetLongAutoLaunchTimeout() { 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Sets a long timeout that lasts longer than the test run. 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view_delegate_.set_auto_launch_timeout(base::TimeDelta::FromDays(1)); 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::TimeDelta GetAutoLaunchTimeout() { 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return view_delegate_.GetAutoLaunchTimeout(); 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void ResetAutoLaunchTimeout() { 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view_delegate_.set_auto_launch_timeout(base::TimeDelta()); 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int GetContentsViewKeyPressCountAndReset() { 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return counter_view_->GetCountAndReset(); 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void KeyPress(ui::KeyboardCode key_code) { 875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE); 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view_->search_box()->OnKeyPressed(event); 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Emulates the input method. 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (::isalnum(static_cast<int>(key_code))) { 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::char16 character = ::tolower(static_cast<int>(key_code)); 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view_->search_box()->InsertText(base::string16(1, character)); 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string GetLastQueryAndReset() { 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16 query = last_query_; 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) last_query_.clear(); 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return base::UTF16ToUTF8(query); 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int GetQueryChangedCountAndReset() { 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int result = query_changed_count_; 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) query_changed_count_ = 0; 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Overridden from SearchBoxViewDelegate: 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void QueryChanged(SearchBoxView* sender) OVERRIDE { 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ++query_changed_count_; 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) last_query_ = sender->search_box()->text(); 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) AppListTestViewDelegate view_delegate_; 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) views::Widget* widget_; 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SearchBoxView* view_; 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) KeyPressCounterView* counter_view_; 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::string16 last_query_; 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int query_changed_count_; 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(SearchBoxViewTest); 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(SearchBoxViewTest, Basic) { 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) KeyPress(ui::VKEY_A); 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ("a", GetLastQueryAndReset()); 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(1, GetQueryChangedCountAndReset()); 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(0, GetContentsViewKeyPressCountAndReset()); 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) KeyPress(ui::VKEY_DOWN); 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(0, GetQueryChangedCountAndReset()); 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(1, GetContentsViewKeyPressCountAndReset()); 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view()->ClearSearch(); 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(1, GetQueryChangedCountAndReset()); 1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_TRUE(GetLastQueryAndReset().empty()); 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(SearchBoxViewTest, CancelAutoLaunch) { 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SetLongAutoLaunchTimeout(); 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ASSERT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Normal key event cancels the timeout. 1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) KeyPress(ui::VKEY_A); 1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); 1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ResetAutoLaunchTimeout(); 1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Unusual key event doesn't cancel -- it will be canceled in 1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // SearchResultListView. 1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SetLongAutoLaunchTimeout(); 1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) KeyPress(ui::VKEY_DOWN); 1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_NE(base::TimeDelta(), GetAutoLaunchTimeout()); 1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ResetAutoLaunchTimeout(); 1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Clearing search box also cancels. 1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) SetLongAutoLaunchTimeout(); 1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) view()->ClearSearch(); 1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(base::TimeDelta(), GetAutoLaunchTimeout()); 1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace test 1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace app_list 164