1 // Copyright (c) 2011 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/memory/scoped_nsobject.h" 8 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 9 #import "chrome/browser/ui/cocoa/wrench_menu/menu_tracked_button.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/platform_test.h" 12 13 // This test does not test what you'd think it does. Testing around event 14 // tracking run loops is probably not worh the effort when the size of the 15 // helper MakeEvent() is larger than the class being tested. If we ever figure 16 // out a good way to test event tracking, this should be revisited. 17 18 @interface MenuTrackedButtonTestReceiver : NSObject { 19 @public 20 BOOL didThat_; 21 } 22 - (void)doThat:(id)sender; 23 @end 24 @implementation MenuTrackedButtonTestReceiver 25 - (void)doThat:(id)sender { 26 didThat_ = YES; 27 } 28 @end 29 30 31 class MenuTrackedButtonTest : public CocoaTest { 32 public: 33 MenuTrackedButtonTest() : event_number_(0) {} 34 35 void SetUp() { 36 listener_.reset([[MenuTrackedButtonTestReceiver alloc] init]); 37 button_.reset( 38 [[MenuTrackedButton alloc] initWithFrame:NSMakeRect(10, 10, 50, 50)]); 39 [[test_window() contentView] addSubview:button()]; 40 [button_ setTarget:listener()]; 41 [button_ setAction:@selector(doThat:)]; 42 } 43 44 // Creates an event of |type|, with |location| in test_window()'s coordinates. 45 NSEvent* MakeEvent(NSEventType type, NSPoint location) { 46 NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate]; 47 location = [test_window() convertBaseToScreen:location]; 48 if (type == NSMouseEntered || type == NSMouseExited) { 49 return [NSEvent enterExitEventWithType:type 50 location:location 51 modifierFlags:0 52 timestamp:now 53 windowNumber:[test_window() windowNumber] 54 context:nil 55 eventNumber:event_number_++ 56 trackingNumber:0 57 userData:nil]; 58 } else { 59 return [NSEvent mouseEventWithType:type 60 location:location 61 modifierFlags:0 62 timestamp:now 63 windowNumber:[test_window() windowNumber] 64 context:nil 65 eventNumber:event_number_++ 66 clickCount:1 67 pressure:1.0]; 68 } 69 } 70 71 MenuTrackedButtonTestReceiver* listener() { return listener_.get(); } 72 NSButton* button() { return button_.get(); } 73 74 scoped_nsobject<MenuTrackedButtonTestReceiver> listener_; 75 scoped_nsobject<MenuTrackedButton> button_; 76 NSInteger event_number_; 77 }; 78 79 // User mouses over and then off. 80 TEST_F(MenuTrackedButtonTest, DISABLED_EnterExit) { 81 [NSApp postEvent:MakeEvent(NSMouseEntered, NSMakePoint(11, 11)) atStart:YES]; 82 [NSApp postEvent:MakeEvent(NSMouseExited, NSMakePoint(9, 9)) atStart:YES]; 83 EXPECT_FALSE(listener()->didThat_); 84 } 85 86 // User mouses over, clicks, drags, and exits. 87 TEST_F(MenuTrackedButtonTest, DISABLED_EnterDragExit) { 88 [NSApp postEvent:MakeEvent(NSMouseEntered, NSMakePoint(11, 11)) atStart:YES]; 89 [NSApp postEvent:MakeEvent(NSLeftMouseDown, NSMakePoint(12, 12)) atStart:YES]; 90 [NSApp postEvent:MakeEvent(NSLeftMouseDragged, NSMakePoint(13, 11)) 91 atStart:YES]; 92 [NSApp postEvent:MakeEvent(NSLeftMouseDragged, NSMakePoint(13, 10)) 93 atStart:YES]; 94 [NSApp postEvent:MakeEvent(NSMouseExited, NSMakePoint(13, 9)) atStart:YES]; 95 EXPECT_FALSE(listener()->didThat_); 96 } 97 98 // User mouses over, clicks, drags, and releases. 99 TEST_F(MenuTrackedButtonTest, DISABLED_EnterDragUp) { 100 [NSApp postEvent:MakeEvent(NSMouseEntered, NSMakePoint(11, 11)) atStart:YES]; 101 [NSApp postEvent:MakeEvent(NSLeftMouseDown, NSMakePoint(12, 12)) atStart:YES]; 102 [NSApp postEvent:MakeEvent(NSLeftMouseDragged, NSMakePoint(13, 13)) 103 atStart:YES]; 104 [NSApp postEvent:MakeEvent(NSLeftMouseUp, NSMakePoint(14, 14)) atStart:YES]; 105 EXPECT_TRUE(listener()->didThat_); 106 } 107 108 // User drags in and releases. 109 TEST_F(MenuTrackedButtonTest, DISABLED_DragUp) { 110 [NSApp postEvent:MakeEvent(NSLeftMouseDragged, NSMakePoint(11, 11)) 111 atStart:YES]; 112 [NSApp postEvent:MakeEvent(NSLeftMouseDragged, NSMakePoint(12, 12)) 113 atStart:YES]; 114 [NSApp postEvent:MakeEvent(NSLeftMouseUp, NSMakePoint(13, 13)) 115 atStart:YES]; 116 EXPECT_TRUE(listener()->didThat_); 117 } 118