Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: ui/android/view_android_unittests.cc

Issue 2708613002: Add EventForwarder for routing touch events (Closed)
Patch Set: base::Bind (doesn't compile yet) Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/android/event_handler.h"
7 #include "ui/android/view_android.h"
8
9 namespace ui {
10
11 using base::android::JavaParamRef;
12
13 class TestViewAndroid : public ViewAndroid {
14 public:
15 TestViewAndroid(ViewClient* client) : ViewAndroid(client) {}
16 float GetDipScale() override { return 1.f; }
17 };
18
19 class TestViewClient : public ViewClient {
20 public:
21 TestViewClient() : handle_event_(true), called_(false) {}
22
23 void SetHandleEvent(bool handle_event) { handle_event_ = handle_event; }
24 bool OnTouchEvent(const MotionEventAndroid& event,
25 bool for_touch_handle) override {
26 called_ = true;
27 return handle_event_;
28 }
29
30 bool EventHandled() { return called_ && handle_event_; }
31 void Reset() { called_ = false; }
32
33 private:
34 bool handle_event_; // Marks as event was consumed. True by default.
35 bool called_;
36 };
37
38 class ViewAndroidBoundsTest : public testing::Test {
39 public:
40 ViewAndroidBoundsTest()
41 : root_(nullptr),
42 view1_(&client1_),
43 view2_(&client2_),
44 view3_(&client3_) {
45 event_handler_.reset(new EventHandler(&root_));
46 root_.SetLayout(0, 0, 0, 0, true); // match parent
47 }
48
49 void Reset() {
50 client1_.Reset();
51 client2_.Reset();
52 client3_.Reset();
53 }
54
55 void GenerateTouchEventAt(float x, float y) {
56 event_handler_->OnTouchEvent(
57 nullptr, JavaParamRef<jobject>(nullptr), JavaParamRef<jobject>(nullptr),
58 0L, // time
59 0, 1, 0, 0, x, y, 0.f, 0.f, // pos
60 0, 0, // pointer_id
61 0.f, 0.f, 0.f, 0.f, // touch
62 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0, 0, 0, 0, false);
63 }
64
65 void ExpectHit(const TestViewClient& hitClient) {
66 TestViewClient* clients[3] = {&client1_, &client2_, &client3_};
67 for (auto& client : clients) {
68 if (&hitClient == client)
69 EXPECT_TRUE(client->EventHandled());
70 else
71 EXPECT_FALSE(client->EventHandled());
72 }
73 Reset();
74 }
75
76 TestViewAndroid root_;
77 std::unique_ptr<EventHandler> event_handler_;
78 TestViewClient client1_;
79 TestViewClient client2_;
80 TestViewClient client3_;
81 TestViewAndroid view1_;
82 TestViewAndroid view2_;
83 TestViewAndroid view3_;
84 };
85
86 TEST_F(ViewAndroidBoundsTest, MatchesViewInFront) {
87 view1_.SetLayout(50, 50, 400, 600, false);
88 view2_.SetLayout(50, 50, 400, 600, false);
89 root_.AddChild(&view2_);
90 root_.AddChild(&view1_);
91
92 GenerateTouchEventAt(100.f, 100.f);
93 ExpectHit(client1_);
94
95 // View 2 moves up to the top, and events should hit it from now.
96 root_.MoveToFront(&view2_);
97 GenerateTouchEventAt(100.f, 100.f);
98 ExpectHit(client2_);
Khushal 2017/03/07 04:17:08 Could you also check that all ancestors to the nod
Jinsuk Kim 2017/03/07 05:02:24 Good suggestion. Added |EventCalled()| and applied
99 }
100
101 TEST_F(ViewAndroidBoundsTest, MatchesViewArea) {
102 view1_.SetLayout(50, 50, 200, 200, false);
103 view2_.SetLayout(20, 20, 400, 600, false);
104
105 root_.AddChild(&view2_);
106 root_.AddChild(&view1_);
107
108 // Falls within |view1_|'s bounds
109 GenerateTouchEventAt(100.f, 100.f);
110 ExpectHit(client1_);
111
112 // Falls within |view2_|'s bounds
113 GenerateTouchEventAt(300.f, 400.f);
114 ExpectHit(client2_);
115 }
116
117 TEST_F(ViewAndroidBoundsTest, MatchesViewAfterMove) {
118 view1_.SetLayout(50, 50, 200, 200, false);
119 view2_.SetLayout(20, 20, 400, 600, false);
120 root_.AddChild(&view2_);
121 root_.AddChild(&view1_);
122
123 GenerateTouchEventAt(100.f, 100.f);
124 ExpectHit(client1_);
125
126 view1_.SetLayout(150, 150, 200, 200, false);
127 GenerateTouchEventAt(100.f, 100.f);
128 ExpectHit(client2_);
129 }
130
131 TEST_F(ViewAndroidBoundsTest, MatchesViewSizeOfkMatchParent) {
132 view1_.SetLayout(20, 20, 400, 600, false);
133 view3_.SetLayout(0, 0, 0, 0, true); // match parent
134 view2_.SetLayout(50, 50, 200, 200, false);
135
136 root_.AddChild(&view1_);
137 root_.AddChild(&view2_);
138 view1_.AddChild(&view3_);
139
140 GenerateTouchEventAt(100.f, 100.f);
141 ExpectHit(client2_);
142
143 GenerateTouchEventAt(300.f, 400.f);
144 ExpectHit(client3_);
145
146 client3_.SetHandleEvent(false);
147 GenerateTouchEventAt(300.f, 400.f);
148 ExpectHit(client1_);
149 }
150
151 TEST_F(ViewAndroidBoundsTest, MatchesViewsWithOffset) {
152 view1_.SetLayout(10, 20, 150, 100, false);
153 view2_.SetLayout(20, 30, 40, 30, false);
154 view3_.SetLayout(70, 30, 40, 30, false);
155
156 root_.AddChild(&view1_);
157 view1_.AddChild(&view2_);
158 view1_.AddChild(&view3_);
159
160 GenerateTouchEventAt(70, 30);
161 ExpectHit(client1_);
162
163 GenerateTouchEventAt(40, 60);
164 ExpectHit(client2_);
165
166 GenerateTouchEventAt(100, 70);
167 ExpectHit(client3_);
168 }
169
170 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698