[Chromoting] Update size to use inner/outer bounds
[chromium-blink-merge.git] / chrome / test / base / interactive_test_utils_mac.mm
blob424f98afd9a84143e38521cb3d1aea03b65c9b9a
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 {
20 namespace {
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);
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(
40       center.x, center.y,
41       base::Bind(&internal::ClickTask, button, state, task));
44 }  // namespace
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;
53   NSResponder* firstResponder = [window firstResponder];
54   if (firstResponder == static_cast<NSResponder*>(view))
55     return true;
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
67                                                           delegate]);
68     if (delegate == view)
69       return true;
70   }
72   return false;
75 void ClickOnView(const Browser* browser, ViewID vid) {
76   NSWindow* window = browser->window()->GetNativeWindow();
77   DCHECK(window);
78   NSView* view = view_id_util::GetView(window, vid);
79   DCHECK(view);
80   MoveMouseToNSViewCenterAndPress(
81       view,
82       ui_controls::LEFT,
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();
90    DCHECK(window);
91    NSView* view = view_id_util::GetView(window, vid);
92    DCHECK(view);
93    [window makeFirstResponder:view];
94  }
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];
107   return true;
110 }  // namespace ui_test_utils