OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #import "ui/views/cocoa/cocoa_mouse_capture.h" | 5 #import "ui/views/cocoa/cocoa_mouse_capture.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 } | 29 } |
30 | 30 |
31 @end | 31 @end |
32 | 32 |
33 namespace views { | 33 namespace views { |
34 namespace { | 34 namespace { |
35 | 35 |
36 // Simple capture delegate that just counts events forwarded. | 36 // Simple capture delegate that just counts events forwarded. |
37 class TestCaptureDelegate : public CocoaMouseCaptureDelegate { | 37 class TestCaptureDelegate : public CocoaMouseCaptureDelegate { |
38 public: | 38 public: |
39 TestCaptureDelegate() : event_count_(0), capture_lost_count_(0) {} | 39 TestCaptureDelegate(NSWindow* window) |
tapted
2016/06/24 07:19:11
nit: explicit
karandeepb
2016/06/24 09:32:34
Done.
| |
40 : event_count_(0), capture_lost_count_(0), window_(window) {} | |
40 | 41 |
41 void Acquire() { mouse_capture_.reset(new CocoaMouseCapture(this)); } | 42 void Acquire() { mouse_capture_.reset(new CocoaMouseCapture(this)); } |
42 bool IsActive() { return mouse_capture_ && mouse_capture_->IsActive(); } | 43 bool IsActive() { return mouse_capture_ && mouse_capture_->IsActive(); } |
43 void SimulateDestroy() { mouse_capture_.reset(); } | 44 void SimulateDestroy() { mouse_capture_.reset(); } |
44 | 45 |
45 int event_count() { return event_count_; } | 46 int event_count() { return event_count_; } |
46 int capture_lost_count() { return capture_lost_count_; } | 47 int capture_lost_count() { return capture_lost_count_; } |
47 | 48 |
48 // CocoaMouseCaptureDelegate: | 49 // CocoaMouseCaptureDelegate: |
49 void PostCapturedEvent(NSEvent* event) override { ++event_count_; } | 50 void PostCapturedEvent(NSEvent* event) override { ++event_count_; } |
50 void OnMouseCaptureLost() override { ++capture_lost_count_; } | 51 void OnMouseCaptureLost() override { ++capture_lost_count_; } |
52 NSWindow* GetWindow() const override { return window_; } | |
51 | 53 |
52 private: | 54 private: |
53 std::unique_ptr<CocoaMouseCapture> mouse_capture_; | 55 std::unique_ptr<CocoaMouseCapture> mouse_capture_; |
54 int event_count_; | 56 int event_count_; |
55 int capture_lost_count_; | 57 int capture_lost_count_; |
58 NSWindow* window_; | |
56 | 59 |
57 DISALLOW_COPY_AND_ASSIGN(TestCaptureDelegate); | 60 DISALLOW_COPY_AND_ASSIGN(TestCaptureDelegate); |
58 }; | 61 }; |
59 | 62 |
60 } // namespace | 63 } // namespace |
61 | 64 |
62 typedef ui::CocoaTest CocoaMouseCaptureTest; | 65 typedef ui::CocoaTest CocoaMouseCaptureTest; |
63 | 66 |
64 // Test that a new capture properly "steals" capture from an existing one. | 67 // Test that a new capture properly "steals" capture from an existing one. |
65 TEST_F(CocoaMouseCaptureTest, OnCaptureLost) { | 68 TEST_F(CocoaMouseCaptureTest, OnCaptureLost) { |
66 TestCaptureDelegate capture; | 69 TestCaptureDelegate capture(nil); |
67 | 70 |
68 capture.Acquire(); | 71 capture.Acquire(); |
69 EXPECT_TRUE(capture.IsActive()); | 72 EXPECT_TRUE(capture.IsActive()); |
70 { | 73 { |
71 TestCaptureDelegate capture2; | 74 TestCaptureDelegate capture2(nil); |
72 EXPECT_EQ(0, capture.capture_lost_count()); | 75 EXPECT_EQ(0, capture.capture_lost_count()); |
73 | 76 |
74 // A second capture steals from the first. | 77 // A second capture steals from the first. |
75 capture2.Acquire(); | 78 capture2.Acquire(); |
76 EXPECT_TRUE(capture2.IsActive()); | 79 EXPECT_TRUE(capture2.IsActive()); |
77 EXPECT_FALSE(capture.IsActive()); | 80 EXPECT_FALSE(capture.IsActive()); |
78 EXPECT_EQ(1, capture.capture_lost_count()); | 81 EXPECT_EQ(1, capture.capture_lost_count()); |
79 EXPECT_EQ(0, capture2.capture_lost_count()); | 82 EXPECT_EQ(0, capture2.capture_lost_count()); |
80 | 83 |
81 // Simulate capture2 going out of scope. Inspect it. | 84 // Simulate capture2 going out of scope. Inspect it. |
(...skipping 21 matching lines...) Expand all Loading... | |
103 [test_window() setContentView:view]; | 106 [test_window() setContentView:view]; |
104 std::pair<NSEvent*, NSEvent*> click = | 107 std::pair<NSEvent*, NSEvent*> click = |
105 cocoa_test_event_utils::MouseClickInView(view, 1); | 108 cocoa_test_event_utils::MouseClickInView(view, 1); |
106 | 109 |
107 // First check that the view receives events normally. | 110 // First check that the view receives events normally. |
108 EXPECT_EQ(0, [view mouseDownCount]); | 111 EXPECT_EQ(0, [view mouseDownCount]); |
109 [NSApp sendEvent:click.first]; | 112 [NSApp sendEvent:click.first]; |
110 EXPECT_EQ(1, [view mouseDownCount]); | 113 EXPECT_EQ(1, [view mouseDownCount]); |
111 | 114 |
112 { | 115 { |
113 TestCaptureDelegate capture; | 116 TestCaptureDelegate capture(test_window()); |
114 capture.Acquire(); | 117 capture.Acquire(); |
115 | 118 |
116 // Now check that the capture captures events. | 119 // Now check that the capture captures events. |
117 EXPECT_EQ(0, capture.event_count()); | 120 EXPECT_EQ(0, capture.event_count()); |
118 [NSApp sendEvent:click.first]; | 121 [NSApp sendEvent:click.first]; |
119 EXPECT_EQ(1, [view mouseDownCount]); | 122 EXPECT_EQ(1, [view mouseDownCount]); |
120 EXPECT_EQ(1, capture.event_count()); | 123 EXPECT_EQ(1, capture.event_count()); |
121 } | 124 } |
122 | 125 |
123 // After the capture goes away, events should be received again. | 126 // After the capture goes away, events should be received again. |
124 [NSApp sendEvent:click.first]; | 127 [NSApp sendEvent:click.first]; |
125 EXPECT_EQ(2, [view mouseDownCount]); | 128 EXPECT_EQ(2, [view mouseDownCount]); |
126 } | 129 } |
127 | 130 |
128 } // namespace views | 131 } // namespace views |
OLD | NEW |