Home | History | Annotate | Download | only in cocoa
      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 "testing/gtest/include/gtest/gtest.h"
      8 #include "ui/base/events/event_constants.h"
      9 #include "ui/base/events/event_utils.h"
     10 #include "ui/base/test/cocoa_test_event_utils.h"
     11 #import "ui/base/test/ui_cocoa_test_helper.h"
     12 #include "ui/gfx/point.h"
     13 
     14 using namespace cocoa_test_event_utils;
     15 
     16 namespace {
     17 
     18 class EventsMacTest : public ui::CocoaTest {
     19  public:
     20   EventsMacTest() {}
     21 
     22  private:
     23   DISALLOW_COPY_AND_ASSIGN(EventsMacTest);
     24 };
     25 
     26 TEST_F(EventsMacTest, EventTypeFromNative) {
     27   NSEvent* native_event = nil;
     28   ui::EventType type = ui::ET_UNKNOWN;
     29 
     30   native_event = MouseEventWithType(NSLeftMouseDown, 0);
     31   type = ui::EventTypeFromNative(native_event);
     32   EXPECT_EQ(ui::ET_MOUSE_PRESSED, type);
     33 
     34   native_event = MouseEventWithType(NSLeftMouseUp, 0);
     35   type = ui::EventTypeFromNative(native_event);
     36   EXPECT_EQ(ui::ET_MOUSE_RELEASED, type);
     37 
     38   native_event = MouseEventWithType(NSRightMouseDown, 0);
     39   type = ui::EventTypeFromNative(native_event);
     40   EXPECT_EQ(ui::ET_MOUSE_PRESSED, type);
     41 
     42   native_event = MouseEventWithType(NSRightMouseUp, 0);
     43   type = ui::EventTypeFromNative(native_event);
     44   EXPECT_EQ(ui::ET_MOUSE_RELEASED, type);
     45 
     46   native_event = MouseEventWithType(NSMouseMoved, 0);
     47   type = ui::EventTypeFromNative(native_event);
     48   EXPECT_EQ(ui::ET_MOUSE_MOVED, type);
     49 
     50   native_event = MouseEventWithType(NSLeftMouseDragged, 0);
     51   type = ui::EventTypeFromNative(native_event);
     52   EXPECT_EQ(ui::ET_MOUSE_DRAGGED, type);
     53 
     54   native_event = MouseEventWithType(NSRightMouseDragged, 0);
     55   type = ui::EventTypeFromNative(native_event);
     56   EXPECT_EQ(ui::ET_MOUSE_DRAGGED, type);
     57 
     58   native_event = EnterExitEventWithType(NSMouseEntered);
     59   type = ui::EventTypeFromNative(native_event);
     60   EXPECT_EQ(ui::ET_MOUSE_ENTERED, type);
     61 
     62   native_event = EnterExitEventWithType(NSMouseExited);
     63   type = ui::EventTypeFromNative(native_event);
     64   EXPECT_EQ(ui::ET_MOUSE_EXITED, type);
     65 
     66   native_event = KeyEventWithType(NSKeyDown, 0);
     67   type = ui::EventTypeFromNative(native_event);
     68   EXPECT_EQ(ui::ET_KEY_PRESSED, type);
     69 
     70   native_event = KeyEventWithType(NSKeyUp, 0);
     71   type = ui::EventTypeFromNative(native_event);
     72   EXPECT_EQ(ui::ET_KEY_RELEASED, type);
     73 
     74   native_event = KeyEventWithType(NSFlagsChanged, 0);
     75   type = ui::EventTypeFromNative(native_event);
     76   EXPECT_EQ(ui::ET_KEY_PRESSED, type);
     77 
     78   native_event = OtherEventWithType(NSAppKitDefined);
     79   type = ui::EventTypeFromNative(native_event);
     80   EXPECT_EQ(ui::ET_UNKNOWN, type);
     81 
     82   native_event = OtherEventWithType(NSSystemDefined);
     83   type = ui::EventTypeFromNative(native_event);
     84   EXPECT_EQ(ui::ET_UNKNOWN, type);
     85 
     86   native_event = OtherEventWithType(NSApplicationDefined);
     87   type = ui::EventTypeFromNative(native_event);
     88   EXPECT_EQ(ui::ET_UNKNOWN, type);
     89 
     90   native_event = OtherEventWithType(NSPeriodic);
     91   type = ui::EventTypeFromNative(native_event);
     92   EXPECT_EQ(ui::ET_UNKNOWN, type);
     93 
     94   native_event = EnterExitEventWithType(NSCursorUpdate);
     95   type = ui::EventTypeFromNative(native_event);
     96   EXPECT_EQ(ui::ET_UNKNOWN, type);
     97 
     98   native_event = MouseEventWithType(NSOtherMouseDown, 0);
     99   type = ui::EventTypeFromNative(native_event);
    100   EXPECT_EQ(ui::ET_MOUSE_PRESSED, type);
    101 
    102   native_event = MouseEventWithType(NSOtherMouseUp, 0);
    103   type = ui::EventTypeFromNative(native_event);
    104   EXPECT_EQ(ui::ET_MOUSE_RELEASED, type);
    105 
    106   native_event = MouseEventWithType(NSOtherMouseDragged, 0);
    107   type = ui::EventTypeFromNative(native_event);
    108   EXPECT_EQ(ui::ET_MOUSE_DRAGGED, type);
    109 }
    110 
    111 TEST_F(EventsMacTest, EventFlagsFromNative) {
    112   NSEvent* native_event = nil;
    113   int flags = 0;
    114 
    115   // No key flags.
    116   native_event = KeyEventWithType(NSKeyDown, 0);
    117   flags = ui::EventFlagsFromNative(native_event);
    118   EXPECT_EQ(0, flags);
    119 
    120   // All key flags.
    121   native_event = KeyEventWithType(NSKeyDown,
    122                                   NSAlphaShiftKeyMask |
    123                                       NSShiftKeyMask |
    124                                       NSControlKeyMask |
    125                                       NSAlternateKeyMask |
    126                                       NSCommandKeyMask);
    127   flags = ui::EventFlagsFromNative(native_event);
    128   EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN |
    129                 ui::EF_SHIFT_DOWN |
    130                 ui::EF_CONTROL_DOWN |
    131                 ui::EF_ALT_DOWN |
    132                 ui::EF_COMMAND_DOWN,
    133             flags);
    134 
    135   // Left mouse flags.
    136   native_event = MouseEventWithType(NSLeftMouseDown, 0);
    137   flags = ui::EventFlagsFromNative(native_event);
    138   EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, flags);
    139 
    140   // Right mouse flags.
    141   native_event = MouseEventWithType(NSRightMouseDown, 0);
    142   flags = ui::EventFlagsFromNative(native_event);
    143   EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, flags);
    144 
    145   // Center mouse flags.
    146   native_event = MouseEventWithType(NSOtherMouseDown, 0);
    147   flags = ui::EventFlagsFromNative(native_event);
    148   EXPECT_EQ(ui::EF_MIDDLE_MOUSE_BUTTON, flags);
    149 }
    150 
    151 TEST_F(EventsMacTest, EventLocationFromNative) {
    152   NSWindow* window = [[NSWindow alloc]
    153                        initWithContentRect:NSMakeRect(10, 25, 200, 205)
    154                                  styleMask:NSTitledWindowMask
    155                                    backing:NSBackingStoreBuffered
    156                                      defer:NO];
    157   [window orderFront:nil];
    158 
    159   // Construct mouse event from window number having position at bottom-left.
    160   // Cocoa origin is at bottom-left.  ui::Event origin is top-left.
    161   NSEvent* native_event =
    162       [NSEvent mouseEventWithType:NSLeftMouseDown
    163                          location:NSMakePoint(10, 15)
    164                     modifierFlags:0
    165                         timestamp:[NSDate timeIntervalSinceReferenceDate]
    166                      windowNumber:[window windowNumber]
    167                           context:[window graphicsContext]
    168                       eventNumber:0
    169                        clickCount:1
    170                          pressure:0.0f];
    171 
    172   // Expect resulting event to be positioned relative to top-left.
    173   gfx::Point location = ui::EventLocationFromNative(native_event);
    174   EXPECT_EQ(gfx::Point(10, 190), location);
    175 
    176   [window orderOut:nil];
    177   [window close];
    178 }
    179 
    180 TEST_F(EventsMacTest, KeyboardCodeFromNative) {
    181   NSEvent* native_event = nil;
    182   ui::KeyboardCode code = ui::VKEY_UNKNOWN;
    183 
    184   // Simple "x".  No modifiers.
    185   native_event =
    186       [NSEvent keyEventWithType:NSKeyDown
    187                        location:NSZeroPoint
    188                   modifierFlags:0
    189                       timestamp:[NSDate timeIntervalSinceReferenceDate]
    190                    windowNumber:0
    191                         context:nil
    192                      characters:@"x"
    193                charactersIgnoringModifiers:@"x"
    194                       isARepeat:NO
    195                         keyCode:0x58];
    196   code = ui::KeyboardCodeFromNative(native_event);
    197   EXPECT_EQ(ui::VKEY_X, code);
    198 
    199   // Option/Alt "x".
    200   native_event =
    201       [NSEvent keyEventWithType:NSKeyDown
    202                        location:NSZeroPoint
    203                   modifierFlags:NSAlternateKeyMask
    204                       timestamp:[NSDate timeIntervalSinceReferenceDate]
    205                    windowNumber:0
    206                         context:nil
    207                      characters:@"\0x2248" // opt-x
    208                charactersIgnoringModifiers:@"x"
    209                       isARepeat:NO
    210                         keyCode:0x58];
    211   code = ui::KeyboardCodeFromNative(native_event);
    212   EXPECT_EQ(ui::VKEY_X, code);
    213 
    214   // Option/Alt (alone).
    215   native_event =
    216       [NSEvent keyEventWithType:NSFlagsChanged
    217                        location:NSZeroPoint
    218                   modifierFlags:NSAlternateKeyMask
    219                       timestamp:[NSDate timeIntervalSinceReferenceDate]
    220                    windowNumber:0
    221                         context:nil
    222                      characters:@""
    223                charactersIgnoringModifiers:@""
    224                       isARepeat:NO
    225                         keyCode:0x3a];
    226   code = ui::KeyboardCodeFromNative(native_event);
    227   EXPECT_EQ(ui::VKEY_MENU, code);
    228 }
    229 
    230 TEST_F(EventsMacTest, IsMouseEvent) {
    231   NSEvent* native_event = nil;
    232 
    233   native_event = MouseEventWithType(NSLeftMouseDown, 0);
    234   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    235 
    236   native_event = MouseEventWithType(NSLeftMouseUp, 0);
    237   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    238 
    239   native_event = MouseEventWithType(NSRightMouseDown, 0);
    240   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    241 
    242   native_event = MouseEventWithType(NSRightMouseUp, 0);
    243   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    244 
    245   native_event = MouseEventWithType(NSMouseMoved, 0);
    246   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    247 
    248   native_event = MouseEventWithType(NSLeftMouseDragged, 0);
    249   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    250 
    251   native_event = MouseEventWithType(NSRightMouseDragged, 0);
    252   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    253 
    254   native_event = EnterExitEventWithType(NSMouseEntered);
    255   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    256 
    257   native_event = EnterExitEventWithType(NSMouseExited);
    258   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    259 
    260   native_event = KeyEventWithType(NSKeyDown, 0);
    261   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    262 
    263   native_event = KeyEventWithType(NSKeyUp, 0);
    264   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    265 
    266   native_event = KeyEventWithType(NSFlagsChanged, 0);
    267   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    268 
    269   native_event = OtherEventWithType(NSAppKitDefined);
    270   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    271 
    272   native_event = OtherEventWithType(NSSystemDefined);
    273   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    274 
    275   native_event = OtherEventWithType(NSApplicationDefined);
    276   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    277 
    278   native_event = OtherEventWithType(NSPeriodic);
    279   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    280 
    281   native_event = EnterExitEventWithType(NSCursorUpdate);
    282   EXPECT_FALSE(ui::IsMouseEvent(native_event));
    283 
    284   native_event = MouseEventWithType(NSOtherMouseDown, 0);
    285   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    286 
    287   native_event = MouseEventWithType(NSOtherMouseUp, 0);
    288   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    289 
    290   native_event = MouseEventWithType(NSOtherMouseDragged, 0);
    291   EXPECT_TRUE(ui::IsMouseEvent(native_event));
    292 }
    293 
    294 TEST_F(EventsMacTest, CreateNoopEvent) {
    295   NSEvent* native_event = ui::CreateNoopEvent();
    296   EXPECT_TRUE(native_event != nil);
    297 }
    298 
    299 }  // namespace
    300