2011-12-22 06:09:41 -08:00
|
|
|
#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/. */
|
2011-12-22 06:09:41 -08:00
|
|
|
|
|
|
|
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;
|
2012-05-26 06:33:53 -07:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2011-12-22 06:09:41 -08:00
|
|
|
|
|
|
|
public class FennecNativeElement implements Element {
|
2012-02-15 06:56:20 -08:00
|
|
|
private final Activity mActivity;
|
|
|
|
private Integer mId;
|
|
|
|
private Solo mSolo;
|
2012-05-26 06:33:53 -07:00
|
|
|
// max time to wait for thread synchronization
|
|
|
|
private static final int MAX_WAIT_MS = 60000;
|
2011-12-22 06:09:41 -08:00
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
public FennecNativeElement(Integer id, Activity activity, Solo solo) {
|
|
|
|
mId = id;
|
|
|
|
mActivity = activity;
|
|
|
|
mSolo = solo;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
public Integer getId() {
|
|
|
|
return mId;
|
|
|
|
}
|
2011-12-22 06:09:41 -08:00
|
|
|
|
2012-05-22 16:25:30 -07:00
|
|
|
private boolean mClickSuccess;
|
|
|
|
|
|
|
|
public boolean click() {
|
2012-02-15 06:56:20 -08:00
|
|
|
final SynchronousQueue syncQueue = new SynchronousQueue();
|
2012-05-22 16:25:30 -07:00
|
|
|
mClickSuccess = false;
|
2012-02-15 06:56:20 -08:00
|
|
|
mActivity.runOnUiThread(
|
|
|
|
new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
View view = (View)mActivity.findViewById(mId);
|
2012-05-22 16:25:30 -07:00
|
|
|
if (view != null) {
|
|
|
|
if (view.performClick()) {
|
|
|
|
mClickSuccess = true;
|
|
|
|
} else {
|
2012-04-10 12:20:46 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
|
2012-02-15 06:56:20 -08:00
|
|
|
"Robocop called click on an element with no listener");
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-22 16:25:30 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
|
|
|
|
"click: unable to find view "+mId);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-05-26 06:33:53 -07:00
|
|
|
try {
|
|
|
|
syncQueue.put(new Object());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
|
|
|
|
}
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
});
|
|
|
|
try {
|
2012-05-26 06:33:53 -07:00
|
|
|
// 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");
|
2012-08-13 09:03:00 -07:00
|
|
|
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
|
2012-05-26 06:33:53 -07:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InterruptedException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-05-22 16:25:30 -07:00
|
|
|
return mClickSuccess;
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
|
|
|
|
private Object mText;
|
|
|
|
|
|
|
|
public String getText() {
|
|
|
|
final SynchronousQueue syncQueue = new SynchronousQueue();
|
2012-05-22 16:25:30 -07:00
|
|
|
mText = null;
|
2012-02-15 06:56:20 -08:00
|
|
|
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) {
|
2012-05-22 16:25:30 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
|
|
|
|
"getText: unable to find view "+mId);
|
2012-02-15 06:56:20 -08:00
|
|
|
} else {
|
2012-05-22 16:25:30 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
|
|
|
|
"getText: unhandled type for view "+mId);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
2012-05-26 06:33:53 -07:00
|
|
|
try {
|
|
|
|
syncQueue.put(new Object());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
|
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
} // end of run() method definition
|
|
|
|
} // end of anonymous Runnable object instantiation
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
// Wait for the UiThread code to finish running
|
2012-05-26 06:33:53 -07:00
|
|
|
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
|
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
|
|
|
|
"getText: time-out waiting for UI thread");
|
2012-08-13 09:03:00 -07:00
|
|
|
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
|
2012-05-26 06:33:53 -07:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InterruptedException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
if (mText == null) {
|
2012-05-22 16:25:30 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
|
|
|
|
"getText: Text is null for view "+mId);
|
|
|
|
return null;
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
return mText.toString();
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
private boolean mDisplayed;
|
2012-01-05 07:20:21 -08:00
|
|
|
|
2012-02-15 06:56:20 -08:00
|
|
|
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;
|
|
|
|
}
|
2012-05-26 06:33:53 -07:00
|
|
|
try {
|
|
|
|
syncQueue.put(new Object());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
|
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
2012-05-26 06:33:53 -07:00
|
|
|
// 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");
|
2012-08-13 09:03:00 -07:00
|
|
|
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
|
2012-05-26 06:33:53 -07:00
|
|
|
}
|
2012-02-15 06:56:20 -08:00
|
|
|
} catch (InterruptedException e) {
|
2012-04-10 12:20:55 -07:00
|
|
|
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
|
2012-02-15 06:56:20 -08:00
|
|
|
}
|
|
|
|
return mDisplayed;
|
2012-01-05 07:20:21 -08:00
|
|
|
}
|
|
|
|
|
2011-12-22 06:09:41 -08:00
|
|
|
}
|