omnibox_view_views_browsertest.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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/browser/ui/views/omnibox/omnibox_views.h" 15#include "chrome/test/base/in_process_browser_test.h" 16#include "chrome/test/base/interactive_test_utils.h" 17#include "grit/generated_resources.h" 18#include "ui/base/clipboard/clipboard.h" 19#include "ui/base/clipboard/scoped_clipboard_writer.h" 20#include "ui/base/test/ui_controls.h" 21#include "ui/views/controls/textfield/native_textfield_wrapper.h" 22 23class OmniboxViewViewsTest : public InProcessBrowserTest { 24 protected: 25 OmniboxViewViewsTest() {} 26 27 static void GetOmniboxViewForBrowser(const Browser* browser, 28 OmniboxView** omnibox_view) { 29 BrowserWindow* window = browser->window(); 30 ASSERT_TRUE(window); 31 LocationBar* location_bar = window->GetLocationBar(); 32 ASSERT_TRUE(location_bar); 33 *omnibox_view = location_bar->GetLocationEntry(); 34 ASSERT_TRUE(*omnibox_view); 35 } 36 37 // Move the mouse to the center of the browser window and left-click. 38 void ClickBrowserWindowCenter() { 39 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( 40 BrowserView::GetBrowserViewForBrowser( 41 browser())->GetBoundsInScreen().CenterPoint())); 42 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, 43 ui_controls::DOWN)); 44 ASSERT_TRUE( 45 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::UP)); 46 } 47 48 // Press and release the mouse in the omnibox at an offset from its origin. 49 // If |release_offset| differs from |press_offset|, the mouse will be moved 50 // between the press and release. 51 void ClickOmnibox(ui_controls::MouseButton button, 52 const gfx::Vector2d& press_offset, 53 const gfx::Vector2d& release_offset) { 54 const views::View* omnibox = BrowserView::GetBrowserViewForBrowser( 55 browser())->GetViewByID(VIEW_ID_OMNIBOX); 56 gfx::Point omnibox_origin = omnibox->GetBoundsInScreen().origin(); 57 gfx::Point press_point = omnibox_origin + press_offset; 58 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(press_point)); 59 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::DOWN)); 60 61 gfx::Point release_point = omnibox_origin + release_offset; 62 if (release_point != press_point) 63 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(release_point)); 64 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::UP)); 65 } 66 67 private: 68 // InProcessBrowserTest: 69 virtual void SetUpOnMainThread() OVERRIDE { 70 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 71 chrome::FocusLocationBar(browser()); 72 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 73 } 74 75 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); 76}; 77 78IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, PasteAndGoDoesNotLeavePopupOpen) { 79 OmniboxView* view = browser()->window()->GetLocationBar()->GetLocationEntry(); 80 OmniboxViewViews* omnibox_view_views = GetOmniboxViewViews(view); 81 // This test is only relevant when OmniboxViewViews is present and is using 82 // the native textfield wrapper. 83 if (!omnibox_view_views) 84 return; 85 views::NativeTextfieldWrapper* native_textfield_wrapper = 86 static_cast<views::NativeTextfieldWrapper*>( 87 omnibox_view_views->GetNativeWrapperForTesting()); 88 if (!native_textfield_wrapper) 89 return; 90 91 // Put an URL on the clipboard. 92 { 93 ui::ScopedClipboardWriter clipboard_writer( 94 ui::Clipboard::GetForCurrentThread(), ui::CLIPBOARD_TYPE_COPY_PASTE); 95 clipboard_writer.WriteURL(ASCIIToUTF16("http://www.example.com/")); 96 } 97 98 // Paste and go. 99 native_textfield_wrapper->ExecuteTextCommand(IDS_PASTE_AND_GO); 100 101 // The popup should not be open. 102 EXPECT_FALSE(view->model()->popup_model()->IsOpen()); 103} 104 105IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnClick) { 106 OmniboxView* omnibox_view = NULL; 107 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view)); 108 omnibox_view->SetUserText(ASCIIToUTF16("http://www.google.com/")); 109 const gfx::Vector2d click(40, 10); 110 111 // Take the focus away from the omnibox. 112 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 113 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 114 EXPECT_FALSE(omnibox_view->IsSelectAll()); 115 116 // Clicking in the omnibox should take focus and select all text. 117 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, click)); 118 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 119 EXPECT_TRUE(omnibox_view->IsSelectAll()); 120 121 // Clicking in another view should clear focus and the selection. 122 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 123 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 124 EXPECT_FALSE(omnibox_view->IsSelectAll()); 125 126 // Clicking in the omnibox again should take focus and select all text again. 127 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, click)); 128 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 129 EXPECT_TRUE(omnibox_view->IsSelectAll()); 130 131 // Clicking another omnibox spot should keep focus but clear the selection. 132 omnibox_view->SelectAll(false); 133 const gfx::Vector2d click_2(click.x() + 10, click.y()); 134 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click_2, click_2)); 135 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 136 EXPECT_FALSE(omnibox_view->IsSelectAll()); 137 138 // Take the focus away and click in the omnibox again, but drag a bit before 139 // releasing. We should focus the omnibox but not select all of its text. 140 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 141 const gfx::Vector2d release(click.x() + 10, click.y()); 142 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, release)); 143 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 144 EXPECT_FALSE(omnibox_view->IsSelectAll()); 145 146 // Middle-clicking should not be handled by the omnibox. 147 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 148 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::MIDDLE, click, click)); 149 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 150 EXPECT_FALSE(omnibox_view->IsSelectAll()); 151} 152