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/app/chrome_command_ids.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #import "chrome/browser/ui/cocoa/view_id_util.h"
17 #include "ui/base/test/ui_controls.h"
19 namespace ui_test_utils {
23 void MoveMouseToNSViewCenterAndPress(
25 ui_controls::MouseButton button,
27 const base::Closure& task) {
28 NSWindow* window = [view window];
29 NSScreen* screen = [window screen];
32 // Converts the center position of the view into the coordinates accepted
33 // by SendMouseMoveNotifyWhenDone() method.
34 NSRect bounds = [view bounds];
35 NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
36 center = [view convertPoint:center toView:nil];
37 center = [window convertBaseToScreen:center];
38 center = NSMakePoint(center.x, [screen frame].size.height - center.y);
40 ui_controls::SendMouseMoveNotifyWhenDone(
42 base::Bind(&internal::ClickTask, button, state, task));
47 bool IsViewFocused(const Browser* browser, ViewID vid) {
48 NSWindow* window = browser->window()->GetNativeWindow();
50 NSView* view = view_id_util::GetView(window, vid);
54 NSResponder* firstResponder = [window firstResponder];
55 if (firstResponder == static_cast<NSResponder*>(view))
58 // Handle special case for VIEW_ID_TAB_CONTAINER. The tab container NSView
59 // always transfers first responder status to its subview, so test whether
60 // |firstResponder| is a descendant.
61 if (vid == VIEW_ID_TAB_CONTAINER &&
62 [firstResponder isKindOfClass:[NSView class]])
63 return [static_cast<NSView*>(firstResponder) isDescendantOf:view];
65 // Handle the special case of focusing a TextField.
66 if ([firstResponder isKindOfClass:[NSTextView class]]) {
67 NSView* delegate = static_cast<NSView*>([(NSTextView*)firstResponder
76 void ClickOnView(const Browser* browser, ViewID vid) {
77 NSWindow* window = browser->window()->GetNativeWindow();
79 NSView* view = view_id_util::GetView(window, vid);
81 MoveMouseToNSViewCenterAndPress(
84 ui_controls::DOWN | ui_controls::UP,
85 base::MessageLoop::QuitClosure());
86 content::RunMessageLoop();
89 void FocusView(const Browser* browser, ViewID vid) {
90 NSWindow* window = browser->window()->GetNativeWindow();
92 NSView* view = view_id_util::GetView(window, vid);
94 [window makeFirstResponder:view];
97 void HideNativeWindow(gfx::NativeWindow window) {
98 [window orderOut:nil];
101 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
102 // Make sure an unbundled program can get the input focus.
103 ProcessSerialNumber psn = { 0, kCurrentProcess };
104 TransformProcessType(&psn,kProcessTransformToForegroundApplication);
105 SetFrontProcess(&psn);
107 [window makeKeyAndOrderFront:nil];
109 // Wait until |window| becomes key window, then make sure the shortcuts for
110 // "Close Window" and "Close Tab" are updated.
111 // This is because normal AppKit menu updating does not get invoked when
112 // events are sent via ui_test_utils::SendKeyPressSync.
113 base::RunLoop().RunUntilIdle();
114 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu];
115 [[file_menu delegate] menuNeedsUpdate:file_menu];
120 } // namespace ui_test_utils