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.
5 #include "ui/base/cocoa/cocoa_base_utils.h"
7 #import <objc/objc-class.h>
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"
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
21 @implementation TestEvent
22 - (NSUInteger)modifierFlags { return NSShiftKeyMask; }
29 class CocoaBaseUtilsTest : public CocoaTest {
32 TEST_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));
37 // Middle Click = new background tab.
38 me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0);
39 EXPECT_EQ(NEW_BACKGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
41 // Shift+Middle Click = new foreground tab.
43 base::mac::ScopedObjCClassSwizzler swizzler(
44 [NSEvent class], [TestEvent class], @selector(modifierFlags));
45 me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp,
47 EXPECT_EQ(NEW_FOREGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
50 // Cmd+Left Click = new background tab.
51 me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
53 EXPECT_EQ(NEW_BACKGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
55 // Cmd+Shift+Left Click = new foreground tab.
56 me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
59 EXPECT_EQ(NEW_FOREGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
61 // Shift+Left Click = new window
62 me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
64 EXPECT_EQ(NEW_WINDOW, WindowOpenDispositionFromNSEvent(me));