Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2011 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 "ui/base/test/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 cocoa_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* MouseEventWithType(NSEventType type, NSUInteger modifiers) {
     49   return MouseEventAtPoint(NSZeroPoint, 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 NSEvent* KeyEventWithCharacter(unichar c) {
     87   return KeyEventWithKeyCode(0, c, NSKeyDown, 0);
     88 }
     89 
     90 NSEvent* KeyEventWithType(NSEventType event_type, NSUInteger modifiers) {
     91   return KeyEventWithKeyCode(0x78, 'x', event_type, modifiers);
     92 }
     93 
     94 NSEvent* KeyEventWithKeyCode(unsigned short key_code,
     95                              unichar c,
     96                              NSEventType event_type,
     97                              NSUInteger modifiers) {
     98   NSString* chars = [NSString stringWithCharacters:&c length:1];
     99   return [NSEvent keyEventWithType:event_type
    100                           location:NSZeroPoint
    101                      modifierFlags:modifiers
    102                          timestamp:0
    103                       windowNumber:0
    104                            context:nil
    105                         characters:chars
    106        charactersIgnoringModifiers:chars
    107                          isARepeat:NO
    108                            keyCode:key_code];
    109 }
    110 
    111 NSEvent* EnterExitEventWithType(NSEventType event_type) {
    112   return [NSEvent enterExitEventWithType:event_type
    113                                 location:NSZeroPoint
    114                            modifierFlags:0
    115                                timestamp:0
    116                             windowNumber:0
    117                                  context:nil
    118                              eventNumber:0
    119                           trackingNumber:0
    120                                 userData:NULL];
    121 }
    122 
    123 NSEvent* OtherEventWithType(NSEventType event_type) {
    124   return [NSEvent otherEventWithType:event_type
    125                             location:NSZeroPoint
    126                        modifierFlags:0
    127                            timestamp:0
    128                         windowNumber:0
    129                              context:nil
    130                              subtype:0
    131                                data1:0
    132                                data2:0];
    133 }
    134 
    135 }  // namespace cocoa_test_event_utils
    136