*/
package com.android.monkeyrunner;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
+import com.google.common.base.Functions;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Collections2;
+
+import com.android.monkeyrunner.core.IMonkeyDevice;
+import com.android.monkeyrunner.core.IMonkeyImage;
+import com.android.monkeyrunner.core.TouchPressType;
+import com.android.monkeyrunner.doc.MonkeyRunnerExported;
+import com.android.monkeyrunner.easy.HierarchyViewer;
import org.python.core.ArgParser;
import org.python.core.ClassDictInit;
import org.python.core.Py;
import org.python.core.PyDictionary;
-import org.python.core.PyException;
import org.python.core.PyObject;
import org.python.core.PyTuple;
-import com.android.monkeyrunner.core.IMonkeyDevice;
-import com.android.monkeyrunner.core.IMonkeyImage;
-import com.android.monkeyrunner.doc.MonkeyRunnerExported;
-import com.android.monkeyrunner.easy.HierarchyViewer;
-
-import com.google.common.base.Functions;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.ImmutableMap;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.logging.Logger;
/*
* Abstract base class that represents a single connected Android
*/
@MonkeyRunnerExported(doc = "Represents a device attached to the system.")
public class MonkeyDevice extends PyObject implements ClassDictInit {
+ private static final Logger LOG = Logger.getLogger(MonkeyDevice.class.getName());
+
public static void classDictInit(PyObject dict) {
JythonUtils.convertDocAnnotationsForClass(MonkeyDevice.class, dict);
}
@MonkeyRunnerExported(doc = "Sends a DOWN event when used with touch() or press().")
- public static final String DOWN = "down";
- @MonkeyRunnerExported(doc = "Sends an UP event when used with touch() or press().")
- public static final String UP = "up";
- @MonkeyRunnerExported(doc = "Sends a DOWN event, immediately followed by an UP event when used with touch() or press()")
- public static final String DOWN_AND_UP = "downAndUp";
-
- // TODO: This may not be accessible from jython; if so, remove it.
- public enum TouchPressType {
- DOWN, UP, DOWN_AND_UP,
- }
+ public static final String DOWN = TouchPressType.DOWN.getIdentifier();
- public static final Map<String, IMonkeyDevice.TouchPressType> TOUCH_NAME_TO_ENUM =
- ImmutableMap.of(DOWN, IMonkeyDevice.TouchPressType.DOWN,
- UP, IMonkeyDevice.TouchPressType.UP,
- DOWN_AND_UP, IMonkeyDevice.TouchPressType.DOWN_AND_UP);
+ @MonkeyRunnerExported(doc = "Sends an UP event when used with touch() or press().")
+ public static final String UP = TouchPressType.UP.getIdentifier();
- private static final Set<String> VALID_DOWN_UP_TYPES = TOUCH_NAME_TO_ENUM.keySet();
+ @MonkeyRunnerExported(doc = "Sends a DOWN event, immediately followed by an UP event when used with touch() or press()")
+ public static final String DOWN_AND_UP = TouchPressType.DOWN_AND_UP.getIdentifier();
private IMonkeyDevice impl;
int x = ap.getInt(0);
int y = ap.getInt(1);
- // Default
- String type = MonkeyDevice.DOWN_AND_UP;
- try {
- String tmpType = ap.getString(1);
- if (VALID_DOWN_UP_TYPES.contains(tmpType)) {
- type = tmpType;
- } else {
- // not a valid type
- type = MonkeyDevice.DOWN_AND_UP;
- }
- } catch (PyException e) {
- // bad stuff was passed in, just use the already specified default value
- type = MonkeyDevice.DOWN_AND_UP;
+ TouchPressType type = TouchPressType.fromIdentifier(ap.getString(2));
+ if (type == null) {
+ LOG.warning(String.format("Invalid TouchPressType specified (%s) default used instead",
+ ap.getString(2)));
+ type = TouchPressType.DOWN_AND_UP;
}
- impl.touch(x, y, TOUCH_NAME_TO_ENUM.get(type));
+
+ impl.touch(x, y, type);
}
@MonkeyRunnerExported(doc = "Simulates dragging (touch, hold, and move) on the device screen.",
Preconditions.checkNotNull(ap);
String name = ap.getString(0);
-
- // Default
- String type = MonkeyDevice.DOWN_AND_UP;
- try {
- String tmpType = ap.getString(1);
- if (VALID_DOWN_UP_TYPES.contains(tmpType)) {
- type = tmpType;
- } else {
- // not a valid type
- type = MonkeyDevice.DOWN_AND_UP;
- }
- } catch (PyException e) {
- // bad stuff was passed in, just use the already specified default value
- type = MonkeyDevice.DOWN_AND_UP;
+ TouchPressType type = TouchPressType.fromIdentifier(ap.getString(1));
+ if (type == null) {
+ LOG.warning(String.format("Invalid TouchPressType specified (%s) default used instead",
+ ap.getString(2)));
+ type = TouchPressType.DOWN_AND_UP;
}
- impl.press(name, TOUCH_NAME_TO_ENUM.get(type));
+
+ impl.press(name, type);
}
@MonkeyRunnerExported(doc = "Types the specified string on the keyboard. This is " +
--- /dev/null
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * 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 com.android.monkeyrunner.core;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * TouchPressType enum contains valid input for the "touch" Monkey command.
+ * When passed as a string, the "identifier" value is used.
+ */
+public enum TouchPressType {
+ DOWN("down"), UP("up"), DOWN_AND_UP("downAndUp");
+
+ private static final Map<String,TouchPressType> identifierToEnum =
+ new HashMap<String,TouchPressType>();
+ static {
+ for (TouchPressType type : values()) {
+ identifierToEnum.put(type.identifier, type);
+ }
+ }
+
+ private String identifier;
+
+ TouchPressType(String identifier) {
+ this.identifier = identifier;
+ }
+
+ public String getIdentifier() {
+ return identifier;
+ }
+
+ public static TouchPressType fromIdentifier(String name) {
+ return identifierToEnum.get(name);
+ }
+}
\ No newline at end of file
import com.google.common.base.Preconditions;
import com.android.hierarchyviewerlib.device.ViewNode;
-import com.android.hierarchyviewerlib.device.ViewNode.Property;
import com.android.monkeyrunner.JythonUtils;
import com.android.monkeyrunner.MonkeyDevice;
-import com.android.monkeyrunner.core.IMonkeyDevice.TouchPressType;
+import com.android.monkeyrunner.core.TouchPressType;
import com.android.monkeyrunner.doc.MonkeyRunnerExported;
import org.eclipse.swt.graphics.Point;
import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyObject;
-import org.python.core.PyString;
import org.python.core.PyTuple;
import java.util.Set;
By selector = getSelector(ap, 0);
String tmpType = ap.getString(1);
- TouchPressType type = MonkeyDevice.TOUCH_NAME_TO_ENUM.get(tmpType);
- if (type == null) type = TouchPressType.DOWN_AND_UP;
+ TouchPressType type = TouchPressType.fromIdentifier(tmpType);
+ Preconditions.checkNotNull(type, "Invalid touch type: " + tmpType);
// TODO: try catch rethrow PyExc
touch(selector, type);
}