diff --git a/src/main/java/io/appium/java_client/LocksDevice.java b/src/main/java/io/appium/java_client/LocksDevice.java new file mode 100644 index 000000000..c1b856e74 --- /dev/null +++ b/src/main/java/io/appium/java_client/LocksDevice.java @@ -0,0 +1,73 @@ +/* + * 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; + +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; + +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()); + } + + /** + * 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 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. + */ + default void lockDevice(Duration duration) { + CommandExecutionHelper.execute(this, lockDeviceCommand(duration)); + } + + /** + * 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()); + } + + /** + * 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/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index db80a1601..023c0974e 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -357,6 +357,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/android/AndroidDriver.java b/src/main/java/io/appium/java_client/android/AndroidDriver.java index 6018f6a3b..fad692d75 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; @@ -48,7 +49,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 3bae85839..c82c324a4 100644 --- a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java +++ b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java @@ -163,7 +163,8 @@ public class AndroidMobileCommandHelper extends MobileCommand { } /** - * This method forms a {@link Map} of parameters for the checking of the device state (is it locked or not). + * This method forms a {@link java.util.Map} of parameters for the + * finger print authentication invocation. * * @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments. */ @@ -264,7 +265,7 @@ public class AndroidMobileCommandHelper extends MobileCommand { } /** - * This method forms a {@link Map} of parameters for the device unlocking. + * This method forms a {@link java.util.Map} of parameters for the element * * @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments. */ 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()); - } -} 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 d39f7baff..71678563f 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; @@ -53,7 +54,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; diff --git a/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java b/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java deleted file mode 100644 index 007c03c73..000000000 --- a/src/main/java/io/appium/java_client/ios/LocksIOSDevice.java +++ /dev/null @@ -1,37 +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.ios; - -import static io.appium.java_client.MobileCommand.lockDeviceCommand; - -import io.appium.java_client.CommandExecutionHelper; -import io.appium.java_client.ExecutesMethod; - -import java.time.Duration; - -public interface LocksIOSDevice extends ExecutesMethod { - - /** - * Lock the device (bring it to the lock screen) for a given number of - * seconds. - * - * @param duration for how long to lock the screen. Minimum time resolution is one second - */ - default void lockDevice(Duration duration) { - CommandExecutionHelper.execute(this, lockDeviceCommand(duration)); - } -} 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() {