OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/scoped_nsautorelease_pool.h" | |
8 #import "base/scoped_nsobject.h" | |
9 #import "chrome/browser/cocoa/tab_controller.h" | |
10 #import "chrome/browser/cocoa/tab_controller_target.h" | |
11 #include "chrome/browser/cocoa/cocoa_test_helper.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 // Implements the target interface for the tab, which gets sent messages when | |
15 // the tab is clicked on by the user and when its close box is clicked. | |
16 @interface TabControllerTestTarget : NSObject<TabControllerTarget> { | |
17 @private | |
18 bool selected_; | |
19 bool closed_; | |
20 } | |
21 - (bool)selected; | |
22 - (bool)closed; | |
23 @end | |
24 | |
25 @implementation TabControllerTestTarget | |
26 - (bool)selected { | |
27 return selected_; | |
28 } | |
29 - (bool)closed { | |
30 return closed_; | |
31 } | |
32 - (void)selectTab:(id)sender { | |
33 selected_ = true; | |
34 } | |
35 - (void)closeTab:(id)sender { | |
36 closed_ = true; | |
37 } | |
38 - (void)mouseTimer:(NSTimer*)timer { | |
39 // Fire the mouseUp to break the TabView drag loop. | |
40 NSEvent* current = [[NSApplication sharedApplication] currentEvent]; | |
41 NSWindow* window = [timer userInfo]; | |
42 NSEvent* up = [NSEvent mouseEventWithType:NSLeftMouseUp | |
43 location:[current locationInWindow] | |
44 modifierFlags:0 | |
45 timestamp:[current timestamp] | |
46 windowNumber:[window windowNumber] | |
47 context:nil | |
48 eventNumber:0 | |
49 clickCount:1 | |
50 pressure:1.0]; | |
51 [window postEvent:up atStart:YES]; | |
52 } | |
53 @end | |
54 | |
55 namespace { | |
56 | |
57 class TabControllerTest : public testing::Test { | |
58 public: | |
59 TabControllerTest() { | |
60 } | |
61 | |
62 CocoaTestHelper cocoa_helper_; | |
63 }; | |
64 | |
65 // Tests creating the controller, sticking it in a window, and removing it. | |
66 TEST_F(TabControllerTest, Creation) { | |
67 NSWindow* window = cocoa_helper_.window(); | |
68 scoped_nsobject<TabController> controller([[TabController alloc] init]); | |
69 [[window contentView] addSubview:[controller view]]; | |
70 EXPECT_TRUE([controller tabView]); | |
71 EXPECT_EQ([[controller view] window], window); | |
72 [[controller view] removeFromSuperview]; | |
73 } | |
74 | |
75 // Tests sending it a close message and ensuring that the target/action get | |
76 // called. Mimics the user clicking on the close button in the tab. | |
77 TEST_F(TabControllerTest, Close) { | |
78 NSWindow* window = cocoa_helper_.window(); | |
79 scoped_nsobject<TabController> controller([[TabController alloc] init]); | |
80 [[window contentView] addSubview:[controller view]]; | |
81 | |
82 scoped_nsobject<TabControllerTestTarget> target( | |
83 [[TabControllerTestTarget alloc] init]); | |
84 EXPECT_FALSE([target closed]); | |
85 [controller setTarget:target]; | |
86 EXPECT_EQ(target.get(), [controller target]); | |
87 | |
88 [controller closeTab:nil]; | |
89 EXPECT_TRUE([target closed]); | |
90 | |
91 [[controller view] removeFromSuperview]; | |
92 } | |
93 | |
94 // Tests setting the |selected| property via code. | |
95 TEST_F(TabControllerTest, APISelection) { | |
96 NSWindow* window = cocoa_helper_.window(); | |
97 scoped_nsobject<TabController> controller([[TabController alloc] init]); | |
98 [[window contentView] addSubview:[controller view]]; | |
99 | |
100 EXPECT_FALSE([controller selected]); | |
101 [controller setSelected:YES]; | |
102 EXPECT_TRUE([controller selected]); | |
103 | |
104 [[controller view] removeFromSuperview]; | |
105 } | |
106 | |
107 // Tests setting the |loading| property via code. | |
108 TEST_F(TabControllerTest, Loading) { | |
109 NSWindow* window = cocoa_helper_.window(); | |
110 scoped_nsobject<TabController> controller([[TabController alloc] init]); | |
111 [[window contentView] addSubview:[controller view]]; | |
112 | |
113 EXPECT_FALSE([controller loading]); | |
114 [controller setLoading:YES]; | |
115 EXPECT_TRUE([controller loading]); | |
116 | |
117 [[controller view] removeFromSuperview]; | |
118 } | |
119 | |
120 // Tests selecting the tab with the mouse click and ensuring the target/action | |
121 // get called. | |
122 // TODO(pinkerton): It's yucky that TabView bakes in the dragging so that we | |
123 // can't test this class w/out lots of extra effort. When cole finishes the | |
124 // rewrite, we should move all that logic out into a separate controller which | |
125 // we can dependency-inject/mock so it has very simple click behavior for unit | |
126 // testing. | |
127 TEST_F(TabControllerTest, UserSelection) { | |
128 NSWindow* window = cocoa_helper_.window(); | |
129 // The dragging code in TabView makes heavy use of autorelease pools. We | |
130 // need to create our own here otherwise the window stays active for the | |
131 // duration of the entire test. | |
132 base::ScopedNSAutoreleasePool pool; | |
133 | |
134 // Create a tab at a known location in the window that we can click on | |
135 // to activate selection. | |
136 scoped_nsobject<TabController> controller([[TabController alloc] init]); | |
137 [[window contentView] addSubview:[controller view]]; | |
138 NSRect frame = [[controller view] frame]; | |
139 frame.size.width = [TabController minTabWidth]; | |
140 frame.origin = NSMakePoint(0, 0); | |
141 [[controller view] setFrame:frame]; | |
142 | |
143 // Set the target and action. | |
144 scoped_nsobject<TabControllerTestTarget> target( | |
145 [[TabControllerTestTarget alloc] init]); | |
146 EXPECT_FALSE([target selected]); | |
147 [controller setTarget:target]; | |
148 [controller setAction:@selector(selectTab:)]; | |
149 EXPECT_EQ(target.get(), [controller target]); | |
150 EXPECT_EQ(@selector(selectTab:), [controller action]); | |
151 | |
152 // In order to track a click, we have to fake a mouse down and a mouse | |
153 // up, but the down goes into a tight drag loop. To break the loop, we have | |
154 // to fire a timer that sends a mouse up event while the "drag" is ongoing. | |
155 [NSTimer scheduledTimerWithTimeInterval:0.1 | |
156 target:target.get() | |
157 selector:@selector(mouseTimer:) | |
158 userInfo:window | |
159 repeats:NO]; | |
160 NSEvent* current = [[NSApplication sharedApplication] currentEvent]; | |
161 NSPoint click_point = NSMakePoint(frame.size.width / 2, | |
162 frame.size.height / 2); | |
163 NSEvent* down = [NSEvent mouseEventWithType:NSLeftMouseDown | |
164 location:click_point | |
165 modifierFlags:0 | |
166 timestamp:[current timestamp] | |
167 windowNumber:[window windowNumber] | |
168 context:nil | |
169 eventNumber:0 | |
170 clickCount:1 | |
171 pressure:1.0]; | |
172 [[controller view] mouseDown:down]; | |
173 | |
174 // Check our target was told the tab got selected. | |
175 EXPECT_TRUE([target selected]); | |
176 | |
177 [[controller view] removeFromSuperview]; | |
178 } | |
179 | |
180 } // namespace | |
OLD | NEW |