gecko/build/mobile/robocop/FennecNativeElement.java.in

171 lines
6.7 KiB
Java
Raw Normal View History

#filter substitution
2012-05-21 04:12:37 -07:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package @ANDROID_PACKAGE_NAME@;
import java.util.List;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextSwitcher;
import android.app.Instrumentation;
import com.jayway.android.robotium.solo.Solo;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
public class FennecNativeElement implements Element {
private final Activity mActivity;
private Integer mId;
private Solo mSolo;
// max time to wait for thread synchronization
private static final int MAX_WAIT_MS = 60000;
public FennecNativeElement(Integer id, Activity activity, Solo solo) {
mId = id;
mActivity = activity;
mSolo = solo;
}
public Integer getId() {
return mId;
}
private boolean mClickSuccess;
public boolean click() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mClickSuccess = false;
mActivity.runOnUiThread(
new Runnable() {
public void run() {
View view = (View)mActivity.findViewById(mId);
if (view != null) {
if (view.performClick()) {
mClickSuccess = true;
} else {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
"Robocop called click on an element with no listener");
}
} else {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"click: unable to find view "+mId);
}
try {
syncQueue.put(new Object());
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
}
});
try {
// Wait for the UiThread code to finish running
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"click: time-out waiting for UI thread");
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
}
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
return mClickSuccess;
}
private Object mText;
public String getText() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mText = null;
mActivity.runOnUiThread(
new Runnable() {
public void run() {
View v = mActivity.findViewById(mId);
if (v instanceof EditText) {
EditText et = (EditText)v;
mText = et.getEditableText();
} else if (v instanceof TextSwitcher) {
TextSwitcher ts = (TextSwitcher)v;
ts.getNextView();
mText = ((TextView)ts.getCurrentView()).getText();
} else if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup)v;
for (int i = 0; i < vg.getChildCount(); i++) {
if (vg.getChildAt(i) instanceof TextView) {
mText = ((TextView)vg.getChildAt(i)).getText();
}
}
} else if (v instanceof TextView) {
mText = ((TextView)v).getText();
} else if (v == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"getText: unable to find view "+mId);
} else {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"getText: unhandled type for view "+mId);
}
try {
syncQueue.put(new Object());
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
} // end of run() method definition
} // end of anonymous Runnable object instantiation
);
try {
// Wait for the UiThread code to finish running
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"getText: time-out waiting for UI thread");
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
}
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
if (mText == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
"getText: Text is null for view "+mId);
return null;
}
return mText.toString();
}
private boolean mDisplayed;
public boolean isDisplayed() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mDisplayed = false;
mActivity.runOnUiThread(
new Runnable() {
public void run() {
View view = (View)mActivity.findViewById(mId);
if (view != null) {
mDisplayed = true;
}
try {
syncQueue.put(new Object());
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
}
});
try {
// Wait for the UiThread code to finish running
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"isDisplayed: time-out waiting for UI thread");
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
}
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
return mDisplayed;
}
}