This source file includes following definitions.
- setUp
- contents
- onFindResultReceived
- loadContentsFromStringSync
- findAllAsyncOnUiThread
- findNextOnUiThread
- clearMatchesOnUiThread
- run
- get
- set
package org.chromium.android_webview.test;
import org.chromium.android_webview.AwContents;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class WebViewFindApisTestBase extends AwTestBase {
private static final String WOODCHUCK =
"How much WOOD would a woodchuck chuck if a woodchuck could chuck wOoD?";
private FindResultListener mFindResultListener;
private AwContents mContents;
@Override
protected void setUp() throws Exception {
super.setUp();
try {
mContents = loadContentsFromStringSync(WOODCHUCK);
} catch (Throwable t) {
throw new Exception(t);
}
}
protected AwContents contents() {
return mContents;
}
private interface FindResultListener {
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
boolean isDoneCounting);
};
private AwContents loadContentsFromStringSync(final String html) throws Throwable {
final TestAwContentsClient contentsClient = new TestAwContentsClient() {
@Override
public void onFindResultReceived(int activeMatchOrdinal,
int numberOfMatches, boolean isDoneCounting) {
if (mFindResultListener == null) return;
mFindResultListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches,
isDoneCounting);
}
};
final AwContents contents =
createAwTestContainerViewOnMainSync(contentsClient).getAwContents();
final String data = "<html><head></head><body>" + html + "</body></html>";
loadDataSync(contents, contentsClient.getOnPageFinishedHelper(),
data, "text/html", false);
return contents;
}
protected int findAllAsyncOnUiThread(final String searchString)
throws Throwable {
final IntegerFuture future = new IntegerFuture() {
@Override
public void run() {
mFindResultListener = new FindResultListener() {
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
boolean isDoneCounting) {
if (isDoneCounting) set(numberOfMatches);
}
};
mContents.findAllAsync(searchString);
}
};
runTestOnUiThread(future);
return future.get(10, TimeUnit.SECONDS);
}
protected int findNextOnUiThread(final boolean forwards)
throws Throwable {
final IntegerFuture future = new IntegerFuture() {
@Override
public void run() {
mFindResultListener = new FindResultListener() {
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
boolean isDoneCounting) {
if (isDoneCounting) set(activeMatchOrdinal);
}
};
mContents.findNext(forwards);
}
};
runTestOnUiThread(future);
return future.get(10, TimeUnit.SECONDS);
}
protected void clearMatchesOnUiThread() throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
mContents.clearMatches();
}
});
}
private abstract static class IntegerFuture implements Runnable {
private CountDownLatch mLatch = new CountDownLatch(1);
private int mValue;
@Override
public abstract void run();
public int get(long timeout, TimeUnit unit) throws Throwable {
if (!mLatch.await(timeout, unit)) {
throw new TimeoutException();
}
return mValue;
}
protected void set(int value) {
mValue = value;
mLatch.countDown();
}
}
}