| 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/base_bubble_controller.h" | 5 #import "chrome/browser/ui/cocoa/base_bubble_controller.h" |
| 6 | 6 |
| 7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
| 8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "base/mac/scoped_objc_class_swizzler.h" | 9 #import "base/mac/scoped_objc_class_swizzler.h" |
| 10 #import "base/mac/sdk_forward_declarations.h" | 10 #import "base/mac/sdk_forward_declarations.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 [controller_ showWindow:nil]; | 139 [controller_ showWindow:nil]; |
| 140 EXPECT_TRUE([bubble_window_ isVisible]); | 140 EXPECT_TRUE([bubble_window_ isVisible]); |
| 141 return keep_alive; | 141 return keep_alive; |
| 142 } | 142 } |
| 143 | 143 |
| 144 // Fake the key state notification. Because unit_tests is a "daemon" process | 144 // Fake the key state notification. Because unit_tests is a "daemon" process |
| 145 // type, its windows can never become key (nor can the app become active). | 145 // type, its windows can never become key (nor can the app become active). |
| 146 // Instead of the hacks below, one could make a browser_test or transform the | 146 // Instead of the hacks below, one could make a browser_test or transform the |
| 147 // process type, but this seems easiest and is best suited to a unit test. | 147 // process type, but this seems easiest and is best suited to a unit test. |
| 148 // | 148 // |
| 149 // On Lion and above, which have the event taps, simply post a notification | 149 // Simply post a notification that will cause the controller to call |
| 150 // that will cause the controller to call |-windowDidResignKey:|. Earlier | 150 // |-windowDidResignKey:|. |
| 151 // OSes can call through directly. | |
| 152 void SimulateKeyStatusChange() { | 151 void SimulateKeyStatusChange() { |
| 153 NSNotification* notif = | 152 NSNotification* notif = |
| 154 [NSNotification notificationWithName:NSWindowDidResignKeyNotification | 153 [NSNotification notificationWithName:NSWindowDidResignKeyNotification |
| 155 object:[controller_ window]]; | 154 object:[controller_ window]]; |
| 156 if (base::mac::IsOSLionOrLater()) | 155 [[NSNotificationCenter defaultCenter] postNotification:notif]; |
| 157 [[NSNotificationCenter defaultCenter] postNotification:notif]; | |
| 158 else | |
| 159 [controller_ windowDidResignKey:notif]; | |
| 160 } | 156 } |
| 161 | 157 |
| 162 protected: | 158 protected: |
| 163 base::scoped_nsobject<InfoBubbleWindow> bubble_window_; | 159 base::scoped_nsobject<InfoBubbleWindow> bubble_window_; |
| 164 BaseBubbleController* controller_; | 160 BaseBubbleController* controller_; |
| 165 | 161 |
| 166 private: | 162 private: |
| 167 DISALLOW_COPY_AND_ASSIGN(BaseBubbleControllerTest); | 163 DISALLOW_COPY_AND_ASSIGN(BaseBubbleControllerTest); |
| 168 }; | 164 }; |
| 169 | 165 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 [other_window makeKeyAndOrderFront:nil]; | 274 [other_window makeKeyAndOrderFront:nil]; |
| 279 SimulateKeyStatusChange(); | 275 SimulateKeyStatusChange(); |
| 280 | 276 |
| 281 EXPECT_FALSE([bubble_window_ isVisible]); | 277 EXPECT_FALSE([bubble_window_ isVisible]); |
| 282 EXPECT_TRUE([other_window isVisible]); | 278 EXPECT_TRUE([other_window isVisible]); |
| 283 } | 279 } |
| 284 | 280 |
| 285 // Test that clicking outside the window causes the bubble to close if | 281 // Test that clicking outside the window causes the bubble to close if |
| 286 // shouldCloseOnResignKey is YES. | 282 // shouldCloseOnResignKey is YES. |
| 287 TEST_F(BaseBubbleControllerTest, LionClickOutsideClosesWithoutContextMenu) { | 283 TEST_F(BaseBubbleControllerTest, LionClickOutsideClosesWithoutContextMenu) { |
| 288 // The event tap is only installed on 10.7+. | |
| 289 if (!base::mac::IsOSLionOrLater()) | |
| 290 return; | |
| 291 | |
| 292 base::scoped_nsobject<BaseBubbleController> keep_alive = ShowBubble(); | 284 base::scoped_nsobject<BaseBubbleController> keep_alive = ShowBubble(); |
| 293 NSWindow* window = [controller_ window]; | 285 NSWindow* window = [controller_ window]; |
| 294 | 286 |
| 295 EXPECT_TRUE([controller_ shouldCloseOnResignKey]); // Verify default value. | 287 EXPECT_TRUE([controller_ shouldCloseOnResignKey]); // Verify default value. |
| 296 [controller_ setShouldCloseOnResignKey:NO]; | 288 [controller_ setShouldCloseOnResignKey:NO]; |
| 297 NSEvent* event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow( | 289 NSEvent* event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow( |
| 298 NSMakePoint(10, 10), test_window()); | 290 NSMakePoint(10, 10), test_window()); |
| 299 [NSApp sendEvent:event]; | 291 [NSApp sendEvent:event]; |
| 300 | 292 |
| 301 EXPECT_TRUE([window isVisible]); | 293 EXPECT_TRUE([window isVisible]); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 320 event = cocoa_test_event_utils::RightMouseDownAtPointInWindow( | 312 event = cocoa_test_event_utils::RightMouseDownAtPointInWindow( |
| 321 NSMakePoint(10, 10), test_window()); | 313 NSMakePoint(10, 10), test_window()); |
| 322 [NSApp sendEvent:event]; | 314 [NSApp sendEvent:event]; |
| 323 | 315 |
| 324 EXPECT_FALSE([window isVisible]); | 316 EXPECT_FALSE([window isVisible]); |
| 325 } | 317 } |
| 326 | 318 |
| 327 // Test that right-clicking the window with displaying a context menu causes | 319 // Test that right-clicking the window with displaying a context menu causes |
| 328 // the bubble to close. | 320 // the bubble to close. |
| 329 TEST_F(BaseBubbleControllerTest, LionRightClickOutsideClosesWithContextMenu) { | 321 TEST_F(BaseBubbleControllerTest, LionRightClickOutsideClosesWithContextMenu) { |
| 330 // The event tap is only installed on 10.7+. | |
| 331 if (!base::mac::IsOSLionOrLater()) | |
| 332 return; | |
| 333 | |
| 334 base::scoped_nsobject<BaseBubbleController> keep_alive = ShowBubble(); | 322 base::scoped_nsobject<BaseBubbleController> keep_alive = ShowBubble(); |
| 335 NSWindow* window = [controller_ window]; | 323 NSWindow* window = [controller_ window]; |
| 336 | 324 |
| 337 base::scoped_nsobject<NSMenu> context_menu( | 325 base::scoped_nsobject<NSMenu> context_menu( |
| 338 [[NSMenu alloc] initWithTitle:@""]); | 326 [[NSMenu alloc] initWithTitle:@""]); |
| 339 [context_menu addItemWithTitle:@"ContextMenuTest" | 327 [context_menu addItemWithTitle:@"ContextMenuTest" |
| 340 action:nil | 328 action:nil |
| 341 keyEquivalent:@""]; | 329 keyEquivalent:@""]; |
| 342 base::scoped_nsobject<ContextMenuController> menu_controller( | 330 base::scoped_nsobject<ContextMenuController> menu_controller( |
| 343 [[ContextMenuController alloc] initWithMenu:context_menu | 331 [[ContextMenuController alloc] initWithMenu:context_menu |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 // Post the "exit fullscreen" notification. | 424 // Post the "exit fullscreen" notification. |
| 437 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; | 425 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 438 [center postNotificationName:NSWindowWillExitFullScreenNotification | 426 [center postNotificationName:NSWindowWillExitFullScreenNotification |
| 439 object:test_window()]; | 427 object:test_window()]; |
| 440 | 428 |
| 441 EXPECT_FALSE([bubble_window_ isVisible]); | 429 EXPECT_FALSE([bubble_window_ isVisible]); |
| 442 } | 430 } |
| 443 | 431 |
| 444 // Tests that a bubble will not close when it's becoming a key window. | 432 // Tests that a bubble will not close when it's becoming a key window. |
| 445 TEST_F(BaseBubbleControllerTest, StayOnFocus) { | 433 TEST_F(BaseBubbleControllerTest, StayOnFocus) { |
| 446 // The event tap is only installed on 10.7+. | |
| 447 if (!base::mac::IsOSLionOrLater()) | |
| 448 return; | |
| 449 | |
| 450 [controller_ setShouldOpenAsKeyWindow:NO]; | 434 [controller_ setShouldOpenAsKeyWindow:NO]; |
| 451 base::scoped_nsobject<BaseBubbleController> keep_alive = ShowBubble(); | 435 base::scoped_nsobject<BaseBubbleController> keep_alive = ShowBubble(); |
| 452 | 436 |
| 453 EXPECT_TRUE([bubble_window_ isVisible]); | 437 EXPECT_TRUE([bubble_window_ isVisible]); |
| 454 EXPECT_TRUE([controller_ shouldCloseOnResignKey]); // Verify default value. | 438 EXPECT_TRUE([controller_ shouldCloseOnResignKey]); // Verify default value. |
| 455 | 439 |
| 456 // Make the bubble a key window. | 440 // Make the bubble a key window. |
| 457 g_key_window = [controller_ window]; | 441 g_key_window = [controller_ window]; |
| 458 base::mac::ScopedObjCClassSwizzler swizzler( | 442 base::mac::ScopedObjCClassSwizzler swizzler( |
| 459 [NSApplication class], [FakeKeyWindow class], @selector(keyWindow)); | 443 [NSApplication class], [FakeKeyWindow class], @selector(keyWindow)); |
| 460 | 444 |
| 461 // Post the "resign key" notification for another window. | 445 // Post the "resign key" notification for another window. |
| 462 NSNotification* notif = | 446 NSNotification* notif = |
| 463 [NSNotification notificationWithName:NSWindowDidResignKeyNotification | 447 [NSNotification notificationWithName:NSWindowDidResignKeyNotification |
| 464 object:test_window()]; | 448 object:test_window()]; |
| 465 [[NSNotificationCenter defaultCenter] postNotification:notif]; | 449 [[NSNotificationCenter defaultCenter] postNotification:notif]; |
| 466 | 450 |
| 467 EXPECT_TRUE([bubble_window_ isVisible]); | 451 EXPECT_TRUE([bubble_window_ isVisible]); |
| 468 g_key_window = nil; | 452 g_key_window = nil; |
| 469 } | 453 } |
| OLD | NEW |