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. 4 5 #include "chrome/test/base/interactive_test_utils.h" 6 7 #include <Carbon/Carbon.h> 8 #import <Cocoa/Cocoa.h> 9 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" 17 18 namespace ui_test_utils { 19 20 namespace { 21 22 void MoveMouseToNSViewCenterAndPress( 23 NSView* view, 24 ui_controls::MouseButton button, 25 int state, 26 const base::Closure& task) { 27 NSWindow* window = [view window]; 28 NSScreen* screen = [window screen]; 29 DCHECK(screen); 30 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); 38 39 ui_controls::SendMouseMoveNotifyWhenDone( 40 center.x, center.y, 41 base::Bind(&internal::ClickTask, button, state, task)); 42 } 43 44 } // namespace 45 46 bool IsViewFocused(const Browser* browser, ViewID vid) { 47 NSWindow* window = browser->window()->GetNativeWindow(); 48 DCHECK(window); 49 NSView* view = view_id_util::GetView(window, vid); 50 if (!view) 51 return false; 52 53 NSResponder* firstResponder = [window firstResponder]; 54 if (firstResponder == static_cast<NSResponder*>(view)) 55 return true; 56 57 // Handle the special case of focusing a TextField. 58 if ([firstResponder isKindOfClass:[NSTextView class]]) { 59 NSView* delegate = static_cast<NSView*>([(NSTextView*)firstResponder 60 delegate]); 61 if (delegate == view) 62 return true; 63 } 64 65 return false; 66 } 67 68 void ClickOnView(const Browser* browser, ViewID vid) { 69 NSWindow* window = browser->window()->GetNativeWindow(); 70 DCHECK(window); 71 NSView* view = view_id_util::GetView(window, vid); 72 DCHECK(view); 73 MoveMouseToNSViewCenterAndPress( 74 view, 75 ui_controls::LEFT, 76 ui_controls::DOWN | ui_controls::UP, 77 base::MessageLoop::QuitClosure()); 78 content::RunMessageLoop(); 79 } 80 81 void FocusView(const Browser* browser, ViewID vid) { 82 NSWindow* window = browser->window()->GetNativeWindow(); 83 DCHECK(window); 84 NSView* view = view_id_util::GetView(window, vid); 85 DCHECK(view); 86 [window makeFirstResponder:view]; 87 } 88 89 void HideNativeWindow(gfx::NativeWindow window) { 90 [window orderOut:nil]; 91 } 92 93 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { 94 // Make sure an unbundled program can get the input focus. 95 ProcessSerialNumber psn = { 0, kCurrentProcess }; 96 TransformProcessType(&psn,kProcessTransformToForegroundApplication); 97 SetFrontProcess(&psn); 98 99 [window makeKeyAndOrderFront:nil]; 100 return true; 101 } 102 103 } // namespace ui_test_utils 104