| 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_client.h" |
| 7 #include "ui/android/view_root.h" |
| 8 |
| 9 namespace ui { |
| 10 |
| 11 using base::android::JavaParamRef; |
| 12 |
| 13 class TestViewRoot : public ViewRoot { |
| 14 public: |
| 15 TestViewRoot() : ViewRoot(0L) {} |
| 16 |
| 17 // Overrides |OnTouchEvent| to skip getting dip scale |
| 18 // and UMA record update not necessary for this test). |
| 19 jboolean OnTouchEvent(JNIEnv* env, |
| 20 const JavaParamRef<jobject>& obj, |
| 21 const JavaParamRef<jobject>& motion_event, |
| 22 jlong time_ms, |
| 23 jint android_action, |
| 24 jint pointer_count, |
| 25 jint history_size, |
| 26 jint action_index, |
| 27 jfloat pos_x_0, |
| 28 jfloat pos_y_0, |
| 29 jfloat pos_x_1, |
| 30 jfloat pos_y_1, |
| 31 jint pointer_id_0, |
| 32 jint pointer_id_1, |
| 33 jfloat touch_major_0, |
| 34 jfloat touch_major_1, |
| 35 jfloat touch_minor_0, |
| 36 jfloat touch_minor_1, |
| 37 jfloat orientation_0, |
| 38 jfloat orientation_1, |
| 39 jfloat tilt_0, |
| 40 jfloat tilt_1, |
| 41 jfloat raw_pos_x, |
| 42 jfloat raw_pos_y, |
| 43 jint android_tool_type_0, |
| 44 jint android_tool_type_1, |
| 45 jint android_button_state, |
| 46 jint android_meta_state, |
| 47 jboolean is_touch_handle_event) { |
| 48 |
| 49 MotionEventAndroid::Pointer pointer0(pointer_id_0, |
| 50 pos_x_0, |
| 51 pos_y_0, |
| 52 touch_major_0, |
| 53 touch_minor_0, |
| 54 orientation_0, |
| 55 tilt_0, |
| 56 android_tool_type_0); |
| 57 MotionEventAndroid::Pointer pointer1(pointer_id_1, |
| 58 pos_x_1, |
| 59 pos_y_1, |
| 60 touch_major_1, |
| 61 touch_minor_1, |
| 62 orientation_1, |
| 63 tilt_1, |
| 64 android_tool_type_1); |
| 65 MotionEventAndroid event(1.f, |
| 66 env, |
| 67 motion_event, |
| 68 time_ms, |
| 69 android_action, |
| 70 pointer_count, |
| 71 history_size, |
| 72 action_index, |
| 73 android_button_state, |
| 74 android_meta_state, |
| 75 raw_pos_x - pos_x_0, |
| 76 raw_pos_y - pos_y_0, |
| 77 &pointer0, |
| 78 &pointer1); |
| 79 |
| 80 return OnTouchEventInternal(event, is_touch_handle_event); |
| 81 } |
| 82 }; |
| 83 |
| 84 class TestViewClient : public ViewClient { |
| 85 public: |
| 86 TestViewClient() : handle_event_(true), |
| 87 called_(false) {} |
| 88 |
| 89 bool OnTouchEvent(const MotionEventAndroid& event, |
| 90 bool is_touch_handle_event) override { |
| 91 called_ = true; |
| 92 return handle_event_; |
| 93 } |
| 94 |
| 95 void SetHandleEvent(bool handle_event) { handle_event_ = handle_event; } |
| 96 bool EventHandled() { return handle_event_ && called_; } |
| 97 void Reset() { called_ = false; } |
| 98 |
| 99 private: |
| 100 bool handle_event_; |
| 101 bool called_; |
| 102 }; |
| 103 |
| 104 class ViewAndroidBoundsTest : public testing::Test { |
| 105 public: |
| 106 ViewAndroidBoundsTest() : view1_(&client1_), |
| 107 view2_(&client2_), |
| 108 view3_(&client3_) {} |
| 109 void Reset() { |
| 110 client1_.Reset(); |
| 111 client2_.Reset(); |
| 112 client3_.Reset(); |
| 113 } |
| 114 |
| 115 void GenerateTouchEventAt(float x, float y) { |
| 116 root_.OnTouchEvent(nullptr, |
| 117 base::android::JavaParamRef<jobject>(nullptr), |
| 118 base::android::JavaParamRef<jobject>(nullptr), |
| 119 0L, // time |
| 120 0, 1, 0, 0, |
| 121 x, y, 0.f, 0.f, // pos |
| 122 0, 0, // pointer_id |
| 123 0.f, 0.f, 0.f, 0.f, // touch |
| 124 0.f, 0.f, 0.f, 0.f, |
| 125 0.f, 0.f, |
| 126 0, 0, 0, 0, |
| 127 false); |
| 128 } |
| 129 |
| 130 TestViewRoot root_; |
| 131 TestViewClient client1_; |
| 132 TestViewClient client2_; |
| 133 TestViewClient client3_; |
| 134 ViewAndroid view1_; |
| 135 ViewAndroid view2_; |
| 136 ViewAndroid view3_; |
| 137 }; |
| 138 |
| 139 TEST_F(ViewAndroidBoundsTest, MatchesViewInFront) { |
| 140 view1_.SetBounds(gfx::Point(50, 50), 400, 600); |
| 141 view2_.SetBounds(gfx::Point(50, 50), 400, 600); |
| 142 root_.AddChild(&view2_); |
| 143 root_.AddChild(&view1_); |
| 144 |
| 145 // root |
| 146 // +-------+ |
| 147 // | | |
| 148 // view1_ view2_ |
| 149 |
| 150 GenerateTouchEventAt(100.f, 100.f); |
| 151 EXPECT_TRUE(client1_.EventHandled()); |
| 152 EXPECT_FALSE(client2_.EventHandled()); |
| 153 |
| 154 Reset(); |
| 155 |
| 156 // View 2 moves up to front, and events should hit it from now. |
| 157 root_.MoveToFront(&view2_); |
| 158 GenerateTouchEventAt(100.f, 100.f); |
| 159 |
| 160 EXPECT_FALSE(client1_.EventHandled()); |
| 161 EXPECT_TRUE(client2_.EventHandled()); |
| 162 } |
| 163 |
| 164 TEST_F(ViewAndroidBoundsTest, MatchesViewArea) { |
| 165 // +----------------+ |
| 166 // | view2_ | |
| 167 // | | |
| 168 // | +--------+ | |
| 169 // | | x | | -> (100, 100) |
| 170 // | | view1_ | | |
| 171 // | | | | |
| 172 // | +--------+ | |
| 173 // | x | -> (300, 400) |
| 174 // +----------------+ |
| 175 view1_.SetBounds(gfx::Point(50, 50), 200, 200); |
| 176 view2_.SetBounds(gfx::Point(20, 20), 400, 600); |
| 177 |
| 178 // root |
| 179 // +-------+ |
| 180 // | | |
| 181 // view1_ view2_ |
| 182 root_.AddChild(&view2_); |
| 183 root_.AddChild(&view1_); |
| 184 |
| 185 // Falls within |view1_|'s bounds |
| 186 GenerateTouchEventAt(100.f, 100.f); |
| 187 EXPECT_TRUE(client1_.EventHandled()); |
| 188 EXPECT_FALSE(client2_.EventHandled()); |
| 189 Reset(); |
| 190 |
| 191 // Falls within |view2_|'s bounds |
| 192 GenerateTouchEventAt(300.f, 400.f); |
| 193 EXPECT_FALSE(client1_.EventHandled()); |
| 194 EXPECT_TRUE(client2_.EventHandled()); |
| 195 } |
| 196 |
| 197 TEST_F(ViewAndroidBoundsTest, MatchesViewAfterMove) { |
| 198 // +----------------+ |
| 199 // | view2_ | |
| 200 // | | |
| 201 // | +--------+ | |
| 202 // | | x | | -> (100, 100) |
| 203 // | | | | |
| 204 // | +--------+ | |
| 205 // | | |
| 206 // +----------------+ |
| 207 view1_.SetBounds(gfx::Point(50, 50), 200, 200); |
| 208 view2_.SetBounds(gfx::Point(20, 20), 400, 600); |
| 209 root_.AddChild(&view2_); |
| 210 root_.AddChild(&view1_); |
| 211 |
| 212 GenerateTouchEventAt(100.f, 100.f); |
| 213 EXPECT_TRUE(client1_.EventHandled()); |
| 214 EXPECT_FALSE(client2_.EventHandled()); |
| 215 |
| 216 Reset(); |
| 217 |
| 218 // The front view moved, so the same event now should hit the view below it. |
| 219 // +----------------+ |
| 220 // | view2_ | |
| 221 // | | |
| 222 // | x | -> (100, 100) |
| 223 // | +--------+ | |
| 224 // | | | | |
| 225 // | | | | |
| 226 // | +--------+ | |
| 227 // +----------------+ |
| 228 view1_.SetBounds(gfx::Point(150, 150), 200, 200); |
| 229 GenerateTouchEventAt(100.f, 100.f); |
| 230 EXPECT_FALSE(client1_.EventHandled()); |
| 231 EXPECT_TRUE(client2_.EventHandled()); |
| 232 } |
| 233 |
| 234 TEST_F(ViewAndroidBoundsTest, MatchesViewSizeOfkMatchParent) { |
| 235 // +----------------+ |
| 236 // | view1_/view3_ | |
| 237 // | | |
| 238 // | +--------+ | |
| 239 // | | | | |
| 240 // | | view2_ | | |
| 241 // | | x | | -> (100, 100) |
| 242 // | +--------+ | |
| 243 // | x | -> (300, 400) |
| 244 // +----------------+ |
| 245 |
| 246 view1_.SetBounds(gfx::Point(20, 20), 400, 600); |
| 247 view3_.SetBounds(gfx::Point(), |
| 248 ViewAndroid::Bounds::kMatchParent, |
| 249 ViewAndroid::Bounds::kMatchParent); |
| 250 view2_.SetBounds(gfx::Point(50, 50), 200, 200); |
| 251 |
| 252 // root |
| 253 // +-------+ |
| 254 // | | |
| 255 // view2_ view1_ |
| 256 // | |
| 257 // view3_ |
| 258 |
| 259 root_.AddChild(&view1_); |
| 260 root_.AddChild(&view2_); |
| 261 view1_.AddChild(&view3_); |
| 262 |
| 263 GenerateTouchEventAt(100.f, 100.f); |
| 264 EXPECT_FALSE(client1_.EventHandled()); |
| 265 EXPECT_TRUE(client2_.EventHandled()); |
| 266 EXPECT_FALSE(client3_.EventHandled()); |
| 267 Reset(); |
| 268 |
| 269 // |view3_|'s bounds matches with that of |view1_|. The event should hit |
| 270 // |view3_| since |view1_|'s client does not handle events. |
| 271 client1_.SetHandleEvent(false); |
| 272 GenerateTouchEventAt(300.f, 400.f); |
| 273 EXPECT_FALSE(client1_.EventHandled()); |
| 274 EXPECT_FALSE(client2_.EventHandled()); |
| 275 EXPECT_TRUE(client3_.EventHandled()); |
| 276 } |
| 277 |
| 278 } // namespace ui |
| OLD | NEW |