Skip to content
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

Android setting engine #477

Merged
merged 5 commits into from
Sep 27, 2016
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
42 changes: 0 additions & 42 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@
import static com.google.common.base.Preconditions.checkNotNull;

import static io.appium.java_client.MobileCommand.GET_SESSION;
import static io.appium.java_client.MobileCommand.GET_SETTINGS;
import static io.appium.java_client.MobileCommand.SET_SETTINGS;
import static io.appium.java_client.MobileCommand.prepareArguments;

import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import io.appium.java_client.remote.AppiumCommandExecutor;
import io.appium.java_client.remote.MobileCapabilityType;
Expand Down Expand Up @@ -387,43 +382,6 @@ public void zoom(int x, int y) {
multiTouch.perform();
}

/**
* Get settings stored for this test session It's probably better to use a
* convenience function, rather than use this function directly. Try finding
* the method for the specific setting you want to read.
*
* @return JsonObject, a straight-up hash of settings.
*/
public JsonObject getSettings() {
Response response = execute(GET_SETTINGS);

JsonParser parser = new JsonParser();
return (JsonObject) parser.parse(response.getValue().toString());
}

/**
* Set settings for this test session It's probably better to use a
* convenience function, rather than use this function directly. Try finding
* the method for the specific setting you want to change.
*
* @param settings Map of setting keys and values.
*/
private void setSettings(ImmutableMap<?, ?> settings) {
execute(SET_SETTINGS, prepareArguments("settings", settings));
}

/**
* Set a setting for this test session It's probably better to use a
* convenience function, rather than use this function directly. Try finding
* the method for the specific setting you want to change.
*
* @param setting AppiumSetting you wish to set.
* @param value value of the setting.
*/
protected void setSetting(AppiumSetting setting, Object value) {
setSettings(prepareArguments(setting.toString(), value));
}

@Override public WebDriver context(String name) {
checkNotNull(name, "Must supply a context name");
execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/appium/java_client/AppiumSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package io.appium.java_client;

import io.appium.java_client.android.Setting;

/**
* Enums defining constants for Appium Settings which can be set and toggled during a test session.
* This enum is deprecated. Was moved to {@link Setting}
*/
@Deprecated
public enum AppiumSetting {

IGNORE_UNIMPORTANT_VIEWS("ignoreUnimportantViews");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public class MobileCommand {
protected static final String CLOSE_APP = "closeApp";
protected static final String LOCK = "lock";
protected static final String COMPLEX_FIND = "complexFind";
protected static final String GET_SETTINGS = "getSettings";
protected static final String SET_SETTINGS = "setSettings";
protected static final String GET_DEVICE_TIME = "getDeviceTime";
protected static final String GET_SESSION = "getSession";
//iOS
Expand All @@ -67,6 +65,8 @@ public class MobileCommand {
protected static final String TOGGLE_LOCATION_SERVICES = "toggleLocationServices";
protected static final String UNLOCK = "unlock";
protected static final String REPLACE_VALUE = "replaceValue";
protected static final String GET_SETTINGS = "getSettings";
protected static final String SET_SETTINGS = "setSettings";

public static final Map<String, CommandInfo> commandRepository = createCommandRepository();

Expand Down
17 changes: 1 addition & 16 deletions src/main/java/io/appium/java_client/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.AppiumSetting;
import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.FindsByAndroidUIAutomator;
import io.appium.java_client.android.internal.JsonToAndroidElementConverter;
Expand All @@ -48,7 +47,7 @@
public class AndroidDriver<T extends WebElement>
extends AppiumDriver<T>
implements AndroidDeviceActionShortcuts, HasNetworkConnection, PushesFiles, StartsActivity,
FindsByAndroidUIAutomator<T>, LocksAndroidDevice {
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasSettings {

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

Expand Down Expand Up @@ -188,18 +187,4 @@ public void openNotifications() {
public void toggleLocationServices() {
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
}

/**
* Set the `ignoreUnimportantViews` setting. *Android-only method*.
* Sets whether Android devices should use `setCompressedLayoutHeirarchy()`
* which ignores all views which are marked IMPORTANT_FOR_ACCESSIBILITY_NO
* or IMPORTANT_FOR_ACCESSIBILITY_AUTO (and have been deemed not important
* by the system), in an attempt to make things less confusing or faster.
*
* @param compress ignores unimportant views if true, doesn't ignore otherwise.
*/
// Should be moved to the subclass
public void ignoreUnimportantViews(Boolean compress) {
setSetting(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS, compress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,13 @@ public class AndroidMobileCommandHelper extends MobileCommand {
return new AbstractMap.SimpleEntry<>(
REPLACE_VALUE, prepareArguments(parameters, values));
}

public static Map.Entry<String, Map<String, ?>> getSettingsCommand() {
return new AbstractMap.SimpleEntry<>(GET_SETTINGS, ImmutableMap.<String, Object>of());
}

public static Map.Entry<String, Map<String, ?>> setSettingsCommand(Setting setting, Object value) {
return new AbstractMap.SimpleEntry<>(SET_SETTINGS, prepareArguments("settings",
prepareArguments(setting.toString(), value)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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;

enum ConfiguratorParameters {
WAIT_FOR_IDLE_TIMEOUT("setWaitForIdleTimeout"),
WAIT_FOR_SELECTOR_TIMEOUT("setWaitForSelectorTimeout"),
WAIT_SCROLL_ACKNOWLEDGMENT_TIMEOUT("setScrollAcknowledgmentTimeout"),
WAIT_ACTION_ACKNOWLEDGMENT_TIMEOUT("setActionAcknowledgmentTimeout"),
KEY_INJECTION_DELAY("setKeyInjectionDelay");

private final String name;

ConfiguratorParameters(String name) {
this.name = name;
}

String format(int value) {
return String.format("{\"method\":\"%s\",\"value\":%d}", name, value);
}
}
122 changes: 122 additions & 0 deletions src/main/java/io/appium/java_client/android/HasSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.android.AndroidMobileCommandHelper.getSettingsCommand;
import static io.appium.java_client.android.AndroidMobileCommandHelper.setSettingsCommand;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.ExecutesMethod;

import org.openqa.selenium.remote.Response;

import java.util.Map;

interface HasSettings extends ExecutesMethod {
/**
* Set a setting for this test session It's probably better to use a
* convenience function, rather than use this function directly. Try finding
* the method for the specific setting you want to change.
*
* @param setting Setting you wish to set.
* @param value value of the setting.
*/
default void setSetting(Setting setting, Object value) {
CommandExecutionHelper.execute(this, setSettingsCommand(setting, value));
}

/**
* Get settings stored for this test session It's probably better to use a
* convenience function, rather than use this function directly. Try finding
* the method for the specific setting you want to read.
*
* @return JsonObject, a straight-up hash of settings.
*/
default JsonObject getSettings() {
Map.Entry<String, Map<String, ?>> keyValuePair = getSettingsCommand();
Response response = execute(keyValuePair.getKey(), keyValuePair.getValue());

JsonParser parser = new JsonParser();
return (JsonObject) parser.parse(response.getValue().toString());
}

/**
* Set the `ignoreUnimportantViews` setting. *Android-only method*.
* Sets whether Android devices should use `setCompressedLayoutHeirarchy()`
* which ignores all views which are marked IMPORTANT_FOR_ACCESSIBILITY_NO
* or IMPORTANT_FOR_ACCESSIBILITY_AUTO (and have been deemed not important
* by the system), in an attempt to make things less confusing or faster.
*
* @param compress ignores unimportant views if true, doesn't ignore otherwise.
*/
default void ignoreUnimportantViews(Boolean compress) {
setSetting(Setting.IGNORE_UNIMPORTANT_VIEWS, compress);
}

/**
* invoke {@code setWaitForIdleTimeout} in {@code com.android.uiautomator.core.Configurator}
*
* @param timeout in milliseconds, 0 would reset to its default value
*/
default void configuratorSetWaitForIdleTimeout(int timeout) {
setSetting(Setting.CONFIGURATOR,
ConfiguratorParameters.WAIT_FOR_IDLE_TIMEOUT.format(timeout));
}

/**
* invoke {@code setWaitForSelectorTimeout} in {@code com.android.uiautomator.core.Configurator}
*
* @param timeout in milliseconds, 0 would reset to its default value
*/
default void configuratorSetWaitForSelectorTimeout(int timeout) {
setSetting(Setting.CONFIGURATOR,
ConfiguratorParameters.WAIT_FOR_SELECTOR_TIMEOUT.format(timeout));
}

/**
* invoke {@code setScrollAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
*
* @param timeout in milliseconds, 0 would reset to its default value
*/
default void configuratorSetScrollAcknowledgmentTimeout(int timeout) {
setSetting(Setting.CONFIGURATOR,
ConfiguratorParameters.WAIT_SCROLL_ACKNOWLEDGMENT_TIMEOUT.format(timeout));
}

/**
* invoke {@code configuratorSetKeyInjectionDelay} in {@code com.android.uiautomator.core.Configurator}
*
* @param delay in milliseconds, 0 would reset to its default value
*/
default void configuratorSetKeyInjectionDelay(int delay) {
setSetting(Setting.CONFIGURATOR,
ConfiguratorParameters.KEY_INJECTION_DELAY.format(delay));
}

/**
* invoke {@code setActionAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
*
* @param timeout in milliseconds, 0 would reset to its default value
*/
default void configuratorSetActionAcknowledgmentTimeout(int timeout) {
setSetting(Setting.CONFIGURATOR,
ConfiguratorParameters.WAIT_ACTION_ACKNOWLEDGMENT_TIMEOUT.format(timeout));
}
}
36 changes: 36 additions & 0 deletions src/main/java/io/appium/java_client/android/Setting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;

/**
* Enums defining constants for Appium Settings which can be set and toggled during a test session.
*/
public enum Setting {

IGNORE_UNIMPORTANT_VIEWS("ignoreUnimportantViews"),
CONFIGURATOR("configurator");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this code is still the two level configurator. Now it should be like ignoreUnimportantViews. Every setting here for configurators should be individivual


private String name;

Setting(String name) {
this.name = name;
}

public String toString() {
return this.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import io.appium.java_client.AppiumSetting;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
Expand Down Expand Up @@ -82,18 +81,6 @@ public class AndroidDriverTest extends BaseAndroidTest {
}
}

@Test public void ignoreUnimportantViews() {
driver.ignoreUnimportantViews(true);
boolean ignoreViews =
driver.getSettings().get(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS.toString())
.getAsBoolean();
assertTrue(ignoreViews);
driver.ignoreUnimportantViews(false);
ignoreViews = driver.getSettings().get(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS.toString())
.getAsBoolean();
assertFalse(ignoreViews);
}

@Test public void toggleLocationServicesTest() {
driver.toggleLocationServices();
}
Expand Down
Loading