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