Skip to content

Commit

Permalink
Disable tap2longtap err (wix#2439)
Browse files Browse the repository at this point in the history
  • Loading branch information
d4vidi authored Oct 29, 2020
1 parent be78d32 commit 4298a1b
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 83 deletions.
25 changes: 25 additions & 0 deletions detox/android/detox/src/main/java/com/wix/detox/common/DetoxLog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.wix.detox.common

import android.util.Log

class DetoxLog {
fun verbose(tag: String, log: String) = Log.v(tag, log)
fun verbose(tag: String, log: String, error: Throwable) = Log.v(tag, log, error)

fun debug(tag: String, log: String) = Log.d(tag, log)
fun debug(tag: String, log: String, error: Throwable) = Log.d(tag, log, error)

fun info(tag: String, log: String) = Log.i(tag, log)
fun info(tag: String, log: String, error: Throwable) = Log.i(tag, log, error)

fun warn(tag: String, log: String) = Log.w(tag, log)
fun warn(tag: String, log: String, error: Throwable) = Log.w(tag, log, error)
fun warn(tag: String, error: Throwable) = Log.w(tag, error)

fun error(tag: String, log: String) = Log.e(tag, log)
fun error(tag: String, log: String, error: Throwable) = Log.e(tag, log, error)

companion object {
val instance = DetoxLog()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@
*/

public class DetoxAction {
private static final String LOG_TAG = "detox";

private DetoxAction() {
// static class
}

public static ViewAction multiClick(int times, boolean strictMode) {
return actionWithAssertions(new GeneralClickAction(new DetoxMultiTap(strictMode, times), GeneralLocation.CENTER, Press.FINGER, 0, 0));
public static ViewAction multiClick(int times) {
return actionWithAssertions(new GeneralClickAction(new DetoxMultiTap(times), GeneralLocation.CENTER, Press.FINGER, 0, 0));
}

public static ViewAction tapAtLocation(final int x, final int y, boolean strictMode) {
public static ViewAction tapAtLocation(final int x, final int y) {
final int px = UiAutomatorHelper.convertDiptoPix(x);
final int py = UiAutomatorHelper.convertDiptoPix(y);
CoordinatesProvider c = new CoordinatesProvider() {
Expand All @@ -62,7 +64,7 @@ public float[] calculateCoordinates(View view) {
return new float[] {fx, fy};
}
};
return actionWithAssertions(new RNClickAction(c, strictMode));
return actionWithAssertions(new RNClickAction(c));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* An alternative to {@link ViewActions} - providing alternative implementations, where needed.
*/
public class DetoxViewActions {
public static ViewAction click(boolean strictMode) {
return actionWithAssertions(new RNClickAction(strictMode));
public static ViewAction click() {
return actionWithAssertions(new RNClickAction());
}

public static ViewAction typeText(String text) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.wix.detox.espresso.action

import android.util.Log
import android.view.MotionEvent
import androidx.test.espresso.UiController
import androidx.test.espresso.action.Tapper
import com.wix.detox.Detox
import com.wix.detox.common.DetoxErrors.DetoxIllegalStateException
import com.wix.detox.common.DetoxLog
import com.wix.detox.common.collect.PairsIterator
import com.wix.detox.common.proxy.CallInfo
import com.wix.detox.espresso.UiControllerSpy
Expand Down Expand Up @@ -38,11 +37,9 @@ open class DetoxMultiTap
private val longTapMinTimeMs: Long = getLongTapMinTime(),
private val tapEvents: TapEvents = TapEvents(),
private val uiControllerCallSpy: UiControllerSpy = UiControllerSpy.instance,
private val strictMode: Boolean = true)
private val log: DetoxLog = DetoxLog.instance)
: Tapper {

constructor(strictMode: Boolean, times: Int): this(times, strictMode = strictMode)

override fun sendTap(uiController: UiController?, coordinates: FloatArray?, precision: FloatArray?)
= sendTap(uiController, coordinates, precision, 0, 0)

Expand Down Expand Up @@ -108,11 +105,7 @@ open class DetoxMultiTap
private fun verifyTapEventTimes(upEvent: CallInfo, downEvent: CallInfo) {
val delta: Long = (upEvent - downEvent)!!
if (delta >= longTapMinTimeMs) {
val message = "Tap handled too slowly, and has turned into a long-tap, instead!!!";
if (strictMode) {
throw DetoxIllegalStateException(message)
}
Log.w(Detox.LOG_TAG, message)
log.warn(Detox.LOG_TAG, "Tap handled too slowly, and turned into a long-tap!") // TODO conditionally turn into an error, based on a global strict-mode detox config
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.wix.detox.espresso.action

open class DetoxSingleTap(strictMode: Boolean) : DetoxMultiTap(1, strictMode = strictMode)
open class DetoxSingleTap : DetoxMultiTap(1)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DetoxTypeTextAction implements ViewAction {

public DetoxTypeTextAction(String text) {
this.text = text;
clickAction = new RNClickAction(false);
clickAction = new RNClickAction();
typeTextAction = new TypeTextAction(text, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
public class RNClickAction implements ViewAction {
private final GeneralClickAction clickAction;

public RNClickAction(boolean strictMode) {
this(GeneralLocation.VISIBLE_CENTER, strictMode);
public RNClickAction() {
clickAction = new GeneralClickAction(
new DetoxSingleTap(),
GeneralLocation.VISIBLE_CENTER,
Press.FINGER,
InputDevice.SOURCE_UNKNOWN,
MotionEvent.BUTTON_PRIMARY);
}

public RNClickAction(CoordinatesProvider coordinatesProvider, boolean strictMode) {
public RNClickAction(CoordinatesProvider coordinatesProvider) {
clickAction = new GeneralClickAction(
new DetoxSingleTap(strictMode),
new DetoxSingleTap(),
coordinatesProvider,
Press.FINGER,
InputDevice.SOURCE_UNKNOWN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.test.espresso.UiController
import androidx.test.espresso.action.Tapper
import com.nhaarman.mockitokotlin2.*
import com.wix.detox.common.DetoxErrors.DetoxIllegalStateException
import com.wix.detox.common.DetoxLog
import com.wix.detox.common.proxy.CallInfo
import com.wix.detox.espresso.UiControllerSpy
import com.wix.detox.espresso.common.TapEvents
Expand All @@ -27,6 +28,7 @@ object DetoxMultiTapSpec: Spek({
lateinit var mock2ndTapEventsSeq: List<MotionEvent>
lateinit var tapEvents: TapEvents
lateinit var uiControllerCallSpy: UiControllerSpy
lateinit var log: DetoxLog

beforeEachTest {
uiController = mock()
Expand All @@ -46,6 +48,8 @@ object DetoxMultiTapSpec: Spek({
uiControllerCallSpy = mock() {
on { eventInjectionsIterator() }.doReturn(emptyList<CallInfo?>().iterator())
}

log = mock()
}

fun verify1stTapEventsSeqGenerated() = verify(tapEvents).createEventsSeq(coordinates, precision, null)
Expand All @@ -62,8 +66,7 @@ object DetoxMultiTapSpec: Spek({
fun givenInjectionCallsHistory(injectionsHistory: List<CallInfo?>) =
whenever(uiControllerCallSpy.eventInjectionsIterator()).thenReturn(injectionsHistory.iterator())

fun uut(times: Int) = DetoxMultiTap(times, interTapsDelayMs, coolDownTimeMs, longTapMinTimeMs, tapEvents, uiControllerCallSpy)
fun uutNonStrict(times: Int) = DetoxMultiTap(times, interTapsDelayMs, coolDownTimeMs, longTapMinTimeMs, tapEvents, uiControllerCallSpy, false)
fun uut(times: Int) = DetoxMultiTap(times, interTapsDelayMs, coolDownTimeMs, longTapMinTimeMs, tapEvents, uiControllerCallSpy, log)
fun sendOneTap(uut: DetoxMultiTap = uut(1)) = uut.sendTap(uiController, coordinates, precision, -1, -1)
fun sendTwoTaps(uut: DetoxMultiTap = uut(2)) = uut.sendTap(uiController, coordinates, precision, -1, -1)

Expand Down Expand Up @@ -184,7 +187,7 @@ object DetoxMultiTapSpec: Spek({
verify(uiControllerCallSpy).stop()
}

it("should throw if ui-controller spy indicates tap has turned into a long-tap") {
it("should warn if ui-controller spy indicates tap has turned into a long-tap") {
givenInjectionSuccess()

val injectionsHistory = listOf(
Expand All @@ -193,12 +196,11 @@ object DetoxMultiTapSpec: Spek({
)
givenInjectionCallsHistory(injectionsHistory)

assertFailsWith(DetoxIllegalStateException::class, "Tap handled too slowly, and turned into a long-tap!") {
sendOneTap()
}
sendOneTap()
verify(log).warn("Detox", "Tap handled too slowly, and turned into a long-tap!")
}

it("should throw if ui-controller spy indicates tap 1 of 2 has turned into a long-tap") {
it("should warn if ui-controller spy indicates tap 1 of 2 has turned into a long-tap") {
givenInjectionSuccess()

val injectionsHistory = listOf(
Expand All @@ -207,21 +209,8 @@ object DetoxMultiTapSpec: Spek({
)
givenInjectionCallsHistory(injectionsHistory)

assertFailsWith(DetoxIllegalStateException::class) {
sendOneTap()
}
}

it("should NOT throw even though ui-controller spy indicates tap has turned into a long-tap, in non-strict mode") {
givenInjectionSuccess()

val injectionsHistory = listOf(
CallInfo(longTapMinTimeMs - 1, longTapMinTimeMs),
CallInfo(0, 1)
)
givenInjectionCallsHistory(injectionsHistory)
uutNonStrict(1).sendTap(uiController, coordinates, precision, -1, -1)
sendOneTap()
verify(log, times(1)).warn("Detox", "Tap handled too slowly, and turned into a long-tap!")
}

}
})
12 changes: 2 additions & 10 deletions detox/src/android/espressoapi/DetoxAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ function sanitize_android_direction(direction) {
}
}
class DetoxAction {
static multiClick(times, strictMode) {
static multiClick(times) {
if (typeof times !== "number") throw new Error("times should be a number, but got " + (times + (" (" + (typeof times + ")"))));
if (typeof strictMode !== "boolean") throw new Error("strictMode should be a boolean, but got " + (strictMode + (" (" + (typeof strictMode + ")"))));
return {
target: {
type: "Class",
Expand All @@ -46,17 +45,13 @@ class DetoxAction {
args: [{
type: "Integer",
value: times
}, {
type: "boolean",
value: strictMode
}]
};
}

static tapAtLocation(x, y, strictMode) {
static tapAtLocation(x, y) {
if (typeof x !== "number") throw new Error("x should be a number, but got " + (x + (" (" + (typeof x + ")"))));
if (typeof y !== "number") throw new Error("y should be a number, but got " + (y + (" (" + (typeof y + ")"))));
if (typeof strictMode !== "boolean") throw new Error("strictMode should be a boolean, but got " + (strictMode + (" (" + (typeof strictMode + ")"))));
return {
target: {
type: "Class",
Expand All @@ -69,9 +64,6 @@ class DetoxAction {
}, {
type: "Integer",
value: y
}, {
type: "boolean",
value: strictMode
}]
};
}
Expand Down
8 changes: 2 additions & 6 deletions detox/src/android/espressoapi/DetoxViewActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@


class DetoxViewActions {
static click(strictMode) {
if (typeof strictMode !== "boolean") throw new Error("strictMode should be a boolean, but got " + (strictMode + (" (" + (typeof strictMode + ")"))));
static click() {
return {
target: {
type: "Class",
value: "com.wix.detox.espresso.DetoxViewActions"
},
method: "click",
args: [{
type: "boolean",
value: strictMode
}]
args: []
};
}

Expand Down
20 changes: 7 additions & 13 deletions detox/src/android/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ class Action {
}

class TapAction extends Action {
constructor(value = {}) {
constructor(value) {
super();

const { strict = true } = value;
this._call = invoke.callDirectly((value.x && value.y) ? DetoxActionApi.tapAtLocation(value.x, value.y, strict) : DetoxViewActionsApi.click(strict));
this._call = invoke.callDirectly(value ? DetoxActionApi.tapAtLocation(value.x, value.y) : DetoxViewActionsApi.click());
}
}

class TapAtPointAction extends Action {
constructor(value) {
super();

const { strict = true } = value;
this._call = invoke.callDirectly(DetoxActionApi.tapAtLocation(value.x, value.y, strict));
this._call = invoke.callDirectly(DetoxActionApi.tapAtLocation(value.x, value.y));
}
}

Expand All @@ -54,11 +50,9 @@ class LongPressAction extends Action {
}

class MultiClickAction extends Action {
constructor(times, value = {}) {
constructor(times) {
super();

const { strict = true } = value;
this._call = invoke.callDirectly(DetoxActionApi.multiClick(times, strict));
this._call = invoke.callDirectly(DetoxActionApi.multiClick(times));
}
}

Expand Down Expand Up @@ -247,8 +241,8 @@ class Element {
return await new ActionInteraction(this._invocationManager, this, new LongPressAction()).execute();
}

async multiTap(times, value) {
return await new ActionInteraction(this._invocationManager, this, new MultiClickAction(times, value)).execute();
async multiTap(times) {
return await new ActionInteraction(this._invocationManager, this, new MultiClickAction(times)).execute();
}

async tapBackspaceKey() {
Expand Down
3 changes: 0 additions & 3 deletions detox/src/android/expect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,10 @@ describe('expect', () => {
describe('element interactions', () => {
it('should tap and long-press', async () => {
await e.element(e.by.label('Tap Me')).tap();
await e.element(e.by.label('Tap Me')).tap({ strict: false });
await e.element(e.by.label('Tap Me')).tap({ x: 10, y: 10 });
await e.element(e.by.label('Tap Me')).tapAtPoint({x: 100, y: 200});
await e.element(e.by.label('Tap Me')).tapAtPoint({x: 100, y: 200, strict: false});
await e.element(e.by.label('Tap Me')).longPress();
await e.element(e.by.id('UniqueId819')).multiTap(3);
await e.element(e.by.id('UniqueId819')).multiTap(3, { strict: false });
});

it('should not tap and long-press given bad args', async () => {
Expand Down
9 changes: 3 additions & 6 deletions detox/test/e2e/03.actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,18 @@ describe('Actions', () => {
});
});

it(':android: should throw if tap handling is too slow', async () => {
it.skip(':android: should throw if tap handling is too slow', async () => {
try {
await driver.sluggishTapElement.tap();
} catch (e) {
console.log('Got an expected error', e);
if (!e.toString().includes('Tap handled too slowly, and has turned into a long-tap, instead!!!')) {
if (!e.toString().includes('Tap handled too slowly, and turned into a long-tap!')) {
throw new Error('Error content isn\'t as expected!');
}
return;
}
throw new Error('Expected an error');
});

it(':android: should NOT throw if tap handling is too slow, if strict-mode is disabled', async () => {
await driver.sluggishTapElement.tap({ strict: false });
throw new Error('Expected an error');
});

it('should type in an element', async () => {
Expand Down
2 changes: 1 addition & 1 deletion detox/test/e2e/drivers/actions-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const driver = {

sluggishTapElement: {
testId: 'sluggishTappableText',
tap: (value) => element(by.id(driver.sluggishTapElement.testId)).tap(value)
tap: () => element(by.id(driver.sluggishTapElement.testId)).tap()
}
};

Expand Down

0 comments on commit 4298a1b

Please sign in to comment.