Skip to content

Add handlers for lock/unlock in iOS #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/main/java/io/appium/java_client/LocksDevice.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
22 changes: 22 additions & 0 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,28 @@ public static ImmutableMap<String, Object> 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<String, Map<String, ?>> unlockDeviceCommand() {
return new AbstractMap.SimpleEntry<>(UNLOCK, ImmutableMap.<String, Object>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<String, Map<String, ?>> getIsDeviceLockedCommand() {
return new AbstractMap.SimpleEntry<>(IS_LOCKED, ImmutableMap.<String, Object>of());
}

public static Map.Entry<String, Map<String, ?>> getSettingsCommand() {
return new AbstractMap.SimpleEntry<>(GET_SETTINGS, ImmutableMap.<String, Object>of());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,7 +49,7 @@
public class AndroidDriver<T extends WebElement>
extends AppiumDriver<T>
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasAndroidSettings, HasDeviceDetails,
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
HasSupportedPerformanceDataType, AuthenticatesByFinger {

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +54,7 @@
public class IOSDriver<T extends WebElement>
extends AppiumDriver<T>
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
FindsByIosUIAutomation<T>, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
FindsByIosUIAutomation<T>, LocksDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
FindsByIosClassChain<T>, PushesFiles {

private static final String IOS_PLATFORM = MobilePlatform.IOS;
Expand Down
37 changes: 0 additions & 37 deletions src/main/java/io/appium/java_client/ios/LocksIOSDevice.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/io/appium/java_client/ios/IOSDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -62,11 +60,13 @@ public void getDeviceTimeTest() {
}

@Test public void lockTest() {
Supplier<Boolean> 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() {
Expand Down