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

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 Reset();
72 }
73
74 TestViewRoot root_;
75 TestViewClient client1_;
76 TestViewClient client2_;
77 TestViewClient client3_;
78 ViewAndroid view1_;
79 ViewAndroid view2_;
80 ViewAndroid view3_;
81 };
82
83 TEST_F(ViewAndroidBoundsTest, MatchesViewInFront) {
84 view1_.SetLayout(50, 50, 400, 600, false);
85 view2_.SetLayout(50, 50, 400, 600, false);
86 root_.AddChild(&view2_);
87 root_.AddChild(&view1_);
88
89 GenerateTouchEventAt(100.f, 100.f);
90 ExpectHit(client1_);
91
92 // View 2 moves up to the top, and events should hit it from now.
93 root_.MoveToTop(&view2_);
94 GenerateTouchEventAt(100.f, 100.f);
95 ExpectHit(client2_);
96 }
97
98 TEST_F(ViewAndroidBoundsTest, MatchesViewArea) {
99 view1_.SetLayout(50, 50, 200, 200, false);
100 view2_.SetLayout(20, 20, 400, 600, false);
101
102 root_.AddChild(&view2_);
103 root_.AddChild(&view1_);
104
105 // Falls within |view1_|'s bounds
106 GenerateTouchEventAt(100.f, 100.f);
107 ExpectHit(client1_);
108
109 // Falls within |view2_|'s bounds
110 GenerateTouchEventAt(300.f, 400.f);
111 ExpectHit(client2_);
112 }
113
114 TEST_F(ViewAndroidBoundsTest, MatchesViewAfterMove) {
115 view1_.SetLayout(50, 50, 200, 200, false);
116 view2_.SetLayout(20, 20, 400, 600, false);
117 root_.AddChild(&view2_);
118 root_.AddChild(&view1_);
119
120 GenerateTouchEventAt(100.f, 100.f);
121 ExpectHit(client1_);
122
123 view1_.SetLayout(150, 150, 200, 200, false);
124 GenerateTouchEventAt(100.f, 100.f);
125 ExpectHit(client2_);
126 }
127
128 TEST_F(ViewAndroidBoundsTest, MatchesViewSizeOfkMatchParent) {
129 view1_.SetLayout(20, 20, 400, 600, false);
130 view3_.SetLayout(0, 0, 0, 0, true); // match parent
131 view2_.SetLayout(50, 50, 200, 200, false);
132
133 root_.AddChild(&view1_);
134 root_.AddChild(&view2_);
135 view1_.AddChild(&view3_);
136
137 GenerateTouchEventAt(100.f, 100.f);
138 ExpectHit(client2_);
139
140 GenerateTouchEventAt(300.f, 400.f);
141 ExpectHit(client3_);
142
143 client3_.SetHandleEvent(false);
144 GenerateTouchEventAt(300.f, 400.f);
145 ExpectHit(client1_);
146 }
147
148 TEST_F(ViewAndroidBoundsTest, MatchesViewsWithOffset) {
149 view1_.SetLayout(10, 20, 150, 100, false);
150 view2_.SetLayout(20, 30, 40, 30, false);
151 view3_.SetLayout(70, 30, 40, 30, false);
152
153 root_.AddChild(&view1_);
154 view1_.AddChild(&view2_);
155 view1_.AddChild(&view3_);
156
157 GenerateTouchEventAt(70, 30);
158 ExpectHit(client1_);
159
160 GenerateTouchEventAt(40, 60);
161 ExpectHit(client2_);
162
163 GenerateTouchEventAt(100, 70);
164 ExpectHit(client3_);
165 }
166
167 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698