| 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/event_forwarder.h" |
| 7 #include "ui/android/view_android.h" |
| 8 #include "ui/android/view_client.h" |
| 9 #include "ui/events/android/motion_event_android.h" |
| 10 |
| 11 namespace ui { |
| 12 |
| 13 using base::android::JavaParamRef; |
| 14 |
| 15 class TestViewClient : public ViewClient { |
| 16 public: |
| 17 TestViewClient() : handle_event_(true), called_(false) {} |
| 18 |
| 19 void SetHandleEvent(bool handle_event) { handle_event_ = handle_event; } |
| 20 bool OnTouchEvent(const MotionEventAndroid& event, |
| 21 bool for_touch_handle) override { |
| 22 called_ = true; |
| 23 return handle_event_; |
| 24 } |
| 25 |
| 26 bool EventHandled() { return called_ && handle_event_; } |
| 27 bool EventCalled() { return called_; } |
| 28 void Reset() { called_ = false; } |
| 29 |
| 30 private: |
| 31 bool handle_event_; // Marks as event was consumed. True by default. |
| 32 bool called_; |
| 33 }; |
| 34 |
| 35 class ViewAndroidBoundsTest : public testing::Test { |
| 36 public: |
| 37 ViewAndroidBoundsTest() |
| 38 : root_(nullptr), |
| 39 view1_(&client1_), |
| 40 view2_(&client2_), |
| 41 view3_(&client3_) { |
| 42 root_.GetEventForwarder(); |
| 43 root_.SetLayout(ViewAndroid::LayoutParams::MatchParent()); |
| 44 } |
| 45 |
| 46 void Reset() { |
| 47 client1_.Reset(); |
| 48 client2_.Reset(); |
| 49 client3_.Reset(); |
| 50 } |
| 51 |
| 52 void GenerateTouchEventAt(float x, float y) { |
| 53 ui::MotionEventAndroid::Pointer pointer0(0, x, y, 0, 0, 0, 0, 0); |
| 54 ui::MotionEventAndroid::Pointer pointer1(0, 0, 0, 0, 0, 0, 0, 0); |
| 55 ui::MotionEventAndroid event(1.f, nullptr, JavaParamRef<jobject>(nullptr), |
| 56 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, &pointer0, |
| 57 &pointer1); |
| 58 root_.OnTouchEvent(event, false); |
| 59 } |
| 60 |
| 61 void ExpectHit(const TestViewClient& hitClient) { |
| 62 TestViewClient* clients[3] = {&client1_, &client2_, &client3_}; |
| 63 for (auto* client : clients) { |
| 64 if (&hitClient == client) |
| 65 EXPECT_TRUE(client->EventHandled()); |
| 66 else |
| 67 EXPECT_FALSE(client->EventHandled()); |
| 68 } |
| 69 Reset(); |
| 70 } |
| 71 |
| 72 ViewAndroid root_; |
| 73 ViewAndroid view1_; |
| 74 ViewAndroid view2_; |
| 75 ViewAndroid view3_; |
| 76 TestViewClient client1_; |
| 77 TestViewClient client2_; |
| 78 TestViewClient client3_; |
| 79 }; |
| 80 |
| 81 TEST_F(ViewAndroidBoundsTest, MatchesViewInFront) { |
| 82 view1_.SetLayout(ViewAndroid::LayoutParams::Normal(50, 50, 400, 600)); |
| 83 view2_.SetLayout(ViewAndroid::LayoutParams::Normal(50, 50, 400, 600)); |
| 84 root_.AddChild(&view2_); |
| 85 root_.AddChild(&view1_); |
| 86 |
| 87 GenerateTouchEventAt(100.f, 100.f); |
| 88 ExpectHit(client1_); |
| 89 |
| 90 // View 2 moves up to the top, and events should hit it from now. |
| 91 root_.MoveToFront(&view2_); |
| 92 GenerateTouchEventAt(100.f, 100.f); |
| 93 ExpectHit(client2_); |
| 94 } |
| 95 |
| 96 TEST_F(ViewAndroidBoundsTest, MatchesViewArea) { |
| 97 view1_.SetLayout(ViewAndroid::LayoutParams::Normal(50, 50, 200, 200)); |
| 98 view2_.SetLayout(ViewAndroid::LayoutParams::Normal(20, 20, 400, 600)); |
| 99 |
| 100 root_.AddChild(&view2_); |
| 101 root_.AddChild(&view1_); |
| 102 |
| 103 // Falls within |view1_|'s bounds |
| 104 GenerateTouchEventAt(100.f, 100.f); |
| 105 ExpectHit(client1_); |
| 106 |
| 107 // Falls within |view2_|'s bounds |
| 108 GenerateTouchEventAt(300.f, 400.f); |
| 109 ExpectHit(client2_); |
| 110 } |
| 111 |
| 112 TEST_F(ViewAndroidBoundsTest, MatchesViewAfterMove) { |
| 113 view1_.SetLayout(ViewAndroid::LayoutParams::Normal(50, 50, 200, 200)); |
| 114 view2_.SetLayout(ViewAndroid::LayoutParams::Normal(20, 20, 400, 600)); |
| 115 root_.AddChild(&view2_); |
| 116 root_.AddChild(&view1_); |
| 117 |
| 118 GenerateTouchEventAt(100.f, 100.f); |
| 119 ExpectHit(client1_); |
| 120 |
| 121 view1_.SetLayout(ViewAndroid::LayoutParams::Normal(150, 150, 200, 200)); |
| 122 GenerateTouchEventAt(100.f, 100.f); |
| 123 ExpectHit(client2_); |
| 124 } |
| 125 |
| 126 TEST_F(ViewAndroidBoundsTest, MatchesViewSizeOfkMatchParent) { |
| 127 view1_.SetLayout(ViewAndroid::LayoutParams::Normal(20, 20, 400, 600)); |
| 128 view3_.SetLayout(ViewAndroid::LayoutParams::MatchParent()); |
| 129 view2_.SetLayout(ViewAndroid::LayoutParams::Normal(50, 50, 200, 200)); |
| 130 |
| 131 root_.AddChild(&view1_); |
| 132 root_.AddChild(&view2_); |
| 133 view1_.AddChild(&view3_); |
| 134 |
| 135 GenerateTouchEventAt(100.f, 100.f); |
| 136 ExpectHit(client2_); |
| 137 |
| 138 GenerateTouchEventAt(300.f, 400.f); |
| 139 ExpectHit(client1_); |
| 140 |
| 141 client1_.SetHandleEvent(false); |
| 142 GenerateTouchEventAt(300.f, 400.f); |
| 143 EXPECT_TRUE(client1_.EventCalled()); |
| 144 ExpectHit(client3_); |
| 145 } |
| 146 |
| 147 TEST_F(ViewAndroidBoundsTest, MatchesViewsWithOffset) { |
| 148 view1_.SetLayout(ViewAndroid::LayoutParams::Normal(10, 20, 150, 100)); |
| 149 view2_.SetLayout(ViewAndroid::LayoutParams::Normal(20, 30, 40, 30)); |
| 150 view3_.SetLayout(ViewAndroid::LayoutParams::Normal(70, 30, 40, 30)); |
| 151 |
| 152 root_.AddChild(&view1_); |
| 153 view1_.AddChild(&view2_); |
| 154 view1_.AddChild(&view3_); |
| 155 |
| 156 GenerateTouchEventAt(70, 30); |
| 157 ExpectHit(client1_); |
| 158 |
| 159 client1_.SetHandleEvent(false); |
| 160 GenerateTouchEventAt(40, 60); |
| 161 EXPECT_TRUE(client1_.EventCalled()); |
| 162 ExpectHit(client2_); |
| 163 |
| 164 GenerateTouchEventAt(100, 70); |
| 165 EXPECT_TRUE(client1_.EventCalled()); |
| 166 ExpectHit(client3_); |
| 167 } |
| 168 |
| 169 } // namespace ui |
| OLD | NEW |