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#import "ui/base/test/cocoa_helper.h"
13#include "ui/events/event_constants.h"
14#import "ui/events/test/cocoa_test_event_utils.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(WindowOpenDisposition::CURRENT_TAB,
36            WindowOpenDispositionFromNSEvent(me));
37
38  // Middle Click = new background tab.
39  me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0);
40  EXPECT_EQ(WindowOpenDisposition::NEW_BACKGROUND_TAB,
41            WindowOpenDispositionFromNSEvent(me));
42
43  // Shift+Middle Click = new foreground tab.
44  {
45    base::mac::ScopedObjCClassSwizzler swizzler(
46        [NSEvent class], [TestEvent class], @selector(modifierFlags));
47    me = cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp,
48                                                    NSShiftKeyMask);
49    EXPECT_EQ(WindowOpenDisposition::NEW_FOREGROUND_TAB,
50              WindowOpenDispositionFromNSEvent(me));
51  }
52
53  // Cmd+Left Click = new background tab.
54  me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
55                                                  NSCommandKeyMask);
56  EXPECT_EQ(WindowOpenDisposition::NEW_BACKGROUND_TAB,
57            WindowOpenDispositionFromNSEvent(me));
58
59  // Cmd+Shift+Left Click = new foreground tab.
60  me = cocoa_test_event_utils::MouseEventWithType(
61      NSLeftMouseUp, NSCommandKeyMask | NSShiftKeyMask);
62  EXPECT_EQ(WindowOpenDisposition::NEW_FOREGROUND_TAB,
63            WindowOpenDispositionFromNSEvent(me));
64
65  // Shift+Left Click = new window
66  me = cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp,
67                                                  NSShiftKeyMask);
68  EXPECT_EQ(WindowOpenDisposition::NEW_WINDOW,
69            WindowOpenDispositionFromNSEvent(me));
70}
71
72}  // namespace
73
74}  // namespace ui
75