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 <objc/objc-class.h>
      6 
      7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
      8 #include "chrome/browser/ui/cocoa/event_utils.h"
      9 #include "chrome/browser/ui/cocoa/test_event_utils.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "testing/platform_test.h"
     12 
     13 // We provide a donor class with a specially modified |modifierFlags|
     14 // implementation that we swap with NSEvent's. This is because we can't create a
     15 // NSEvent that represents a middle click with modifiers.
     16 @interface TestEvent : NSObject
     17 @end
     18 @implementation TestEvent
     19 - (NSUInteger)modifierFlags { return NSShiftKeyMask; }
     20 @end
     21 
     22 namespace {
     23 
     24 class EventUtilsTest : public CocoaTest {
     25 };
     26 
     27 TEST_F(EventUtilsTest, TestWindowOpenDispositionFromNSEvent) {
     28   // Left Click = same tab.
     29   NSEvent* me = test_event_utils::MakeMouseEvent(NSLeftMouseUp, 0);
     30   EXPECT_EQ(CURRENT_TAB, event_utils::WindowOpenDispositionFromNSEvent(me));
     31 
     32   // Middle Click = new background tab.
     33   me = test_event_utils::MakeMouseEvent(NSOtherMouseUp, 0);
     34   EXPECT_EQ(NEW_BACKGROUND_TAB,
     35             event_utils::WindowOpenDispositionFromNSEvent(me));
     36 
     37   // Shift+Middle Click = new foreground tab.
     38   {
     39     ScopedClassSwizzler swizzler([NSEvent class], [TestEvent class],
     40                                  @selector(modifierFlags));
     41     me = test_event_utils::MakeMouseEvent(NSOtherMouseUp, NSShiftKeyMask);
     42     EXPECT_EQ(NEW_FOREGROUND_TAB,
     43               event_utils::WindowOpenDispositionFromNSEvent(me));
     44   }
     45 
     46   // Cmd+Left Click = new background tab.
     47   me = test_event_utils::MakeMouseEvent(NSLeftMouseUp, NSCommandKeyMask);
     48   EXPECT_EQ(NEW_BACKGROUND_TAB,
     49             event_utils::WindowOpenDispositionFromNSEvent(me));
     50 
     51   // Cmd+Shift+Left Click = new foreground tab.
     52   me = test_event_utils::MakeMouseEvent(NSLeftMouseUp, NSCommandKeyMask | NSShiftKeyMask);
     53   EXPECT_EQ(NEW_FOREGROUND_TAB,
     54             event_utils::WindowOpenDispositionFromNSEvent(me));
     55 
     56   // Shift+Left Click = new window
     57   me = test_event_utils::MakeMouseEvent(NSLeftMouseUp, NSShiftKeyMask);
     58   EXPECT_EQ(NEW_WINDOW, event_utils::WindowOpenDispositionFromNSEvent(me));
     59 }
     60 
     61 }  // namespace
     62