OLD | NEW |
---|---|
(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/view_root.h" | |
7 | |
8 namespace ui { | |
9 | |
10 using base::android::JavaParamRef; | |
11 | |
12 class TestViewRoot : public ViewRoot { | |
13 public: | |
14 TestViewRoot() : ViewRoot(0L) {} | |
15 float GetDipScale() override { return 1.f; } | |
16 }; | |
17 | |
18 class TestViewClient : public ViewClient { | |
19 public: | |
20 TestViewClient() : handle_event_(true), | |
21 called_(false) {} | |
22 | |
23 void SetHandleEvent(bool handle_event) { handle_event_ = handle_event; } | |
24 bool OnTouchEvent(const MotionEventData& event) override { | |
25 called_ = true; | |
26 return handle_event_; | |
27 } | |
28 | |
29 bool EventHandled() { return called_ && handle_event_; } | |
30 void Reset() { called_ = false; } | |
31 | |
32 private: | |
33 bool handle_event_; // Marks as event was consumed. True by default. | |
34 bool called_; | |
35 }; | |
36 | |
37 class ViewAndroidBoundsTest : public testing::Test { | |
38 public: | |
39 ViewAndroidBoundsTest() : view1_(&client1_), | |
40 view2_(&client2_), | |
41 view3_(&client3_) {} | |
42 void Reset() { | |
43 client1_.Reset(); | |
44 client2_.Reset(); | |
45 client3_.Reset(); | |
46 } | |
47 | |
48 void GenerateTouchEventAt(float x, float y) { | |
49 root_.OnTouchEvent(nullptr, | |
50 JavaParamRef<jobject>(nullptr), | |
51 JavaParamRef<jobject>(nullptr), | |
52 0L, // time | |
53 0, 1, 0, 0, | |
54 x, y, 0.f, 0.f, // pos | |
55 0, 0, // pointer_id | |
56 0.f, 0.f, 0.f, 0.f, // touch | |
57 0.f, 0.f, 0.f, 0.f, | |
58 0.f, 0.f, | |
59 0, 0, 0, 0, | |
60 false); | |
61 } | |
62 | |
63 TestViewRoot root_; | |
64 TestViewClient client1_; | |
65 TestViewClient client2_; | |
66 TestViewClient client3_; | |
67 ViewAndroid view1_; | |
68 ViewAndroid view2_; | |
69 ViewAndroid view3_; | |
70 }; | |
71 | |
72 TEST_F(ViewAndroidBoundsTest, MatchesViewInFront) { | |
73 view1_.SetBounds(gfx::Point(50, 50), 400, 600); | |
74 view2_.SetBounds(gfx::Point(50, 50), 400, 600); | |
75 root_.AddChild(&view2_); | |
76 root_.AddChild(&view1_); | |
77 | |
78 // root | |
79 // +-------+ | |
boliu
2017/01/24 23:47:13
err, first I got confused this is a diagram of coo
Jinsuk Kim
2017/01/25 02:34:34
Removed diagrams.
| |
80 // | | | |
81 // view1_ view2_ | |
82 | |
83 GenerateTouchEventAt(100.f, 100.f); | |
84 EXPECT_TRUE(client1_.EventHandled()); | |
boliu
2017/01/24 23:47:13
check view2 did not handle event, here and everywh
Jinsuk Kim
2017/01/25 02:34:34
Done.
| |
85 | |
86 Reset(); | |
87 | |
88 // View 2 moves up to front, and events should hit it from now. | |
89 root_.MoveToFront(&view2_); | |
90 GenerateTouchEventAt(100.f, 100.f); | |
91 | |
92 EXPECT_TRUE(client2_.EventHandled()); | |
93 } | |
94 | |
95 TEST_F(ViewAndroidBoundsTest, MatchesViewArea) { | |
96 // +----------------+ | |
97 // | view2_ | | |
98 // | | | |
99 // | +--------+ | | |
100 // | | x | | -> (100, 100) | |
101 // | | view1_ | | | |
102 // | | | | | |
103 // | +--------+ | | |
104 // | x | -> (300, 400) | |
105 // +----------------+ | |
106 view1_.SetBounds(gfx::Point(50, 50), 200, 200); | |
107 view2_.SetBounds(gfx::Point(20, 20), 400, 600); | |
108 | |
109 // root | |
110 // +-------+ | |
111 // | | | |
112 // view1_ view2_ | |
113 root_.AddChild(&view2_); | |
114 root_.AddChild(&view1_); | |
115 | |
116 // Falls within |view1_|'s bounds | |
117 GenerateTouchEventAt(100.f, 100.f); | |
118 EXPECT_TRUE(client1_.EventHandled()); | |
119 Reset(); | |
120 | |
121 // Falls within |view2_|'s bounds | |
122 GenerateTouchEventAt(300.f, 400.f); | |
123 EXPECT_TRUE(client2_.EventHandled()); | |
124 } | |
125 | |
126 TEST_F(ViewAndroidBoundsTest, MatchesViewAfterMove) { | |
127 // +----------------+ | |
128 // | view2_ | | |
129 // | | | |
130 // | +--------+ | | |
131 // | | x | | -> (100, 100) | |
132 // | | | | | |
133 // | +--------+ | | |
134 // | | | |
135 // +----------------+ | |
136 view1_.SetBounds(gfx::Point(50, 50), 200, 200); | |
137 view2_.SetBounds(gfx::Point(20, 20), 400, 600); | |
138 root_.AddChild(&view2_); | |
139 root_.AddChild(&view1_); | |
140 | |
141 GenerateTouchEventAt(100.f, 100.f); | |
142 EXPECT_TRUE(client1_.EventHandled()); | |
143 | |
144 Reset(); | |
145 | |
146 // The front view moved, so the same event now should hit the view below it. | |
147 // +----------------+ | |
148 // | view2_ | | |
149 // | | | |
150 // | x | -> (100, 100) | |
151 // | +--------+ | | |
152 // | | | | | |
153 // | | | | | |
154 // | +--------+ | | |
155 // +----------------+ | |
156 view1_.SetBounds(gfx::Point(150, 150), 200, 200); | |
157 GenerateTouchEventAt(100.f, 100.f); | |
158 EXPECT_TRUE(client2_.EventHandled()); | |
159 } | |
160 | |
161 TEST_F(ViewAndroidBoundsTest, MatchesViewSizeOfkMatchParent) { | |
162 // +----------------+ | |
163 // | view1_/view3_ | | |
164 // | | | |
165 // | +--------+ | | |
166 // | | | | | |
167 // | | view2_ | | | |
168 // | | x | | -> (100, 100) | |
169 // | +--------+ | | |
170 // | x | -> (300, 400) | |
171 // +----------------+ | |
172 | |
173 view1_.SetBounds(gfx::Point(20, 20), 400, 600); | |
174 view3_.SetBounds(gfx::Point(), | |
175 ViewAndroid::Bounds::kMatchParent, | |
176 ViewAndroid::Bounds::kMatchParent); | |
177 view2_.SetBounds(gfx::Point(50, 50), 200, 200); | |
178 | |
179 // root | |
180 // +-------+ | |
181 // | | | |
182 // view2_ view1_ | |
183 // | | |
184 // view3_ | |
185 | |
186 root_.AddChild(&view1_); | |
187 root_.AddChild(&view2_); | |
188 view1_.AddChild(&view3_); | |
189 | |
190 GenerateTouchEventAt(100.f, 100.f); | |
191 EXPECT_TRUE(client2_.EventHandled()); | |
192 Reset(); | |
193 | |
194 GenerateTouchEventAt(300.f, 400.f); | |
195 EXPECT_TRUE(client3_.EventHandled()); | |
196 Reset(); | |
197 | |
198 client3_.SetHandleEvent(false); | |
199 GenerateTouchEventAt(300.f, 400.f); | |
200 EXPECT_TRUE(client1_.EventHandled()); | |
201 Reset(); | |
202 } | |
203 | |
204 TEST_F(ViewAndroidBoundsTest, MatchesViewsWithOffset) { | |
205 // (10,20) | |
206 // +--------------------------+ | |
207 // | view1_ x | -> (70, 30) | |
208 // | +--------+ +--------+ | | |
209 // | | view2_ | | view3_ | | | |
210 // | | x | | | | -> (40, 60) | |
211 // | | | | x | | -> (100, 70) | |
212 // | +--------+ +--------+ | | |
213 // | | | |
214 // +--------------------------+ | |
215 view1_.SetBounds(gfx::Point(10, 20), 150, 100); | |
216 view2_.SetBounds(gfx::Point(20, 30), 40, 30); | |
217 view3_.SetBounds(gfx::Point(70, 30), 40, 30); | |
218 | |
219 // root | |
220 // | | |
221 // view1_ | |
222 // +-------+ | |
223 // | | | |
224 // view2_ view3_ | |
225 root_.AddChild(&view1_); | |
226 view1_.AddChild(&view2_); | |
227 view1_.AddChild(&view3_); | |
228 | |
229 GenerateTouchEventAt(70, 30); | |
230 EXPECT_TRUE(client1_.EventHandled()); | |
231 Reset(); | |
232 | |
233 GenerateTouchEventAt(40, 60); | |
234 EXPECT_TRUE(client2_.EventHandled()); | |
235 Reset(); | |
236 | |
237 GenerateTouchEventAt(100, 70); | |
238 EXPECT_TRUE(client3_.EventHandled()); | |
239 } | |
240 | |
241 } // namespace ui | |
OLD | NEW |