Home | History | Annotate | Download | only in cocoa
      1 // Copyright 2013 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 <objc/objc-class.h>
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 #include "testing/platform_test.h"
      9 #include "ui/base/cocoa/cocoa_event_utils.h"
     10 #include "ui/base/events/event_constants.h"
     11 #import "ui/base/test/cocoa_test_event_utils.h"
     12 #import "ui/base/test/ui_cocoa_test_helper.h"
     13 
     14 // We provide a donor class with a specially modified |modifierFlags|
     15 // implementation that we swap with NSEvent's. This is because we can't create a
     16 // NSEvent that represents a middle click with modifiers.
     17 @interface TestEvent : NSObject
     18 @end
     19 @implementation TestEvent
     20 - (NSUInteger)modifierFlags { return NSShiftKeyMask; }
     21 @end
     22 
     23 namespace ui {
     24 
     25 namespace {
     26 
     27 class EventUtilsTest : public CocoaTest {
     28 };
     29 
     30 TEST_F(EventUtilsTest, TestWindowOpenDispositionFromNSEvent) {
     31   // Left Click = same tab.
     32   NSEvent* me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0);
     33   EXPECT_EQ(CURRENT_TAB, WindowOpenDispositionFromNSEvent(me));
     34 
     35   // Middle Click = new background tab.
     36   me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0);
     37   EXPECT_EQ(NEW_BACKGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
     38 
     39   // Shift+Middle Click = new foreground tab.
     40   {
     41     ScopedClassSwizzler swizzler([NSEvent class], [TestEvent class],
     42                                  @selector(modifierFlags));
     43     me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp,
     44                                                     NSShiftKeyMask);
     45     EXPECT_EQ(NEW_FOREGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
     46   }
     47 
     48   // Cmd+Left Click = new background tab.
     49   me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
     50                                                   NSCommandKeyMask);
     51   EXPECT_EQ(NEW_BACKGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
     52 
     53   // Cmd+Shift+Left Click = new foreground tab.
     54   me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
     55                                               NSCommandKeyMask |
     56                                               NSShiftKeyMask);
     57   EXPECT_EQ(NEW_FOREGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
     58 
     59   // Shift+Left Click = new window
     60   me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
     61                                                   NSShiftKeyMask);
     62   EXPECT_EQ(NEW_WINDOW, WindowOpenDispositionFromNSEvent(me));
     63 }
     64 
     65 
     66 TEST_F(EventUtilsTest, TestEventFlagsFromNSEvent) {
     67   // Left click.
     68   NSEvent* left = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0);
     69   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON, EventFlagsFromNSEvent(left));
     70 
     71   // Right click.
     72   NSEvent* right = cocoa_test_event_utils::MouseEventWithType(NSRightMouseUp,
     73                                                               0);
     74   EXPECT_EQ(EF_RIGHT_MOUSE_BUTTON, EventFlagsFromNSEvent(right));
     75 
     76   // Middle click.
     77   NSEvent* middle = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp,
     78                                                                0);
     79   EXPECT_EQ(EF_MIDDLE_MOUSE_BUTTON, EventFlagsFromNSEvent(middle));
     80 
     81   // Caps + Left
     82   NSEvent* caps =
     83       cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
     84                                                  NSAlphaShiftKeyMask);
     85   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_CAPS_LOCK_DOWN,
     86             EventFlagsFromNSEvent(caps));
     87 
     88   // Shift + Left
     89   NSEvent* shift = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
     90                                                               NSShiftKeyMask);
     91   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_SHIFT_DOWN, EventFlagsFromNSEvent(shift));
     92 
     93   // Ctrl + Left
     94   NSEvent* ctrl = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
     95                                                              NSControlKeyMask);
     96   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_CONTROL_DOWN,
     97             EventFlagsFromNSEvent(ctrl));
     98 
     99   // Alt + Left
    100   NSEvent* alt = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
    101                                                             NSAlternateKeyMask);
    102   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_ALT_DOWN, EventFlagsFromNSEvent(alt));
    103 
    104   // Cmd + Left
    105   NSEvent* cmd = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
    106                                                             NSCommandKeyMask);
    107   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_COMMAND_DOWN, EventFlagsFromNSEvent(cmd));
    108 
    109   // Shift + Ctrl + Left
    110   NSEvent* shiftctrl =
    111       cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
    112                                                  NSShiftKeyMask |
    113                                                  NSControlKeyMask);
    114   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_SHIFT_DOWN | EF_CONTROL_DOWN,
    115             EventFlagsFromNSEvent(shiftctrl));
    116 
    117   // Cmd + Alt + Right
    118   NSEvent* cmdalt =
    119       cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
    120                                                  NSCommandKeyMask |
    121                                                  NSAlternateKeyMask);
    122   EXPECT_EQ(EF_LEFT_MOUSE_BUTTON | EF_COMMAND_DOWN | EF_ALT_DOWN,
    123             EventFlagsFromNSEvent(cmdalt));
    124 }
    125 
    126 }  // namespace
    127 
    128 }  // namespace ui
    129