| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/base/interactive_test_utils.h" | 5 #include "chrome/test/base/interactive_test_utils.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> | 7 #include <Carbon/Carbon.h> |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 11 #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" | 12 #import "ui/base/test/windowed_nsnotification_observer.h" |
| 19 | 13 |
| 20 namespace ui_test_utils { | 14 namespace ui_test_utils { |
| 21 | 15 |
| 22 namespace { | |
| 23 | |
| 24 void MoveMouseToNSViewCenterAndPress( | |
| 25 NSView* view, | |
| 26 ui_controls::MouseButton button, | |
| 27 int state, | |
| 28 const base::Closure& task) { | |
| 29 NSWindow* window = [view window]; | |
| 30 NSScreen* screen = [window screen]; | |
| 31 DCHECK(screen); | |
| 32 | |
| 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); | |
| 40 | |
| 41 ui_controls::SendMouseMoveNotifyWhenDone( | |
| 42 center.x, center.y, | |
| 43 base::Bind(&internal::ClickTask, button, state, task)); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 bool IsViewFocused(const Browser* browser, ViewID vid) { | |
| 49 NSWindow* window = browser->window()->GetNativeWindow(); | |
| 50 DCHECK(window); | |
| 51 NSView* view = view_id_util::GetView(window, vid); | |
| 52 if (!view) | |
| 53 return false; | |
| 54 | |
| 55 NSResponder* firstResponder = [window firstResponder]; | |
| 56 if (firstResponder == static_cast<NSResponder*>(view)) | |
| 57 return true; | |
| 58 | |
| 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]; | |
| 65 | |
| 66 // Handle the special case of focusing a TextField. | |
| 67 if ([firstResponder isKindOfClass:[NSTextView class]]) { | |
| 68 NSView* delegate = static_cast<NSView*>([(NSTextView*)firstResponder | |
| 69 delegate]); | |
| 70 if (delegate == view) | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 void ClickOnView(const Browser* browser, ViewID vid) { | |
| 78 NSWindow* window = browser->window()->GetNativeWindow(); | |
| 79 DCHECK(window); | |
| 80 NSView* view = view_id_util::GetView(window, vid); | |
| 81 DCHECK(view); | |
| 82 MoveMouseToNSViewCenterAndPress( | |
| 83 view, | |
| 84 ui_controls::LEFT, | |
| 85 ui_controls::DOWN | ui_controls::UP, | |
| 86 base::MessageLoop::QuitWhenIdleClosure()); | |
| 87 content::RunMessageLoop(); | |
| 88 } | |
| 89 | |
| 90 void FocusView(const Browser* browser, ViewID vid) { | |
| 91 NSWindow* window = browser->window()->GetNativeWindow(); | |
| 92 DCHECK(window); | |
| 93 NSView* view = view_id_util::GetView(window, vid); | |
| 94 DCHECK(view); | |
| 95 [window makeFirstResponder:view]; | |
| 96 } | |
| 97 | |
| 98 void HideNativeWindow(gfx::NativeWindow window) { | 16 void HideNativeWindow(gfx::NativeWindow window) { |
| 99 [window orderOut:nil]; | 17 [window orderOut:nil]; |
| 100 } | 18 } |
| 101 | 19 |
| 102 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { | 20 bool ShowAndFocusNativeWindow(gfx::NativeWindow window) { |
| 103 // Make sure an unbundled program can get the input focus. | 21 // Make sure an unbundled program can get the input focus. |
| 104 ProcessSerialNumber psn = { 0, kCurrentProcess }; | 22 ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 105 TransformProcessType(&psn,kProcessTransformToForegroundApplication); | 23 TransformProcessType(&psn,kProcessTransformToForegroundApplication); |
| 106 SetFrontProcess(&psn); | 24 SetFrontProcess(&psn); |
| 107 | 25 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 120 // events are sent via ui_test_utils::SendKeyPressSync. | 38 // events are sent via ui_test_utils::SendKeyPressSync. |
| 121 BOOL notification_observed = [async_waiter wait]; | 39 BOOL notification_observed = [async_waiter wait]; |
| 122 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. | 40 base::RunLoop().RunUntilIdle(); // There may be other events queued. Flush. |
| 123 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; | 41 NSMenu* file_menu = [[[NSApp mainMenu] itemWithTag:IDC_FILE_MENU] submenu]; |
| 124 [[file_menu delegate] menuNeedsUpdate:file_menu]; | 42 [[file_menu delegate] menuNeedsUpdate:file_menu]; |
| 125 | 43 |
| 126 return !async_waiter || notification_observed; | 44 return !async_waiter || notification_observed; |
| 127 } | 45 } |
| 128 | 46 |
| 129 } // namespace ui_test_utils | 47 } // namespace ui_test_utils |
| OLD | NEW |