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..ecd8fb65db41dba5350cf45e8196604e115ac3f7 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,9 +4,12 @@ |
| package org.chromium.android_webview.test; |
| +import android.app.Activity; |
| import android.graphics.Bitmap; |
| import android.graphics.BitmapFactory; |
| import android.graphics.Canvas; |
| +import android.graphics.Color; |
| +import android.graphics.Point; |
| import android.os.Handler; |
| import android.os.Looper; |
| import android.os.Message; |
| @@ -16,13 +19,16 @@ import android.test.suitebuilder.annotation.SmallTest; |
| import android.util.Pair; |
| import android.view.KeyEvent; |
| import android.view.View; |
| +import android.widget.LinearLayout; |
| 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; |
| +import org.chromium.android_webview.test.util.GraphicsTestUtils; |
| import org.chromium.base.test.util.Feature; |
| import org.chromium.content.browser.test.util.CallbackHelper; |
| import org.chromium.content_public.browser.LoadUrlParams; |
| @@ -35,9 +41,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 +570,219 @@ public class AwContentsTest extends AwTestBase { |
| loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); |
| assertEquals(onSslErrorCallCount + 1, onReceivedSslErrorHelper.getCallCount()); |
| } |
| + |
|
mkosiba (inactive)
2015/01/19 11:15:58
There are enough of these tests to warrant a separ
Ignacio Solla
2015/01/19 18:26:41
Done.
|
| + @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 testLoadBlankNowDrawsBackgroundImmediately() throws Throwable { |
| + // This test loads a page with a blue background color. It then waits until this page |
| + // is drawn and then calls loadBlankNow 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<>(); |
| + 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); |
| + } |
| + }); |
| + |
| + assertTrue(bluePageDrawnSignal.await(AwTestBase.WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + Bitmap blueBitmap = GraphicsTestUtils.drawAwContents(awContents, 1, 1); |
| + 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.loadBlankNow(); |
| + Bitmap redBitmap = GraphicsTestUtils.drawAwContents(awContents, 1, 1); |
| + assertEquals(Color.RED, redBitmap.getPixel(0, 0)); |
| + } |
| + }); |
| + } |
| + |
| + @Feature({"AndroidWebView"}) |
| + @SmallTest |
| + public void testLoadBlankNowDoesNotBlockSubsequentLoads() throws Throwable { |
| + // This test briefly loads a yellow page and then immediately calls loadBlankNow |
| + // 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.loadBlankNow(); |
| + Bitmap redBitmap = GraphicsTestUtils.drawAwContents(awContents, 1, 1); |
| + assertEquals(Color.RED, redBitmap.getPixel(0, 0)); |
| + |
| + // Loading blank previously should not block this load. |
| + awContents.loadUrl(bluePageUrl); |
| + } |
| + }); |
| + |
| + assertTrue(bluePageDrawnSignal.await(AwTestBase.WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + Bitmap blueBitmap = GraphicsTestUtils.drawAwContents(awContents, 1, 1); |
| + assertEquals(Color.BLUE, blueBitmap.getPixel(0, 0)); |
| + } |
| + }); |
| + } |
| + |
| + @Feature({"AndroidWebView"}) |
| + @SmallTest |
| + public void testLoadBlankWhenPausedAndOffScreen() throws Throwable { |
| + // This tests that the WebView recovers correctly when loadBlankNow is called when the |
| + // WebView is paused and offscreen. |
| + 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() { |
| + setOffScreenAndPause(testView, awContents); |
| + awContents.setBackgroundColor(Color.RED); |
| + awContents.loadBlankNow(); |
| + awContents.loadUrl(bluePageUrl); |
| + |
| + setOnScreenAndResume(testView, awContents); |
| + Bitmap redBitmap = GraphicsTestUtils.drawAwContents(awContents, 1, 1); |
| + assertEquals(Color.RED, redBitmap.getPixel(0, 0)); |
| + } |
| + }); |
| + |
| + assertTrue(bluePageDrawnSignal.await(AwTestBase.WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + Bitmap blueBitmap = GraphicsTestUtils.drawAwContents(awContents, 1, 1); |
| + assertEquals(Color.BLUE, blueBitmap.getPixel(0, 0)); |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * Pauses the {@link AwContents} and moves the container view off screen. |
| + */ |
| + private void setOffScreenAndPause(View containerView, AwContents awContents) { |
| + Point size = new Point(); |
| + ((Activity) awContents.getContext()).getWindowManager().getDefaultDisplay().getSize(size); |
|
mkosiba (inactive)
2015/01/19 11:15:58
you can use getActivity() in the test case instead
Ignacio Solla
2015/01/19 18:26:41
Done.
|
| + int screenWidth = size.x; |
| + LinearLayout.LayoutParams params = |
| + (LinearLayout.LayoutParams) containerView.getLayoutParams(); |
| + params.setMargins(-screenWidth, 0, screenWidth, 0); |
| + containerView.setLayoutParams(params); |
| + awContents.onPause(); |
| + } |
| + |
| + /** |
| + * Resumes the {@link AwContents} and moves the container view on screen. |
| + */ |
| + private void setOnScreenAndResume(View containerView, AwContents awContents) { |
| + LinearLayout.LayoutParams params = |
| + (LinearLayout.LayoutParams) containerView.getLayoutParams(); |
| + params.setMargins(0, 0, 0, 0); |
| + containerView.setLayoutParams(params); |
| + awContents.onResume(); |
| + } |
| + |
| + 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); |
| + } |
| } |