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"
18 #import "ui/base/test/windowed_nsnotification_observer.h"
20 namespace ui_test_utils {
24 void MoveMouseToNSViewCenterAndPress(
26 ui_controls::MouseButton button,
28 const base::Closure& task) {
29 NSWindow* window = [view window];
30 NSScreen* screen = [window screen];
33 // Converts the center position of the view into the coordinates accepted
34 // by SendMouseMoveNotifyWhenDone() method.
35 NSRect bounds = [view bounds];
36 NSPoint center = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
37 center = [view convertPoint:center toView:nil];
38 center = [window convertBaseToScreen:center];
39 center = NSMakePoint(center.x, [screen frame].size.height - center.y);
41 ui_controls::SendMouseMoveNotifyWhenDone(
43 base::Bind(&internal::ClickTask, button, state, task));
48 bool IsViewFocused(const Browser* browser, ViewID vid) {
49 NSWindow* window = browser->window()->GetNativeWindow();
51 NSView* view = view_id_util::GetView(window, vid);
55 NSResponder* firstResponder = [window firstResponder];
56 if (firstResponder == static_cast<NSResponder*>(view))
59 // Handle special case for VIEW_ID_TAB_CONTAINER. The tab container NSView
60 // always transfers first responder status to its subview, so test whether
61 // |firstResponder| is a descendant.
62 if (vid == VIEW_ID_TAB_CONTAINER &&
63 [firstResponder isKindOfClass:[NSView class]])
64 return [static_cast<NSView*>(firstResponder) isDescendantOf:view];
66 // Handle the special case of focusing a TextField.
67 if ([firstResponder isKindOfClass:[NSTextView class]]) {
68 NSView* delegate = static_cast<NSView*>([(NSTextView*)firstResponder
77 void ClickOnView(const Browser* browser, ViewID vid) {
78 NSWindow* window = browser->window()->GetNativeWindow();
80 NSView* view = view_id_util::GetView(window, vid);
82 MoveMouseToNSViewCenterAndPress(
85 ui_controls::DOWN | ui_controls::UP,
86 base::MessageLoop::QuitClosure());
87 content::RunMessageLoop();
90 void FocusView(const Browser* browser, ViewID vid) {
91 NSWindow* window = browser->window()->GetNativeWindow();
93 NSView* view = view_id_util::GetView(window, vid);
95 [window makeFirstResponder:view];
98 void HideNativeWindow(gfx::NativeWindow window) {
99 [window orderOut:nil];
102 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) {
103 // Make sure an unbundled program can get the input focus.
104 ProcessSerialNumber psn = { 0, kCurrentProcess };
105 TransformProcessType(&psn,kProcessTransformToForegroundApplication);
106 SetFrontProcess(&psn);
108 base::scoped_nsobject<WindowedNSNotificationObserver> async_waiter;
109 if (![window isKeyWindow]) {
110 // Only wait when expecting a change to actually occur.
111 async_waiter.reset([[WindowedNSNotificationObserver alloc]
112 initForNotification:NSWindowDidBecomeKeyNotification
115 [window makeKeyAndOrderFront:nil];
117 // Wait until |window| becomes key window, then make sure the shortcuts for
118 // "Close Window" and "Close Tab" are updated.
119 // This is because normal AppKit menu updating does not get invoked when
120 // events are sent via ui_test_utils::SendKeyPressSync.
121 BOOL notification_observed = [async_waiter wait];
122 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush.
123 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu];
124 [[file_menu delegate] menuNeedsUpdate:file_menu];
126 return !async_waiter || notification_observed;
129 } // namespace ui_test_utils