Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7646)

Unified Diff: chrome/browser/ui/cocoa/base_bubble_controller_unittest.mm

Issue 300113009: Fix BaseBubbleController close issue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: style fixing Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/ui/cocoa/base_bubble_controller.mm ('k') | ui/events/test/cocoa_test_event_utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/ui/cocoa/base_bubble_controller_unittest.mm
diff --git a/chrome/browser/ui/cocoa/base_bubble_controller_unittest.mm b/chrome/browser/ui/cocoa/base_bubble_controller_unittest.mm
index 8e04a9ec123599553c7cf637f99c8ab424dc267b..c5248f75b2e2d3fbdc9cacad4933bccd8334eb91 100644
--- a/chrome/browser/ui/cocoa/base_bubble_controller_unittest.mm
+++ b/chrome/browser/ui/cocoa/base_bubble_controller_unittest.mm
@@ -8,7 +8,6 @@
#import "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#import "chrome/browser/ui/cocoa/info_bubble_view.h"
-#import "chrome/browser/ui/cocoa/run_loop_testing.h"
#import "ui/events/test/cocoa_test_event_utils.h"
namespace {
@@ -18,6 +17,72 @@ const CGFloat kAnchorPointX = 400;
const CGFloat kAnchorPointY = 300;
} // namespace
+@interface ContextMenuController : NSObject<NSMenuDelegate> {
+ @private
+ NSMenu* menu_;
+ NSWindow* window_;
+ BOOL isMenuOpen_;
+ BOOL didOpen_;
+}
+
+- (id)initWithMenu:(NSMenu*)menu andWindow:(NSWindow*)window;
+
+- (BOOL)isMenuOpen;
+- (BOOL)didOpen;
+- (BOOL)isWindowVisible;
+
+// NSMenuDelegate methods
+- (void)menuWillOpen:(NSMenu*)menu;
+- (void)menuDidClose:(NSMenu*)menu;
+
+@end
+
+@implementation ContextMenuController
+
+- (id)initWithMenu:(NSMenu*)menu andWindow:(NSWindow*)window {
+ if (self = [super init]) {
+ menu_ = menu;
+ window_ = window;
+ isMenuOpen_ = NO;
+ didOpen_ = NO;
+ [menu_ setDelegate:self];
+ }
+ return self;
+}
+
+- (BOOL)isMenuOpen {
+ return isMenuOpen_;
+}
+
+- (BOOL)didOpen {
+ return didOpen_;
+}
+
+- (BOOL)isWindowVisible {
+ if (window_) {
+ return [window_ isVisible];
+ }
+ return NO;
+}
+
+- (void)menuWillOpen:(NSMenu*)menu {
+ isMenuOpen_ = YES;
+ didOpen_ = NO;
+
+ NSArray* modes = @[NSEventTrackingRunLoopMode, NSDefaultRunLoopMode];
+ [menu_ performSelector:@selector(cancelTracking)
+ withObject:nil
+ afterDelay:0.1
+ inModes:modes];
+}
+
+- (void)menuDidClose:(NSMenu*)menu {
+ isMenuOpen_ = NO;
+ didOpen_ = YES;
+}
+
+@end
+
class BaseBubbleControllerTest : public CocoaTest {
public:
virtual void SetUp() OVERRIDE {
@@ -174,7 +239,7 @@ TEST_F(BaseBubbleControllerTest, ResignKeyCloses) {
// Test that clicking outside the window causes the bubble to close if
// shouldCloseOnResignKey is YES.
-TEST_F(BaseBubbleControllerTest, LionClickOutsideCloses) {
+TEST_F(BaseBubbleControllerTest, LionClickOutsideClosesWithoutContextMenu) {
// The event tap is only installed on 10.7+.
if (!base::mac::IsOSLionOrLater())
return;
@@ -194,7 +259,12 @@ TEST_F(BaseBubbleControllerTest, LionClickOutsideCloses) {
NSEvent* event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
NSMakePoint(10, 10), test_window());
[NSApp sendEvent:event];
- chrome::testing::NSRunLoopRunAllPending();
+
+ EXPECT_TRUE([window isVisible]);
+
+ event = cocoa_test_event_utils::RightMouseDownAtPointInWindow(
+ NSMakePoint(10, 10), test_window());
+ [NSApp sendEvent:event];
EXPECT_TRUE([window isVisible]);
@@ -202,7 +272,70 @@ TEST_F(BaseBubbleControllerTest, LionClickOutsideCloses) {
event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
NSMakePoint(10, 10), test_window());
[NSApp sendEvent:event];
- chrome::testing::NSRunLoopRunAllPending();
EXPECT_FALSE([window isVisible]);
+
+ [controller_ showWindow:nil]; // Show it again
+ EXPECT_TRUE([window isVisible]);
+ EXPECT_TRUE([controller_ shouldCloseOnResignKey]); // Verify.
+
+ event = cocoa_test_event_utils::RightMouseDownAtPointInWindow(
+ NSMakePoint(10, 10), test_window());
+ [NSApp sendEvent:event];
+
+ EXPECT_FALSE([window isVisible]);
+}
+
+// Test that right-clicking the window with displaying a context menu causes
+// the bubble to close.
+TEST_F(BaseBubbleControllerTest, LionRightClickOutsideClosesWithContextMenu) {
+ // The event tap is only installed on 10.7+.
+ if (!base::mac::IsOSLionOrLater())
+ return;
+
+ // Closing the bubble will autorelease the controller.
+ base::scoped_nsobject<BaseBubbleController> keep_alive([controller_ retain]);
+ NSWindow* window = [controller_ window];
+
+ EXPECT_TRUE([controller_ shouldCloseOnResignKey]); // Verify default value.
+ EXPECT_FALSE([window isVisible]);
+
+ [controller_ showWindow:nil];
+
+ EXPECT_TRUE([window isVisible]);
+
+ base::scoped_nsobject<NSMenu> context_menu(
+ [[NSMenu alloc] initWithTitle:@""]);
+ [context_menu addItemWithTitle:@"ContextMenuTest"
+ action:nil
+ keyEquivalent:@""];
+ base::scoped_nsobject<ContextMenuController> menu_controller(
+ [[ContextMenuController alloc] initWithMenu:context_menu
+ andWindow:window]);
+
+ // Set the menu as the contextual menu of contentView of test_window().
+ [[test_window() contentView] setMenu:context_menu];
+
+ // RightMouseDown in test_window() would close the bubble window and then
+ // dispaly the contextual menu.
+ NSEvent* event = cocoa_test_event_utils::RightMouseDownAtPointInWindow(
+ NSMakePoint(10, 10), test_window());
+ // Verify bubble's window is closed when contextual menu is open.
+ CFRunLoopPerformBlock(CFRunLoopGetCurrent(), NSEventTrackingRunLoopMode, ^{
+ EXPECT_TRUE([menu_controller isMenuOpen]);
+ EXPECT_FALSE([menu_controller isWindowVisible]);
+ });
+
+ EXPECT_FALSE([menu_controller isMenuOpen]);
+ EXPECT_FALSE([menu_controller didOpen]);
+
+ [NSApp sendEvent:event];
+
+ // When we got here, menu has already run its RunLoop.
+ // See -[ContextualMenuController menuWillOpen:].
+ EXPECT_FALSE([window isVisible]);
+
+ EXPECT_FALSE([menu_controller isMenuOpen]);
+ EXPECT_TRUE([menu_controller didOpen]);
}
+
« no previous file with comments | « chrome/browser/ui/cocoa/base_bubble_controller.mm ('k') | ui/events/test/cocoa_test_event_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698