diff options
author | Terence Haddock <thaddock@google.com> | 2011-04-15 10:35:51 +0200 |
---|---|---|
committer | Terence Haddock <thaddock@google.com> | 2011-04-15 15:41:08 +0200 |
commit | 42a7ac915d595a9820546b5d97f3f3c6ca666057 (patch) | |
tree | 4648762129c16021d23c30b96389836fc034859e | |
parent | f31a5baeafc0bac9185e3440fae8676d0b486568 (diff) | |
download | sdk-42a7ac915d595a9820546b5d97f3f3c6ca666057.zip sdk-42a7ac915d595a9820546b5d97f3f3c6ca666057.tar.gz sdk-42a7ac915d595a9820546b5d97f3f3c6ca666057.tar.bz2 |
Use enum for converting TouchPressType values
Change-Id: I072425caf7da7c1c01bf757243005ba0ebd97014
8 files changed, 97 insertions, 72 deletions
diff --git a/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java b/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java index 649e33c..100da68 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java +++ b/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java @@ -15,28 +15,27 @@ */ 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 @@ -46,28 +45,20 @@ import com.google.common.collect.ImmutableMap; */ @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; @@ -129,21 +120,14 @@ public class MonkeyDevice extends PyObject implements ClassDictInit { 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.", @@ -191,22 +175,14 @@ public class MonkeyDevice extends PyObject implements ClassDictInit { 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 " + diff --git a/monkeyrunner/src/com/android/monkeyrunner/adb/AdbMonkeyDevice.java b/monkeyrunner/src/com/android/monkeyrunner/adb/AdbMonkeyDevice.java index 60eaba9..050292a 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/adb/AdbMonkeyDevice.java +++ b/monkeyrunner/src/com/android/monkeyrunner/adb/AdbMonkeyDevice.java @@ -29,6 +29,7 @@ import com.android.monkeyrunner.MonkeyManager; import com.android.monkeyrunner.adb.LinearInterpolator.Point; import com.android.monkeyrunner.core.IMonkeyImage; import com.android.monkeyrunner.core.IMonkeyDevice; +import com.android.monkeyrunner.core.TouchPressType; import com.android.monkeyrunner.easy.HierarchyViewer; import java.io.IOException; diff --git a/monkeyrunner/src/com/android/monkeyrunner/core/IMonkeyDevice.java b/monkeyrunner/src/com/android/monkeyrunner/core/IMonkeyDevice.java index c081a56..9c06ec4 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/core/IMonkeyDevice.java +++ b/monkeyrunner/src/com/android/monkeyrunner/core/IMonkeyDevice.java @@ -27,10 +27,6 @@ import javax.annotation.Nullable; * MonkeyDevice interface. */ public interface IMonkeyDevice { - enum TouchPressType { - DOWN, UP, DOWN_AND_UP, - } - /** * Create a MonkeyMananger for talking to this device. * diff --git a/monkeyrunner/src/com/android/monkeyrunner/core/TouchPressType.java b/monkeyrunner/src/com/android/monkeyrunner/core/TouchPressType.java new file mode 100644 index 0000000..cfa878a --- /dev/null +++ b/monkeyrunner/src/com/android/monkeyrunner/core/TouchPressType.java @@ -0,0 +1,49 @@ +/* + * 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 diff --git a/monkeyrunner/src/com/android/monkeyrunner/easy/EasyMonkeyDevice.java b/monkeyrunner/src/com/android/monkeyrunner/easy/EasyMonkeyDevice.java index e72e462..1c6c71b 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/easy/EasyMonkeyDevice.java +++ b/monkeyrunner/src/com/android/monkeyrunner/easy/EasyMonkeyDevice.java @@ -19,10 +19,9 @@ package com.android.monkeyrunner.easy; 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; @@ -32,7 +31,6 @@ import org.python.core.Py; 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; @@ -76,8 +74,8 @@ public class EasyMonkeyDevice extends PyObject implements ClassDictInit { 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); } diff --git a/monkeyrunner/src/com/android/monkeyrunner/recorder/MonkeyRecorder.java b/monkeyrunner/src/com/android/monkeyrunner/recorder/MonkeyRecorder.java index 914a5b9..bde882e 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/recorder/MonkeyRecorder.java +++ b/monkeyrunner/src/com/android/monkeyrunner/recorder/MonkeyRecorder.java @@ -15,6 +15,7 @@ */ package com.android.monkeyrunner.recorder; +import com.android.monkeyrunner.MonkeyDevice; import com.android.monkeyrunner.adb.AdbBackend; import com.android.monkeyrunner.core.IMonkeyBackend; import com.android.monkeyrunner.core.IMonkeyDevice; @@ -45,7 +46,11 @@ public class MonkeyRecorder { * * @param device */ - public static void start(final IMonkeyDevice device) { + public static void start(final MonkeyDevice device) { + start(device.getImpl()); + } + + /* package */static void start(final IMonkeyDevice device) { MonkeyRecorderFrame frame = new MonkeyRecorderFrame(device); // TODO: this is a hack until the window listener works. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); diff --git a/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/PressAction.java b/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/PressAction.java index 66a933a..88c3fa7 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/PressAction.java +++ b/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/PressAction.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableBiMap; import com.android.monkeyrunner.MonkeyDevice; import com.android.monkeyrunner.core.IMonkeyDevice; +import com.android.monkeyrunner.core.TouchPressType; /** * Action to press a certain button. @@ -62,7 +63,6 @@ public class PressAction implements Action { @Override public void execute(IMonkeyDevice device) { - device.press(key, - MonkeyDevice.TOUCH_NAME_TO_ENUM.get(downUpFlag)); + device.press(key, TouchPressType.fromIdentifier(downUpFlag)); } } diff --git a/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/TouchAction.java b/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/TouchAction.java index 4e0ae2d..1001bef 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/TouchAction.java +++ b/monkeyrunner/src/com/android/monkeyrunner/recorder/actions/TouchAction.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableBiMap; import com.android.monkeyrunner.MonkeyDevice; import com.android.monkeyrunner.core.IMonkeyDevice; +import com.android.monkeyrunner.core.TouchPressType; /** * Action to touch the touchscreen at a certain location. @@ -48,8 +49,7 @@ public class TouchAction implements Action { @Override public void execute(IMonkeyDevice device) throws Exception { - device.touch(x, y, - MonkeyDevice.TOUCH_NAME_TO_ENUM.get(direction)); + device.touch(x, y, TouchPressType.fromIdentifier(direction)); } @Override |