18086eac30b939adde43080f9026bbd54cb156fadMichael Wright/*
28086eac30b939adde43080f9026bbd54cb156fadMichael Wright * Copyright (C) 2011 The Android Open Source Project
38086eac30b939adde43080f9026bbd54cb156fadMichael Wright *
48086eac30b939adde43080f9026bbd54cb156fadMichael Wright * Licensed under the Apache License, Version 2.0 (the "License");
58086eac30b939adde43080f9026bbd54cb156fadMichael Wright * you may not use this file except in compliance with the License.
68086eac30b939adde43080f9026bbd54cb156fadMichael Wright * You may obtain a copy of the License at
78086eac30b939adde43080f9026bbd54cb156fadMichael Wright *
88086eac30b939adde43080f9026bbd54cb156fadMichael Wright *      http://www.apache.org/licenses/LICENSE-2.0
98086eac30b939adde43080f9026bbd54cb156fadMichael Wright *
108086eac30b939adde43080f9026bbd54cb156fadMichael Wright * Unless required by applicable law or agreed to in writing, software
118086eac30b939adde43080f9026bbd54cb156fadMichael Wright * distributed under the License is distributed on an "AS IS" BASIS,
128086eac30b939adde43080f9026bbd54cb156fadMichael Wright * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138086eac30b939adde43080f9026bbd54cb156fadMichael Wright * See the License for the specific language governing permissions and
148086eac30b939adde43080f9026bbd54cb156fadMichael Wright * limitations under the License.
158086eac30b939adde43080f9026bbd54cb156fadMichael Wright */
168086eac30b939adde43080f9026bbd54cb156fadMichael Wrightpackage com.android.monkeyrunner;
178086eac30b939adde43080f9026bbd54cb156fadMichael Wright
188086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport com.google.common.base.Preconditions;
198086eac30b939adde43080f9026bbd54cb156fadMichael Wright
208086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport com.android.chimpchat.core.IChimpView;
218086eac30b939adde43080f9026bbd54cb156fadMichael Wright
228086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport com.android.monkeyrunner.doc.MonkeyRunnerExported;
238086eac30b939adde43080f9026bbd54cb156fadMichael Wright
248086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport org.python.core.ArgParser;
258086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport org.python.core.ClassDictInit;
268086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport org.python.core.PyBoolean;
278086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport org.python.core.PyInteger;
288086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport org.python.core.PyList;
298086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport org.python.core.PyObject;
308086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport org.python.core.PyString;
318086eac30b939adde43080f9026bbd54cb156fadMichael Wright
328086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport java.util.List;
338086eac30b939adde43080f9026bbd54cb156fadMichael Wrightimport java.util.logging.Logger;
348086eac30b939adde43080f9026bbd54cb156fadMichael Wright
358086eac30b939adde43080f9026bbd54cb156fadMichael Wright/*
368086eac30b939adde43080f9026bbd54cb156fadMichael Wright * Jython wrapper for the ChimpView class
378086eac30b939adde43080f9026bbd54cb156fadMichael Wright */
388086eac30b939adde43080f9026bbd54cb156fadMichael Wright@MonkeyRunnerExported(doc = "Represents a view object.")
398086eac30b939adde43080f9026bbd54cb156fadMichael Wrightpublic class MonkeyView extends PyObject implements ClassDictInit {
408086eac30b939adde43080f9026bbd54cb156fadMichael Wright    private static final Logger LOG = Logger.getLogger(MonkeyView.class.getName());
418086eac30b939adde43080f9026bbd54cb156fadMichael Wright
428086eac30b939adde43080f9026bbd54cb156fadMichael Wright    private IChimpView impl;
438086eac30b939adde43080f9026bbd54cb156fadMichael Wright
448086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public static void classDictInit(PyObject dict) {
458086eac30b939adde43080f9026bbd54cb156fadMichael Wright        JythonUtils.convertDocAnnotationsForClass(MonkeyView.class, dict);
468086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
478086eac30b939adde43080f9026bbd54cb156fadMichael Wright
488086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public MonkeyView(IChimpView impl) {
498086eac30b939adde43080f9026bbd54cb156fadMichael Wright        this.impl = impl;
508086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
518086eac30b939adde43080f9026bbd54cb156fadMichael Wright
528086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Get the checked status of the view",
538086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "A boolean value for whether the item is checked or not")
548086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyBoolean getChecked(PyObject[] args, String[] kws) {
558086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
568086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
578086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return new PyBoolean(impl.getChecked());
588086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
598086eac30b939adde43080f9026bbd54cb156fadMichael Wright
608086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the class name of the view",
618086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The class name of the view as a string")
628086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyString getViewClass(PyObject[] args, String[] kws) {
638086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
648086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
658086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return new PyString(impl.getViewClass());
668086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
678086eac30b939adde43080f9026bbd54cb156fadMichael Wright
688086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the text contained by the view",
698086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The text contained in the view")
708086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyString getText(PyObject[] args, String[] kws) {
718086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
728086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
738086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return new PyString(impl.getText());
748086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
758086eac30b939adde43080f9026bbd54cb156fadMichael Wright
768086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the location of the view in the form of a MonkeyRect",
778086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The location of the view as a MonkeyRect object")
788086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public MonkeyRect getLocation(PyObject[] args, String[] kws) {
798086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
808086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
818086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return new MonkeyRect(impl.getLocation());
828086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
838086eac30b939adde43080f9026bbd54cb156fadMichael Wright
848086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the enabled status of the view",
858086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The enabled status of the view as a boolean")
868086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyBoolean getEnabled(PyObject[] args, String[] kws) {
878086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
888086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
898086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return new PyBoolean(impl.getEnabled());
908086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
918086eac30b939adde43080f9026bbd54cb156fadMichael Wright
928086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the selected status of the view",
938086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The selected status of the view as a boolean")
948086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyBoolean getSelected(PyObject[] args, String[] kws) {
958086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
968086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
978086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return new PyBoolean(impl.getSelected());
988086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
998086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1008086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Sets the selected status of the view",
1018086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          args = {"selected"},
1028086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          argDocs = { "The boolean value to set selected to" })
1038086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public void setSelected(PyObject[] args, String[] kws) {
1048086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
1058086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
1068086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1078086eac30b939adde43080f9026bbd54cb156fadMichael Wright        PyBoolean pySelected = (PyBoolean) ap.getPyObject(0, new PyBoolean(false));
1088086eac30b939adde43080f9026bbd54cb156fadMichael Wright        boolean selected = (Boolean) pySelected.__tojava__(Boolean.class);
1098086eac30b939adde43080f9026bbd54cb156fadMichael Wright        impl.setSelected(selected);
1108086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
1118086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1128086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the focused status of the view",
1138086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The focused status of the view as a boolean")
1148086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyBoolean getFocused(PyObject[] args, String[] kws) {
1158086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
1168086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
1178086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return new PyBoolean(impl.getFocused());
1188086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
1198086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1208086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Sets the focused status of the view",
1218086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          args = {"focused"},
1228086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          argDocs = { "The boolean value to set focused to" })
1238086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public void setFocused(PyObject[] args, String[] kws) {
1248086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
1258086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
1268086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1278086eac30b939adde43080f9026bbd54cb156fadMichael Wright        PyBoolean pyFocused = (PyBoolean) ap.getPyObject(0, new PyBoolean(false));
1288086eac30b939adde43080f9026bbd54cb156fadMichael Wright        boolean focused = (Boolean) pyFocused.__tojava__(Boolean.class);
1298086eac30b939adde43080f9026bbd54cb156fadMichael Wright        impl.setFocused(focused);
1308086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
1318086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1328086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the parent of the current view",
1338086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The parent of the view as a MonkeyView object")
1348086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public MonkeyView getParent(PyObject[] args, String[] kws) {
1358086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
1368086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
1378086eac30b939adde43080f9026bbd54cb156fadMichael Wright        MonkeyView parent = new MonkeyView(impl.getParent());
1388086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return parent;
1398086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
1408086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1418086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the children of the current view",
1428086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The children of the view as a list of MonkeyView objects")
1438086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyList getChildren(PyObject[] args, String[] kws) {
1448086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
1458086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
1468086eac30b939adde43080f9026bbd54cb156fadMichael Wright        List<IChimpView> chimpChildren = impl.getChildren();
1478086eac30b939adde43080f9026bbd54cb156fadMichael Wright        PyList children = new PyList();
1488086eac30b939adde43080f9026bbd54cb156fadMichael Wright        for (IChimpView child : chimpChildren) {
1498086eac30b939adde43080f9026bbd54cb156fadMichael Wright            children.append(new MonkeyView(child));
1508086eac30b939adde43080f9026bbd54cb156fadMichael Wright        }
1518086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return children;
1528086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
1538086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1548086eac30b939adde43080f9026bbd54cb156fadMichael Wright    @MonkeyRunnerExported(doc = "Returns the accessibility ids of the current view",
1558086eac30b939adde43080f9026bbd54cb156fadMichael Wright                          returns = "The accessibility ids of the view as a list of ints")
1568086eac30b939adde43080f9026bbd54cb156fadMichael Wright    public PyList getAccessibilityIds(PyObject[] args, String[] kws) {
1578086eac30b939adde43080f9026bbd54cb156fadMichael Wright        ArgParser ap = JythonUtils.createArgParser(args, kws);
1588086eac30b939adde43080f9026bbd54cb156fadMichael Wright        Preconditions.checkNotNull(ap);
1598086eac30b939adde43080f9026bbd54cb156fadMichael Wright        int[] ids = impl.getAccessibilityIds();
1608086eac30b939adde43080f9026bbd54cb156fadMichael Wright        PyList pyIds = new PyList();
1618086eac30b939adde43080f9026bbd54cb156fadMichael Wright        for (int i = 0; i < ids.length; i++) {
1628086eac30b939adde43080f9026bbd54cb156fadMichael Wright            pyIds.append(new PyInteger(ids[i]));
1638086eac30b939adde43080f9026bbd54cb156fadMichael Wright        }
1648086eac30b939adde43080f9026bbd54cb156fadMichael Wright        return pyIds;
1658086eac30b939adde43080f9026bbd54cb156fadMichael Wright    }
1668086eac30b939adde43080f9026bbd54cb156fadMichael Wright
1678086eac30b939adde43080f9026bbd54cb156fadMichael Wright}
168