OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #import "ui/views/cocoa/cocoa_mouse_capture.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 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" |
| 13 |
| 14 @interface CocoaMouseCaptureTestView : NSView { |
| 15 int mouseDownCount_; |
| 16 } |
| 17 @property(readonly, nonatomic) int mouseDownCount; |
| 18 @end |
| 19 |
| 20 @implementation CocoaMouseCaptureTestView |
| 21 |
| 22 @synthesize mouseDownCount = mouseDownCount_; |
| 23 |
| 24 - (void)mouseDown:(NSEvent*)theEvent { |
| 25 ++mouseDownCount_; |
| 26 } |
| 27 |
| 28 @end |
| 29 |
| 30 namespace views { |
| 31 namespace { |
| 32 |
| 33 // Simple capture delegate that just counts events. |
| 34 class TestCaptureDelegate : public CocoaMouseCaptureDelegate { |
| 35 public: |
| 36 TestCaptureDelegate() : event_count_(0), capture_lost_count_(0) {} |
| 37 |
| 38 void Acquire() { mouse_capture_.reset(new CocoaMouseCapture(this)); } |
| 39 bool IsActive() { return mouse_capture_ && mouse_capture_->IsActive(); } |
| 40 void SimulateDestroy() { mouse_capture_.reset(); } |
| 41 |
| 42 int event_count() { return event_count_; } |
| 43 int capture_lost_count() { return capture_lost_count_; } |
| 44 |
| 45 // CocoaMouseCaptureDelegate: |
| 46 void PostCapturedEvent(NSEvent* event) override { ++event_count_; } |
| 47 void OnMouseCaptureLost() override { ++capture_lost_count_; } |
| 48 |
| 49 private: |
| 50 scoped_ptr<CocoaMouseCapture> mouse_capture_; |
| 51 int event_count_; |
| 52 int capture_lost_count_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(TestCaptureDelegate); |
| 55 }; |
| 56 |
| 57 } // namespace |
| 58 |
| 59 typedef ui::CocoaTest CocoaMouseCaptureTest; |
| 60 |
| 61 // Test that a new capture properly "steals" capture from an existing one. |
| 62 TEST_F(CocoaMouseCaptureTest, OnCaptureLost) { |
| 63 TestCaptureDelegate capture; |
| 64 |
| 65 capture.Acquire(); |
| 66 EXPECT_TRUE(capture.IsActive()); |
| 67 { |
| 68 TestCaptureDelegate capture2; |
| 69 EXPECT_EQ(0, capture.capture_lost_count()); |
| 70 |
| 71 // A second capture steals from the first. |
| 72 capture2.Acquire(); |
| 73 EXPECT_TRUE(capture2.IsActive()); |
| 74 EXPECT_FALSE(capture.IsActive()); |
| 75 EXPECT_EQ(1, capture.capture_lost_count()); |
| 76 EXPECT_EQ(0, capture2.capture_lost_count()); |
| 77 |
| 78 // Simulate capture2 going out of scope. Inspect it. |
| 79 capture2.SimulateDestroy(); |
| 80 EXPECT_FALSE(capture2.IsActive()); |
| 81 EXPECT_EQ(1, capture2.capture_lost_count()); |
| 82 } |
| 83 |
| 84 // Re-acquiring is fine (not stealing). |
| 85 EXPECT_FALSE(capture.IsActive()); |
| 86 capture.Acquire(); |
| 87 EXPECT_TRUE(capture.IsActive()); |
| 88 |
| 89 // Having no CocoaMouseCapture instance is fine. |
| 90 capture.SimulateDestroy(); |
| 91 EXPECT_FALSE(capture.IsActive()); |
| 92 // Receives OnMouseCaptureLost again, since reacquired. |
| 93 EXPECT_EQ(2, capture.capture_lost_count()); |
| 94 } |
| 95 |
| 96 // Test event capture. |
| 97 TEST_F(CocoaMouseCaptureTest, CaptureEvents) { |
| 98 base::scoped_nsobject<CocoaMouseCaptureTestView> view( |
| 99 [[CocoaMouseCaptureTestView alloc] initWithFrame:NSZeroRect]); |
| 100 [test_window() setContentView:view]; |
| 101 std::pair<NSEvent*, NSEvent*> click = |
| 102 cocoa_test_event_utils::MouseClickInView(view, 1); |
| 103 |
| 104 // First check that the view receives events normally. |
| 105 EXPECT_EQ(0, [view mouseDownCount]); |
| 106 [NSApp sendEvent:click.first]; |
| 107 EXPECT_EQ(1, [view mouseDownCount]); |
| 108 |
| 109 { |
| 110 TestCaptureDelegate capture; |
| 111 capture.Acquire(); |
| 112 |
| 113 // Now check that the capture captures events. |
| 114 EXPECT_EQ(0, capture.event_count()); |
| 115 [NSApp sendEvent:click.first]; |
| 116 EXPECT_EQ(1, [view mouseDownCount]); |
| 117 EXPECT_EQ(1, capture.event_count()); |
| 118 } |
| 119 |
| 120 // After the capture goes away, events should be received again. |
| 121 [NSApp sendEvent:click.first]; |
| 122 EXPECT_EQ(2, [view mouseDownCount]); |
| 123 } |
| 124 |
| 125 } // namespace views |
OLD | NEW |