1 // Copyright 2020 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.weblayer.test;
6 
7 import android.graphics.Bitmap;
8 import android.net.Uri;
9 
10 import androidx.test.filters.SmallTest;
11 
12 import org.junit.Assert;
13 import org.junit.Rule;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 
17 import org.chromium.base.test.util.CallbackHelper;
18 import org.chromium.content_public.browser.test.util.TestThreadUtils;
19 import org.chromium.weblayer.FaviconCallback;
20 import org.chromium.weblayer.shell.InstrumentationActivity;
21 
22 /** Tests for FaviconFetcher. */
23 @RunWith(WebLayerJUnit4ClassRunner.class)
24 public class FaviconFetcherTest {
25     @Rule
26     public InstrumentationActivityTestRule mActivityTestRule =
27             new InstrumentationActivityTestRule();
28 
29     private InstrumentationActivity mActivity;
30 
31     @Test
32     @SmallTest
33     @MinWebLayerVersion(86)
testFaviconFetcher()34     public void testFaviconFetcher() throws Exception {
35         mActivity = mActivityTestRule.launchShellWithUrl("about:blank");
36 
37         final CallbackHelper callbackHelper = new CallbackHelper();
38         TestThreadUtils.runOnUiThreadBlocking(() -> {
39             mActivity.getTab().createFaviconFetcher(new FaviconCallback() {
40                 @Override
41                 public void onFaviconChanged(Bitmap bitmap) {
42                     if (bitmap != null) {
43                         Assert.assertTrue(bitmap.getWidth() > 0);
44                         Assert.assertTrue(bitmap.getHeight() > 0);
45                         callbackHelper.notifyCalled();
46                     }
47                 }
48             });
49         });
50         String url = mActivityTestRule.getTestDataURL("simple_page_with_favicon.html");
51         mActivityTestRule.navigateAndWait(url);
52         callbackHelper.waitForFirst();
53 
54         // Verify the favicon can get obtained from the Profile.
55         final CallbackHelper downloadCallbackHelper = new CallbackHelper();
56         TestThreadUtils.runOnUiThreadBlocking(() -> {
57             mActivity.getBrowser().getProfile().getCachedFaviconForPageUri(
58                     Uri.parse(url), (Bitmap bitmap) -> {
59                         Assert.assertTrue(bitmap != null);
60                         Assert.assertTrue(bitmap.getWidth() > 0);
61                         Assert.assertTrue(bitmap.getHeight() > 0);
62                         downloadCallbackHelper.notifyCalled();
63                     });
64         });
65         downloadCallbackHelper.waitForFirst();
66     }
67 }
68