Skip to content

Commit

Permalink
Make scroll tracking configurable with a cap (#284)
Browse files Browse the repository at this point in the history
* Make scroll tracking configurable with a cap

* Address comments

* Shift to a setting
  • Loading branch information
imurchie authored Jun 11, 2019
1 parent 49ad6b2 commit 4deab30
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

import io.appium.uiautomator2.common.exceptions.UiAutomator2Exception;

import io.appium.uiautomator2.model.AppiumUIA2Driver;
import io.appium.uiautomator2.model.Session;
import io.appium.uiautomator2.model.settings.Settings;
import io.appium.uiautomator2.model.settings.TrackScrollEvents;
import io.appium.uiautomator2.utils.Logger;

import static io.appium.uiautomator2.utils.ReflectionUtils.invoke;
import static io.appium.uiautomator2.utils.ReflectionUtils.method;

Expand Down Expand Up @@ -62,48 +68,81 @@ public boolean injectEventSync(final InputEvent event) throws UiAutomator2Except
return injectEventSync(event, true);
}

public boolean shouldTrackScrollEvents() {
Session session = AppiumUIA2Driver.getInstance().getSessionOrThrow();
final TrackScrollEvents trackScrollEventsSetting =
(TrackScrollEvents) Settings.TRACK_SCROLL_EVENTS.getSetting();
Boolean trackScrollEvents = (Boolean) trackScrollEventsSetting.getValue();
Logger.error(String.format("Setting '%s' is set to %b",
trackScrollEventsSetting.getName(), trackScrollEvents));

return trackScrollEvents;
}

private boolean doTouchDown(final int x, final int y) {
return (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER,
METHOD_TOUCH_DOWN, int.class, int.class), interactionController, x, y);
}

public boolean touchDown(final int x, final int y) throws UiAutomator2Exception {
return EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {
Boolean result = (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER,
METHOD_TOUCH_DOWN, int.class, int.class), interactionController, x, y);
setResult(result);
}
});
return shouldTrackScrollEvents()
? EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {
setResult(doTouchDown(x, y));
}
})
: doTouchDown(x, y);
}

private boolean doTouchUp(final int x, final int y) {
return (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER, METHOD_TOUCH_UP,
int.class, int.class), interactionController, x, y);
}

public boolean touchUp(final int x, final int y) throws UiAutomator2Exception {
return EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {
Boolean result = (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER, METHOD_TOUCH_UP,
int.class, int.class), interactionController, x, y);
setResult(result);
}
});
return shouldTrackScrollEvents()
? EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {
setResult(doTouchUp(x, y));
}
})
: doTouchUp(x, y);
}

private boolean doTouchMove(final int x, final int y) {
return (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER,
METHOD_TOUCH_MOVE, int.class, int.class), interactionController, x, y);
}

public boolean touchMove(final int x, final int y) throws UiAutomator2Exception {
return EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {
Boolean result = (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER,
METHOD_TOUCH_MOVE, int.class, int.class), interactionController, x, y);
setResult(result);
}
});
return shouldTrackScrollEvents()
? EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {;
setResult(doTouchMove(x, y));
}
})
: doTouchMove(x, y);
}

private boolean doPerformMultiPointerGesture(final PointerCoords[][] pcs) {
return (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER,
METHOD_PERFORM_MULTI_POINTER_GESTURE, PointerCoords[][].class),
interactionController, (Object) pcs);
}

public Boolean performMultiPointerGesture(final PointerCoords[][] pcs) throws UiAutomator2Exception {
return EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {
Boolean result = (Boolean) invoke(method(CLASS_INTERACTION_CONTROLLER,
METHOD_PERFORM_MULTI_POINTER_GESTURE, PointerCoords[][].class),
interactionController, (Object) pcs);
setResult(result);
}
});
if (shouldTrackScrollEvents()) {
return EventRegister.runAndRegisterScrollEvents(new ReturningRunnable<Boolean>() {
@Override
public void run() {
setResult(doPerformMultiPointerGesture(pcs));
}
});
} else {
return doPerformMultiPointerGesture(pcs);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public enum Settings {
WAIT_FOR_IDLE_TIMEOUT(new WaitForIdleTimeout()),
WAIT_FOR_SELECTOR_TIMEOUT(new WaitForSelectorTimeout()),
NORMALIZE_TAG_NAMES(new NormalizeTagNames()),
SHUTDOWN_ON_POWER_DISCONNECT(new ShutdownOnPowerDisconnect());
SHUTDOWN_ON_POWER_DISCONNECT(new ShutdownOnPowerDisconnect()),
TRACK_SCROLL_EVENTS(new TrackScrollEvents());

private final ISetting setting;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.uiautomator2.model.settings;

public class TrackScrollEvents extends AbstractSetting<Boolean> {
private static final String SETTING_NAME = "trackScrollEvents";

private boolean value = true;

public TrackScrollEvents() {
super(Boolean.class, SETTING_NAME);
}

@Override
public Boolean getValue() {
return value;
}

@Override
protected void apply(Boolean value) {
this.value = value;
}
}

0 comments on commit 4deab30

Please sign in to comment.