OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/panels/panel_browser_window_cocoa.h" |
| 6 |
| 7 #include <Carbon/Carbon.h> |
| 8 #import <Cocoa/Cocoa.h> |
| 9 |
| 10 #include "base/command_line.h" |
| 11 #include "base/debug/debugger.h" |
| 12 #include "base/mac/scoped_nsautorelease_pool.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/sys_string_conversions.h" |
| 15 #include "chrome/app/chrome_command_ids.h" // IDC_* |
| 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_list.h" |
| 18 #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
| 19 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
| 20 #import "chrome/browser/ui/cocoa/run_loop_testing.h" |
| 21 #include "chrome/browser/ui/panels/panel.h" |
| 22 #include "chrome/browser/ui/panels/panel_browser_window.h" |
| 23 #include "chrome/browser/ui/panels/panel_manager.h" |
| 24 #import "chrome/browser/ui/panels/panel_titlebar_view_cocoa.h" |
| 25 #import "chrome/browser/ui/panels/panel_window_controller_cocoa.h" |
| 26 #include "chrome/common/chrome_notification_types.h" |
| 27 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/test/base/ui_test_utils.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "testing/gtest_mac.h" |
| 31 |
| 32 class PanelAnimatedBoundsObserver : |
| 33 public ui_test_utils::WindowedNotificationObserver { |
| 34 public: |
| 35 PanelAnimatedBoundsObserver(Panel* panel) |
| 36 : ui_test_utils::WindowedNotificationObserver( |
| 37 chrome::NOTIFICATION_PANEL_BOUNDS_ANIMATIONS_FINISHED, |
| 38 content::Source<Panel>(panel)) { } |
| 39 virtual ~PanelAnimatedBoundsObserver() { } |
| 40 }; |
| 41 |
| 42 // Main test class. |
| 43 class PanelBrowserWindowCocoaTest : public CocoaProfileTest { |
| 44 public: |
| 45 virtual void SetUp() { |
| 46 CocoaProfileTest::SetUp(); |
| 47 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnablePanels); |
| 48 } |
| 49 |
| 50 Panel* CreateTestPanel(const std::string& panel_name) { |
| 51 // Opening panels on a Mac causes NSWindowController of the Panel window |
| 52 // to be autoreleased. We need a pool drained after it's done so the test |
| 53 // can close correctly. |
| 54 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
| 55 |
| 56 PanelManager* manager = PanelManager::GetInstance(); |
| 57 int panels_count = manager->num_panels(); |
| 58 |
| 59 Browser* panel_browser = Browser::CreateWithParams( |
| 60 Browser::CreateParams::CreateForApp( |
| 61 Browser::TYPE_PANEL, panel_name, gfx::Rect(), profile())); |
| 62 EXPECT_EQ(panels_count + 1, manager->num_panels()); |
| 63 |
| 64 PanelBrowserWindow* panel_browser_window = |
| 65 static_cast<PanelBrowserWindow*>(panel_browser->window()); |
| 66 Panel* panel = panel_browser_window->panel(); |
| 67 EXPECT_TRUE(panel); |
| 68 EXPECT_TRUE(panel->native_panel()); // Native panel is created right away. |
| 69 PanelBrowserWindowCocoa* native_window = |
| 70 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 71 EXPECT_EQ(panel, native_window->panel_); // Back pointer initialized. |
| 72 |
| 73 PanelAnimatedBoundsObserver bounds_observer(panel); |
| 74 |
| 75 // Window should not load before Show(). |
| 76 // Note: Loading the wnidow causes Cocoa to autorelease a few objects. |
| 77 // This is the reason we do this within the scope of the |
| 78 // ScopedNSAutoreleasePool. |
| 79 EXPECT_FALSE([native_window->controller_ isWindowLoaded]); |
| 80 panel->Show(); |
| 81 EXPECT_TRUE([native_window->controller_ isWindowLoaded]); |
| 82 EXPECT_TRUE([native_window->controller_ window]); |
| 83 |
| 84 // Wait until bounds animate to their specified values. |
| 85 bounds_observer.Wait(); |
| 86 |
| 87 return panel; |
| 88 } |
| 89 |
| 90 void VerifyTitlebarLocation(NSView* contentView, NSView* titlebar) { |
| 91 NSRect content_frame = [contentView frame]; |
| 92 NSRect titlebar_frame = [titlebar frame]; |
| 93 // Since contentView and titlebar are both children of window's root view, |
| 94 // we can compare their frames since they are in the same coordinate system. |
| 95 EXPECT_EQ(NSMinX(content_frame), NSMinX(titlebar_frame)); |
| 96 EXPECT_EQ(NSWidth(content_frame), NSWidth(titlebar_frame)); |
| 97 EXPECT_EQ(NSHeight([[titlebar superview] bounds]), NSMaxY(titlebar_frame)); |
| 98 } |
| 99 |
| 100 void ClosePanelAndWait(Panel* panel) { |
| 101 EXPECT_TRUE(panel); |
| 102 // Closing a panel may involve several async tasks. Need to use |
| 103 // message pump and wait for the notification. |
| 104 PanelManager* manager = PanelManager::GetInstance(); |
| 105 int panel_count = manager->num_panels(); |
| 106 ui_test_utils::WindowedNotificationObserver signal( |
| 107 chrome::NOTIFICATION_PANEL_CLOSED, |
| 108 content::Source<Panel>(panel)); |
| 109 panel->Close(); |
| 110 signal.Wait(); |
| 111 // Now we have one less panel. |
| 112 EXPECT_EQ(panel_count - 1, manager->num_panels()); |
| 113 } |
| 114 |
| 115 NSMenuItem* CreateMenuItem(NSMenu* menu, int command_id) { |
| 116 NSMenuItem* item = |
| 117 [menu addItemWithTitle:@"" |
| 118 action:@selector(commandDispatch:) |
| 119 keyEquivalent:@""]; |
| 120 [item setTag:command_id]; |
| 121 return item; |
| 122 } |
| 123 }; |
| 124 |
| 125 TEST_F(PanelBrowserWindowCocoaTest, CreateClose) { |
| 126 PanelManager* manager = PanelManager::GetInstance(); |
| 127 EXPECT_EQ(0, manager->num_panels()); // No panels initially. |
| 128 |
| 129 Panel* panel = CreateTestPanel("Test Panel"); |
| 130 ASSERT_TRUE(panel); |
| 131 |
| 132 gfx::Rect bounds = panel->GetBounds(); |
| 133 EXPECT_TRUE(bounds.width() > 0); |
| 134 EXPECT_TRUE(bounds.height() > 0); |
| 135 |
| 136 PanelBrowserWindowCocoa* native_window = |
| 137 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 138 ASSERT_TRUE(native_window); |
| 139 // NSWindows created by NSWindowControllers don't have this bit even if |
| 140 // their NIB has it. The controller's lifetime is the window's lifetime. |
| 141 EXPECT_EQ(NO, [[native_window->controller_ window] isReleasedWhenClosed]); |
| 142 |
| 143 ClosePanelAndWait(panel); |
| 144 EXPECT_EQ(0, manager->num_panels()); |
| 145 } |
| 146 |
| 147 TEST_F(PanelBrowserWindowCocoaTest, AssignedBounds) { |
| 148 Panel* panel1 = CreateTestPanel("Test Panel 1"); |
| 149 Panel* panel2 = CreateTestPanel("Test Panel 2"); |
| 150 Panel* panel3 = CreateTestPanel("Test Panel 3"); |
| 151 |
| 152 gfx::Rect bounds1 = panel1->GetBounds(); |
| 153 gfx::Rect bounds2 = panel2->GetBounds(); |
| 154 gfx::Rect bounds3 = panel3->GetBounds(); |
| 155 |
| 156 // This checks panelManager calculating and assigning bounds right. |
| 157 // Panels should stack on the bottom right to left. |
| 158 EXPECT_LT(bounds3.x() + bounds3.width(), bounds2.x()); |
| 159 EXPECT_LT(bounds2.x() + bounds2.width(), bounds1.x()); |
| 160 EXPECT_EQ(bounds1.y(), bounds2.y()); |
| 161 EXPECT_EQ(bounds2.y(), bounds3.y()); |
| 162 |
| 163 // After panel2 is closed, panel3 should take its place. |
| 164 ClosePanelAndWait(panel2); |
| 165 bounds3 = panel3->GetBounds(); |
| 166 EXPECT_EQ(bounds2, bounds3); |
| 167 |
| 168 // After panel1 is closed, panel3 should take its place. |
| 169 ClosePanelAndWait(panel1); |
| 170 EXPECT_EQ(bounds1, panel3->GetBounds()); |
| 171 |
| 172 ClosePanelAndWait(panel3); |
| 173 } |
| 174 |
| 175 // Same test as AssignedBounds, but checks actual bounds on native OS windows. |
| 176 TEST_F(PanelBrowserWindowCocoaTest, NativeBounds) { |
| 177 Panel* panel1 = CreateTestPanel("Test Panel 1"); |
| 178 Panel* panel2 = CreateTestPanel("Test Panel 2"); |
| 179 Panel* panel3 = CreateTestPanel("Test Panel 3"); |
| 180 |
| 181 PanelBrowserWindowCocoa* native_window1 = |
| 182 static_cast<PanelBrowserWindowCocoa*>(panel1->native_panel()); |
| 183 PanelBrowserWindowCocoa* native_window2 = |
| 184 static_cast<PanelBrowserWindowCocoa*>(panel2->native_panel()); |
| 185 PanelBrowserWindowCocoa* native_window3 = |
| 186 static_cast<PanelBrowserWindowCocoa*>(panel3->native_panel()); |
| 187 |
| 188 NSRect bounds1 = [[native_window1->controller_ window] frame]; |
| 189 NSRect bounds2 = [[native_window2->controller_ window] frame]; |
| 190 NSRect bounds3 = [[native_window3->controller_ window] frame]; |
| 191 |
| 192 EXPECT_LT(bounds3.origin.x + bounds3.size.width, bounds2.origin.x); |
| 193 EXPECT_LT(bounds2.origin.x + bounds2.size.width, bounds1.origin.x); |
| 194 EXPECT_EQ(bounds1.origin.y, bounds2.origin.y); |
| 195 EXPECT_EQ(bounds2.origin.y, bounds3.origin.y); |
| 196 |
| 197 { |
| 198 // After panel2 is closed, panel3 should take its place. |
| 199 PanelAnimatedBoundsObserver bounds_observer(panel3); |
| 200 ClosePanelAndWait(panel2); |
| 201 bounds_observer.Wait(); |
| 202 bounds3 = [[native_window3->controller_ window] frame]; |
| 203 EXPECT_EQ(bounds2.origin.x, bounds3.origin.x); |
| 204 EXPECT_EQ(bounds2.origin.y, bounds3.origin.y); |
| 205 EXPECT_EQ(bounds2.size.width, bounds3.size.width); |
| 206 EXPECT_EQ(bounds2.size.height, bounds3.size.height); |
| 207 } |
| 208 |
| 209 { |
| 210 // After panel1 is closed, panel3 should take its place. |
| 211 PanelAnimatedBoundsObserver bounds_observer(panel3); |
| 212 ClosePanelAndWait(panel1); |
| 213 bounds_observer.Wait(); |
| 214 bounds3 = [[native_window3->controller_ window] frame]; |
| 215 EXPECT_EQ(bounds1.origin.x, bounds3.origin.x); |
| 216 EXPECT_EQ(bounds1.origin.y, bounds3.origin.y); |
| 217 EXPECT_EQ(bounds1.size.width, bounds3.size.width); |
| 218 EXPECT_EQ(bounds1.size.height, bounds3.size.height); |
| 219 } |
| 220 |
| 221 ClosePanelAndWait(panel3); |
| 222 } |
| 223 |
| 224 // Verify the titlebar is being created. |
| 225 TEST_F(PanelBrowserWindowCocoaTest, TitlebarViewCreate) { |
| 226 Panel* panel = CreateTestPanel("Test Panel"); |
| 227 |
| 228 PanelBrowserWindowCocoa* native_window = |
| 229 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 230 |
| 231 PanelTitlebarViewCocoa* titlebar = [native_window->controller_ titlebarView]; |
| 232 EXPECT_TRUE(titlebar); |
| 233 EXPECT_EQ(native_window->controller_, [titlebar controller]); |
| 234 |
| 235 ClosePanelAndWait(panel); |
| 236 } |
| 237 |
| 238 // Verify the sizing of titlebar - should be affixed on top of regular titlebar. |
| 239 TEST_F(PanelBrowserWindowCocoaTest, TitlebarViewSizing) { |
| 240 Panel* panel = CreateTestPanel("Test Panel"); |
| 241 |
| 242 PanelBrowserWindowCocoa* native_window = |
| 243 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 244 PanelTitlebarViewCocoa* titlebar = [native_window->controller_ titlebarView]; |
| 245 |
| 246 NSView* contentView = [[native_window->controller_ window] contentView]; |
| 247 VerifyTitlebarLocation(contentView, titlebar); |
| 248 |
| 249 // In local coordinate system, width of titlebar should match width of |
| 250 // content view of the window. They both use the same scale factor. |
| 251 EXPECT_EQ(NSWidth([contentView bounds]), NSWidth([titlebar bounds])); |
| 252 |
| 253 NSRect oldTitleFrame = [[titlebar title] frame]; |
| 254 NSRect oldIconFrame = [[titlebar icon] frame]; |
| 255 |
| 256 // Now resize the Panel, see that titlebar follows. |
| 257 const int kDelta = 153; // random number |
| 258 gfx::Rect bounds = panel->GetBounds(); |
| 259 // Grow panel in a way so that its titlebar moves and grows. |
| 260 bounds.set_x(bounds.x() - kDelta); |
| 261 bounds.set_y(bounds.y() - kDelta); |
| 262 bounds.set_width(bounds.width() + kDelta); |
| 263 bounds.set_height(bounds.height() + kDelta); |
| 264 |
| 265 PanelAnimatedBoundsObserver bounds_observer(panel); |
| 266 native_window->SetPanelBounds(bounds); |
| 267 bounds_observer.Wait(); |
| 268 |
| 269 // Verify the panel resized. |
| 270 NSRect window_frame = [[native_window->controller_ window] frame]; |
| 271 EXPECT_EQ(NSWidth(window_frame), bounds.width()); |
| 272 EXPECT_EQ(NSHeight(window_frame), bounds.height()); |
| 273 |
| 274 // Verify the titlebar is still on top of regular titlebar. |
| 275 VerifyTitlebarLocation(contentView, titlebar); |
| 276 |
| 277 // Verify that the title/icon frames were updated. |
| 278 NSRect newTitleFrame = [[titlebar title] frame]; |
| 279 NSRect newIconFrame = [[titlebar icon] frame]; |
| 280 |
| 281 EXPECT_EQ(newTitleFrame.origin.x - newIconFrame.origin.x, |
| 282 oldTitleFrame.origin.x - oldIconFrame.origin.x); |
| 283 // Icon and Text should remain at the same left-aligned position. |
| 284 EXPECT_EQ(newTitleFrame.origin.x, oldTitleFrame.origin.x); |
| 285 EXPECT_EQ(newIconFrame.origin.x, oldIconFrame.origin.x); |
| 286 |
| 287 ClosePanelAndWait(panel); |
| 288 } |
| 289 |
| 290 // Verify closing behavior of titlebar close button. |
| 291 TEST_F(PanelBrowserWindowCocoaTest, TitlebarViewClose) { |
| 292 Panel* panel = CreateTestPanel("Test Panel"); |
| 293 |
| 294 PanelBrowserWindowCocoa* native_window = |
| 295 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 296 |
| 297 PanelTitlebarViewCocoa* titlebar = [native_window->controller_ titlebarView]; |
| 298 EXPECT_TRUE(titlebar); |
| 299 |
| 300 PanelManager* manager = PanelManager::GetInstance(); |
| 301 EXPECT_EQ(1, manager->num_panels()); |
| 302 // Simulate clicking Close Button and wait until the Panel closes. |
| 303 ui_test_utils::WindowedNotificationObserver signal( |
| 304 chrome::NOTIFICATION_PANEL_CLOSED, |
| 305 content::Source<Panel>(panel)); |
| 306 [titlebar simulateCloseButtonClick]; |
| 307 signal.Wait(); |
| 308 EXPECT_EQ(0, manager->num_panels()); |
| 309 } |
| 310 |
| 311 // Verify some menu items being properly enabled/disabled for panels. |
| 312 TEST_F(PanelBrowserWindowCocoaTest, MenuItems) { |
| 313 Panel* panel = CreateTestPanel("Test Panel"); |
| 314 |
| 315 scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]); |
| 316 NSMenuItem* close_tab_menu_item = CreateMenuItem(menu, IDC_CLOSE_TAB); |
| 317 NSMenuItem* close_window_menu_item = CreateMenuItem(menu, IDC_CLOSE_WINDOW); |
| 318 NSMenuItem* find_menu_item = CreateMenuItem(menu, IDC_FIND); |
| 319 NSMenuItem* find_previous_menu_item = CreateMenuItem(menu, IDC_FIND_PREVIOUS); |
| 320 NSMenuItem* find_next_menu_item = CreateMenuItem(menu, IDC_FIND_NEXT); |
| 321 NSMenuItem* fullscreen_menu_item = CreateMenuItem(menu, IDC_FULLSCREEN); |
| 322 NSMenuItem* presentation_menu_item = |
| 323 CreateMenuItem(menu, IDC_PRESENTATION_MODE); |
| 324 NSMenuItem* sync_menu_item = CreateMenuItem(menu, IDC_SHOW_SYNC_SETUP); |
| 325 |
| 326 PanelBrowserWindowCocoa* native_window = |
| 327 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 328 PanelWindowControllerCocoa* panel_controller = native_window->controller_; |
| 329 for (NSMenuItem *item in [menu itemArray]) |
| 330 [item setTarget:panel_controller]; |
| 331 |
| 332 [menu update]; // Trigger validation of menu items. |
| 333 EXPECT_FALSE([close_tab_menu_item isEnabled]); |
| 334 EXPECT_TRUE([close_window_menu_item isEnabled]); |
| 335 EXPECT_TRUE([find_menu_item isEnabled]); |
| 336 EXPECT_TRUE([find_previous_menu_item isEnabled]); |
| 337 EXPECT_TRUE([find_next_menu_item isEnabled]); |
| 338 EXPECT_FALSE([fullscreen_menu_item isEnabled]); |
| 339 EXPECT_FALSE([presentation_menu_item isEnabled]); |
| 340 EXPECT_FALSE([sync_menu_item isEnabled]); |
| 341 |
| 342 ClosePanelAndWait(panel); |
| 343 } |
| 344 |
| 345 TEST_F(PanelBrowserWindowCocoaTest, KeyEvent) { |
| 346 Panel* panel = CreateTestPanel("Test Panel"); |
| 347 NSEvent* event = [NSEvent keyEventWithType:NSKeyDown |
| 348 location:NSZeroPoint |
| 349 modifierFlags:NSControlKeyMask |
| 350 timestamp:0.0 |
| 351 windowNumber:0 |
| 352 context:nil |
| 353 characters:@"" |
| 354 charactersIgnoringModifiers:@"" |
| 355 isARepeat:NO |
| 356 keyCode:kVK_Tab]; |
| 357 PanelBrowserWindowCocoa* native_window = |
| 358 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 359 [BrowserWindowUtils handleKeyboardEvent:event |
| 360 inWindow:[native_window->controller_ window]]; |
| 361 ClosePanelAndWait(panel); |
| 362 } |
| 363 |
| 364 // Verify that the theme provider is properly plumbed through. |
| 365 TEST_F(PanelBrowserWindowCocoaTest, ThemeProvider) { |
| 366 Panel* panel = CreateTestPanel("Test Panel"); |
| 367 ASSERT_TRUE(panel); |
| 368 |
| 369 PanelBrowserWindowCocoa* native_window = |
| 370 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 371 ASSERT_TRUE(native_window); |
| 372 EXPECT_TRUE(NULL != [[native_window->controller_ window] themeProvider]); |
| 373 ClosePanelAndWait(panel); |
| 374 } |
| 375 |
| 376 TEST_F(PanelBrowserWindowCocoaTest, SetTitle) { |
| 377 NSString *appName = @"Test Panel"; |
| 378 Panel* panel = CreateTestPanel(base::SysNSStringToUTF8(appName)); |
| 379 ASSERT_TRUE(panel); |
| 380 |
| 381 PanelBrowserWindowCocoa* native_window = |
| 382 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 383 ASSERT_TRUE(native_window); |
| 384 NSString* previousTitle = [[native_window->controller_ window] title]; |
| 385 EXPECT_NSNE(appName, previousTitle); |
| 386 [native_window->controller_ updateTitleBar]; |
| 387 chrome::testing::NSRunLoopRunAllPending(); |
| 388 NSString* currentTitle = [[native_window->controller_ window] title]; |
| 389 EXPECT_NSEQ(appName, currentTitle); |
| 390 EXPECT_NSNE(currentTitle, previousTitle); |
| 391 ClosePanelAndWait(panel); |
| 392 } |
| 393 |
| 394 TEST_F(PanelBrowserWindowCocoaTest, ActivatePanel) { |
| 395 Panel* panel = CreateTestPanel("Test Panel"); |
| 396 Panel* panel2 = CreateTestPanel("Test Panel 2"); |
| 397 ASSERT_TRUE(panel); |
| 398 ASSERT_TRUE(panel2); |
| 399 |
| 400 PanelBrowserWindowCocoa* native_window = |
| 401 static_cast<PanelBrowserWindowCocoa*>(panel->native_panel()); |
| 402 ASSERT_TRUE(native_window); |
| 403 PanelBrowserWindowCocoa* native_window2 = |
| 404 static_cast<PanelBrowserWindowCocoa*>(panel2->native_panel()); |
| 405 ASSERT_TRUE(native_window2); |
| 406 |
| 407 // No one has a good answer why but apparently windows can't take keyboard |
| 408 // focus outside of interactive UI tests. BrowserWindowController uses the |
| 409 // same way of testing this. |
| 410 native_window->ActivatePanel(); |
| 411 NSWindow* frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0]; |
| 412 EXPECT_NSEQ(frontmostWindow, [native_window->controller_ window]); |
| 413 |
| 414 native_window2->ActivatePanel(); |
| 415 frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0]; |
| 416 EXPECT_NSEQ(frontmostWindow, [native_window2->controller_ window]); |
| 417 |
| 418 ClosePanelAndWait(panel); |
| 419 ClosePanelAndWait(panel2); |
| 420 } |
OLD | NEW |