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
5package org.chromium.android_webview.test;
6
7import android.graphics.Bitmap;
8import android.graphics.Color;
9import android.test.suitebuilder.annotation.SmallTest;
10
11import org.chromium.android_webview.AwContents;
12import org.chromium.base.ThreadUtils;
13import org.chromium.base.test.util.Feature;
14
15import java.util.concurrent.Callable;
16
17/**
18 * AwContents rendering / pixel tests.
19 */
20public class AwContentsRenderTest extends AwTestBase {
21
22    private TestAwContentsClient mContentsClient;
23    private AwContents mAwContents;
24
25    @Override
26    public void setUp() throws Exception {
27        super.setUp();
28        mContentsClient = new TestAwContentsClient();
29        final AwTestContainerView testContainerView =
30                createAwTestContainerViewOnMainSync(mContentsClient);
31        mAwContents = testContainerView.getAwContents();
32    }
33
34    void setBackgroundColorOnUiThread(final int c) {
35        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
36            @Override
37            public void run() {
38                mAwContents.setBackgroundColor(c);
39            }
40        });
41    }
42
43    Bitmap grabViewToBitmap() {
44        final Bitmap result = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
45        mAwContents.onDraw(new android.graphics.Canvas(result));
46        return result;
47    }
48
49    int sampleBackgroundColorOnUiThread() throws Exception {
50        return ThreadUtils.runOnUiThreadBlocking(new Callable<Integer>() {
51            @Override
52            public Integer call() throws Exception {
53                return grabViewToBitmap().getPixel(0, 0);
54            }
55        });
56    }
57
58    void pollForBackgroundColor(final int c) throws Throwable {
59        poll(new Callable<Boolean>() {
60            @Override
61            public Boolean call() throws Exception {
62                return sampleBackgroundColorOnUiThread() == c;
63            }
64        });
65    }
66
67    /*
68    @SmallTest
69    @Feature({"AndroidWebView"})
70    */
71    public void testSetGetBackgroundColor() throws Throwable {
72        setBackgroundColorOnUiThread(Color.MAGENTA);
73        pollForBackgroundColor(Color.MAGENTA);
74
75        setBackgroundColorOnUiThread(Color.CYAN);
76        pollForBackgroundColor(Color.CYAN);
77
78        loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), "about:blank");
79        assertEquals(Color.CYAN, sampleBackgroundColorOnUiThread());
80
81        setBackgroundColorOnUiThread(Color.YELLOW);
82        pollForBackgroundColor(Color.YELLOW);
83
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));
88
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.
93        Thread.sleep(500);
94        assertEquals(Color.rgb(0x22, 0x77, 0x88), sampleBackgroundColorOnUiThread());
95    }
96
97    @SmallTest
98    @Feature({"AndroidWebView"})
99    public void testPictureListener() throws Throwable {
100        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
101            @Override
102            public void run() {
103                mAwContents.enableOnNewPicture(true, true);
104            }
105        });
106
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());
112    }
113}
114