| 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.content.BroadcastReceiver; |
| 8 | import android.content.Context; |
| 9 | import android.content.Intent; |
| 10 | import android.content.IntentFilter; |
| 11 | import android.graphics.Bitmap; |
| 12 | import android.graphics.Color; |
| 13 | import android.net.Proxy; |
| 14 | import android.test.FlakyTest; |
| 15 | import android.test.mock.MockContext; |
| 16 | import android.test.suitebuilder.annotation.SmallTest; |
| 17 | |
| 18 | import org.chromium.android_webview.AwContents; |
| 19 | import org.chromium.base.test.util.DisabledTest; |
| 20 | import org.chromium.base.test.util.Feature; |
| 21 | import org.chromium.base.test.util.UrlUtils; |
| 22 | import org.chromium.base.ThreadUtils; |
| 23 | import org.chromium.content.browser.ContentViewCore; |
| 24 | import org.chromium.content.browser.ContentViewStatics; |
| 25 | import org.chromium.content.browser.test.util.Criteria; |
| 26 | import org.chromium.content.browser.test.util.CriteriaHelper; |
| 27 | import org.chromium.net.ProxyChangeListener; |
| 28 | |
| 29 | import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | import java.util.concurrent.atomic.AtomicReference; |
| 31 | import java.util.concurrent.Callable; |
| 32 | |
| 33 | /** |
| 34 | * AwContents rendering / pixel tests. |
| 35 | */ |
| 36 | public class AwContentsRenderTest extends AwTestBase { |
| 37 | |
| 38 | private TestAwContentsClient mContentsClient; |
| 39 | private AwContents mAwContents; |
| 40 | |
| 41 | @Override |
| 42 | public void setUp() throws Exception { |
| 43 | super.setUp(); |
| 44 | mContentsClient = new TestAwContentsClient(); |
| 45 | final AwTestContainerView testContainerView = |
| 46 | createAwTestContainerViewOnMainSync(mContentsClient); |
| 47 | mAwContents = testContainerView.getAwContents(); |
| 48 | } |
| 49 | |
| 50 | void setBackgroundColorOnUiThread(final int c) { |
| 51 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 52 | @Override |
| 53 | public void run() { |
| 54 | mAwContents.setBackgroundColor(c); |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | Bitmap grabViewToBitmap() { |
| 60 | final Bitmap result = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888); |
| 61 | mAwContents.onDraw(new android.graphics.Canvas(result)); |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | int sampleBackgroundColorOnUiThread() throws Throwable { |
| 66 | return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() { |
| 67 | @Override |
| 68 | public Integer call() throws Exception { |
| 69 | return grabViewToBitmap().getPixel(0, 0); |
| 70 | } |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | boolean waitForBackgroundColor(final int c) throws Throwable { |
| 75 | return CriteriaHelper.pollForCriteria(new Criteria() { |
| 76 | @Override |
| 77 | public boolean isSatisfied() { |
| 78 | try { |
| 79 | return sampleBackgroundColorOnUiThread() == c; |
| 80 | } catch (Throwable e) { |
| 81 | throw new RuntimeException(e); |
| 82 | } |
| 83 | } |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | @SmallTest |
| 88 | @Feature({"AndroidWebView"}) |
| 89 | public void testSetGetBackgroundColor() throws Throwable { |
| 90 | setBackgroundColorOnUiThread(Color.MAGENTA); |
| 91 | assertTrue(waitForBackgroundColor(Color.MAGENTA)); |
| 92 | |
| 93 | setBackgroundColorOnUiThread(Color.CYAN); |
| 94 | assertTrue(waitForBackgroundColor(Color.CYAN)); |
| 95 | |
| 96 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), "about:blank"); |
| 97 | assertEquals(Color.CYAN, sampleBackgroundColorOnUiThread()); |
| 98 | |
| 99 | setBackgroundColorOnUiThread(Color.YELLOW); |
| 100 | assertTrue(waitForBackgroundColor(Color.YELLOW)); |
| 101 | |
| 102 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
| 103 | "data:text/html,<html><head><style>body {background-color:#227788}</style></head>" + |
| 104 | "<body><br>HelloWorld</body></html>"); |
| 105 | assertTrue(waitForBackgroundColor(Color.rgb(0x22, 0x77, 0x88))); |
| 106 | |
| 107 | // Changing the base background should not override CSS background. |
| 108 | setBackgroundColorOnUiThread(Color.MAGENTA); |
| 109 | assertEquals(Color.rgb(0x22, 0x77, 0x88), sampleBackgroundColorOnUiThread()); |
| 110 | // ...setting the background is asynchronous, so pause a bit and retest just to be sure. |
| 111 | Thread.sleep(500); |
| 112 | assertEquals(Color.rgb(0x22, 0x77, 0x88), sampleBackgroundColorOnUiThread()); |
| 113 | } |
| 114 | |
| 115 | @SmallTest |
| 116 | @Feature({"AndroidWebView"}) |
| 117 | public void testPictureListener() throws Throwable { |
| 118 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 119 | @Override |
| 120 | public void run() { |
| 121 | mAwContents.enableOnNewPicture(true, true); |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | int pictureCount = mContentsClient.getPictureListenerHelper().getCallCount(); |
| 126 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), "about:blank"); |
| 127 | mContentsClient.getPictureListenerHelper().waitForCallback(pictureCount, 1); |
| 128 | // Invalidation only, so picture should be null. |
| 129 | assertNull(mContentsClient.getPictureListenerHelper().getPicture()); |
| 130 | } |
| 131 | } |