Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3a33d422 authored by Michał Brzeziński's avatar Michał Brzeziński Committed by Android (Google) Code Review
Browse files

Merge "Unignoring tests and fixing FakeMotionEvent" into main

parents c7d44bca 9b83c640
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -29,11 +29,9 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith

@Ignore
@SmallTest
@RunWith(AndroidJUnit4::class)
class BackGestureMonitorTest : SysuiTestCase() {
+28 −6
Original line number Diff line number Diff line
@@ -21,8 +21,11 @@ import android.view.InputDevice.SOURCE_MOUSE
import android.view.MotionEvent
import android.view.MotionEvent.CLASSIFICATION_NONE
import android.view.MotionEvent.TOOL_TYPE_FINGER
import java.lang.reflect.Method
import org.mockito.kotlin.doNothing
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.spy
import org.mockito.kotlin.whenever

fun motionEvent(
    action: Int,
@@ -37,12 +40,31 @@ fun motionEvent(
    val event =
        MotionEvent.obtain(/* downTime= */ 0, /* eventTime= */ 0, action, x, y, /* metaState= */ 0)
    event.source = source
    return spy<MotionEvent>(event) {
    val spy =
        spy<MotionEvent>(event) {
            on { getToolType(0) } doReturn toolType
            on { getPointerCount() } doReturn pointerCount
            axisValues.forEach { (key, value) -> on { getAxisValue(key) } doReturn value }
            on { getClassification() } doReturn classification
        }
    ensureFinalizeIsNotCalledTwice(spy)
    return spy
}

private fun ensureFinalizeIsNotCalledTwice(spy: MotionEvent) {
    // Spy in mockito will create copy of the spied object, copying all its field etc. Here it means
    // we create copy of MotionEvent and its mNativePtr, so we have two separate objects of type
    // MotionEvents with the same mNativePtr. That breaks because MotionEvent has custom finalize()
    // method which goes to native code and tries to delete the reference from mNativePtr. It works
    // first time but second time reference is already deleted and it breaks. That's why we have to
    // avoid calling finalize twice
    doNothing().whenever(spy).finalizeUsingReflection()
}

private fun MotionEvent.finalizeUsingReflection() {
    val finalizeMethod: Method = MotionEvent::class.java.getDeclaredMethod("finalize")
    finalizeMethod.isAccessible = true
    finalizeMethod.invoke(this)
}

fun touchpadEvent(
+0 −2
Original line number Diff line number Diff line
@@ -34,11 +34,9 @@ import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.touchpad.tutorial.ui.gesture.TouchpadGesture.BACK
import com.google.common.truth.Truth.assertThat
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith

@Ignore
@SmallTest
@RunWith(AndroidJUnit4::class)
class TouchpadGestureHandlerTest : SysuiTestCase() {