omnibox_view_views_browsertest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright (c) 2013 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 "chrome/browser/ui/views/omnibox/omnibox_view_views.h" 6 7#include "chrome/browser/ui/browser.h" 8#include "chrome/browser/ui/browser_commands.h" 9#include "chrome/browser/ui/browser_window.h" 10#include "chrome/browser/ui/omnibox/location_bar.h" 11#include "chrome/browser/ui/omnibox/omnibox_popup_model.h" 12#include "chrome/browser/ui/view_ids.h" 13#include "chrome/browser/ui/views/frame/browser_view.h" 14#include "chrome/test/base/in_process_browser_test.h" 15#include "chrome/test/base/interactive_test_utils.h" 16#include "grit/generated_resources.h" 17#include "ui/base/clipboard/clipboard.h" 18#include "ui/base/clipboard/scoped_clipboard_writer.h" 19#include "ui/base/test/ui_controls.h" 20 21#if defined(USE_AURA) 22#include "ui/aura/test/event_generator.h" 23#include "ui/aura/window.h" 24#include "ui/aura/window_tree_host.h" 25#include "ui/events/event_processor.h" 26#endif // defined(USE_AURA) 27 28class OmniboxViewViewsTest : public InProcessBrowserTest { 29 protected: 30 OmniboxViewViewsTest() {} 31 32 static void GetOmniboxViewForBrowser(const Browser* browser, 33 OmniboxView** omnibox_view) { 34 BrowserWindow* window = browser->window(); 35 ASSERT_TRUE(window); 36 LocationBar* location_bar = window->GetLocationBar(); 37 ASSERT_TRUE(location_bar); 38 *omnibox_view = location_bar->GetOmniboxView(); 39 ASSERT_TRUE(*omnibox_view); 40 } 41 42 // Move the mouse to the center of the browser window and left-click. 43 void ClickBrowserWindowCenter() { 44 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( 45 BrowserView::GetBrowserViewForBrowser( 46 browser())->GetBoundsInScreen().CenterPoint())); 47 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, 48 ui_controls::DOWN)); 49 ASSERT_TRUE( 50 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::UP)); 51 } 52 53 // Press and release the mouse at the specified locations. If 54 // |release_offset| differs from |press_offset|, the mouse will be moved 55 // between the press and release. 56 void Click(ui_controls::MouseButton button, 57 const gfx::Point& press_location, 58 const gfx::Point& release_location) { 59 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(press_location)); 60 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::DOWN)); 61 62 if (press_location != release_location) 63 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(release_location)); 64 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::UP)); 65 } 66 67#if defined(USE_AURA) 68 // Tap the center of the browser window. 69 void TapBrowserWindowCenter() { 70 gfx::Point center = BrowserView::GetBrowserViewForBrowser( 71 browser())->GetBoundsInScreen().CenterPoint(); 72 aura::test::EventGenerator generator(browser()->window()-> 73 GetNativeWindow()->GetRootWindow()); 74 generator.GestureTapAt(center); 75 } 76 77 // Touch down and release at the specified locations. 78 void Tap(const gfx::Point& press_location, 79 const gfx::Point& release_location) { 80 ui::EventProcessor* dispatcher = 81 browser()->window()->GetNativeWindow()->GetHost()->event_processor(); 82 83 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, press_location, 84 5, base::TimeDelta::FromMilliseconds(0)); 85 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&press); 86 ASSERT_FALSE(details.dispatcher_destroyed); 87 88 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, release_location, 89 5, base::TimeDelta::FromMilliseconds(50)); 90 details = dispatcher->OnEventFromSource(&release); 91 ASSERT_FALSE(details.dispatcher_destroyed); 92 } 93#endif // defined(USE_AURA) 94 95 private: 96 // InProcessBrowserTest: 97 virtual void SetUpOnMainThread() OVERRIDE { 98 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 99 chrome::FocusLocationBar(browser()); 100 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 101 } 102 103 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); 104}; 105 106IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, PasteAndGoDoesNotLeavePopupOpen) { 107 OmniboxView* view = browser()->window()->GetLocationBar()->GetOmniboxView(); 108 OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view); 109 110 // Put an URL on the clipboard. 111 { 112 ui::ScopedClipboardWriter clipboard_writer( 113 ui::Clipboard::GetForCurrentThread(), ui::CLIPBOARD_TYPE_COPY_PASTE); 114 clipboard_writer.WriteURL(base::ASCIIToUTF16("http://www.example.com/")); 115 } 116 117 // Paste and go. 118 omnibox_view_views->ExecuteCommand(IDS_PASTE_AND_GO, ui::EF_NONE); 119 120 // The popup should not be open. 121 EXPECT_FALSE(view->model()->popup_model()->IsOpen()); 122} 123 124IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnClick) { 125 OmniboxView* omnibox_view = NULL; 126 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view)); 127 omnibox_view->SetUserText(base::ASCIIToUTF16("http://www.google.com/")); 128 129 // Take the focus away from the omnibox. 130 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 131 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 132 EXPECT_FALSE(omnibox_view->IsSelectAll()); 133 134 // Clicking in the omnibox should take focus and select all text. 135 const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser( 136 browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen(); 137 const gfx::Point click_location = omnibox_bounds.CenterPoint(); 138 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, 139 click_location, click_location)); 140 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 141 EXPECT_TRUE(omnibox_view->IsSelectAll()); 142 143 // Clicking in another view should clear focus and the selection. 144 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 145 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 146 EXPECT_FALSE(omnibox_view->IsSelectAll()); 147 148 // Clicking in the omnibox again should take focus and select all text again. 149 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, 150 click_location, click_location)); 151 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 152 EXPECT_TRUE(omnibox_view->IsSelectAll()); 153 154 // Clicking another omnibox spot should keep focus but clear the selection. 155 omnibox_view->SelectAll(false); 156 const gfx::Point click2_location = omnibox_bounds.origin() + 157 gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4); 158 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, 159 click2_location, click2_location)); 160 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 161 EXPECT_FALSE(omnibox_view->IsSelectAll()); 162 163 // Take the focus away and click in the omnibox again, but drag a bit before 164 // releasing. We should focus the omnibox but not select all of its text. 165 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 166 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, 167 click_location, click2_location)); 168 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 169 EXPECT_FALSE(omnibox_view->IsSelectAll()); 170 171 // Middle-clicking should not be handled by the omnibox. 172 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 173 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE, 174 click_location, click_location)); 175 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 176 EXPECT_FALSE(omnibox_view->IsSelectAll()); 177} 178 179#if defined(USE_AURA) 180IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnTap) { 181 OmniboxView* omnibox_view = NULL; 182 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view)); 183 omnibox_view->SetUserText(base::ASCIIToUTF16("http://www.google.com/")); 184 185 // Take the focus away from the omnibox. 186 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter()); 187 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 188 EXPECT_FALSE(omnibox_view->IsSelectAll()); 189 190 // Tapping in the omnibox should take focus and select all text. 191 const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser( 192 browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen(); 193 const gfx::Point tap_location = omnibox_bounds.CenterPoint(); 194 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location)); 195 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 196 EXPECT_TRUE(omnibox_view->IsSelectAll()); 197 198 // Tapping in another view should clear focus and the selection. 199 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter()); 200 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 201 EXPECT_FALSE(omnibox_view->IsSelectAll()); 202 203 // Tapping in the omnibox again should take focus and select all text again. 204 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location)); 205 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 206 EXPECT_TRUE(omnibox_view->IsSelectAll()); 207 208 // Tapping another omnibox spot should keep focus and selection. 209 omnibox_view->SelectAll(false); 210 const gfx::Point tap2_location = omnibox_bounds.origin() + 211 gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4); 212 ASSERT_NO_FATAL_FAILURE(Tap(tap2_location, tap2_location)); 213 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 214 // We don't test if the all text is selected because it depends on whether or 215 // not there was text under the tap, which appears to be flaky. 216 217 // Take the focus away and tap in the omnibox again, but drag a bit before 218 // releasing. We should focus the omnibox but not select all of its text. 219 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter()); 220 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap2_location)); 221 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 222 EXPECT_FALSE(omnibox_view->IsSelectAll()); 223} 224#endif // defined(USE_AURA) 225