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

Unified Diff: ui/android/view_android_unittest.cc

Issue 2595263002: Introduce ViewRoot forwarding input/view events to native (Closed)
Patch Set: unittest Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/android/view_android.cc ('k') | ui/android/view_client.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..96dae46cf01701dff2f196265a27235c4f2aa985
--- /dev/null
+++ b/ui/android/view_android_unittest.cc
@@ -0,0 +1,278 @@
+// 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_client.h"
+#include "ui/android/view_root.h"
+
+namespace ui {
+
+using base::android::JavaParamRef;
+
+class TestViewRoot : public ViewRoot {
+ public:
+ TestViewRoot() : ViewRoot(0L) {}
+
+ // Overrides |OnTouchEvent| to skip getting dip scale
+ // and UMA record update not necessary for this test).
+ jboolean OnTouchEvent(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ const JavaParamRef<jobject>& motion_event,
+ jlong time_ms,
+ jint android_action,
+ jint pointer_count,
+ jint history_size,
+ jint action_index,
+ jfloat pos_x_0,
+ jfloat pos_y_0,
+ jfloat pos_x_1,
+ jfloat pos_y_1,
+ jint pointer_id_0,
+ jint pointer_id_1,
+ jfloat touch_major_0,
+ jfloat touch_major_1,
+ jfloat touch_minor_0,
+ jfloat touch_minor_1,
+ jfloat orientation_0,
+ jfloat orientation_1,
+ jfloat tilt_0,
+ jfloat tilt_1,
+ jfloat raw_pos_x,
+ jfloat raw_pos_y,
+ jint android_tool_type_0,
+ jint android_tool_type_1,
+ jint android_button_state,
+ jint android_meta_state,
+ jboolean is_touch_handle_event) {
+
+ MotionEventAndroid::Pointer pointer0(pointer_id_0,
+ pos_x_0,
+ pos_y_0,
+ touch_major_0,
+ touch_minor_0,
+ orientation_0,
+ tilt_0,
+ android_tool_type_0);
+ MotionEventAndroid::Pointer pointer1(pointer_id_1,
+ pos_x_1,
+ pos_y_1,
+ touch_major_1,
+ touch_minor_1,
+ orientation_1,
+ tilt_1,
+ android_tool_type_1);
+ MotionEventAndroid event(1.f,
+ env,
+ motion_event,
+ time_ms,
+ android_action,
+ pointer_count,
+ history_size,
+ action_index,
+ android_button_state,
+ android_meta_state,
+ raw_pos_x - pos_x_0,
+ raw_pos_y - pos_y_0,
+ &pointer0,
+ &pointer1);
+
+ return OnTouchEventInternal(event, is_touch_handle_event);
+ }
+};
+
+class TestViewClient : public ViewClient {
+ public:
+ TestViewClient() : handle_event_(true),
+ called_(false) {}
+
+ bool OnTouchEvent(const MotionEventAndroid& event,
+ bool is_touch_handle_event) override {
+ called_ = true;
+ return handle_event_;
+ }
+
+ void SetHandleEvent(bool handle_event) { handle_event_ = handle_event; }
+ bool EventHandled() { return handle_event_ && called_; }
+ void Reset() { called_ = false; }
+
+ private:
+ bool handle_event_;
+ 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,
+ base::android::JavaParamRef<jobject>(nullptr),
+ base::android::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
+ // +-------+
+ // | |
+ // view1_ view2_
+
+ GenerateTouchEventAt(100.f, 100.f);
+ EXPECT_TRUE(client1_.EventHandled());
+ EXPECT_FALSE(client2_.EventHandled());
+
+ Reset();
+
+ // View 2 moves up to front, and events should hit it from now.
+ root_.MoveToFront(&view2_);
+ GenerateTouchEventAt(100.f, 100.f);
+
+ EXPECT_FALSE(client1_.EventHandled());
+ 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());
+ EXPECT_FALSE(client2_.EventHandled());
+ Reset();
+
+ // Falls within |view2_|'s bounds
+ GenerateTouchEventAt(300.f, 400.f);
+ EXPECT_FALSE(client1_.EventHandled());
+ 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());
+ EXPECT_FALSE(client2_.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_FALSE(client1_.EventHandled());
+ 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_FALSE(client1_.EventHandled());
+ EXPECT_TRUE(client2_.EventHandled());
+ EXPECT_FALSE(client3_.EventHandled());
+ Reset();
+
+ // |view3_|'s bounds matches with that of |view1_|. The event should hit
+ // |view3_| since |view1_|'s client does not handle events.
+ client1_.SetHandleEvent(false);
+ GenerateTouchEventAt(300.f, 400.f);
+ EXPECT_FALSE(client1_.EventHandled());
+ EXPECT_FALSE(client2_.EventHandled());
+ EXPECT_TRUE(client3_.EventHandled());
+}
+
+} // namespace ui
« no previous file with comments | « ui/android/view_android.cc ('k') | ui/android/view_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698