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 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
11 #include "ui/events/event_constants.h"
12 #import "ui/events/test/cocoa_test_event_utils.h"
13 #import "ui/gfx/test/ui_cocoa_test_helper.h"
15 // We provide a donor class with a specially modified |modifierFlags|
16 // implementation that we swap with NSEvent's. This is because we can't create a
17 // NSEvent that represents a middle click with modifiers.
18 @interface TestEvent : NSObject
20 @implementation TestEvent
21 - (NSUInteger)modifierFlags { return NSShiftKeyMask; }
28 class CocoaBaseUtilsTest : public CocoaTest {
31 TEST_F(CocoaBaseUtilsTest, WindowOpenDispositionFromNSEvent) {
32 // Left Click = same tab.
33 NSEvent* me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0);
34 EXPECT_EQ(CURRENT_TAB, WindowOpenDispositionFromNSEvent(me));
36 // Middle Click = new background tab.
37 me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0);
38 EXPECT_EQ(NEW_BACKGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
40 // Shift+Middle Click = new foreground tab.
42 ScopedClassSwizzler swizzler([NSEvent class], [TestEvent class],
43 @selector(modifierFlags));
44 me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp,
46 EXPECT_EQ(NEW_FOREGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
49 // Cmd+Left Click = new background tab.
50 me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
52 EXPECT_EQ(NEW_BACKGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
54 // Cmd+Shift+Left Click = new foreground tab.
55 me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
58 EXPECT_EQ(NEW_FOREGROUND_TAB, WindowOpenDispositionFromNSEvent(me));
60 // Shift+Left Click = new window
61 me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
63 EXPECT_EQ(NEW_WINDOW, WindowOpenDispositionFromNSEvent(me));