Skip to content

Commit a9de13f

Browse files
Merge pull request #799 from mykola-mokhnach/lock
Add handlers for lock/unlock in iOS
2 parents 72562c9 + 0e9c2ea commit a9de13f

File tree

9 files changed

+117
-103
lines changed

9 files changed

+117
-103
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client;
18+
19+
import java.time.Duration;
20+
21+
import static io.appium.java_client.MobileCommand.getIsDeviceLockedCommand;
22+
import static io.appium.java_client.MobileCommand.lockDeviceCommand;
23+
import static io.appium.java_client.MobileCommand.unlockDeviceCommand;
24+
25+
public interface LocksDevice extends ExecutesMethod {
26+
/**
27+
* Check if the device is locked.
28+
*
29+
* @deprecated Use {@link #isDeviceLocked()} instead
30+
* @return true if device is locked. False otherwise
31+
*/
32+
@Deprecated
33+
default boolean isLocked() {
34+
return CommandExecutionHelper.execute(this, getIsDeviceLockedCommand());
35+
}
36+
37+
/**
38+
* This method locks a device. It will return silently if the device
39+
* is already locked.
40+
*/
41+
default void lockDevice() {
42+
lockDevice(Duration.ofSeconds(0));
43+
}
44+
45+
/**
46+
* Lock the device (bring it to the lock screen) for a given number of
47+
* seconds or forever (until the command for unlocking is called). The call
48+
* is ignored if the device has been already locked.
49+
*
50+
* @param duration for how long to lock the screen. Minimum time resolution is one second.
51+
* A negative/zero value will lock the device and return immediately.
52+
*/
53+
default void lockDevice(Duration duration) {
54+
CommandExecutionHelper.execute(this, lockDeviceCommand(duration));
55+
}
56+
57+
/**
58+
* Unlock the device if it is locked. This method will return silently if the device
59+
* is not locked.
60+
*/
61+
default void unlockDevice() {
62+
CommandExecutionHelper.execute(this, unlockDeviceCommand());
63+
}
64+
65+
/**
66+
* Check if the device is locked.
67+
*
68+
* @return true if the device is locked or false otherwise.
69+
*/
70+
default boolean isDeviceLocked() {
71+
return CommandExecutionHelper.execute(this, getIsDeviceLockedCommand());
72+
}
73+
}

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,28 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
357357
LOCK, prepareArguments("seconds", duration.getSeconds()));
358358
}
359359

360+
/**
361+
* This method forms a {@link java.util.Map} of parameters for the
362+
* device unlocking.
363+
*
364+
* @return a key-value pair. The key is the command name. The value is a
365+
* {@link java.util.Map} command arguments.
366+
*/
367+
public static Map.Entry<String, Map<String, ?>> unlockDeviceCommand() {
368+
return new AbstractMap.SimpleEntry<>(UNLOCK, ImmutableMap.<String, Object>of());
369+
}
370+
371+
/**
372+
* This method forms a {@link java.util.Map} of parameters for the
373+
* device locked query.
374+
*
375+
* @return a key-value pair. The key is the command name. The value is a
376+
* {@link java.util.Map} command arguments.
377+
*/
378+
public static Map.Entry<String, Map<String, ?>> getIsDeviceLockedCommand() {
379+
return new AbstractMap.SimpleEntry<>(IS_LOCKED, ImmutableMap.<String, Object>of());
380+
}
381+
360382
public static Map.Entry<String, Map<String, ?>> getSettingsCommand() {
361383
return new AbstractMap.SimpleEntry<>(GET_SETTINGS, ImmutableMap.<String, Object>of());
362384
}

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.appium.java_client.AppiumDriver;
2424
import io.appium.java_client.CommandExecutionHelper;
2525
import io.appium.java_client.FindsByAndroidUIAutomator;
26+
import io.appium.java_client.LocksDevice;
2627
import io.appium.java_client.PressesKeyCode;
2728
import io.appium.java_client.remote.MobilePlatform;
2829
import io.appium.java_client.service.local.AppiumDriverLocalService;
@@ -48,7 +49,7 @@
4849
public class AndroidDriver<T extends WebElement>
4950
extends AppiumDriver<T>
5051
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
51-
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasAndroidSettings, HasDeviceDetails,
52+
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
5253
HasSupportedPerformanceDataType, AuthenticatesByFinger {
5354

5455
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ public class AndroidMobileCommandHelper extends MobileCommand {
163163
}
164164

165165
/**
166-
* This method forms a {@link Map} of parameters for the checking of the device state (is it locked or not).
166+
* This method forms a {@link java.util.Map} of parameters for the
167+
* finger print authentication invocation.
167168
*
168169
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
169170
*/
@@ -264,7 +265,7 @@ public class AndroidMobileCommandHelper extends MobileCommand {
264265
}
265266

266267
/**
267-
* This method forms a {@link Map} of parameters for the device unlocking.
268+
* This method forms a {@link java.util.Map} of parameters for the element
268269
*
269270
* @return a key-value pair. The key is the command name. The value is a {@link Map} command arguments.
270271
*/

src/main/java/io/appium/java_client/android/LocksAndroidDevice.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/java/io/appium/java_client/ios/IOSDriver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.appium.java_client.FindsByIosNSPredicate;
2525
import io.appium.java_client.FindsByIosUIAutomation;
2626
import io.appium.java_client.HidesKeyboardWithKeyName;
27+
import io.appium.java_client.LocksDevice;
2728
import io.appium.java_client.remote.MobilePlatform;
2829
import io.appium.java_client.service.local.AppiumDriverLocalService;
2930
import io.appium.java_client.service.local.AppiumServiceBuilder;
@@ -53,7 +54,7 @@
5354
public class IOSDriver<T extends WebElement>
5455
extends AppiumDriver<T>
5556
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
56-
FindsByIosUIAutomation<T>, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
57+
FindsByIosUIAutomation<T>, LocksDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
5758
FindsByIosClassChain<T>, PushesFiles {
5859

5960
private static final String IOS_PLATFORM = MobilePlatform.IOS;

src/main/java/io/appium/java_client/ios/LocksIOSDevice.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/test/java/io/appium/java_client/android/AndroidDriverTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,13 @@ public class AndroidDriverTest extends BaseAndroidTest {
102102
}
103103

104104
@Test public void lockTest() {
105-
driver.lockDevice();
106-
assertEquals(true, driver.isLocked());
107-
driver.unlockDevice();
108-
assertEquals(false, driver.isLocked());
105+
try {
106+
driver.lockDevice();
107+
assertTrue(driver.isDeviceLocked());
108+
} finally {
109+
driver.unlockDevice();
110+
assertFalse(driver.isDeviceLocked());
111+
}
109112
}
110113

111114
@Test public void runAppInBackgroundTest() {

src/test/java/io/appium/java_client/ios/IOSDriverTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.appium.java_client.ios;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertTrue;
2122

2223
import io.appium.java_client.MobileElement;
@@ -27,9 +28,6 @@
2728
import org.openqa.selenium.ScreenOrientation;
2829
import org.openqa.selenium.html5.Location;
2930

30-
import java.time.Duration;
31-
import java.util.function.Supplier;
32-
3331
public class IOSDriverTest extends AppIOSTest {
3432

3533
//TODO There is no ability to check this function usibg simulators.
@@ -62,11 +60,13 @@ public void getDeviceTimeTest() {
6260
}
6361

6462
@Test public void lockTest() {
65-
Supplier<Boolean> lock = () -> {
66-
driver.lockDevice(Duration.ofSeconds(20));
67-
return true;
68-
};
69-
assertTrue(lock.get());
63+
try {
64+
driver.lockDevice();
65+
assertTrue(driver.isDeviceLocked());
66+
} finally {
67+
driver.unlockDevice();
68+
assertFalse(driver.isDeviceLocked());
69+
}
7070
}
7171

7272
@Test public void pullFileTest() {

0 commit comments

Comments
 (0)