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

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

Issue 2645353004: ViewRoot class for event forwarding on Android (Closed)
Patch Set: comments Created 3 years, 10 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/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 void ExpectHit(const TestViewClient& hitClient) {
64 TestViewClient* clients[3] = { &client1_, &client2_, &client3_ };
65 for (auto& client : clients) {
66 if (&hitClient == client)
67 EXPECT_TRUE(client->EventHandled());
68 else
69 EXPECT_FALSE(client->EventHandled());
70 }
71 }
72
73 TestViewRoot root_;
74 TestViewClient client1_;
75 TestViewClient client2_;
76 TestViewClient client3_;
77 ViewAndroid view1_;
78 ViewAndroid view2_;
79 ViewAndroid view3_;
80 };
81
82 TEST_F(ViewAndroidBoundsTest, MatchesViewInFront) {
83 view1_.SetLayout(gfx::Rect(50, 50, 400, 600), false);
84 view2_.SetLayout(gfx::Rect(50, 50, 400, 600), false);
85 root_.AddChild(&view2_);
86 root_.AddChild(&view1_);
87
88 GenerateTouchEventAt(100.f, 100.f);
89 ExpectHit(client1_);
90
91 Reset();
92
93 // View 2 moves up to front, and events should hit it from now.
94 root_.MoveToFront(&view2_);
95 GenerateTouchEventAt(100.f, 100.f);
96
97 ExpectHit(client2_);
98 }
99
100 TEST_F(ViewAndroidBoundsTest, MatchesViewArea) {
101 view1_.SetLayout(gfx::Rect(50, 50, 200, 200), false);
102 view2_.SetLayout(gfx::Rect(20, 20, 400, 600), false);
103
104 root_.AddChild(&view2_);
105 root_.AddChild(&view1_);
106
107 // Falls within |view1_|'s bounds
108 GenerateTouchEventAt(100.f, 100.f);
109 ExpectHit(client1_);
110 Reset();
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(gfx::Rect(50, 50, 200, 200), false);
119 view2_.SetLayout(gfx::Rect(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 Reset();
127
128 view1_.SetLayout(gfx::Rect(150, 150, 200, 200), false);
129 GenerateTouchEventAt(100.f, 100.f);
130 ExpectHit(client2_);
131 }
132
133 TEST_F(ViewAndroidBoundsTest, MatchesViewSizeOfkMatchParent) {
134 view1_.SetLayout(gfx::Rect(20, 20, 400, 600), false);
135 view3_.SetLayout(gfx::Rect(), true);
136 view2_.SetLayout(gfx::Rect(50, 50, 200, 200), false);
137
138 root_.AddChild(&view1_);
139 root_.AddChild(&view2_);
140 view1_.AddChild(&view3_);
141
142 GenerateTouchEventAt(100.f, 100.f);
143 ExpectHit(client2_);
144 Reset();
145
146 GenerateTouchEventAt(300.f, 400.f);
147 ExpectHit(client3_);
148 Reset();
149
150 client3_.SetHandleEvent(false);
151 GenerateTouchEventAt(300.f, 400.f);
152 ExpectHit(client1_);
153 Reset();
154 }
155
156 TEST_F(ViewAndroidBoundsTest, MatchesViewsWithOffset) {
157 view1_.SetLayout(gfx::Rect(10, 20, 150, 100), false);
158 view2_.SetLayout(gfx::Rect(20, 30, 40, 30), false);
159 view3_.SetLayout(gfx::Rect(70, 30, 40, 30), false);
160
161 root_.AddChild(&view1_);
162 view1_.AddChild(&view2_);
163 view1_.AddChild(&view3_);
164
165 GenerateTouchEventAt(70, 30);
166 ExpectHit(client1_);
167 Reset();
168
169 GenerateTouchEventAt(40, 60);
170 ExpectHit(client2_);
171 Reset();
172
173 GenerateTouchEventAt(100, 70);
174 ExpectHit(client3_);
175 }
176
177 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698