OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #import "base/memory/scoped_nsobject.h" | 7 #import "base/memory/scoped_nsobject.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 9 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
10 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h" | 10 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h" |
11 #import "chrome/browser/ui/cocoa/tabs/tab_controller_target.h" | 11 #import "chrome/browser/ui/cocoa/tabs/tab_controller_target.h" |
12 #import "chrome/browser/ui/cocoa/tabs/tab_strip_drag_controller.h" | 12 #import "chrome/browser/ui/cocoa/tabs/tab_strip_drag_controller.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #import "testing/gtest_mac.h" | 14 #import "testing/gtest_mac.h" |
15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 16 #import "third_party/ocmock/OCMock/OCMock.h" |
16 | 17 |
17 // Implements the target interface for the tab, which gets sent messages when | 18 // Implements the target interface for the tab, which gets sent messages when |
18 // the tab is clicked on by the user and when its close box is clicked. | 19 // the tab is clicked on by the user and when its close box is clicked. |
19 @interface TabControllerTestTarget : NSObject<TabControllerTarget> { | 20 @interface TabControllerTestTarget : NSObject<TabControllerTarget> { |
20 @private | 21 @private |
21 bool selected_; | 22 bool selected_; |
22 bool closed_; | 23 bool closed_; |
| 24 bool closedOtherTabs_; |
23 scoped_nsobject<TabStripDragController> dragController_; | 25 scoped_nsobject<TabStripDragController> dragController_; |
24 } | 26 } |
25 - (bool)selected; | 27 - (bool)selected; |
26 - (bool)closed; | 28 - (bool)closed; |
| 29 - (bool)closedOtherTabs; |
27 @end | 30 @end |
28 | 31 |
29 @implementation TabControllerTestTarget | 32 @implementation TabControllerTestTarget |
30 - (id)init { | 33 - (id)init { |
31 if ((self = [super init])) { | 34 if ((self = [super init])) { |
32 dragController_.reset( | 35 dragController_.reset( |
33 [[TabStripDragController alloc] initWithTabStripController:nil]); | 36 [[TabStripDragController alloc] initWithTabStripController:nil]); |
34 } | 37 } |
35 return self; | 38 return self; |
36 } | 39 } |
37 - (bool)selected { | 40 - (bool)selected { |
38 return selected_; | 41 return selected_; |
39 } | 42 } |
40 - (bool)closed { | 43 - (bool)closed { |
41 return closed_; | 44 return closed_; |
42 } | 45 } |
| 46 - (bool)closedOtherTabs { |
| 47 return closedOtherTabs_; |
| 48 } |
43 - (void)selectTab:(id)sender { | 49 - (void)selectTab:(id)sender { |
44 selected_ = true; | 50 selected_ = true; |
45 } | 51 } |
46 - (void)closeTab:(id)sender { | 52 - (void)closeTab:(id)sender { |
47 closed_ = true; | 53 closed_ = true; |
48 } | 54 } |
| 55 - (void)closeOtherTabs:(id)sender { |
| 56 closedOtherTabs_ = true; |
| 57 } |
49 - (void)mouseTimer:(NSTimer*)timer { | 58 - (void)mouseTimer:(NSTimer*)timer { |
50 // Fire the mouseUp to break the TabView drag loop. | 59 // Fire the mouseUp to break the TabView drag loop. |
51 NSEvent* current = [NSApp currentEvent]; | 60 NSEvent* current = [NSApp currentEvent]; |
52 NSWindow* window = [timer userInfo]; | 61 NSWindow* window = [timer userInfo]; |
53 NSEvent* up = [NSEvent mouseEventWithType:NSLeftMouseUp | 62 NSEvent* up = [NSEvent mouseEventWithType:NSLeftMouseUp |
54 location:[current locationInWindow] | 63 location:[current locationInWindow] |
55 modifierFlags:0 | 64 modifierFlags:[current modifierFlags] |
56 timestamp:[current timestamp] | 65 timestamp:[current timestamp] |
57 windowNumber:[window windowNumber] | 66 windowNumber:[window windowNumber] |
58 context:nil | 67 context:nil |
59 eventNumber:0 | 68 eventNumber:0 |
60 clickCount:1 | 69 clickCount:1 |
61 pressure:1.0]; | 70 pressure:1.0]; |
62 [window postEvent:up atStart:YES]; | 71 [window postEvent:up atStart:YES]; |
63 } | 72 } |
64 - (void)commandDispatch:(TabStripModel::ContextMenuCommand)command | 73 - (void)commandDispatch:(TabStripModel::ContextMenuCommand)command |
65 forController:(TabController*)controller { | 74 forController:(TabController*)controller { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 EXPECT_FALSE([target closed]); | 130 EXPECT_FALSE([target closed]); |
122 [controller setTarget:target]; | 131 [controller setTarget:target]; |
123 EXPECT_EQ(target.get(), [controller target]); | 132 EXPECT_EQ(target.get(), [controller target]); |
124 | 133 |
125 [controller closeTab:nil]; | 134 [controller closeTab:nil]; |
126 EXPECT_TRUE([target closed]); | 135 EXPECT_TRUE([target closed]); |
127 | 136 |
128 [[controller view] removeFromSuperview]; | 137 [[controller view] removeFromSuperview]; |
129 } | 138 } |
130 | 139 |
| 140 // Tests sending it a closeTab message with a mousedown event and ensuring |
| 141 // that the target/action get called. closeOtherTabs message should be called |
| 142 // when the tab is clicked with option key. |
| 143 TEST_F(TabControllerTest, CloseOtherTabs) { |
| 144 NSWindow* window = test_window(); |
| 145 scoped_nsobject<TabController> controller([[TabController alloc] init]); |
| 146 [[window contentView] addSubview:[controller view]]; |
| 147 |
| 148 scoped_nsobject<TabControllerTestTarget> target( |
| 149 [[TabControllerTestTarget alloc] init]); |
| 150 EXPECT_FALSE([target closedOtherTabs]); |
| 151 [controller setTarget:target]; |
| 152 [controller setAction:@selector(closeTab:)]; |
| 153 EXPECT_EQ(target.get(), [controller target]); |
| 154 EXPECT_EQ(@selector(closeTab:), [controller action]); |
| 155 |
| 156 // Create a mouse event with |NSAlternateKeyMask| and mock NSApp so that |
| 157 // [NSApp currentEvent] returns the event. |
| 158 NSEvent* current = [NSApp currentEvent]; |
| 159 NSEvent* down = [NSEvent mouseEventWithType:NSLeftMouseDown |
| 160 location:[current locationInWindow] |
| 161 modifierFlags:NSAlternateKeyMask |
| 162 timestamp:[current timestamp] |
| 163 windowNumber:[window windowNumber] |
| 164 context:nil |
| 165 eventNumber:0 |
| 166 clickCount:1 |
| 167 pressure:1.0]; |
| 168 // OCMock will swap NSApp internally and will restore it when the mock gets |
| 169 // deallocated. This happens when the autorelease pool drains at the end of |
| 170 // the test. |
| 171 id fakeApp = [OCMockObject partialMockForObject:NSApp]; |
| 172 [[[fakeApp stub] andReturn:down] currentEvent]; |
| 173 |
| 174 [controller closeTab:nil]; |
| 175 EXPECT_TRUE([target closedOtherTabs]); |
| 176 EXPECT_FALSE([target closed]); |
| 177 [[controller view] removeFromSuperview]; |
| 178 } |
| 179 |
131 // Tests setting the |selected| property via code. | 180 // Tests setting the |selected| property via code. |
132 TEST_F(TabControllerTest, APISelection) { | 181 TEST_F(TabControllerTest, APISelection) { |
133 NSWindow* window = test_window(); | 182 NSWindow* window = test_window(); |
134 scoped_nsobject<TabController> controller([[TabController alloc] init]); | 183 scoped_nsobject<TabController> controller([[TabController alloc] init]); |
135 [[window contentView] addSubview:[controller view]]; | 184 [[window contentView] addSubview:[controller view]]; |
136 | 185 |
137 EXPECT_FALSE([controller selected]); | 186 EXPECT_FALSE([controller selected]); |
138 [controller setSelected:YES]; | 187 [controller setSelected:YES]; |
139 EXPECT_TRUE([controller selected]); | 188 EXPECT_TRUE([controller selected]); |
140 | 189 |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 NSWidth([[controller titleView] frame])); | 398 NSWidth([[controller titleView] frame])); |
350 EXPECT_EQ(LeftMargin(originalTabFrame, originalTitleFrame), | 399 EXPECT_EQ(LeftMargin(originalTabFrame, originalTitleFrame), |
351 LeftMargin([[controller view] frame], | 400 LeftMargin([[controller view] frame], |
352 [[controller titleView] frame])); | 401 [[controller titleView] frame])); |
353 EXPECT_EQ(RightMargin(originalTabFrame, originalTitleFrame), | 402 EXPECT_EQ(RightMargin(originalTabFrame, originalTitleFrame), |
354 RightMargin([[controller view] frame], | 403 RightMargin([[controller view] frame], |
355 [[controller titleView] frame])); | 404 [[controller titleView] frame])); |
356 } | 405 } |
357 | 406 |
358 } // namespace | 407 } // namespace |
OLD | NEW |