diff --git a/src/main/java/io/appium/java_client/MissingParameterException.java b/src/main/java/io/appium/java_client/MissingParameterException.java deleted file mode 100644 index 1f9782cf4..000000000 --- a/src/main/java/io/appium/java_client/MissingParameterException.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.java_client; - -public class MissingParameterException - extends IllegalArgumentException { - - private static final long serialVersionUID = 1L; - - public MissingParameterException(String reason) { - super(reason); - } - - public MissingParameterException(String reason, Throwable cause) { - super(reason, cause); - } -} diff --git a/src/main/java/io/appium/java_client/MultiTouchAction.java b/src/main/java/io/appium/java_client/MultiTouchAction.java index f2a3e08be..c7bdd6525 100644 --- a/src/main/java/io/appium/java_client/MultiTouchAction.java +++ b/src/main/java/io/appium/java_client/MultiTouchAction.java @@ -16,10 +16,15 @@ package io.appium.java_client; +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.stream.Collectors.toList; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import java.util.List; +import java.util.Map; + /** * Used for Webdriver 3 multi-touch gestures * See the Webriver 3 spec https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html @@ -62,28 +67,22 @@ public MultiTouchAction add(TouchAction action) { * Perform the multi-touch action on the mobile performsTouchActions. */ public MultiTouchAction perform() { - int size = actions.build().size(); - if (size > 1) { + List touchActions = actions.build(); + checkArgument(touchActions.size() > 0, + "MultiTouch action must have at least one TouchAction added before it can be performed"); + if (touchActions.size() > 1) { performsTouchActions.performMultiTouchAction(this); - } else if (size == 1) { - //android doesn't like having multi-touch actions with only a single TouchAction... - performsTouchActions.performTouchAction(actions.build().get(0)); - } else { - throw new MissingParameterException( - "MultiTouch action must have at least one TouchAction " - + "added before it can be performed"); - } + return this; + } //android doesn't like having multi-touch actions with only a single TouchAction... + performsTouchActions.performTouchAction(touchActions.get(0)); return this; } - protected ImmutableMap> getParameters() { - ImmutableList.Builder listOfActionChains = ImmutableList.builder(); + protected Map> getParameters() { ImmutableList touchActions = actions.build(); - - touchActions.forEach(action -> { - listOfActionChains.add(action.getParameters().get("actions")); - }); - return ImmutableMap.of("actions", listOfActionChains.build()); + return ImmutableMap.of("actions", + touchActions.stream().map(touchAction -> + touchAction.getParameters().get("actions")).collect(toList())); } /** diff --git a/src/main/java/io/appium/java_client/PerformsTouchActions.java b/src/main/java/io/appium/java_client/PerformsTouchActions.java index 2f01246eb..fae9c2403 100644 --- a/src/main/java/io/appium/java_client/PerformsTouchActions.java +++ b/src/main/java/io/appium/java_client/PerformsTouchActions.java @@ -19,8 +19,8 @@ import static io.appium.java_client.MobileCommand.PERFORM_MULTI_TOUCH; import static io.appium.java_client.MobileCommand.PERFORM_TOUCH_ACTION; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; +import java.util.List; +import java.util.Map; public interface PerformsTouchActions extends ExecutesMethod { /** @@ -37,7 +37,7 @@ public interface PerformsTouchActions extends ExecutesMethod { * @return the same touch action object */ default TouchAction performTouchAction(TouchAction touchAction) { - ImmutableMap> parameters = touchAction.getParameters(); + Map> parameters = touchAction.getParameters(); execute(PERFORM_TOUCH_ACTION, parameters); return touchAction.clearParameters(); } @@ -54,7 +54,7 @@ default TouchAction performTouchAction(TouchAction touchAction) { * @param multiAction the MultiTouchAction object to perform. */ default void performMultiTouchAction(MultiTouchAction multiAction) { - ImmutableMap> parameters = multiAction.getParameters(); + Map> parameters = multiAction.getParameters(); execute(PERFORM_MULTI_TOUCH, parameters); multiAction.clearActions(); } diff --git a/src/main/java/io/appium/java_client/TouchAction.java b/src/main/java/io/appium/java_client/TouchAction.java index 41b847877..cc27ed8e2 100644 --- a/src/main/java/io/appium/java_client/TouchAction.java +++ b/src/main/java/io/appium/java_client/TouchAction.java @@ -21,6 +21,7 @@ import static io.appium.java_client.touch.LongPressOptions.longPressOptions; import static io.appium.java_client.touch.offset.ElementOption.element; import static io.appium.java_client.touch.offset.PointOption.point; +import static java.util.stream.Collectors.toList; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -34,6 +35,8 @@ import org.openqa.selenium.WebElement; import java.time.Duration; +import java.util.List; +import java.util.Map; /** * Used for Webdriver 3 touch actions @@ -415,13 +418,10 @@ public T perform() { * * @return A map of parameters for this touch action to pass as part of mjsonwp. */ - protected ImmutableMap> getParameters() { - - ImmutableList.Builder parameters = builder(); - ImmutableList actionList = parameterBuilder.build(); - - actionList.forEach(action -> parameters.add(action.getParameterMap())); - return ImmutableMap.of("actions", parameters.build()); + protected Map> getParameters() { + List actionList = parameterBuilder.build(); + return ImmutableMap.of("actions", actionList.stream() + .map(ActionParameter::getParameterMap).collect(toList())); } /**