From 5657d2d3bd12148678797f88cb5ff0e8e05676a1 Mon Sep 17 00:00:00 2001 From: Siddarth Kumar Date: Wed, 9 Aug 2023 10:05:14 -0700 Subject: [PATCH] (refactor): kotlinify `JSPointerDispatcherTest.java` (#38878) Summary: This PR converts the java logic inside of `JSPointerDispatcherTest.java` to Kotlin as requested in https://github.com/facebook/react-native/issues/38825 We also swap `JSPointerDispatcherTest.java` for `JSPointerDispatcherTest.kt` ## Changelog: [Internal][Changed]: Convert JSPointerDispatcherTest to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/38878 Test Plan: `./gradlew :packages:react-native:ReactAndroid:test` must pass and CI must be green ## Screenshot of Tests passing locally: ![Screenshot 2023-08-09 at 12 48 04 PM](https://github.com/facebook/react-native/assets/64726664/566fc648-adb7-4f52-87fc-67ef2f39a1a2) Reviewed By: rshest Differential Revision: D48189629 Pulled By: cortinico fbshipit-source-id: 92d869f690457986413bcae33a15225aa6e006be --- .../uimanager/JSPointerDispatcherTest.java | 97 ------------------- .../uimanager/JSPointerDispatcherTest.kt | 82 ++++++++++++++++ 2 files changed, 82 insertions(+), 97 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.java deleted file mode 100644 index 1e2e256af6b..00000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.uimanager; - -import android.content.Context; -import android.graphics.Rect; -import android.os.SystemClock; -import android.view.MotionEvent; -import android.view.View; -import android.view.ViewGroup; -import android.widget.LinearLayout; -import android.widget.TextView; -import com.facebook.react.uimanager.events.Event; -import com.facebook.react.uimanager.events.EventDispatcher; -import com.facebook.react.uimanager.events.PointerEventHelper; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentMatcher; -import org.mockito.Mockito; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class JSPointerDispatcherTest { - - private ViewGroup mRoot; - private JSPointerDispatcher mPointerDispatcher; - - class EventWithName implements ArgumentMatcher { - private String mEventName; - - public EventWithName(String eventName) { - mEventName = eventName; - } - - @Override - public boolean matches(Event argument) { - return argument.getEventName().equals(mEventName); - } - - @Override - public String toString() { - return "[event with name: " + mEventName + "]"; - } - } - - @Before - public void setupViewHierarchy() { - Context ctx = RuntimeEnvironment.getApplication(); - mRoot = new LinearLayout(ctx); - TextView childView = new TextView(ctx); - childView.append("Hello, world!"); - // need > 0 ID to consider it as a react view (see - // TouchTargetHelper::findTargetPathAndCoordinatesForTouch) - childView.setId(100); - mRoot.addView(childView); - // needed for test to ensure that child has dimensions - mRoot.measure(500, 500); - mRoot.layout(0, 0, 500, 500); - mPointerDispatcher = new JSPointerDispatcher(mRoot); - } - - private static MotionEvent createMotionEvent(int action, float x, float y) { - long downTime = SystemClock.uptimeMillis(); - long eventTime = downTime; - int metaState = 0; // no modifiers pressed - - return MotionEvent.obtain(downTime, eventTime, action, x, y, metaState); - } - - private Rect getChildViewRectInRootCoordinates(int childIndex) { - View child = mRoot.getChildAt(childIndex); - Rect outRect = new Rect(); - child.getDrawingRect(outRect); - - mRoot.offsetDescendantRectToMyCoords(child, outRect); - - return outRect; - } - - @Test - public void testPointerEnter() { - Rect childRect = getChildViewRectInRootCoordinates(0); - MotionEvent ev = - createMotionEvent(MotionEvent.ACTION_DOWN, childRect.centerX(), childRect.centerY()); - EventDispatcher mockDispatcher = Mockito.mock(EventDispatcher.class); - mPointerDispatcher.handleMotionEvent(ev, mockDispatcher, false); - Mockito.verify(mockDispatcher) - .dispatchEvent(Mockito.argThat(new EventWithName(PointerEventHelper.POINTER_DOWN))); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt new file mode 100644 index 00000000000..3505242b36b --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/JSPointerDispatcherTest.kt @@ -0,0 +1,82 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.uimanager + +import android.content.Context +import android.graphics.Rect +import android.os.SystemClock +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.TextView +import com.facebook.react.uimanager.events.Event +import com.facebook.react.uimanager.events.EventDispatcher +import com.facebook.react.uimanager.events.PointerEventHelper +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatcher +import org.mockito.Mockito.argThat +import org.mockito.Mockito.mock +import org.mockito.Mockito.verify +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +@RunWith(RobolectricTestRunner::class) +class JSPointerDispatcherTest { + + private lateinit var root: ViewGroup + private lateinit var pointerDispatcher: JSPointerDispatcher + + class EventWithName(private val eventName: String) : ArgumentMatcher>> { + override fun matches(argument: Event>?): Boolean = argument?.eventName == eventName + + override fun toString(): String = "[event with name: $eventName]" + } + + @Before + fun setupViewHierarchy() { + val ctx: Context = RuntimeEnvironment.application + root = LinearLayout(ctx) + val childView = TextView(ctx) + childView.append("Hello, world!") + childView.id = 100 + root.addView(childView) + root.measure(500, 500) + root.layout(0, 0, 500, 500) + pointerDispatcher = JSPointerDispatcher(root) + } + + private fun createMotionEvent(action: Int, x: Float, y: Float): MotionEvent { + val downTime = SystemClock.uptimeMillis() + val eventTime = downTime + val metaState = 0 // no modifiers pressed + + return MotionEvent.obtain(downTime, eventTime, action, x, y, metaState) + } + + private fun getChildViewRectInRootCoordinates(childIndex: Int): Rect { + val child: View = root.getChildAt(childIndex) + val outRect = Rect() + child.getDrawingRect(outRect) + root.offsetDescendantRectToMyCoords(child, outRect) + return outRect + } + + @Test + fun testPointerEnter() { + val childRect = getChildViewRectInRootCoordinates(0) + val ev = + createMotionEvent( + MotionEvent.ACTION_DOWN, childRect.centerX().toFloat(), childRect.centerY().toFloat()) + val mockDispatcher: EventDispatcher = mock(EventDispatcher::class.java) + pointerDispatcher.handleMotionEvent(ev, mockDispatcher, false) + verify(mockDispatcher).dispatchEvent(argThat(EventWithName(PointerEventHelper.POINTER_DOWN))) + } +}