From 07779072756ffb85b796d71c048177f170273556 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Fri, 5 Jan 2018 16:44:12 +0100 Subject: [PATCH 1/4] Add handlers for lock/unlock in iOS --- .../io/appium/java_client/MobileCommand.java | 22 ++++++++++++++++ .../java_client/ios/LocksIOSDevice.java | 25 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/appium/java_client/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index bb43506ff..ca1e97175 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -352,6 +352,28 @@ public static ImmutableMap prepareArguments(String[] params, LOCK, prepareArguments("seconds", duration.getSeconds())); } + /** + * This method forms a {@link java.util.Map} of parameters for the + * device unlocking. + * + * @return a key-value pair. The key is the command name. The value is a + * {@link java.util.Map} command arguments. + */ + public static Map.Entry> unlockDeviceCommand() { + return new AbstractMap.SimpleEntry<>(UNLOCK, ImmutableMap.of()); + } + + /** + * This method forms a {@link java.util.Map} of parameters for the + * device locked query. + * + * @return a key-value pair. The key is the command name. The value is a + * {@link java.util.Map} command arguments. + */ + public static Map.Entry> getIsDeviceLockedCommand() { + return new AbstractMap.SimpleEntry<>(IS_LOCKED, ImmutableMap.of()); + } + public static Map.Entry> getSettingsCommand() { return new AbstractMap.SimpleEntry<>(GET_SETTINGS, ImmutableMap.of()); } diff --git a/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java b/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java index 007c03c73..5e76e700c 100644 --- a/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java +++ b/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java @@ -16,7 +16,9 @@ package io.appium.java_client.ios; +import static io.appium.java_client.MobileCommand.getIsDeviceLockedCommand; import static io.appium.java_client.MobileCommand.lockDeviceCommand; +import static io.appium.java_client.MobileCommand.unlockDeviceCommand; import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; @@ -27,11 +29,30 @@ public interface LocksIOSDevice extends ExecutesMethod { /** * Lock the device (bring it to the lock screen) for a given number of - * seconds. + * seconds or forever (until the command for unlocking is called). The call + * is ignored if the device already locked. * - * @param duration for how long to lock the screen. Minimum time resolution is one second + * @param duration for how long to lock the screen. Minimum time resolution is one second. + * A negative/zero value will lock the device and return immediately. */ default void lockDevice(Duration duration) { CommandExecutionHelper.execute(this, lockDeviceCommand(duration)); } + + /** + * Unlocks the device if it is locked or returns immediately if the device is already unlocked. + */ + default void unlockDevice() { + CommandExecutionHelper.execute(this, unlockDeviceCommand()); + } + + /** + * Get the current state of the device. + * + * @return true if the device is locked or false otherwise. + */ + default boolean isDeviceLocked() { + return CommandExecutionHelper.execute(this, getIsDeviceLockedCommand()); + } + } From 2ef45ee108ad7557369033018066fbef593ea215 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Fri, 5 Jan 2018 18:12:16 +0100 Subject: [PATCH 2/4] Address review comments --- .../LocksIOSDevice.java => LocksDevice.java} | 35 +++++++++---- .../java_client/android/AndroidDriver.java | 3 +- .../android/AndroidMobileCommandHelper.java | 24 --------- .../android/LocksAndroidDevice.java | 52 ------------------- .../io/appium/java_client/ios/IOSDriver.java | 3 +- 5 files changed, 29 insertions(+), 88 deletions(-) rename src/main/java/io/appium/java_client/{ios/LocksIOSDevice.java => LocksDevice.java} (68%) delete mode 100644 src/main/java/io/appium/java_client/android/LocksAndroidDevice.java diff --git a/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java b/src/main/java/io/appium/java_client/LocksDevice.java similarity index 68% rename from src/main/java/io/appium/java_client/ios/LocksIOSDevice.java rename to src/main/java/io/appium/java_client/LocksDevice.java index 5e76e700c..c1b856e74 100644 --- a/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java +++ b/src/main/java/io/appium/java_client/LocksDevice.java @@ -14,23 +14,38 @@ * limitations under the License. */ -package io.appium.java_client.ios; +package io.appium.java_client; + +import java.time.Duration; import static io.appium.java_client.MobileCommand.getIsDeviceLockedCommand; import static io.appium.java_client.MobileCommand.lockDeviceCommand; import static io.appium.java_client.MobileCommand.unlockDeviceCommand; -import io.appium.java_client.CommandExecutionHelper; -import io.appium.java_client.ExecutesMethod; - -import java.time.Duration; +public interface LocksDevice extends ExecutesMethod { + /** + * Check if the device is locked. + * + * @deprecated Use {@link #isDeviceLocked()} instead + * @return true if device is locked. False otherwise + */ + @Deprecated + default boolean isLocked() { + return CommandExecutionHelper.execute(this, getIsDeviceLockedCommand()); + } -public interface LocksIOSDevice extends ExecutesMethod { + /** + * This method locks a device. It will return silently if the device + * is already locked. + */ + default void lockDevice() { + lockDevice(Duration.ofSeconds(0)); + } /** * Lock the device (bring it to the lock screen) for a given number of * seconds or forever (until the command for unlocking is called). The call - * is ignored if the device already locked. + * is ignored if the device has been already locked. * * @param duration for how long to lock the screen. Minimum time resolution is one second. * A negative/zero value will lock the device and return immediately. @@ -40,19 +55,19 @@ default void lockDevice(Duration duration) { } /** - * Unlocks the device if it is locked or returns immediately if the device is already unlocked. + * Unlock the device if it is locked. This method will return silently if the device + * is not locked. */ default void unlockDevice() { CommandExecutionHelper.execute(this, unlockDeviceCommand()); } /** - * Get the current state of the device. + * Check if the device is locked. * * @return true if the device is locked or false otherwise. */ default boolean isDeviceLocked() { return CommandExecutionHelper.execute(this, getIsDeviceLockedCommand()); } - } diff --git a/src/main/java/io/appium/java_client/android/AndroidDriver.java b/src/main/java/io/appium/java_client/android/AndroidDriver.java index 129ae01ea..ccea3624b 100644 --- a/src/main/java/io/appium/java_client/android/AndroidDriver.java +++ b/src/main/java/io/appium/java_client/android/AndroidDriver.java @@ -23,6 +23,7 @@ import io.appium.java_client.AppiumDriver; import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.FindsByAndroidUIAutomator; +import io.appium.java_client.LocksDevice; import io.appium.java_client.PressesKeyCode; import io.appium.java_client.remote.MobilePlatform; import io.appium.java_client.service.local.AppiumDriverLocalService; @@ -46,7 +47,7 @@ public class AndroidDriver extends AppiumDriver implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity, - FindsByAndroidUIAutomator, LocksAndroidDevice, HasAndroidSettings, HasDeviceDetails, + FindsByAndroidUIAutomator, LocksDevice, HasAndroidSettings, HasDeviceDetails, HasSupportedPerformanceDataType, AuthenticatesByFinger { private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID; diff --git a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java index 375eed815..579b6aa63 100644 --- a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java +++ b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java @@ -172,18 +172,6 @@ public class AndroidMobileCommandHelper extends MobileCommand { IS_KEYBOARD_SHOWN, ImmutableMap.of()); } - /** - * This method forms a {@link java.util.Map} of parameters for the - * checking of the device state (is it locked or not). - * - * @return a key-value pair. The key is the command name. The value is a - * {@link java.util.Map} command arguments. - */ - public static Map.Entry> isLockedCommand() { - return new AbstractMap.SimpleEntry<>( - IS_LOCKED, ImmutableMap.of()); - } - /** * This method forms a {@link java.util.Map} of parameters for the * finger print authentication invocation. @@ -284,18 +272,6 @@ public class AndroidMobileCommandHelper extends MobileCommand { return new AbstractMap.SimpleEntry<>(TOGGLE_LOCATION_SERVICES, ImmutableMap.of()); } - /** - * This method forms a {@link java.util.Map} of parameters for the - * device unlocking. - * - * @return a key-value pair. The key is the command name. The value is a - * {@link java.util.Map} command arguments. - */ - public static Map.Entry> unlockCommand() { - return new AbstractMap.SimpleEntry<>(UNLOCK, ImmutableMap.of()); - } - - /** * This method forms a {@link java.util.Map} of parameters for the element * value replacement. It is used against input elements diff --git a/src/main/java/io/appium/java_client/android/LocksAndroidDevice.java b/src/main/java/io/appium/java_client/android/LocksAndroidDevice.java deleted file mode 100644 index f5b46d00e..000000000 --- a/src/main/java/io/appium/java_client/android/LocksAndroidDevice.java +++ /dev/null @@ -1,52 +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.android; - -import static io.appium.java_client.MobileCommand.lockDeviceCommand; -import static io.appium.java_client.android.AndroidMobileCommandHelper.isLockedCommand; -import static io.appium.java_client.android.AndroidMobileCommandHelper.unlockCommand; -import static java.time.Duration.ofMillis; - -import io.appium.java_client.CommandExecutionHelper; -import io.appium.java_client.ExecutesMethod; - -import java.time.Duration; - -public interface LocksAndroidDevice extends ExecutesMethod { - /** - * Check if the device is locked. - * - * @return true if device is locked. False otherwise - */ - default boolean isLocked() { - return CommandExecutionHelper.execute(this, isLockedCommand()); - } - - /** - * This method locks a device. - */ - default void lockDevice() { - CommandExecutionHelper.execute(this, lockDeviceCommand(ofMillis(0))); - } - - /** - * This method unlocks a device. - */ - default void unlockDevice() { - CommandExecutionHelper.execute(this, unlockCommand()); - } -} diff --git a/src/main/java/io/appium/java_client/ios/IOSDriver.java b/src/main/java/io/appium/java_client/ios/IOSDriver.java index ba5eb4fdf..e8e23ec3f 100644 --- a/src/main/java/io/appium/java_client/ios/IOSDriver.java +++ b/src/main/java/io/appium/java_client/ios/IOSDriver.java @@ -24,6 +24,7 @@ import io.appium.java_client.FindsByIosNSPredicate; import io.appium.java_client.FindsByIosUIAutomation; import io.appium.java_client.HidesKeyboardWithKeyName; +import io.appium.java_client.LocksDevice; import io.appium.java_client.remote.MobilePlatform; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; @@ -51,7 +52,7 @@ public class IOSDriver extends AppiumDriver implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings, - FindsByIosUIAutomation, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate, + FindsByIosUIAutomation, LocksDevice, PerformsTouchID, FindsByIosNSPredicate, FindsByIosClassChain, PushesFiles { private static final String IOS_PLATFORM = MobilePlatform.IOS; From a67b61cf7cc21f2f894bebee9c313f026cf074f9 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 30 Jan 2018 22:43:26 +0100 Subject: [PATCH 3/4] Fix merge --- .../android/LocksAndroidDevice.java | 50 ------------------- 1 file changed, 50 deletions(-) delete mode 100644 src/main/java/io/appium/java_client/android/LocksAndroidDevice.java diff --git a/src/main/java/io/appium/java_client/android/LocksAndroidDevice.java b/src/main/java/io/appium/java_client/android/LocksAndroidDevice.java deleted file mode 100644 index 9d0a55234..000000000 --- a/src/main/java/io/appium/java_client/android/LocksAndroidDevice.java +++ /dev/null @@ -1,50 +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.android; - -import static io.appium.java_client.MobileCommand.lockDeviceCommand; -import static io.appium.java_client.android.AndroidMobileCommandHelper.isLockedCommand; -import static io.appium.java_client.android.AndroidMobileCommandHelper.unlockCommand; -import static java.time.Duration.ofMillis; - -import io.appium.java_client.CommandExecutionHelper; -import io.appium.java_client.ExecutesMethod; - -public interface LocksAndroidDevice extends ExecutesMethod { - /** - * Check if the device is locked. - * - * @return true if device is locked. False otherwise - */ - default boolean isLocked() { - return CommandExecutionHelper.execute(this, isLockedCommand()); - } - - /** - * This method locks a device. - */ - default void lockDevice() { - CommandExecutionHelper.execute(this, lockDeviceCommand(ofMillis(0))); - } - - /** - * This method unlocks a device. - */ - default void unlockDevice() { - CommandExecutionHelper.execute(this, unlockCommand()); - } -} From 0e9c2eaeed42288589f22956d08f03186762b174 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 30 Jan 2018 22:47:18 +0100 Subject: [PATCH 4/4] Added locking tests for iOS --- .../java_client/android/AndroidDriverTest.java | 11 +++++++---- .../io/appium/java_client/ios/IOSDriverTest.java | 16 ++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java index 5459323ff..acbf1adb2 100644 --- a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java +++ b/src/test/java/io/appium/java_client/android/AndroidDriverTest.java @@ -102,10 +102,13 @@ public class AndroidDriverTest extends BaseAndroidTest { } @Test public void lockTest() { - driver.lockDevice(); - assertEquals(true, driver.isLocked()); - driver.unlockDevice(); - assertEquals(false, driver.isLocked()); + try { + driver.lockDevice(); + assertTrue(driver.isDeviceLocked()); + } finally { + driver.unlockDevice(); + assertFalse(driver.isDeviceLocked()); + } } @Test public void runAppInBackgroundTest() { diff --git a/src/test/java/io/appium/java_client/ios/IOSDriverTest.java b/src/test/java/io/appium/java_client/ios/IOSDriverTest.java index e768f227e..e777fd170 100644 --- a/src/test/java/io/appium/java_client/ios/IOSDriverTest.java +++ b/src/test/java/io/appium/java_client/ios/IOSDriverTest.java @@ -17,6 +17,7 @@ package io.appium.java_client.ios; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import io.appium.java_client.MobileElement; @@ -27,9 +28,6 @@ import org.openqa.selenium.ScreenOrientation; import org.openqa.selenium.html5.Location; -import java.time.Duration; -import java.util.function.Supplier; - public class IOSDriverTest extends AppIOSTest { //TODO There is no ability to check this function usibg simulators. @@ -62,11 +60,13 @@ public void getDeviceTimeTest() { } @Test public void lockTest() { - Supplier lock = () -> { - driver.lockDevice(Duration.ofSeconds(20)); - return true; - }; - assertTrue(lock.get()); + try { + driver.lockDevice(); + assertTrue(driver.isDeviceLocked()); + } finally { + driver.unlockDevice(); + assertFalse(driver.isDeviceLocked()); + } } @Test public void pullFileTest() {