• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
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 com.android.monkeyrunner.easy;
18 
19 import com.google.common.base.Preconditions;
20 
21 import com.android.chimpchat.hierarchyviewer.HierarchyViewer;
22 import com.android.hierarchyviewerlib.device.ViewNode;
23 import com.android.monkeyrunner.JythonUtils;
24 import com.android.monkeyrunner.doc.MonkeyRunnerExported;
25 
26 import org.python.core.ArgParser;
27 import org.python.core.ClassDictInit;
28 import org.python.core.PyObject;
29 
30 /**
31  * Select a view object based on some criteria.
32  *
33  * Currently supports the By.id criteria to search for an element by id.
34  * In the future it will support other criteria such as:
35  *   By.classid - search by class.
36  *   By.hash - search by hashcode
37  * and recursively searching under an already selected object.
38  *
39  * WARNING: This API is under development, expect the interface to change
40  * without notice.
41  *
42  * TODO: Implement other selectors, like classid, hash, and so on.
43  * TODO: separate java-only core from jython wrapper
44  */
45 public class By extends PyObject implements ClassDictInit {
classDictInit(PyObject dict)46     public static void classDictInit(PyObject dict) {
47         JythonUtils.convertDocAnnotationsForClass(By.class, dict);
48     }
49 
50     private String id;
51 
By(String id)52     By(String id) {
53         this.id = id;
54     }
55 
56     @MonkeyRunnerExported(doc = "Select an object by id.",
57             args = { "id" },
58             argDocs = { "The identifier of the object." })
id(PyObject[] args, String[] kws)59     public static By id(PyObject[] args, String[] kws) {
60         ArgParser ap = JythonUtils.createArgParser(args, kws);
61         Preconditions.checkNotNull(ap);
62 
63         String id = ap.getString(0);
64         return new By(id);
65     }
66 
id(String id)67     public static By id(String id) {
68         return new By(id);
69     }
70 
findView(HierarchyViewer viewer)71     public ViewNode findView(HierarchyViewer viewer) {
72         return viewer.findViewById(id);
73     }
74 
75 
76 }
77