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