| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/memory/scoped_nsobject.h" | 5 #include "base/mac/scoped_nsobject.h" |
| 6 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 6 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 7 #import "chrome/browser/ui/cocoa/draggable_button.h" | 7 #import "chrome/browser/ui/cocoa/draggable_button.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "testing/platform_test.h" | 9 #include "testing/platform_test.h" |
| 10 #import "ui/base/test/cocoa_test_event_utils.h" | 10 #import "ui/base/test/cocoa_test_event_utils.h" |
| 11 | 11 |
| 12 @interface TestableDraggableButton : DraggableButton { | 12 @interface TestableDraggableButton : DraggableButton { |
| 13 NSUInteger dragCount_; | 13 NSUInteger dragCount_; |
| 14 BOOL wasTriggered_; | 14 BOOL wasTriggered_; |
| 15 } | 15 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 - (NSUInteger)dragCount { | 41 - (NSUInteger)dragCount { |
| 42 return dragCount_; | 42 return dragCount_; |
| 43 } | 43 } |
| 44 @end | 44 @end |
| 45 | 45 |
| 46 class DraggableButtonTest : public CocoaTest {}; | 46 class DraggableButtonTest : public CocoaTest {}; |
| 47 | 47 |
| 48 // Make sure the basic case of "click" still works. | 48 // Make sure the basic case of "click" still works. |
| 49 TEST_F(DraggableButtonTest, DownUp) { | 49 TEST_F(DraggableButtonTest, DownUp) { |
| 50 scoped_nsobject<TestableDraggableButton> button( | 50 base::scoped_nsobject<TestableDraggableButton> button( |
| 51 [[TestableDraggableButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]); | 51 [[TestableDraggableButton alloc] |
| 52 initWithFrame:NSMakeRect(0, 0, 500, 500)]); |
| 52 [[test_window() contentView] addSubview:button.get()]; | 53 [[test_window() contentView] addSubview:button.get()]; |
| 53 [button setTarget:button]; | 54 [button setTarget:button]; |
| 54 [button setAction:@selector(trigger:)]; | 55 [button setAction:@selector(trigger:)]; |
| 55 EXPECT_FALSE([button wasTriggered]); | 56 EXPECT_FALSE([button wasTriggered]); |
| 56 NSEvent* downEvent = | 57 NSEvent* downEvent = |
| 57 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), | 58 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), |
| 58 NSLeftMouseDown, | 59 NSLeftMouseDown, |
| 59 0); | 60 0); |
| 60 NSEvent* upEvent = | 61 NSEvent* upEvent = |
| 61 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), | 62 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), |
| 62 NSLeftMouseUp, | 63 NSLeftMouseUp, |
| 63 0); | 64 0); |
| 64 [NSApp postEvent:upEvent atStart:YES]; | 65 [NSApp postEvent:upEvent atStart:YES]; |
| 65 [test_window() sendEvent:downEvent]; | 66 [test_window() sendEvent:downEvent]; |
| 66 EXPECT_TRUE([button wasTriggered]); // confirms target/action fired | 67 EXPECT_TRUE([button wasTriggered]); // confirms target/action fired |
| 67 } | 68 } |
| 68 | 69 |
| 69 TEST_F(DraggableButtonTest, DraggableHysteresis) { | 70 TEST_F(DraggableButtonTest, DraggableHysteresis) { |
| 70 scoped_nsobject<TestableDraggableButton> button( | 71 base::scoped_nsobject<TestableDraggableButton> button( |
| 71 [[TestableDraggableButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]); | 72 [[TestableDraggableButton alloc] |
| 73 initWithFrame:NSMakeRect(0, 0, 500, 500)]); |
| 72 [[test_window() contentView] addSubview:button.get()]; | 74 [[test_window() contentView] addSubview:button.get()]; |
| 73 NSEvent* downEvent = | 75 NSEvent* downEvent = |
| 74 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), | 76 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), |
| 75 NSLeftMouseDown, | 77 NSLeftMouseDown, |
| 76 0); | 78 0); |
| 77 NSEvent* firstMove = | 79 NSEvent* firstMove = |
| 78 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(11,11), | 80 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(11,11), |
| 79 NSLeftMouseDragged, | 81 NSLeftMouseDragged, |
| 80 0); | 82 0); |
| 81 NSEvent* firstUpEvent = | 83 NSEvent* firstUpEvent = |
| (...skipping 17 matching lines...) Expand all Loading... |
| 99 | 101 |
| 100 // If the mouse moves > 5 pixels in either direciton | 102 // If the mouse moves > 5 pixels in either direciton |
| 101 // it should cause a drag. | 103 // it should cause a drag. |
| 102 [NSApp postEvent:secondUpEvent atStart:YES]; | 104 [NSApp postEvent:secondUpEvent atStart:YES]; |
| 103 [NSApp postEvent:secondMove atStart:YES]; | 105 [NSApp postEvent:secondMove atStart:YES]; |
| 104 [button mouseDown:downEvent]; | 106 [button mouseDown:downEvent]; |
| 105 EXPECT_EQ(1U, [button dragCount]); | 107 EXPECT_EQ(1U, [button dragCount]); |
| 106 } | 108 } |
| 107 | 109 |
| 108 TEST_F(DraggableButtonTest, ResetState) { | 110 TEST_F(DraggableButtonTest, ResetState) { |
| 109 scoped_nsobject<TestableDraggableButton> button( | 111 base::scoped_nsobject<TestableDraggableButton> button( |
| 110 [[TestableDraggableButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]); | 112 [[TestableDraggableButton alloc] |
| 113 initWithFrame:NSMakeRect(0, 0, 500, 500)]); |
| 111 [[test_window() contentView] addSubview:button.get()]; | 114 [[test_window() contentView] addSubview:button.get()]; |
| 112 NSEvent* downEvent = | 115 NSEvent* downEvent = |
| 113 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), | 116 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10), |
| 114 NSLeftMouseDown, | 117 NSLeftMouseDown, |
| 115 0); | 118 0); |
| 116 NSEvent* moveEvent = | 119 NSEvent* moveEvent = |
| 117 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100), | 120 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100), |
| 118 NSLeftMouseDragged, | 121 NSLeftMouseDragged, |
| 119 0); | 122 0); |
| 120 NSEvent* upEvent = | 123 NSEvent* upEvent = |
| 121 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100), | 124 cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100), |
| 122 NSLeftMouseUp, | 125 NSLeftMouseUp, |
| 123 0); | 126 0); |
| 124 // If the mouse moves > 5 pixels in either direciton it should cause a drag. | 127 // If the mouse moves > 5 pixels in either direciton it should cause a drag. |
| 125 [NSApp postEvent:upEvent atStart:YES]; | 128 [NSApp postEvent:upEvent atStart:YES]; |
| 126 [NSApp postEvent:moveEvent atStart:YES]; | 129 [NSApp postEvent:moveEvent atStart:YES]; |
| 127 [button mouseDown:downEvent]; | 130 [button mouseDown:downEvent]; |
| 128 | 131 |
| 129 // The button should not be highlighted after the drag finishes. | 132 // The button should not be highlighted after the drag finishes. |
| 130 EXPECT_FALSE([[button cell] isHighlighted]); | 133 EXPECT_FALSE([[button cell] isHighlighted]); |
| 131 EXPECT_EQ(1U, [button dragCount]); | 134 EXPECT_EQ(1U, [button dragCount]); |
| 132 | 135 |
| 133 // We should be able to initiate another drag immediately after the first one. | 136 // We should be able to initiate another drag immediately after the first one. |
| 134 [NSApp postEvent:upEvent atStart:YES]; | 137 [NSApp postEvent:upEvent atStart:YES]; |
| 135 [NSApp postEvent:moveEvent atStart:YES]; | 138 [NSApp postEvent:moveEvent atStart:YES]; |
| 136 [button mouseDown:downEvent]; | 139 [button mouseDown:downEvent]; |
| 137 EXPECT_EQ(2U, [button dragCount]); | 140 EXPECT_EQ(2U, [button dragCount]); |
| 138 EXPECT_FALSE([[button cell] isHighlighted]); | 141 EXPECT_FALSE([[button cell] isHighlighted]); |
| 139 } | 142 } |
| OLD | NEW |