Chromium Code Reviews| 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 fbba821a7ced05b9885da13e305a8441deac84e9..fac192a7c3b75046e314890868d5ed5ee3b8f60c 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 |
| @@ -7,6 +7,7 @@ package org.chromium.android_webview.test; |
| import android.graphics.Bitmap; |
| import android.graphics.BitmapFactory; |
| import android.graphics.Canvas; |
| +import android.graphics.Color; |
| import android.os.Handler; |
| import android.os.Looper; |
| import android.os.Message; |
| @@ -20,6 +21,7 @@ import android.view.View; |
| import org.apache.http.Header; |
| import org.apache.http.HttpRequest; |
| import org.chromium.android_webview.AwContents; |
| +import org.chromium.android_webview.AwContents.VisualStateFlushCallback; |
| import org.chromium.android_webview.AwSettings; |
| import org.chromium.android_webview.test.TestAwContentsClient.OnDownloadStartHelper; |
| import org.chromium.android_webview.test.util.CommonResources; |
| @@ -35,9 +37,11 @@ import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.concurrent.Callable; |
| +import java.util.concurrent.CountDownLatch; |
| import java.util.concurrent.Semaphore; |
| import java.util.concurrent.TimeUnit; |
| import java.util.concurrent.atomic.AtomicInteger; |
| +import java.util.concurrent.atomic.AtomicReference; |
| /** |
| * AwContents tests. |
| @@ -562,4 +566,156 @@ public class AwContentsTest extends AwTestBase { |
| loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); |
| assertEquals(onSslErrorCallCount + 1, onReceivedSslErrorHelper.getCallCount()); |
| } |
| + |
| + @Feature({"AndroidWebView"}) |
| + @SmallTest |
| + public void testFlushVisualState() throws Throwable { |
| + AwTestContainerView testContainer = |
| + createAwTestContainerViewOnMainSync(mContentsClient); |
| + final AwContents awContents = testContainer.getAwContents(); |
| + loadUrlSync(awContents, |
| + mContentsClient.getOnPageFinishedHelper(), CommonResources.ABOUT_HTML); |
| + final CallbackHelper ch = new CallbackHelper(); |
| + final int chCount = ch.getCallCount(); |
| + runTestOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + awContents.flushVisualState(new AwContents.VisualStateFlushCallback() { |
| + @Override |
| + public void onComplete() { |
| + ch.notifyCalled(); |
| + } |
| + |
| + @Override |
| + public void onFailure(int reason) {} |
| + }); |
| + } |
| + }); |
| + ch.waitForCallback(chCount); |
| + } |
| + |
| + @Feature({"AndroidWebView"}) |
| + @SmallTest |
| + public void testLoadBlankImmediatelyDrawsBackgroundImmediately() throws Throwable { |
| + // This test loads a page with a blue background color. It then waits until this page |
| + // is drawn and then calls loadBlankImmediatel and checks that the background color of the |
| + // view (in this case red) is drawn immediately instead of the contents of the blue page. |
| + final CountDownLatch bluePageDrawnSignal = new CountDownLatch(1); |
| + final LoadUrlParams bluePageUrl = createTestPageUrl("blue"); |
| + |
| + final AtomicReference<AwContents> awContentsRef = new AtomicReference<>(); |
|
mkosiba (inactive)
2015/01/14 18:17:12
why do you need this? Why can't you just declare a
Ignacio Solla
2015/01/16 16:02:07
I can only initialize awContents after createAwTes
mkosiba (inactive)
2015/01/19 11:15:57
oh, I didn't notice the "circular" ref. This is fi
Ignacio Solla
2015/01/19 18:26:40
Acknowledged.
|
| + final AwTestContainerView testView = createAwTestContainerViewOnMainSync( |
| + new TestAwContentsClient() { |
| + @Override |
| + public void onPageFinished(String url) { |
| + if (bluePageUrl.getUrl().equals(url)) { |
| + // We flush from onPageFinished to ensure that the blue page contents |
| + // have been drawn when the flush callback is received. |
| + awContentsRef.get().flushVisualState( |
| + createFlushCallback(bluePageDrawnSignal)); |
| + } |
| + } |
| + }); |
| + final AwContents awContents = testView.getAwContents(); |
| + awContentsRef.set(awContents); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + awContents.setBackgroundColor(Color.RED); |
| + awContents.loadUrl(bluePageUrl); |
| + } |
| + }); |
| + |
| + bluePageDrawnSignal.await(); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + Bitmap blueBitmap = drawIntoBitmap(awContents); |
| + assertEquals(Color.BLUE, blueBitmap.getPixel(0, 0)); |
| + |
| + // Check that the background color is drawn immediately after loading blank. |
| + // Note that we don't need to wait for any events such as onPageFinished. |
| + awContents.loadBlankImmediately(); |
| + Bitmap redBitmap = drawIntoBitmap(awContents); |
| + assertEquals(Color.RED, redBitmap.getPixel(0, 0)); |
| + } |
| + }); |
| + } |
| + |
| + @Feature({"AndroidWebView"}) |
| + @SmallTest |
| + public void testLoadBlankImmediatelyDoesNotBlockSubsequentLoads() throws Throwable { |
| + // This test briefly loads a yellow page and then immediately calls loadBlankImmediatel |
| + // which should draw the background color of the view immediately (in this case red). It |
| + // then tries to load a page with blue background and waits until this page is drawn. |
| + final CountDownLatch bluePageDrawnSignal = new CountDownLatch(1); |
| + final LoadUrlParams bluePageUrl = createTestPageUrl("blue"); |
| + |
| + final AtomicReference<AwContents> awContentsRef = new AtomicReference<>(); |
| + final AwTestContainerView testView = createAwTestContainerViewOnMainSync( |
| + new TestAwContentsClient() { |
| + @Override |
| + public void onPageFinished(String url) { |
| + if (bluePageUrl.getUrl().equals(url)) { |
| + awContentsRef.get().flushVisualState( |
| + createFlushCallback(bluePageDrawnSignal)); |
| + } |
| + } |
| + }); |
| + final AwContents awContents = testView.getAwContents(); |
| + awContentsRef.set(awContents); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + awContents.loadUrl(createTestPageUrl("yellow")); |
| + awContents.setBackgroundColor(Color.RED); |
| + awContents.loadBlankImmediately(); |
| + Bitmap redBitmap = drawIntoBitmap(awContents); |
| + assertEquals(Color.RED, redBitmap.getPixel(0, 0)); |
| + |
| + // Loading blank previously should not block this load. |
| + awContents.loadUrl(bluePageUrl); |
| + } |
| + }); |
| + |
| + bluePageDrawnSignal.await(); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + Bitmap blueBitmap = drawIntoBitmap(awContents); |
| + assertEquals(Color.BLUE, blueBitmap.getPixel(0, 0)); |
| + } |
| + }); |
| + } |
| + |
| + private static VisualStateFlushCallback createFlushCallback(final CountDownLatch latch) { |
| + return new VisualStateFlushCallback() { |
| + @Override |
| + public void onFailure(int reason) { |
| + fail("onFailure received"); |
| + } |
| + |
| + @Override |
| + public void onComplete() { |
| + latch.countDown(); |
| + } |
| + }; |
| + } |
| + |
| + private static final LoadUrlParams createTestPageUrl(String backgroundColor) { |
| + return LoadUrlParams.createLoadDataParams( |
| + "<html><body bgcolor=" + backgroundColor + "></body></html>", |
| + "text/html", false); |
| + } |
| + |
| + private Bitmap drawIntoBitmap(AwContents awContents) { |
|
mkosiba (inactive)
2015/01/14 18:17:12
we already have like 2 places in tests where we do
Ignacio Solla
2015/01/16 16:02:07
Done.
mkosiba (inactive)
2015/01/19 11:15:57
Thanks!
Ignacio Solla
2015/01/19 18:26:40
Acknowledged.
|
| + Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| + Canvas canvas = new Canvas(bitmap); |
| + awContents.onDraw(canvas); |
| + return bitmap; |
| + } |
| } |