OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con
troller.h" | 5 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con
troller.h" |
6 | 6 |
7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 7 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
8 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sh
eet.h" | 8 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sh
eet.h" |
9 #import "testing/gtest_mac.h" | 9 #import "testing/gtest_mac.h" |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 protected: | 68 protected: |
69 virtual ~ConstrainedWindowSheetControllerTest() { | 69 virtual ~ConstrainedWindowSheetControllerTest() { |
70 } | 70 } |
71 | 71 |
72 virtual void SetUp() OVERRIDE { | 72 virtual void SetUp() OVERRIDE { |
73 CocoaTest::SetUp(); | 73 CocoaTest::SetUp(); |
74 | 74 |
75 // Center the window so that the sheet doesn't go offscreen. | 75 // Center the window so that the sheet doesn't go offscreen. |
76 [test_window() center]; | 76 [test_window() center]; |
77 | 77 |
| 78 // The real view setup is quite a few levels deep; recreate something |
| 79 // similar. |
| 80 NSRect dummy_rect = NSMakeRect(0, 0, 50, 50); |
| 81 tab_view_parent_ = [test_window() contentView]; |
| 82 for (int i = 0; i < 3; ++i) { |
| 83 base::scoped_nsobject<NSView> new_view( |
| 84 [[NSView alloc] initWithFrame:dummy_rect]); |
| 85 [tab_view_parent_ addSubview:new_view.get()]; |
| 86 tab_view_parent_ = new_view.get(); |
| 87 } |
| 88 |
78 // Create two dummy tabs and make the first one active. | 89 // Create two dummy tabs and make the first one active. |
79 NSRect dummyRect = NSMakeRect(0, 0, 50, 50); | |
80 tab_views_.reset([[NSMutableArray alloc] init]); | 90 tab_views_.reset([[NSMutableArray alloc] init]); |
81 for (int i = 0; i < 2; ++i) { | 91 for (int i = 0; i < 2; ++i) { |
82 base::scoped_nsobject<NSView> view( | 92 base::scoped_nsobject<NSView> view( |
83 [[NSView alloc] initWithFrame:dummyRect]); | 93 [[NSView alloc] initWithFrame:dummy_rect]); |
84 [tab_views_ addObject:view]; | 94 [tab_views_ addObject:view]; |
85 } | 95 } |
86 tab0_.reset([[tab_views_ objectAtIndex:0] retain]); | 96 tab0_ = [tab_views_ objectAtIndex:0]; |
87 tab1_.reset([[tab_views_ objectAtIndex:1] retain]); | 97 tab1_ = [tab_views_ objectAtIndex:1]; |
88 ActivateTabView(tab0_); | 98 ActivateTabView(tab0_); |
89 | 99 |
90 // Create a test sheet. | 100 // Create a test sheet. |
91 sheet_window_.reset([[NSWindow alloc] | 101 sheet_window_.reset([[NSWindow alloc] |
92 initWithContentRect:dummyRect | 102 initWithContentRect:dummy_rect |
93 styleMask:NSTitledWindowMask | 103 styleMask:NSTitledWindowMask |
94 backing:NSBackingStoreBuffered | 104 backing:NSBackingStoreBuffered |
95 defer:NO]); | 105 defer:NO]); |
96 [sheet_window_ setReleasedWhenClosed:NO]; | 106 [sheet_window_ setReleasedWhenClosed:NO]; |
97 sheet_.reset([[CustomConstrainedWindowSheet alloc] | 107 sheet_.reset([[CustomConstrainedWindowSheet alloc] |
98 initWithCustomWindow:sheet_window_]); | 108 initWithCustomWindow:sheet_window_]); |
99 | 109 |
100 controller_.reset([[ConstrainedWindowSheetController | 110 controller_.reset([[ConstrainedWindowSheetController |
101 controllerForParentWindow:test_window()] retain]); | 111 controllerForParentWindow:test_window()] retain]); |
102 EXPECT_TRUE(controller_); | 112 EXPECT_TRUE(controller_); |
103 EXPECT_FALSE([ConstrainedWindowSheetController controllerForSheet:sheet_]); | 113 EXPECT_FALSE([ConstrainedWindowSheetController controllerForSheet:sheet_]); |
104 } | 114 } |
105 | 115 |
106 virtual void TearDown() OVERRIDE { | 116 virtual void TearDown() OVERRIDE { |
107 sheet_.reset(); | 117 sheet_.reset(); |
108 sheet_window_.reset(); | 118 sheet_window_.reset(); |
109 CocoaTest::TearDown(); | 119 CocoaTest::TearDown(); |
110 } | 120 } |
111 | 121 |
112 void ActivateTabView(NSView* tab_view) { | 122 void ActivateTabView(NSView* tab_view) { |
113 for (NSView* view in tab_views_.get()) { | 123 for (NSView* view in tab_views_.get()) |
114 [view removeFromSuperview]; | 124 [view removeFromSuperview]; |
115 } | 125 [tab_view_parent_ addSubview:tab_view]; |
116 [[test_window() contentView] addSubview:tab_view]; | 126 active_tab_view_ = tab_view; |
117 active_tab_view_.reset([tab_view retain]); | |
118 | 127 |
119 ConstrainedWindowSheetController* controller = | 128 ConstrainedWindowSheetController* controller = |
120 [ConstrainedWindowSheetController | 129 [ConstrainedWindowSheetController |
121 controllerForParentWindow:test_window()]; | 130 controllerForParentWindow:test_window()]; |
122 EXPECT_TRUE(controller); | 131 EXPECT_TRUE(controller); |
123 [controller parentViewDidBecomeActive:active_tab_view_]; | 132 [controller parentViewDidBecomeActive:active_tab_view_]; |
124 } | 133 } |
125 | 134 |
126 NSRect GetViewFrameInScreenCoordinates(NSView* view) { | 135 NSRect GetViewFrameInScreenCoordinates(NSView* view) { |
127 NSRect rect = [view convertRect:[view bounds] toView:nil]; | 136 NSRect rect = [view convertRect:[view bounds] toView:nil]; |
(...skipping 10 matching lines...) Expand all Loading... |
138 | 147 |
139 CGFloat GetSheetYOffset(NSRect sheet_frame, NSView* parent_view) { | 148 CGFloat GetSheetYOffset(NSRect sheet_frame, NSView* parent_view) { |
140 return NSMaxY(sheet_frame) - | 149 return NSMaxY(sheet_frame) - |
141 NSMaxY(GetViewFrameInScreenCoordinates(parent_view)); | 150 NSMaxY(GetViewFrameInScreenCoordinates(parent_view)); |
142 } | 151 } |
143 | 152 |
144 base::scoped_nsobject<NSWindow> sheet_window_; | 153 base::scoped_nsobject<NSWindow> sheet_window_; |
145 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet_; | 154 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet_; |
146 base::scoped_nsobject<ConstrainedWindowSheetController> controller_; | 155 base::scoped_nsobject<ConstrainedWindowSheetController> controller_; |
147 base::scoped_nsobject<NSMutableArray> tab_views_; | 156 base::scoped_nsobject<NSMutableArray> tab_views_; |
148 base::scoped_nsobject<NSView> active_tab_view_; | 157 NSView* tab_view_parent_; |
149 base::scoped_nsobject<NSView> tab0_; | 158 NSView* active_tab_view_; |
150 base::scoped_nsobject<NSView> tab1_; | 159 NSView* tab0_; |
| 160 NSView* tab1_; |
151 }; | 161 }; |
152 | 162 |
153 // Test showing then hiding the sheet. | 163 // Test showing then hiding the sheet. |
154 TEST_F(ConstrainedWindowSheetControllerTest, ShowHide) { | 164 TEST_F(ConstrainedWindowSheetControllerTest, ShowHide) { |
155 EXPECT_FALSE([sheet_window_ isVisible]); | 165 EXPECT_FALSE([sheet_window_ isVisible]); |
156 [controller_ showSheet:sheet_ forParentView:active_tab_view_]; | 166 [controller_ showSheet:sheet_ forParentView:active_tab_view_]; |
157 EXPECT_TRUE([ConstrainedWindowSheetController controllerForSheet:sheet_]); | 167 EXPECT_TRUE([ConstrainedWindowSheetController controllerForSheet:sheet_]); |
158 EXPECT_TRUE([sheet_window_ isVisible]); | 168 EXPECT_TRUE([sheet_window_ isVisible]); |
159 | 169 |
160 [controller_ closeSheet:sheet_]; | 170 [controller_ closeSheet:sheet_]; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 EXPECT_TRUE(controller2); | 212 EXPECT_TRUE(controller2); |
203 EXPECT_NSNE(controller_, controller2); | 213 EXPECT_NSNE(controller_, controller2); |
204 | 214 |
205 [controller2 showSheet:sheet_ forParentView:[parent_window2 contentView]]; | 215 [controller2 showSheet:sheet_ forParentView:[parent_window2 contentView]]; |
206 EXPECT_NSEQ(controller2, | 216 EXPECT_NSEQ(controller2, |
207 [ConstrainedWindowSheetController controllerForSheet:sheet_]); | 217 [ConstrainedWindowSheetController controllerForSheet:sheet_]); |
208 | 218 |
209 [parent_window2 close]; | 219 [parent_window2 close]; |
210 } | 220 } |
211 | 221 |
212 // Test that using a top level parent view works. | |
213 TEST_F(ConstrainedWindowSheetControllerTest, TopLevelView) { | |
214 NSView* parentView = [[test_window() contentView] superview]; | |
215 [controller_ parentViewDidBecomeActive:parentView]; | |
216 | |
217 EXPECT_FALSE([sheet_window_ isVisible]); | |
218 [controller_ showSheet:sheet_ forParentView:parentView]; | |
219 EXPECT_TRUE([ConstrainedWindowSheetController controllerForSheet:sheet_]); | |
220 EXPECT_TRUE([sheet_window_ isVisible]); | |
221 VerifySheetXPosition([sheet_window_ frame], parentView); | |
222 } | |
223 | |
224 // Test that resizing sheet works. | 222 // Test that resizing sheet works. |
225 TEST_F(ConstrainedWindowSheetControllerTest, Resize) { | 223 TEST_F(ConstrainedWindowSheetControllerTest, Resize) { |
226 [controller_ showSheet:sheet_ forParentView:active_tab_view_]; | 224 [controller_ showSheet:sheet_ forParentView:active_tab_view_]; |
227 | 225 |
228 NSRect old_frame = [sheet_window_ frame]; | 226 NSRect old_frame = [sheet_window_ frame]; |
229 | 227 |
230 NSRect sheet_frame; | 228 NSRect sheet_frame; |
231 sheet_frame.size = NSMakeSize(NSWidth(old_frame) + 100, | 229 sheet_frame.size = NSMakeSize(NSWidth(old_frame) + 100, |
232 NSHeight(old_frame) + 50); | 230 NSHeight(old_frame) + 50); |
233 sheet_frame.origin = [controller_ originForSheet:sheet_ | 231 sheet_frame.origin = [controller_ originForSheet:sheet_ |
(...skipping 28 matching lines...) Expand all Loading... |
262 // Test resizing parent window keeps the sheet anchored. | 260 // Test resizing parent window keeps the sheet anchored. |
263 TEST_F(ConstrainedWindowSheetControllerTest, ResizeParentWindow) { | 261 TEST_F(ConstrainedWindowSheetControllerTest, ResizeParentWindow) { |
264 [controller_ showSheet:sheet_ forParentView:active_tab_view_]; | 262 [controller_ showSheet:sheet_ forParentView:active_tab_view_]; |
265 CGFloat sheet_offset = | 263 CGFloat sheet_offset = |
266 GetSheetYOffset([sheet_window_ frame], active_tab_view_); | 264 GetSheetYOffset([sheet_window_ frame], active_tab_view_); |
267 | 265 |
268 // Test 3x3 different parent window sizes. | 266 // Test 3x3 different parent window sizes. |
269 CGFloat insets[] = {-10, 0, 10}; | 267 CGFloat insets[] = {-10, 0, 10}; |
270 NSRect old_frame = [test_window() frame]; | 268 NSRect old_frame = [test_window() frame]; |
271 | 269 |
272 for (size_t x = 0; x < sizeof(insets) / sizeof(CGFloat); x++) { | 270 for (size_t x = 0; x < arraysize(insets); x++) { |
273 for (size_t y = 0; y < sizeof(insets) / sizeof(CGFloat); y++) { | 271 for (size_t y = 0; y < arraysize(insets); y++) { |
274 NSRect resized_frame = NSInsetRect(old_frame, insets[x], insets[y]); | 272 NSRect resized_frame = NSInsetRect(old_frame, insets[x], insets[y]); |
275 [test_window() setFrame:resized_frame display:YES]; | 273 [test_window() setFrame:resized_frame display:YES]; |
276 NSRect sheet_frame = [sheet_window_ frame]; | 274 NSRect sheet_frame = [sheet_window_ frame]; |
277 | 275 |
278 // Y pos should track parent view's position. | 276 // Y pos should track parent view's position. |
279 EXPECT_EQ(sheet_offset, GetSheetYOffset(sheet_frame, active_tab_view_)); | 277 EXPECT_EQ(sheet_offset, GetSheetYOffset(sheet_frame, active_tab_view_)); |
280 | 278 |
281 // X pos should be centered on parent view. | 279 // X pos should be centered on parent view. |
282 VerifySheetXPosition(sheet_frame, active_tab_view_); | 280 VerifySheetXPosition(sheet_frame, active_tab_view_); |
283 } | 281 } |
284 } | 282 } |
285 } | 283 } |
286 | 284 |
287 // Test system sheets. | 285 // Test system sheets. |
288 TEST_F(ConstrainedWindowSheetControllerTest, SystemSheet) { | 286 TEST_F(ConstrainedWindowSheetControllerTest, SystemSheet) { |
289 base::scoped_nsobject<ConstrainedWindowSystemSheetTest> system_sheet( | 287 base::scoped_nsobject<ConstrainedWindowSystemSheetTest> system_sheet( |
290 [[ConstrainedWindowSystemSheetTest alloc] init]); | 288 [[ConstrainedWindowSystemSheetTest alloc] init]); |
291 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); | 289 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); |
292 [system_sheet setAlert:alert]; | 290 [system_sheet setAlert:alert]; |
293 | 291 |
294 EXPECT_FALSE([[alert window] isVisible]); | 292 EXPECT_FALSE([[alert window] isVisible]); |
295 [controller_ showSheet:system_sheet | 293 [controller_ showSheet:system_sheet forParentView:active_tab_view_]; |
296 forParentView:active_tab_view_]; | |
297 EXPECT_TRUE([[alert window] isVisible]); | 294 EXPECT_TRUE([[alert window] isVisible]); |
298 | 295 |
299 [controller_ closeSheet:system_sheet]; | 296 [controller_ closeSheet:system_sheet]; |
300 EXPECT_FALSE([[alert window] isVisible]); | 297 EXPECT_FALSE([[alert window] isVisible]); |
301 EXPECT_EQ(kSystemSheetReturnCode, [system_sheet returnCode]); | 298 EXPECT_EQ(kSystemSheetReturnCode, [system_sheet returnCode]); |
302 } | 299 } |
303 | 300 |
304 // Test showing a system sheet on an inactive tab. | 301 // Test showing a system sheet on an inactive tab. |
305 TEST_F(ConstrainedWindowSheetControllerTest, SystemSheetAddToInactiveTab) { | 302 TEST_F(ConstrainedWindowSheetControllerTest, SystemSheetAddToInactiveTab) { |
306 base::scoped_nsobject<ConstrainedWindowSystemSheetTest> system_sheet( | 303 base::scoped_nsobject<ConstrainedWindowSystemSheetTest> system_sheet( |
307 [[ConstrainedWindowSystemSheetTest alloc] init]); | 304 [[ConstrainedWindowSystemSheetTest alloc] init]); |
308 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); | 305 base::scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); |
309 [system_sheet setAlert:alert]; | 306 [system_sheet setAlert:alert]; |
310 | 307 |
311 EXPECT_FALSE([[alert window] isVisible]); | 308 EXPECT_FALSE([[alert window] isVisible]); |
312 [controller_ showSheet:system_sheet | 309 [controller_ showSheet:system_sheet forParentView:tab1_]; |
313 forParentView:tab1_]; | |
314 EXPECT_FALSE([[alert window] isVisible]); | 310 EXPECT_FALSE([[alert window] isVisible]); |
315 | 311 |
316 ActivateTabView(tab1_); | 312 ActivateTabView(tab1_); |
317 EXPECT_TRUE([[alert window] isVisible]); | 313 EXPECT_TRUE([[alert window] isVisible]); |
318 EXPECT_EQ(1.0, [[alert window] alphaValue]); | 314 EXPECT_EQ(1.0, [[alert window] alphaValue]); |
319 | 315 |
320 [controller_ closeSheet:system_sheet]; | 316 [controller_ closeSheet:system_sheet]; |
321 } | 317 } |
OLD | NEW |