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.
5 package org.chromium.android_webview.test;
7 import android.graphics.Bitmap;
8 import android.graphics.Color;
9 import android.test.suitebuilder.annotation.SmallTest;
11 import org.chromium.android_webview.AwContents;
12 import org.chromium.base.ThreadUtils;
13 import org.chromium.base.test.util.Feature;
15 import java.util.concurrent.Callable;
18 * AwContents rendering / pixel tests.
20 public class AwContentsRenderTest extends AwTestBase {
22 private TestAwContentsClient mContentsClient;
23 private AwContents mAwContents;
26 public void setUp() throws Exception {
28 mContentsClient = new TestAwContentsClient();
29 final AwTestContainerView testContainerView =
30 createAwTestContainerViewOnMainSync(mContentsClient);
31 mAwContents = testContainerView.getAwContents();
34 void setBackgroundColorOnUiThread(final int c) {
35 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
38 mAwContents.setBackgroundColor(c);
43 Bitmap grabViewToBitmap() {
44 final Bitmap result = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
45 mAwContents.onDraw(new android.graphics.Canvas(result));
49 int sampleBackgroundColorOnUiThread() throws Exception {
50 return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() {
52 public Integer call() throws Exception {
53 return grabViewToBitmap().getPixel(0, 0);
58 void pollForBackgroundColor(final int c) throws Throwable {
59 poll(new Callable<Boolean>() {
61 public Boolean call() throws Exception {
62 return sampleBackgroundColorOnUiThread() == c;
69 @Feature({"AndroidWebView"})
71 public void testSetGetBackgroundColor() throws Throwable {
72 setBackgroundColorOnUiThread(Color.MAGENTA);
73 pollForBackgroundColor(Color.MAGENTA);
75 setBackgroundColorOnUiThread(Color.CYAN);
76 pollForBackgroundColor(Color.CYAN);
78 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), "about:blank");
79 assertEquals(Color.CYAN, sampleBackgroundColorOnUiThread());
81 setBackgroundColorOnUiThread(Color.YELLOW);
82 pollForBackgroundColor(Color.YELLOW);
84 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
85 "data:text/html,<html><head><style>body {background-color:#227788}</style></head>" +
86 "<body></body></html>");
87 pollForBackgroundColor(Color.rgb(0x22, 0x77, 0x88));
89 // Changing the base background should not override CSS background.
90 setBackgroundColorOnUiThread(Color.MAGENTA);
91 assertEquals(Color.rgb(0x22, 0x77, 0x88), sampleBackgroundColorOnUiThread());
92 // ...setting the background is asynchronous, so pause a bit and retest just to be sure.
94 assertEquals(Color.rgb(0x22, 0x77, 0x88), sampleBackgroundColorOnUiThread());
98 @Feature({"AndroidWebView"})
99 public void testPictureListener() throws Throwable {
100 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
103 mAwContents.enableOnNewPicture(true, true);
107 int pictureCount = mContentsClient.getPictureListenerHelper().getCallCount();
108 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), "about:blank");
109 mContentsClient.getPictureListenerHelper().waitForCallback(pictureCount, 1);
110 // Invalidation only, so picture should be null.
111 assertNull(mContentsClient.getPictureListenerHelper().getPicture());