Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2009 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 #import <Cocoa/Cocoa.h>
      6 
      7 #include "chrome/browser/ui/cocoa/test_event_utils.h"
      8 
      9 ScopedClassSwizzler::ScopedClassSwizzler(Class target, Class source,
     10                                          SEL selector) {
     11   old_selector_impl_ = class_getInstanceMethod(target, selector);
     12   new_selector_impl_ = class_getInstanceMethod(source, selector);
     13   method_exchangeImplementations(old_selector_impl_, new_selector_impl_);
     14 }
     15 
     16 ScopedClassSwizzler::~ScopedClassSwizzler() {
     17   method_exchangeImplementations(old_selector_impl_, new_selector_impl_);
     18 }
     19 
     20 namespace test_event_utils {
     21 
     22 NSEvent* MouseEventAtPoint(NSPoint point, NSEventType type,
     23                            NSUInteger modifiers) {
     24   if (type == NSOtherMouseUp) {
     25     // To synthesize middle clicks we need to create a CGEvent with the
     26     // "center" button flags so that our resulting NSEvent will have the
     27     // appropriate buttonNumber field. NSEvent provides no way to create a
     28     // mouse event with a buttonNumber directly.
     29     CGPoint location = { point.x, point.y };
     30     CGEventRef cg_event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseUp,
     31                                                   location,
     32                                                   kCGMouseButtonCenter);
     33     NSEvent* event = [NSEvent eventWithCGEvent:cg_event];
     34     CFRelease(cg_event);
     35     return event;
     36   }
     37   return [NSEvent mouseEventWithType:type
     38                             location:point
     39                        modifierFlags:modifiers
     40                            timestamp:0
     41                         windowNumber:0
     42                              context:nil
     43                          eventNumber:0
     44                           clickCount:1
     45                             pressure:1.0];
     46 }
     47 
     48 NSEvent* MakeMouseEvent(NSEventType type, NSUInteger modifiers) {
     49   return MouseEventAtPoint(NSMakePoint(0, 0), type, modifiers);
     50 }
     51 
     52 static NSEvent* MouseEventAtPointInWindow(NSPoint point,
     53                                           NSEventType type,
     54                                           NSWindow* window,
     55                                           NSUInteger clickCount) {
     56   return [NSEvent mouseEventWithType:type
     57                             location:point
     58                        modifierFlags:0
     59                            timestamp:0
     60                         windowNumber:[window windowNumber]
     61                              context:nil
     62                          eventNumber:0
     63                           clickCount:clickCount
     64                             pressure:1.0];
     65 }
     66 
     67 NSEvent* LeftMouseDownAtPointInWindow(NSPoint point, NSWindow* window) {
     68   return MouseEventAtPointInWindow(point, NSLeftMouseDown, window, 1);
     69 }
     70 
     71 NSEvent* LeftMouseDownAtPoint(NSPoint point) {
     72   return LeftMouseDownAtPointInWindow(point, nil);
     73 }
     74 
     75 std::pair<NSEvent*,NSEvent*> MouseClickInView(NSView* view,
     76                                               NSUInteger clickCount) {
     77   const NSRect bounds = [view convertRect:[view bounds] toView:nil];
     78   const NSPoint mid_point = NSMakePoint(NSMidX(bounds), NSMidY(bounds));
     79   NSEvent* down = MouseEventAtPointInWindow(mid_point, NSLeftMouseDown,
     80                                             [view window], clickCount);
     81   NSEvent* up = MouseEventAtPointInWindow(mid_point, NSLeftMouseUp,
     82                                           [view window], clickCount);
     83   return std::make_pair(down, up);
     84 }
     85 
     86 }  // namespace test_event_utils
     87