1 // Copyright 2014 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.
5 #import "ui/views/cocoa/cocoa_mouse_capture.h"
7 #import <Cocoa/Cocoa.h>
9 #import "base/mac/scoped_nsobject.h"
10 #import "ui/events/test/cocoa_test_event_utils.h"
11 #import "ui/gfx/test/ui_cocoa_test_helper.h"
12 #import "ui/views/cocoa/cocoa_mouse_capture_delegate.h"
14 // Simple test view that counts calls to -[NSView mouseDown:].
15 @interface CocoaMouseCaptureTestView : NSView {
19 @property(readonly, nonatomic) int mouseDownCount;
22 @implementation CocoaMouseCaptureTestView
24 @synthesize mouseDownCount = mouseDownCount_;
26 - (void)mouseDown:(NSEvent*)theEvent {
35 // Simple capture delegate that just counts events forwarded.
36 class TestCaptureDelegate : public CocoaMouseCaptureDelegate {
38 TestCaptureDelegate() : event_count_(0), capture_lost_count_(0) {}
40 void Acquire() { mouse_capture_.reset(new CocoaMouseCapture(this)); }
41 bool IsActive() { return mouse_capture_ && mouse_capture_->IsActive(); }
42 void SimulateDestroy() { mouse_capture_.reset(); }
44 int event_count() { return event_count_; }
45 int capture_lost_count() { return capture_lost_count_; }
47 // CocoaMouseCaptureDelegate:
48 void PostCapturedEvent(NSEvent* event) override { ++event_count_; }
49 void OnMouseCaptureLost() override { ++capture_lost_count_; }
52 scoped_ptr<CocoaMouseCapture> mouse_capture_;
54 int capture_lost_count_;
56 DISALLOW_COPY_AND_ASSIGN(TestCaptureDelegate);
61 typedef ui::CocoaTest CocoaMouseCaptureTest;
63 // Test that a new capture properly "steals" capture from an existing one.
64 TEST_F(CocoaMouseCaptureTest, OnCaptureLost) {
65 TestCaptureDelegate capture;
68 EXPECT_TRUE(capture.IsActive());
70 TestCaptureDelegate capture2;
71 EXPECT_EQ(0, capture.capture_lost_count());
73 // A second capture steals from the first.
75 EXPECT_TRUE(capture2.IsActive());
76 EXPECT_FALSE(capture.IsActive());
77 EXPECT_EQ(1, capture.capture_lost_count());
78 EXPECT_EQ(0, capture2.capture_lost_count());
80 // Simulate capture2 going out of scope. Inspect it.
81 capture2.SimulateDestroy();
82 EXPECT_FALSE(capture2.IsActive());
83 EXPECT_EQ(1, capture2.capture_lost_count());
86 // Re-acquiring is fine (not stealing).
87 EXPECT_FALSE(capture.IsActive());
89 EXPECT_TRUE(capture.IsActive());
91 // Having no CocoaMouseCapture instance is fine.
92 capture.SimulateDestroy();
93 EXPECT_FALSE(capture.IsActive());
94 // Receives OnMouseCaptureLost again, since reacquired.
95 EXPECT_EQ(2, capture.capture_lost_count());
98 // Test event capture.
99 TEST_F(CocoaMouseCaptureTest, CaptureEvents) {
100 base::scoped_nsobject<CocoaMouseCaptureTestView> view(
101 [[CocoaMouseCaptureTestView alloc] initWithFrame:NSZeroRect]);
102 [test_window() setContentView:view];
103 std::pair<NSEvent*, NSEvent*> click =
104 cocoa_test_event_utils::MouseClickInView(view, 1);
106 // First check that the view receives events normally.
107 EXPECT_EQ(0, [view mouseDownCount]);
108 [NSApp sendEvent:click.first];
109 EXPECT_EQ(1, [view mouseDownCount]);
112 TestCaptureDelegate capture;
115 // Now check that the capture captures events.
116 EXPECT_EQ(0, capture.event_count());
117 [NSApp sendEvent:click.first];
118 EXPECT_EQ(1, [view mouseDownCount]);
119 EXPECT_EQ(1, capture.event_count());
122 // After the capture goes away, events should be received again.
123 [NSApp sendEvent:click.first];
124 EXPECT_EQ(2, [view mouseDownCount]);