Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / chrome / test / base / interactive_test_utils_views.cc
blobac5047fa6fc3204231e7ce58185e7fa893490676
1 // Copyright (c) 2012 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.
5 #include "chrome/test/base/interactive_test_utils.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/views/frame/browser_view.h"
11 #include "ui/base/test/ui_controls.h"
12 #include "ui/compositor/layer.h"
13 #include "ui/compositor/layer_animator.h"
14 #include "ui/views/focus/focus_manager.h"
16 namespace ui_test_utils {
18 // Until the whole browser UI is ported to tookit-views on Mac, these need to
19 // use the definitions in interactive_test_utils_mac.mm.
20 #if !defined(OS_MACOSX)
22 bool IsViewFocused(const Browser* browser, ViewID vid) {
23 BrowserWindow* browser_window = browser->window();
24 DCHECK(browser_window);
25 gfx::NativeWindow window = browser_window->GetNativeWindow();
26 DCHECK(window);
27 const views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
28 DCHECK(widget);
29 const views::FocusManager* focus_manager = widget->GetFocusManager();
30 DCHECK(focus_manager);
31 DCHECK(focus_manager->GetFocusedView());
32 return focus_manager->GetFocusedView()->id() == vid;
35 void ClickOnView(const Browser* browser, ViewID vid) {
36 views::View* view =
37 BrowserView::GetBrowserViewForBrowser(browser)->GetViewByID(vid);
38 DCHECK(view);
39 MoveMouseToCenterAndPress(view,
40 ui_controls::LEFT,
41 ui_controls::DOWN | ui_controls::UP,
42 base::MessageLoop::QuitClosure());
43 content::RunMessageLoop();
46 void FocusView(const Browser* browser, ViewID vid) {
47 views::View* view =
48 BrowserView::GetBrowserViewForBrowser(browser)->GetViewByID(vid);
49 DCHECK(view);
50 view->RequestFocus();
53 #endif // defined(OS_MACOSX)
55 void MoveMouseToCenterAndPress(views::View* view,
56 ui_controls::MouseButton button,
57 int state,
58 const base::Closure& closure) {
59 DCHECK(view);
60 DCHECK(view->GetWidget());
61 // Complete any in-progress animation before sending the events so that the
62 // mouse-event targetting happens reliably, and does not flake because of
63 // unreliable animation state.
64 ui::Layer* layer = view->GetWidget()->GetLayer();
65 if (layer) {
66 ui::LayerAnimator* animator = layer->GetAnimator();
67 if (animator && animator->is_animating())
68 animator->StopAnimating();
71 gfx::Point view_center(view->width() / 2, view->height() / 2);
72 views::View::ConvertPointToScreen(view, &view_center);
73 ui_controls::SendMouseMoveNotifyWhenDone(
74 view_center.x(),
75 view_center.y(),
76 base::Bind(&internal::ClickTask, button, state, closure));
79 } // namespace ui_test_utils