|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko; |
|
6 |
|
7 import android.app.Activity; |
|
8 import android.view.View; |
|
9 import android.view.ViewGroup; |
|
10 import android.widget.EditText; |
|
11 import android.widget.TextSwitcher; |
|
12 import android.widget.TextView; |
|
13 |
|
14 public class FennecNativeElement implements Element { |
|
15 private final Activity mActivity; |
|
16 private Integer mId; |
|
17 |
|
18 public FennecNativeElement(Integer id, Activity activity) { |
|
19 mId = id; |
|
20 mActivity = activity; |
|
21 } |
|
22 |
|
23 public Integer getId() { |
|
24 return mId; |
|
25 } |
|
26 |
|
27 private boolean mClickSuccess; |
|
28 |
|
29 public boolean click() { |
|
30 mClickSuccess = false; |
|
31 RobocopUtils.runOnUiThreadSync(mActivity, |
|
32 new Runnable() { |
|
33 public void run() { |
|
34 View view = (View)mActivity.findViewById(mId); |
|
35 if (view != null) { |
|
36 if (view.performClick()) { |
|
37 mClickSuccess = true; |
|
38 } else { |
|
39 FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN, |
|
40 "Robocop called click on an element with no listener"); |
|
41 } |
|
42 } else { |
|
43 FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, |
|
44 "click: unable to find view "+mId); |
|
45 } |
|
46 } |
|
47 }); |
|
48 return mClickSuccess; |
|
49 } |
|
50 |
|
51 private Object mText; |
|
52 |
|
53 public String getText() { |
|
54 mText = null; |
|
55 RobocopUtils.runOnUiThreadSync(mActivity, |
|
56 new Runnable() { |
|
57 public void run() { |
|
58 View v = mActivity.findViewById(mId); |
|
59 if (v instanceof EditText) { |
|
60 EditText et = (EditText)v; |
|
61 mText = et.getEditableText(); |
|
62 } else if (v instanceof TextSwitcher) { |
|
63 TextSwitcher ts = (TextSwitcher)v; |
|
64 ts.getNextView(); |
|
65 mText = ((TextView)ts.getCurrentView()).getText(); |
|
66 } else if (v instanceof ViewGroup) { |
|
67 ViewGroup vg = (ViewGroup)v; |
|
68 for (int i = 0; i < vg.getChildCount(); i++) { |
|
69 if (vg.getChildAt(i) instanceof TextView) { |
|
70 mText = ((TextView)vg.getChildAt(i)).getText(); |
|
71 } |
|
72 } |
|
73 } else if (v instanceof TextView) { |
|
74 mText = ((TextView)v).getText(); |
|
75 } else if (v == null) { |
|
76 FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, |
|
77 "getText: unable to find view "+mId); |
|
78 } else { |
|
79 FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, |
|
80 "getText: unhandled type for view "+mId); |
|
81 } |
|
82 } // end of run() method definition |
|
83 } // end of anonymous Runnable object instantiation |
|
84 ); |
|
85 if (mText == null) { |
|
86 FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN, |
|
87 "getText: Text is null for view "+mId); |
|
88 return null; |
|
89 } |
|
90 return mText.toString(); |
|
91 } |
|
92 |
|
93 private boolean mDisplayed; |
|
94 |
|
95 public boolean isDisplayed() { |
|
96 mDisplayed = false; |
|
97 RobocopUtils.runOnUiThreadSync(mActivity, |
|
98 new Runnable() { |
|
99 public void run() { |
|
100 View view = (View)mActivity.findViewById(mId); |
|
101 if (view != null) { |
|
102 mDisplayed = true; |
|
103 } |
|
104 } |
|
105 }); |
|
106 return mDisplayed; |
|
107 } |
|
108 } |