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 <Carbon/Carbon.h>
8 #import <Cocoa/Cocoa.h>
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #import "chrome/browser/ui/cocoa/view_id_util.h"
16 #include "ui/base/test/ui_controls.h"
18 namespace ui_test_utils {
22 void MoveMouseToNSViewCenterAndPress(
24 ui_controls::MouseButton button,
26 const base::Closure& task) {
27 NSWindow* window = [view window];
28 NSScreen* screen = [window screen];
31 // Converts the center position of the view into the coordinates accepted
32 // by SendMouseMoveNotifyWhenDone() method.
33 NSRect bounds = [view bounds];
34 NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
35 center = [view convertPoint:center toView:nil];
36 center = [window convertBaseToScreen:center];
37 center = NSMakePoint(center.x, [screen frame].size.height - center.y);
39 ui_controls::SendMouseMoveNotifyWhenDone(
41 base::Bind(&internal::ClickTask, button, state, task));
46 bool IsViewFocused(const Browser* browser, ViewID vid) {
47 NSWindow* window = browser->window()->GetNativeWindow();
49 NSView* view = view_id_util::GetView(window, vid);
53 NSResponder* firstResponder = [window firstResponder];
54 if (firstResponder == static_cast<NSResponder*>(view))
57 // Handle special case for VIEW_ID_TAB_CONTAINER. The tab container NSView
58 // always transfers first responder status to its subview, so test whether
59 // |firstResponder| is a descendant.
60 if (vid == VIEW_ID_TAB_CONTAINER &&
61 [firstResponder isKindOfClass:[NSView class]])
62 return [static_cast<NSView*>(firstResponder) isDescendantOf:view];
64 // Handle the special case of focusing a TextField.
65 if ([firstResponder isKindOfClass:[NSTextView class]]) {
66 NSView* delegate = static_cast<NSView*>([(NSTextView*)firstResponder
75 void ClickOnView(const Browser* browser, ViewID vid) {
76 NSWindow* window = browser->window()->GetNativeWindow();
78 NSView* view = view_id_util::GetView(window, vid);
80 MoveMouseToNSViewCenterAndPress(
83 ui_controls::DOWN | ui_controls::UP,
84 base::MessageLoop::QuitClosure());
85 content::RunMessageLoop();
88 void FocusView(const Browser* browser, ViewID vid) {
89 NSWindow* window = browser->window()->GetNativeWindow();
91 NSView* view = view_id_util::GetView(window, vid);
93 [window makeFirstResponder:view];
96 void HideNativeWindow(gfx::NativeWindow window) {
97 [window orderOut:nil];
100 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
101 // Make sure an unbundled program can get the input focus.
102 ProcessSerialNumber psn = { 0, kCurrentProcess };
103 TransformProcessType(&psn,kProcessTransformToForegroundApplication);
104 SetFrontProcess(&psn);
106 [window makeKeyAndOrderFront:nil];
110 } // namespace ui_test_utils