1 | // Copyright (c) 2012 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.graphics.Bitmap; |
8 | import android.graphics.BitmapFactory; |
9 | import android.test.suitebuilder.annotation.SmallTest; |
10 | |
11 | import org.chromium.android_webview.AwContents; |
12 | import org.chromium.android_webview.test.util.CommonResources; |
13 | import org.chromium.content.browser.test.util.CallbackHelper; |
14 | import org.chromium.net.test.util.TestWebServer; |
15 | |
16 | import java.io.InputStream; |
17 | import java.net.URL; |
18 | import java.util.HashMap; |
19 | |
20 | /** |
21 | * Tests for the Favicon and TouchIcon related APIs. |
22 | */ |
23 | public class AwContentsClientFaviconTest extends AwTestBase { |
24 | |
25 | private static final String FAVICON1_URL = "/favicon1.png"; |
26 | private static final String FAVICON1_PAGE_URL = "/favicon1.html"; |
27 | private static final String FAVICON1_PAGE_HTML = |
28 | CommonResources.makeHtmlPageFrom( |
29 | "<link rel=\"icon\" href=\""+ FAVICON1_URL + "\" />", |
30 | "Body"); |
31 | |
32 | private static final String TOUCHICON_REL_LINK = "touch.png"; |
33 | private static final String TOUCHICON_REL_LINK_72 = "touch_72.png"; |
34 | private static final String TOUCHICON_REL_URL = "/" + TOUCHICON_REL_LINK; |
35 | private static final String TOUCHICON_REL_URL_72 = "/" + TOUCHICON_REL_LINK_72; |
36 | private static final String TOUCHICON_REL_PAGE_HTML = |
37 | CommonResources.makeHtmlPageFrom( |
38 | "<link rel=\"apple-touch-icon\" href=\""+ TOUCHICON_REL_URL + "\" />" + |
39 | "<link rel=\"apple-touch-icon\" sizes=\"72x72\" href=\""+ TOUCHICON_REL_URL_72 + "\" />", |
40 | "Body"); |
41 | |
42 | private static class FaviconHelper extends CallbackHelper { |
43 | private Bitmap mIcon; |
44 | private HashMap<String, Boolean> mTouchIcons = new HashMap<String, Boolean>(); |
45 | |
46 | public void notifyFavicon(Bitmap icon) { |
47 | mIcon = icon; |
48 | super.notifyCalled(); |
49 | } |
50 | |
51 | public void notifyTouchIcon(String url, boolean precomposed) { |
52 | mTouchIcons.put(url, precomposed); |
53 | super.notifyCalled(); |
54 | } |
55 | } |
56 | |
57 | private static class TestAwContentsClientBase |
58 | extends org.chromium.android_webview.test.TestAwContentsClient { |
59 | FaviconHelper mFaviconHelper = new FaviconHelper(); |
60 | } |
61 | |
62 | private static class TestAwContentsClientFavicon extends TestAwContentsClientBase { |
63 | @Override |
64 | public void onReceivedIcon(Bitmap bitmap) { |
65 | // We don't inform the API client about the URL of the icon. |
66 | mFaviconHelper.notifyFavicon(bitmap); |
67 | } |
68 | } |
69 | |
70 | private static class TestAwContentsClientTouchIcon extends TestAwContentsClientBase { |
71 | @Override |
72 | public void onReceivedTouchIconUrl(String url, boolean precomposed) { |
73 | mFaviconHelper.notifyTouchIcon(url, precomposed); |
74 | } |
75 | } |
76 | |
77 | private TestAwContentsClientBase mContentsClient; |
78 | private AwContents mAwContents; |
79 | private TestWebServer mWebServer; |
80 | |
81 | @Override |
82 | protected void setUp() throws Exception { |
83 | super.setUp(); |
84 | mWebServer = new TestWebServer(false); |
85 | } |
86 | |
87 | private void init(TestAwContentsClientBase contentsClient) throws Exception { |
88 | mContentsClient = contentsClient; |
89 | AwTestContainerView testContainerView = |
90 | createAwTestContainerViewOnMainSync(mContentsClient); |
91 | mAwContents = testContainerView.getAwContents(); |
92 | } |
93 | |
94 | @Override |
95 | protected void tearDown() throws Exception { |
96 | if (mWebServer != null) mWebServer.shutdown(); |
97 | super.tearDown(); |
98 | } |
99 | |
100 | @SmallTest |
101 | public void testReceiveBasicFavicon() throws Throwable { |
102 | init(new TestAwContentsClientFavicon()); |
103 | int callCount = mContentsClient.mFaviconHelper.getCallCount(); |
104 | |
105 | final String faviconUrl = mWebServer.setResponseBase64(FAVICON1_URL, |
106 | CommonResources.FAVICON_DATA_BASE64, CommonResources.getImagePngHeaders(true)); |
107 | final String pageUrl = mWebServer.setResponse(FAVICON1_PAGE_URL, FAVICON1_PAGE_HTML, |
108 | CommonResources.getTextHtmlHeaders(true)); |
109 | |
110 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); |
111 | |
112 | mContentsClient.mFaviconHelper.waitForCallback(callCount); |
113 | Object originalFaviconSource = (new URL(faviconUrl)).getContent(); |
114 | Bitmap originalFavicon = BitmapFactory.decodeStream((InputStream)originalFaviconSource); |
115 | assertNotNull(originalFavicon); |
116 | assertNotNull(mContentsClient.mFaviconHelper.mIcon); |
117 | assertTrue(mContentsClient.mFaviconHelper.mIcon.sameAs(originalFavicon)); |
118 | } |
119 | |
120 | @SmallTest |
121 | public void testReceiveBasicTouchIconLinkRel() throws Throwable { |
122 | init(new TestAwContentsClientTouchIcon()); |
123 | int callCount = mContentsClient.mFaviconHelper.getCallCount(); |
124 | |
125 | final String pageUrl = mWebServer.setResponse(TOUCHICON_REL_URL, TOUCHICON_REL_PAGE_HTML, |
126 | CommonResources.getTextHtmlHeaders(true)); |
127 | |
128 | loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), pageUrl); |
129 | |
130 | mContentsClient.mFaviconHelper.waitForCallback(callCount,2); |
131 | HashMap<String, Boolean> touchIcons = mContentsClient.mFaviconHelper.mTouchIcons; |
132 | assertEquals(2, touchIcons.size()); |
133 | assertFalse(touchIcons.get(mWebServer.getBaseUrl() + TOUCHICON_REL_LINK)); |
134 | assertFalse(touchIcons.get(mWebServer.getBaseUrl() + TOUCHICON_REL_LINK_72)); |
135 | } |
136 | } |