Skip to content

Commit ab2ff11

Browse files
Merge pull request #477 from TikhomirovSergey/master
Android setting engine
2 parents e456f49 + c005127 commit ab2ff11

File tree

10 files changed

+256
-74
lines changed

10 files changed

+256
-74
lines changed

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

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

2222
import static io.appium.java_client.MobileCommand.GET_SESSION;
23-
import static io.appium.java_client.MobileCommand.GET_SETTINGS;
24-
import static io.appium.java_client.MobileCommand.SET_SETTINGS;
25-
import static io.appium.java_client.MobileCommand.prepareArguments;
2623

2724
import com.google.common.collect.ImmutableMap;
28-
import com.google.gson.JsonObject;
29-
import com.google.gson.JsonParser;
3025

3126
import io.appium.java_client.remote.AppiumCommandExecutor;
3227
import io.appium.java_client.remote.MobileCapabilityType;
@@ -387,43 +382,6 @@ public void zoom(int x, int y) {
387382
multiTouch.perform();
388383
}
389384

390-
/**
391-
* Get settings stored for this test session It's probably better to use a
392-
* convenience function, rather than use this function directly. Try finding
393-
* the method for the specific setting you want to read.
394-
*
395-
* @return JsonObject, a straight-up hash of settings.
396-
*/
397-
public JsonObject getSettings() {
398-
Response response = execute(GET_SETTINGS);
399-
400-
JsonParser parser = new JsonParser();
401-
return (JsonObject) parser.parse(response.getValue().toString());
402-
}
403-
404-
/**
405-
* Set settings for this test session It's probably better to use a
406-
* convenience function, rather than use this function directly. Try finding
407-
* the method for the specific setting you want to change.
408-
*
409-
* @param settings Map of setting keys and values.
410-
*/
411-
private void setSettings(ImmutableMap<?, ?> settings) {
412-
execute(SET_SETTINGS, prepareArguments("settings", settings));
413-
}
414-
415-
/**
416-
* Set a setting for this test session It's probably better to use a
417-
* convenience function, rather than use this function directly. Try finding
418-
* the method for the specific setting you want to change.
419-
*
420-
* @param setting AppiumSetting you wish to set.
421-
* @param value value of the setting.
422-
*/
423-
protected void setSetting(AppiumSetting setting, Object value) {
424-
setSettings(prepareArguments(setting.toString(), value));
425-
}
426-
427385
@Override public WebDriver context(String name) {
428386
checkNotNull(name, "Must supply a context name");
429387
execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package io.appium.java_client;
1818

19+
import io.appium.java_client.android.Setting;
20+
1921
/**
20-
* Enums defining constants for Appium Settings which can be set and toggled during a test session.
22+
* This enum is deprecated. Was moved to {@link Setting}
2123
*/
24+
@Deprecated
2225
public enum AppiumSetting {
2326

2427
IGNORE_UNIMPORTANT_VIEWS("ignoreUnimportantViews");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public class MobileCommand {
4747
protected static final String CLOSE_APP = "closeApp";
4848
protected static final String LOCK = "lock";
4949
protected static final String COMPLEX_FIND = "complexFind";
50-
protected static final String GET_SETTINGS = "getSettings";
51-
protected static final String SET_SETTINGS = "setSettings";
5250
protected static final String GET_DEVICE_TIME = "getDeviceTime";
5351
protected static final String GET_SESSION = "getSession";
5452
//iOS
@@ -67,6 +65,8 @@ public class MobileCommand {
6765
protected static final String TOGGLE_LOCATION_SERVICES = "toggleLocationServices";
6866
protected static final String UNLOCK = "unlock";
6967
protected static final String REPLACE_VALUE = "replaceValue";
68+
protected static final String GET_SETTINGS = "getSettings";
69+
protected static final String SET_SETTINGS = "setSettings";
7070

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

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;
2222

2323
import io.appium.java_client.AppiumDriver;
24-
import io.appium.java_client.AppiumSetting;
2524
import io.appium.java_client.CommandExecutionHelper;
2625
import io.appium.java_client.FindsByAndroidUIAutomator;
2726
import io.appium.java_client.android.internal.JsonToAndroidElementConverter;
@@ -48,7 +47,7 @@
4847
public class AndroidDriver<T extends WebElement>
4948
extends AppiumDriver<T>
5049
implements AndroidDeviceActionShortcuts, HasNetworkConnection, PushesFiles, StartsActivity,
51-
FindsByAndroidUIAutomator<T>, LocksAndroidDevice {
50+
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasSettings {
5251

5352
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
5453

@@ -188,18 +187,4 @@ public void openNotifications() {
188187
public void toggleLocationServices() {
189188
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
190189
}
191-
192-
/**
193-
* Set the `ignoreUnimportantViews` setting. *Android-only method*.
194-
* Sets whether Android devices should use `setCompressedLayoutHeirarchy()`
195-
* which ignores all views which are marked IMPORTANT_FOR_ACCESSIBILITY_NO
196-
* or IMPORTANT_FOR_ACCESSIBILITY_AUTO (and have been deemed not important
197-
* by the system), in an attempt to make things less confusing or faster.
198-
*
199-
* @param compress ignores unimportant views if true, doesn't ignore otherwise.
200-
*/
201-
// Should be moved to the subclass
202-
public void ignoreUnimportantViews(Boolean compress) {
203-
setSetting(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS, compress);
204-
}
205190
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,13 @@ public class AndroidMobileCommandHelper extends MobileCommand {
290290
return new AbstractMap.SimpleEntry<>(
291291
REPLACE_VALUE, prepareArguments(parameters, values));
292292
}
293+
294+
public static Map.Entry<String, Map<String, ?>> getSettingsCommand() {
295+
return new AbstractMap.SimpleEntry<>(GET_SETTINGS, ImmutableMap.<String, Object>of());
296+
}
297+
298+
public static Map.Entry<String, Map<String, ?>> setSettingsCommand(Setting setting, Object value) {
299+
return new AbstractMap.SimpleEntry<>(SET_SETTINGS, prepareArguments("settings",
300+
prepareArguments(setting.toString(), value)));
301+
}
293302
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.android;
18+
19+
enum ConfiguratorParameters {
20+
WAIT_FOR_IDLE_TIMEOUT("setWaitForIdleTimeout"),
21+
WAIT_FOR_SELECTOR_TIMEOUT("setWaitForSelectorTimeout"),
22+
WAIT_SCROLL_ACKNOWLEDGMENT_TIMEOUT("setScrollAcknowledgmentTimeout"),
23+
WAIT_ACTION_ACKNOWLEDGMENT_TIMEOUT("setActionAcknowledgmentTimeout"),
24+
KEY_INJECTION_DELAY("setKeyInjectionDelay");
25+
26+
private final String name;
27+
28+
ConfiguratorParameters(String name) {
29+
this.name = name;
30+
}
31+
32+
String format(int value) {
33+
return String.format("{\"method\":\"%s\",\"value\":%d}", name, value);
34+
}
35+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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.android;
18+
19+
import static io.appium.java_client.android.AndroidMobileCommandHelper.getSettingsCommand;
20+
import static io.appium.java_client.android.AndroidMobileCommandHelper.setSettingsCommand;
21+
22+
import com.google.gson.JsonObject;
23+
import com.google.gson.JsonParser;
24+
25+
import io.appium.java_client.CommandExecutionHelper;
26+
import io.appium.java_client.ExecutesMethod;
27+
28+
import org.openqa.selenium.remote.Response;
29+
30+
import java.util.Map;
31+
32+
interface HasSettings extends ExecutesMethod {
33+
/**
34+
* Set a setting for this test session It's probably better to use a
35+
* convenience function, rather than use this function directly. Try finding
36+
* the method for the specific setting you want to change.
37+
*
38+
* @param setting Setting you wish to set.
39+
* @param value value of the setting.
40+
*/
41+
default void setSetting(Setting setting, Object value) {
42+
CommandExecutionHelper.execute(this, setSettingsCommand(setting, value));
43+
}
44+
45+
/**
46+
* Get settings stored for this test session It's probably better to use a
47+
* convenience function, rather than use this function directly. Try finding
48+
* the method for the specific setting you want to read.
49+
*
50+
* @return JsonObject, a straight-up hash of settings.
51+
*/
52+
default JsonObject getSettings() {
53+
Map.Entry<String, Map<String, ?>> keyValuePair = getSettingsCommand();
54+
Response response = execute(keyValuePair.getKey(), keyValuePair.getValue());
55+
56+
JsonParser parser = new JsonParser();
57+
return (JsonObject) parser.parse(response.getValue().toString());
58+
}
59+
60+
/**
61+
* Set the `ignoreUnimportantViews` setting. *Android-only method*.
62+
* Sets whether Android devices should use `setCompressedLayoutHeirarchy()`
63+
* which ignores all views which are marked IMPORTANT_FOR_ACCESSIBILITY_NO
64+
* or IMPORTANT_FOR_ACCESSIBILITY_AUTO (and have been deemed not important
65+
* by the system), in an attempt to make things less confusing or faster.
66+
*
67+
* @param compress ignores unimportant views if true, doesn't ignore otherwise.
68+
*/
69+
default void ignoreUnimportantViews(Boolean compress) {
70+
setSetting(Setting.IGNORE_UNIMPORTANT_VIEWS, compress);
71+
}
72+
73+
/**
74+
* invoke {@code setWaitForIdleTimeout} in {@code com.android.uiautomator.core.Configurator}
75+
*
76+
* @param timeout in milliseconds, 0 would reset to its default value
77+
*/
78+
default void configuratorSetWaitForIdleTimeout(int timeout) {
79+
setSetting(Setting.CONFIGURATOR,
80+
ConfiguratorParameters.WAIT_FOR_IDLE_TIMEOUT.format(timeout));
81+
}
82+
83+
/**
84+
* invoke {@code setWaitForSelectorTimeout} in {@code com.android.uiautomator.core.Configurator}
85+
*
86+
* @param timeout in milliseconds, 0 would reset to its default value
87+
*/
88+
default void configuratorSetWaitForSelectorTimeout(int timeout) {
89+
setSetting(Setting.CONFIGURATOR,
90+
ConfiguratorParameters.WAIT_FOR_SELECTOR_TIMEOUT.format(timeout));
91+
}
92+
93+
/**
94+
* invoke {@code setScrollAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
95+
*
96+
* @param timeout in milliseconds, 0 would reset to its default value
97+
*/
98+
default void configuratorSetScrollAcknowledgmentTimeout(int timeout) {
99+
setSetting(Setting.CONFIGURATOR,
100+
ConfiguratorParameters.WAIT_SCROLL_ACKNOWLEDGMENT_TIMEOUT.format(timeout));
101+
}
102+
103+
/**
104+
* invoke {@code configuratorSetKeyInjectionDelay} in {@code com.android.uiautomator.core.Configurator}
105+
*
106+
* @param delay in milliseconds, 0 would reset to its default value
107+
*/
108+
default void configuratorSetKeyInjectionDelay(int delay) {
109+
setSetting(Setting.CONFIGURATOR,
110+
ConfiguratorParameters.KEY_INJECTION_DELAY.format(delay));
111+
}
112+
113+
/**
114+
* invoke {@code setActionAcknowledgmentTimeout} in {@code com.android.uiautomator.core.Configurator}
115+
*
116+
* @param timeout in milliseconds, 0 would reset to its default value
117+
*/
118+
default void configuratorSetActionAcknowledgmentTimeout(int timeout) {
119+
setSetting(Setting.CONFIGURATOR,
120+
ConfiguratorParameters.WAIT_ACTION_ACKNOWLEDGMENT_TIMEOUT.format(timeout));
121+
}
122+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.android;
18+
19+
/**
20+
* Enums defining constants for Appium Settings which can be set and toggled during a test session.
21+
*/
22+
public enum Setting {
23+
24+
IGNORE_UNIMPORTANT_VIEWS("ignoreUnimportantViews"),
25+
CONFIGURATOR("configurator");
26+
27+
private String name;
28+
29+
Setting(String name) {
30+
this.name = name;
31+
}
32+
33+
public String toString() {
34+
return this.name;
35+
}
36+
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertTrue;
2424

25-
import io.appium.java_client.AppiumSetting;
2625
import org.apache.commons.codec.binary.Base64;
2726
import org.apache.commons.io.FileUtils;
2827
import org.junit.Test;
@@ -82,18 +81,6 @@ public class AndroidDriverTest extends BaseAndroidTest {
8281
}
8382
}
8483

85-
@Test public void ignoreUnimportantViews() {
86-
driver.ignoreUnimportantViews(true);
87-
boolean ignoreViews =
88-
driver.getSettings().get(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS.toString())
89-
.getAsBoolean();
90-
assertTrue(ignoreViews);
91-
driver.ignoreUnimportantViews(false);
92-
ignoreViews = driver.getSettings().get(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS.toString())
93-
.getAsBoolean();
94-
assertFalse(ignoreViews);
95-
}
96-
9784
@Test public void toggleLocationServicesTest() {
9885
driver.toggleLocationServices();
9986
}

0 commit comments

Comments
 (0)