| Index: android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java
|
| diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java
|
| index f052070ce0553963b3078d189695929630df26c7..ab907cc7718a956702fc450f802e6c758dba5571 100644
|
| --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java
|
| +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsTest.java
|
| @@ -4,62 +4,78 @@
|
|
|
| package org.chromium.android_webview.test;
|
|
|
| -import android.content.Context;
|
| import android.os.Handler;
|
| import android.os.Looper;
|
| +import android.os.Message;
|
| +import android.test.UiThreadTest;
|
| import android.test.suitebuilder.annotation.SmallTest;
|
| -import android.widget.GridLayout;
|
|
|
| import org.chromium.android_webview.AwContents;
|
| -import org.chromium.android_webview.AwContentsClient;
|
| import org.chromium.base.test.Feature;
|
| -import org.chromium.content.browser.ContentViewCore;
|
| +import org.chromium.content.browser.test.CallbackHelper;
|
|
|
| import java.util.concurrent.Semaphore;
|
| import java.util.concurrent.TimeUnit;
|
| +import java.util.concurrent.atomic.AtomicInteger;
|
|
|
| /**
|
| * AwContents tests.
|
| */
|
| public class AwContentsTest extends AndroidWebViewTestBase {
|
| - private AwContentsClient mContentsClient = new NullContentsClient();
|
| -
|
| - private AwContents createContents(Context context) {
|
| - GridLayout viewGroup = new GridLayout(context);
|
| -
|
| - // TODO: Required ContentViewCore changes are not upstreamed yet.
|
| - // ContentViewCore contentViewCore = new ContentViewCore(
|
| - // context, ContentViewCore.PERSONALITY_VIEW);
|
| - ContentViewCore contentViewCore = new ContentViewCore(
|
| - context, viewGroup, null, 0, ContentViewCore.PERSONALITY_VIEW);
|
| - AwContents awContents = new AwContents(viewGroup, null, contentViewCore,
|
| - mContentsClient, false, false);
|
| - return awContents;
|
| - }
|
| + private TestAwContentsClient mContentsClient = new TestAwContentsClient();
|
|
|
| @SmallTest
|
| @Feature({"Android-WebView"})
|
| + @UiThreadTest
|
| public void testCreateDestroy() throws Throwable {
|
| - final Throwable[] error = new Throwable[1];
|
| - final Semaphore s = new Semaphore(0);
|
| - final Context context = getActivity();
|
| + // NOTE this test runs on UI thread, so we cannot call any async methods.
|
| + createAwTestContainerView(false, mContentsClient).getAwContents().destroy();
|
| + }
|
|
|
| - Runnable r = new Runnable() {
|
| + private int callDocumentHasImagesSync(final AwContents awContents)
|
| + throws Throwable, InterruptedException {
|
| + // Set up a container to hold the result object and a semaphore to
|
| + // make the test wait for the result.
|
| + final AtomicInteger val = new AtomicInteger();
|
| + final Semaphore s = new Semaphore(0);
|
| + final Message msg = Message.obtain(new Handler(Looper.getMainLooper()) {
|
| + @Override
|
| + public void handleMessage(Message msg) {
|
| + val.set(msg.arg1);
|
| + s.release();
|
| + }
|
| + });
|
| + runTestOnUiThread(new Runnable() {
|
| @Override
|
| public void run() {
|
| - try {
|
| - createContents(context).destroy();
|
| - } catch (Throwable t) {
|
| - error[0] = t;
|
| - } finally {
|
| - s.release();
|
| - }
|
| + awContents.documentHasImages(msg);
|
| }
|
| - };
|
| - new Handler(Looper.getMainLooper()).post(r);
|
| - assertTrue(s.tryAcquire(10000, TimeUnit.MILLISECONDS));
|
| - if (error[0] != null) {
|
| - throw error[0];
|
| - }
|
| + });
|
| + assertTrue(s.tryAcquire(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
|
| + int result = val.get();
|
| + return result;
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Android-WebView"})
|
| + public void testDocumentHasImages() throws Throwable {
|
| + AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
|
| + AwContents awContents = testView.getAwContents();
|
| +
|
| + final CallbackHelper loadHelper = mContentsClient.getOnPageFinishedHelper();
|
| +
|
| + final String mime = "text/html";
|
| + final String emptyDoc = "<head/><body/>";
|
| + final String imageDoc = "<head/><body><img/><img/></body>";
|
| +
|
| + // Make sure a document that does not have images returns 0
|
| + loadDataSync(awContents.getContentViewCore(), loadHelper, emptyDoc, mime, false);
|
| + int result = callDocumentHasImagesSync(awContents);
|
| + assertEquals(0, result);
|
| +
|
| + // Make sure a document that does have images returns 1
|
| + loadDataSync(awContents.getContentViewCore(), loadHelper, imageDoc, mime, false);
|
| + result = callDocumentHasImagesSync(awContents);
|
| + assertEquals(1, result);
|
| }
|
| -}
|
| +}
|
|
|