1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/draggable_button.h"
#import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#import "ui/events/test/cocoa_test_event_utils.h"
@interface TestableDraggableButton : DraggableButton {
NSUInteger dragCount_;
BOOL wasTriggered_;
}
- (void)trigger:(id)sender;
- (BOOL)wasTriggered;
- (NSUInteger)dragCount;
@end
@implementation TestableDraggableButton
- (id)initWithFrame:(NSRect)frame {
if ((self = [super initWithFrame:frame])) {
dragCount_ = 0;
wasTriggered_ = NO;
}
return self;
}
- (void)beginDrag:(NSEvent*)theEvent {
dragCount_++;
}
- (void)trigger:(id)sender {
wasTriggered_ = YES;
}
- (BOOL)wasTriggered {
return wasTriggered_;
}
- (NSUInteger)dragCount {
return dragCount_;
}
@end
class DraggableButtonTest : public CocoaTest {};
// Make sure the basic case of "click" still works.
TEST_F(DraggableButtonTest, DownUp) {
base::scoped_nsobject<TestableDraggableButton> button(
[[TestableDraggableButton alloc]
initWithFrame:NSMakeRect(0, 0, 500, 500)]);
[[test_window() contentView] addSubview:button.get()];
[button setTarget:button];
[button setAction:@selector(trigger:)];
EXPECT_FALSE([button wasTriggered]);
NSEvent* downEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10),
NSLeftMouseDown,
0);
NSEvent* upEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10),
NSLeftMouseUp,
0);
[NSApp postEvent:upEvent atStart:YES];
[test_window() sendEvent:downEvent];
EXPECT_TRUE([button wasTriggered]); // confirms target/action fired
}
TEST_F(DraggableButtonTest, DraggableHysteresis) {
base::scoped_nsobject<TestableDraggableButton> button(
[[TestableDraggableButton alloc]
initWithFrame:NSMakeRect(0, 0, 500, 500)]);
[[test_window() contentView] addSubview:button.get()];
NSEvent* downEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10),
NSLeftMouseDown,
0);
NSEvent* firstMove =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(11,11),
NSLeftMouseDragged,
0);
NSEvent* firstUpEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(11,11),
NSLeftMouseUp,
0);
NSEvent* secondMove =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100),
NSLeftMouseDragged,
0);
NSEvent* secondUpEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100),
NSLeftMouseUp,
0);
// If the mouse only moves one pixel in each direction
// it should not cause a drag.
[NSApp postEvent:firstUpEvent atStart:YES];
[NSApp postEvent:firstMove atStart:YES];
[button mouseDown:downEvent];
EXPECT_EQ(0U, [button dragCount]);
// If the mouse moves > 5 pixels in either direciton
// it should cause a drag.
[NSApp postEvent:secondUpEvent atStart:YES];
[NSApp postEvent:secondMove atStart:YES];
[button mouseDown:downEvent];
EXPECT_EQ(1U, [button dragCount]);
}
TEST_F(DraggableButtonTest, ResetState) {
base::scoped_nsobject<TestableDraggableButton> button(
[[TestableDraggableButton alloc]
initWithFrame:NSMakeRect(0, 0, 500, 500)]);
[[test_window() contentView] addSubview:button.get()];
NSEvent* downEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10),
NSLeftMouseDown,
0);
NSEvent* moveEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100),
NSLeftMouseDragged,
0);
NSEvent* upEvent =
cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(100,100),
NSLeftMouseUp,
0);
// If the mouse moves > 5 pixels in either direciton it should cause a drag.
[NSApp postEvent:upEvent atStart:YES];
[NSApp postEvent:moveEvent atStart:YES];
[button mouseDown:downEvent];
// The button should not be highlighted after the drag finishes.
EXPECT_FALSE([[button cell] isHighlighted]);
EXPECT_EQ(1U, [button dragCount]);
// We should be able to initiate another drag immediately after the first one.
[NSApp postEvent:upEvent atStart:YES];
[NSApp postEvent:moveEvent atStart:YES];
[button mouseDown:downEvent];
EXPECT_EQ(2U, [button dragCount]);
EXPECT_FALSE([[button cell] isHighlighted]);
}
|