Index: ui/android/view_android_unittest.cc |
diff --git a/ui/android/view_android_unittest.cc b/ui/android/view_android_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..570d8545db96393efd9cce35454b33afa4956a4d |
--- /dev/null |
+++ b/ui/android/view_android_unittest.cc |
@@ -0,0 +1,241 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/android/view_root.h" |
+ |
+namespace ui { |
+ |
+using base::android::JavaParamRef; |
+ |
+class TestViewRoot : public ViewRoot { |
+ public: |
+ TestViewRoot() : ViewRoot(0L) {} |
+ float GetDipScale() override { return 1.f; } |
+}; |
+ |
+class TestViewClient : public ViewClient { |
+ public: |
+ TestViewClient() : handle_event_(true), |
+ called_(false) {} |
+ |
+ void SetHandleEvent(bool handle_event) { handle_event_ = handle_event; } |
+ bool OnTouchEvent(const MotionEventData& event) override { |
+ called_ = true; |
+ return handle_event_; |
+ } |
+ |
+ bool EventHandled() { return called_ && handle_event_; } |
+ void Reset() { called_ = false; } |
+ |
+ private: |
+ bool handle_event_; // Marks as event was consumed. True by default. |
+ bool called_; |
+}; |
+ |
+class ViewAndroidBoundsTest : public testing::Test { |
+ public: |
+ ViewAndroidBoundsTest() : view1_(&client1_), |
+ view2_(&client2_), |
+ view3_(&client3_) {} |
+ void Reset() { |
+ client1_.Reset(); |
+ client2_.Reset(); |
+ client3_.Reset(); |
+ } |
+ |
+ void GenerateTouchEventAt(float x, float y) { |
+ root_.OnTouchEvent(nullptr, |
+ JavaParamRef<jobject>(nullptr), |
+ JavaParamRef<jobject>(nullptr), |
+ 0L, // time |
+ 0, 1, 0, 0, |
+ x, y, 0.f, 0.f, // pos |
+ 0, 0, // pointer_id |
+ 0.f, 0.f, 0.f, 0.f, // touch |
+ 0.f, 0.f, 0.f, 0.f, |
+ 0.f, 0.f, |
+ 0, 0, 0, 0, |
+ false); |
+ } |
+ |
+ TestViewRoot root_; |
+ TestViewClient client1_; |
+ TestViewClient client2_; |
+ TestViewClient client3_; |
+ ViewAndroid view1_; |
+ ViewAndroid view2_; |
+ ViewAndroid view3_; |
+}; |
+ |
+TEST_F(ViewAndroidBoundsTest, MatchesViewInFront) { |
+ view1_.SetBounds(gfx::Point(50, 50), 400, 600); |
+ view2_.SetBounds(gfx::Point(50, 50), 400, 600); |
+ root_.AddChild(&view2_); |
+ root_.AddChild(&view1_); |
+ |
+ // root |
+ // +-------+ |
boliu
2017/01/24 23:47:13
err, first I got confused this is a diagram of coo
Jinsuk Kim
2017/01/25 02:34:34
Removed diagrams.
|
+ // | | |
+ // view1_ view2_ |
+ |
+ GenerateTouchEventAt(100.f, 100.f); |
+ EXPECT_TRUE(client1_.EventHandled()); |
boliu
2017/01/24 23:47:13
check view2 did not handle event, here and everywh
Jinsuk Kim
2017/01/25 02:34:34
Done.
|
+ |
+ Reset(); |
+ |
+ // View 2 moves up to front, and events should hit it from now. |
+ root_.MoveToFront(&view2_); |
+ GenerateTouchEventAt(100.f, 100.f); |
+ |
+ EXPECT_TRUE(client2_.EventHandled()); |
+} |
+ |
+TEST_F(ViewAndroidBoundsTest, MatchesViewArea) { |
+ // +----------------+ |
+ // | view2_ | |
+ // | | |
+ // | +--------+ | |
+ // | | x | | -> (100, 100) |
+ // | | view1_ | | |
+ // | | | | |
+ // | +--------+ | |
+ // | x | -> (300, 400) |
+ // +----------------+ |
+ view1_.SetBounds(gfx::Point(50, 50), 200, 200); |
+ view2_.SetBounds(gfx::Point(20, 20), 400, 600); |
+ |
+ // root |
+ // +-------+ |
+ // | | |
+ // view1_ view2_ |
+ root_.AddChild(&view2_); |
+ root_.AddChild(&view1_); |
+ |
+ // Falls within |view1_|'s bounds |
+ GenerateTouchEventAt(100.f, 100.f); |
+ EXPECT_TRUE(client1_.EventHandled()); |
+ Reset(); |
+ |
+ // Falls within |view2_|'s bounds |
+ GenerateTouchEventAt(300.f, 400.f); |
+ EXPECT_TRUE(client2_.EventHandled()); |
+} |
+ |
+TEST_F(ViewAndroidBoundsTest, MatchesViewAfterMove) { |
+ // +----------------+ |
+ // | view2_ | |
+ // | | |
+ // | +--------+ | |
+ // | | x | | -> (100, 100) |
+ // | | | | |
+ // | +--------+ | |
+ // | | |
+ // +----------------+ |
+ view1_.SetBounds(gfx::Point(50, 50), 200, 200); |
+ view2_.SetBounds(gfx::Point(20, 20), 400, 600); |
+ root_.AddChild(&view2_); |
+ root_.AddChild(&view1_); |
+ |
+ GenerateTouchEventAt(100.f, 100.f); |
+ EXPECT_TRUE(client1_.EventHandled()); |
+ |
+ Reset(); |
+ |
+ // The front view moved, so the same event now should hit the view below it. |
+ // +----------------+ |
+ // | view2_ | |
+ // | | |
+ // | x | -> (100, 100) |
+ // | +--------+ | |
+ // | | | | |
+ // | | | | |
+ // | +--------+ | |
+ // +----------------+ |
+ view1_.SetBounds(gfx::Point(150, 150), 200, 200); |
+ GenerateTouchEventAt(100.f, 100.f); |
+ EXPECT_TRUE(client2_.EventHandled()); |
+} |
+ |
+TEST_F(ViewAndroidBoundsTest, MatchesViewSizeOfkMatchParent) { |
+ // +----------------+ |
+ // | view1_/view3_ | |
+ // | | |
+ // | +--------+ | |
+ // | | | | |
+ // | | view2_ | | |
+ // | | x | | -> (100, 100) |
+ // | +--------+ | |
+ // | x | -> (300, 400) |
+ // +----------------+ |
+ |
+ view1_.SetBounds(gfx::Point(20, 20), 400, 600); |
+ view3_.SetBounds(gfx::Point(), |
+ ViewAndroid::Bounds::kMatchParent, |
+ ViewAndroid::Bounds::kMatchParent); |
+ view2_.SetBounds(gfx::Point(50, 50), 200, 200); |
+ |
+ // root |
+ // +-------+ |
+ // | | |
+ // view2_ view1_ |
+ // | |
+ // view3_ |
+ |
+ root_.AddChild(&view1_); |
+ root_.AddChild(&view2_); |
+ view1_.AddChild(&view3_); |
+ |
+ GenerateTouchEventAt(100.f, 100.f); |
+ EXPECT_TRUE(client2_.EventHandled()); |
+ Reset(); |
+ |
+ GenerateTouchEventAt(300.f, 400.f); |
+ EXPECT_TRUE(client3_.EventHandled()); |
+ Reset(); |
+ |
+ client3_.SetHandleEvent(false); |
+ GenerateTouchEventAt(300.f, 400.f); |
+ EXPECT_TRUE(client1_.EventHandled()); |
+ Reset(); |
+} |
+ |
+TEST_F(ViewAndroidBoundsTest, MatchesViewsWithOffset) { |
+ // (10,20) |
+ // +--------------------------+ |
+ // | view1_ x | -> (70, 30) |
+ // | +--------+ +--------+ | |
+ // | | view2_ | | view3_ | | |
+ // | | x | | | | -> (40, 60) |
+ // | | | | x | | -> (100, 70) |
+ // | +--------+ +--------+ | |
+ // | | |
+ // +--------------------------+ |
+ view1_.SetBounds(gfx::Point(10, 20), 150, 100); |
+ view2_.SetBounds(gfx::Point(20, 30), 40, 30); |
+ view3_.SetBounds(gfx::Point(70, 30), 40, 30); |
+ |
+ // root |
+ // | |
+ // view1_ |
+ // +-------+ |
+ // | | |
+ // view2_ view3_ |
+ root_.AddChild(&view1_); |
+ view1_.AddChild(&view2_); |
+ view1_.AddChild(&view3_); |
+ |
+ GenerateTouchEventAt(70, 30); |
+ EXPECT_TRUE(client1_.EventHandled()); |
+ Reset(); |
+ |
+ GenerateTouchEventAt(40, 60); |
+ EXPECT_TRUE(client2_.EventHandled()); |
+ Reset(); |
+ |
+ GenerateTouchEventAt(100, 70); |
+ EXPECT_TRUE(client3_.EventHandled()); |
+} |
+ |
+} // namespace ui |