| 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 // Simple test view that counts calls to -[NSView mouseDown:]. |
| 15 @interface CocoaMouseCaptureTestView : NSView { |
| 16 @private |
| 17 int mouseDownCount_; |
| 18 } |
| 19 @property(readonly, nonatomic) int mouseDownCount; |
| 20 @end |
| 21 |
| 22 @implementation CocoaMouseCaptureTestView |
| 23 |
| 24 @synthesize mouseDownCount = mouseDownCount_; |
| 25 |
| 26 - (void)mouseDown:(NSEvent*)theEvent { |
| 27 ++mouseDownCount_; |
| 28 } |
| 29 |
| 30 @end |
| 31 |
| 32 namespace views { |
| 33 namespace { |
| 34 |
| 35 // Simple capture delegate that just counts events forwarded. |
| 36 class TestCaptureDelegate : public CocoaMouseCaptureDelegate { |
| 37 public: |
| 38 TestCaptureDelegate() : event_count_(0), capture_lost_count_(0) {} |
| 39 |
| 40 void Acquire() { mouse_capture_.reset(new CocoaMouseCapture(this)); } |
| 41 bool IsActive() { return mouse_capture_ && mouse_capture_->IsActive(); } |
| 42 void SimulateDestroy() { mouse_capture_.reset(); } |
| 43 |
| 44 int event_count() { return event_count_; } |
| 45 int capture_lost_count() { return capture_lost_count_; } |
| 46 |
| 47 // CocoaMouseCaptureDelegate: |
| 48 void PostCapturedEvent(NSEvent* event) override { ++event_count_; } |
| 49 void OnMouseCaptureLost() override { ++capture_lost_count_; } |
| 50 |
| 51 private: |
| 52 scoped_ptr<CocoaMouseCapture> mouse_capture_; |
| 53 int event_count_; |
| 54 int capture_lost_count_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(TestCaptureDelegate); |
| 57 }; |
| 58 |
| 59 } // namespace |
| 60 |
| 61 typedef ui::CocoaTest CocoaMouseCaptureTest; |
| 62 |
| 63 // Test that a new capture properly "steals" capture from an existing one. |
| 64 TEST_F(CocoaMouseCaptureTest, OnCaptureLost) { |
| 65 TestCaptureDelegate capture; |
| 66 |
| 67 capture.Acquire(); |
| 68 EXPECT_TRUE(capture.IsActive()); |
| 69 { |
| 70 TestCaptureDelegate capture2; |
| 71 EXPECT_EQ(0, capture.capture_lost_count()); |
| 72 |
| 73 // A second capture steals from the first. |
| 74 capture2.Acquire(); |
| 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()); |
| 79 |
| 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()); |
| 84 } |
| 85 |
| 86 // Re-acquiring is fine (not stealing). |
| 87 EXPECT_FALSE(capture.IsActive()); |
| 88 capture.Acquire(); |
| 89 EXPECT_TRUE(capture.IsActive()); |
| 90 |
| 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()); |
| 96 } |
| 97 |
| 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); |
| 105 |
| 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]); |
| 110 |
| 111 { |
| 112 TestCaptureDelegate capture; |
| 113 capture.Acquire(); |
| 114 |
| 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()); |
| 120 } |
| 121 |
| 122 // After the capture goes away, events should be received again. |
| 123 [NSApp sendEvent:click.first]; |
| 124 EXPECT_EQ(2, [view mouseDownCount]); |
| 125 } |
| 126 |
| 127 } // namespace views |
| OLD | NEW |