diff options
Diffstat (limited to 'monkeyrunner')
3 files changed, 328 insertions, 7 deletions
diff --git a/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java b/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java index 61e92a0..e60d12e 100644 --- a/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java +++ b/monkeyrunner/src/com/android/monkeyrunner/MonkeyDevice.java @@ -20,6 +20,8 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Collections2; import com.android.chimpchat.ChimpChat; +import com.android.chimpchat.core.By; +import com.android.chimpchat.core.IChimpView; import com.android.chimpchat.core.IChimpDevice; import com.android.chimpchat.core.IChimpImage; import com.android.chimpchat.core.TouchPressType; @@ -31,11 +33,13 @@ import org.python.core.ArgParser; import org.python.core.ClassDictInit; import org.python.core.Py; import org.python.core.PyDictionary; +import org.python.core.PyList; import org.python.core.PyObject; import org.python.core.PyTuple; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -177,19 +181,14 @@ public class MonkeyDevice extends PyObject implements ClassDictInit { Preconditions.checkNotNull(ap); String name = ap.getString(0); - String touchType = ap.getString(1); + String touchType = ap.getString(1, DOWN_AND_UP); // The old docs had this string, and so in favor of maintaining // backwards compatibility, let's special case it to the new one. if (touchType.equals("DOWN_AND_UP")){ touchType = "downAndUp"; } - TouchPressType type = TouchPressType.fromIdentifier(ap.getString(1)); - if (type == null) { - LOG.warning(String.format("Invalid TouchPressType specified (%s) default used instead", - ap.getString(1))); - type = TouchPressType.DOWN_AND_UP; - } + TouchPressType type = TouchPressType.fromIdentifier(touchType); impl.press(name, type); } @@ -357,4 +356,74 @@ public class MonkeyDevice extends PyObject implements ClassDictInit { impl.wake(); } + + + @MonkeyRunnerExported(doc = "Retrieve the properties that can be queried") + public PyList getPropertyList(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + Collection<String> properties = impl.getPropertyList(); + return new PyList(properties); + } + + @MonkeyRunnerExported(doc = "Retrieve the view ids for the current application") + public PyList getViewIdList(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + Collection<String> viewIds = impl.getViewIdList(); + return new PyList(viewIds); + } + + //Because the pythonic way is to have flatter hierarchies, rather than doing the + //findView(By.id("foo")) style the java code uses, I'm going to expose them as individual + //method calls. This is similar to WebDriver's python bindings. + @MonkeyRunnerExported(doc = "Obtains the view with the specified id.", + args = {"id"}, + argDocs = {"The id of the view to retrieve."}, + returns = "The view object with the specified id.") + public MonkeyView getViewById(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + String id = ap.getString(0); + IChimpView view = impl.getView(By.id(id)); + return new MonkeyView(view); + } + + @MonkeyRunnerExported(doc = "Obtains the view with the specified accessibility ids.", + args = {"windowId", "accessibility id"}, + argDocs = {"The window id of the view to retrieve.", + "The accessibility id of the view to retrieve."}, + returns = "The view object with the specified id.") + public MonkeyView getViewByAccessibilityIds(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + int windowId = ap.getInt(0); + int accessibilityId = ap.getInt(1); + IChimpView view = impl.getView(By.accessibilityIds(windowId, accessibilityId)); + return new MonkeyView(view); + } + + @MonkeyRunnerExported(doc = "Obtains current root view", + returns = "The root view object") + public MonkeyView getRootView(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new MonkeyView(impl.getRootView()); + } + + @MonkeyRunnerExported(doc = "Obtains a list of views that contain the specified text.", + args = {"text"}, + argDocs = {"The text to search for"}, + returns = "A list of view objects that contain the specified text.") + public PyList getViewsByText(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + String text = ap.getString(0); + Collection<IChimpView> views = impl.getViews(By.text(text)); + PyList pyViews = new PyList(); + for (IChimpView view : views) { + pyViews.append(new MonkeyView(view)); + } + return pyViews; + } } diff --git a/monkeyrunner/src/com/android/monkeyrunner/MonkeyRect.java b/monkeyrunner/src/com/android/monkeyrunner/MonkeyRect.java new file mode 100644 index 0000000..98b2ecc --- /dev/null +++ b/monkeyrunner/src/com/android/monkeyrunner/MonkeyRect.java @@ -0,0 +1,85 @@ +/* + * 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; + +import com.android.chimpchat.core.ChimpRect; + +import com.android.monkeyrunner.doc.MonkeyRunnerExported; + +import org.python.core.ArgParser; +import org.python.core.ClassDictInit; +import org.python.core.PyInteger; +import org.python.core.PyList; +import org.python.core.PyObject; + +import java.util.List; +import java.util.LinkedList; +import java.util.logging.Logger; + +/* + * A Jython wrap for the ChimpRect class that stores coordinate information for views + */ +@MonkeyRunnerExported(doc = "Represents the coordinates of a rectangular object") +public class MonkeyRect extends PyObject implements ClassDictInit { + private static final Logger LOG = Logger.getLogger(MonkeyRect.class.getName()); + + private ChimpRect rect; + + @MonkeyRunnerExported(doc = "The x coordinate of the left side of the rectangle") + public int left; + @MonkeyRunnerExported(doc = "The y coordinate of the top side of the rectangle") + public int top; + @MonkeyRunnerExported(doc = "The x coordinate of the right side of the rectangle") + public int right; + @MonkeyRunnerExported(doc = "The y coordinate of the bottom side of the rectangle") + public int bottom; + + public static void classDictInit(PyObject dict) { + JythonUtils.convertDocAnnotationsForClass(MonkeyRect.class, dict); + } + + public MonkeyRect(ChimpRect rect) { + this.rect = rect; + this.left = rect.left; + this.right = rect.right; + this.top = rect.top; + this.bottom = rect.bottom; + } + + @MonkeyRunnerExported(doc = "Returns the width of the rectangle", + returns = "The width of the rectangle as an integer") + public PyInteger getWidth() { + return new PyInteger(right-left); + } + + @MonkeyRunnerExported(doc = "Returns the height of the rectangle", + returns = "The height of the rectangle as an integer") + public PyInteger getHeight() { + return new PyInteger(bottom-top); + } + + @MonkeyRunnerExported(doc = "Returns a two item list that contains the x and y value of " + + "the center of the rectangle", + returns = "The center coordinates as a two item list of integers") + public PyList getCenter(){ + List<PyInteger> center = new LinkedList<PyInteger>(); + /* Center x coordinate */ + center.add(new PyInteger(left+(right-left)/2)); + /* Center y coordinate */ + center.add(new PyInteger(top+(bottom-top)/2)); + return new PyList(center); + } +} diff --git a/monkeyrunner/src/com/android/monkeyrunner/MonkeyView.java b/monkeyrunner/src/com/android/monkeyrunner/MonkeyView.java new file mode 100644 index 0000000..8c1edb8 --- /dev/null +++ b/monkeyrunner/src/com/android/monkeyrunner/MonkeyView.java @@ -0,0 +1,167 @@ +/* + * 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; + +import com.google.common.base.Preconditions; + +import com.android.chimpchat.core.IChimpView; + +import com.android.monkeyrunner.doc.MonkeyRunnerExported; + +import org.python.core.ArgParser; +import org.python.core.ClassDictInit; +import org.python.core.PyBoolean; +import org.python.core.PyInteger; +import org.python.core.PyList; +import org.python.core.PyObject; +import org.python.core.PyString; + +import java.util.List; +import java.util.logging.Logger; + +/* + * Jython wrapper for the ChimpView class + */ +@MonkeyRunnerExported(doc = "Represents a view object.") +public class MonkeyView extends PyObject implements ClassDictInit { + private static final Logger LOG = Logger.getLogger(MonkeyView.class.getName()); + + private IChimpView impl; + + public static void classDictInit(PyObject dict) { + JythonUtils.convertDocAnnotationsForClass(MonkeyView.class, dict); + } + + public MonkeyView(IChimpView impl) { + this.impl = impl; + } + + @MonkeyRunnerExported(doc = "Get the checked status of the view", + returns = "A boolean value for whether the item is checked or not") + public PyBoolean getChecked(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new PyBoolean(impl.getChecked()); + } + + @MonkeyRunnerExported(doc = "Returns the class name of the view", + returns = "The class name of the view as a string") + public PyString getViewClass(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new PyString(impl.getViewClass()); + } + + @MonkeyRunnerExported(doc = "Returns the text contained by the view", + returns = "The text contained in the view") + public PyString getText(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new PyString(impl.getText()); + } + + @MonkeyRunnerExported(doc = "Returns the location of the view in the form of a MonkeyRect", + returns = "The location of the view as a MonkeyRect object") + public MonkeyRect getLocation(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new MonkeyRect(impl.getLocation()); + } + + @MonkeyRunnerExported(doc = "Returns the enabled status of the view", + returns = "The enabled status of the view as a boolean") + public PyBoolean getEnabled(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new PyBoolean(impl.getEnabled()); + } + + @MonkeyRunnerExported(doc = "Returns the selected status of the view", + returns = "The selected status of the view as a boolean") + public PyBoolean getSelected(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new PyBoolean(impl.getSelected()); + } + + @MonkeyRunnerExported(doc = "Sets the selected status of the view", + args = {"selected"}, + argDocs = { "The boolean value to set selected to" }) + public void setSelected(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + + PyBoolean pySelected = (PyBoolean) ap.getPyObject(0, new PyBoolean(false)); + boolean selected = (Boolean) pySelected.__tojava__(Boolean.class); + impl.setSelected(selected); + } + + @MonkeyRunnerExported(doc = "Returns the focused status of the view", + returns = "The focused status of the view as a boolean") + public PyBoolean getFocused(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + return new PyBoolean(impl.getFocused()); + } + + @MonkeyRunnerExported(doc = "Sets the focused status of the view", + args = {"focused"}, + argDocs = { "The boolean value to set focused to" }) + public void setFocused(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + + PyBoolean pyFocused = (PyBoolean) ap.getPyObject(0, new PyBoolean(false)); + boolean focused = (Boolean) pyFocused.__tojava__(Boolean.class); + impl.setFocused(focused); + } + + @MonkeyRunnerExported(doc = "Returns the parent of the current view", + returns = "The parent of the view as a MonkeyView object") + public MonkeyView getParent(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + MonkeyView parent = new MonkeyView(impl.getParent()); + return parent; + } + + @MonkeyRunnerExported(doc = "Returns the children of the current view", + returns = "The children of the view as a list of MonkeyView objects") + public PyList getChildren(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + List<IChimpView> chimpChildren = impl.getChildren(); + PyList children = new PyList(); + for (IChimpView child : chimpChildren) { + children.append(new MonkeyView(child)); + } + return children; + } + + @MonkeyRunnerExported(doc = "Returns the accessibility ids of the current view", + returns = "The accessibility ids of the view as a list of ints") + public PyList getAccessibilityIds(PyObject[] args, String[] kws) { + ArgParser ap = JythonUtils.createArgParser(args, kws); + Preconditions.checkNotNull(ap); + int[] ids = impl.getAccessibilityIds(); + PyList pyIds = new PyList(); + for (int i = 0; i < ids.length; i++) { + pyIds.append(new PyInteger(ids[i])); + } + return pyIds; + } + +} |