| Index: chrome/android/junit/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetectorTest.java
|
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetectorTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetectorTest.java
|
| index 4bb7346ae9ede2d7278f30e5dd9fb13193c26c48..27af6cbd45bcb407dbff5e1c8f0dbf62bf6c64af 100644
|
| --- a/chrome/android/junit/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetectorTest.java
|
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/webapps/ManifestUpgradeDetectorTest.java
|
| @@ -7,10 +7,13 @@ package org.chromium.chrome.browser.webapps;
|
| import android.content.Context;
|
| import android.content.pm.ApplicationInfo;
|
| import android.content.pm.PackageInfo;
|
| +import android.graphics.Bitmap;
|
| +import android.graphics.Color;
|
| import android.os.Bundle;
|
|
|
| import org.chromium.base.ContextUtils;
|
| import org.chromium.blink_public.platform.WebDisplayMode;
|
| +import org.chromium.chrome.browser.ShortcutHelper;
|
| import org.chromium.chrome.browser.tab.Tab;
|
| import org.chromium.content_public.common.ScreenOrientationValues;
|
| import org.chromium.testing.local.LocalRobolectricTestRunner;
|
| @@ -25,6 +28,7 @@ import org.mockito.stubbing.Answer;
|
| import org.robolectric.Robolectric;
|
| import org.robolectric.annotation.Config;
|
| import org.robolectric.res.builder.RobolectricPackageManager;
|
| +import org.robolectric.shadows.ShadowBitmap;
|
|
|
| /**
|
| * Tests the ManifestUpgradeDetector.
|
| @@ -37,6 +41,8 @@ public class ManifestUpgradeDetectorTest {
|
| private static final String WEBAPK_SCOPE_URL = "/";
|
| private static final String WEBAPK_NAME = "Long Name";
|
| private static final String WEBAPK_SHORT_NAME = "Short Name";
|
| + private static final String WEBAPK_ICON_URL = "/icon.png";
|
| + private static final long WEBAPK_ICON_MURMUR2_HASH = 3L;
|
| private static final int WEBAPK_DISPLAY_MODE = WebDisplayMode.Standalone;
|
| private static final int WEBAPK_ORIENTATION = ScreenOrientationValues.LANDSCAPE;
|
| private static final long WEBAPK_THEME_COLOR = 1L;
|
| @@ -47,13 +53,19 @@ public class ManifestUpgradeDetectorTest {
|
| private RobolectricPackageManager mPackageManager;
|
|
|
| /**
|
| - * Data from downloaded Web Manifest. Same as old Web Manifest data by default.
|
| + * Used to represent either:
|
| + * - Data that the WebAPK was created with.
|
| + * OR
|
| + * - Data fetched by ManifestUpgradeDetector.
|
| */
|
| - private static class FetchedData {
|
| + private static class Data {
|
| public String startUrl = WEBAPK_START_URL;
|
| public String scopeUrl = WEBAPK_SCOPE_URL;
|
| public String name = WEBAPK_NAME;
|
| public String shortName = WEBAPK_SHORT_NAME;
|
| + public String iconUrl = WEBAPK_ICON_URL;
|
| + public long iconMurmur2Hash = WEBAPK_ICON_MURMUR2_HASH;
|
| + public Bitmap icon = createBitmap(Color.GREEN);
|
| public int displayMode = WEBAPK_DISPLAY_MODE;
|
| public int orientation = WEBAPK_ORIENTATION;
|
| public long themeColor = WEBAPK_THEME_COLOR;
|
| @@ -68,11 +80,12 @@ public class ManifestUpgradeDetectorTest {
|
| * - Tracks whether "upgrade needed checking logic" has terminated.
|
| */
|
| private static class TestManifestUpgradeDetector extends ManifestUpgradeDetector {
|
| + public String mRequestedIconUrl;
|
| public boolean mIsUpgraded;
|
| public boolean mCompleted;
|
| - private FetchedData mFetchedData;
|
| + private Data mFetchedData;
|
|
|
| - public TestManifestUpgradeDetector(Tab tab, WebappInfo info, FetchedData fetchedData) {
|
| + public TestManifestUpgradeDetector(Tab tab, WebappInfo info, Data fetchedData) {
|
| super(tab, info);
|
| mFetchedData = fetchedData;
|
| }
|
| @@ -87,9 +100,9 @@ public class ManifestUpgradeDetectorTest {
|
| ManifestUpgradeDetectorFetcher.Callback callback =
|
| (ManifestUpgradeDetectorFetcher.Callback) invocation.getArguments()[0];
|
| callback.onGotManifestData(mFetchedData.startUrl, mFetchedData.scopeUrl,
|
| - mFetchedData.name, mFetchedData.shortName, mFetchedData.displayMode,
|
| - mFetchedData.orientation, mFetchedData.themeColor,
|
| - mFetchedData.backgroundColor);
|
| + mFetchedData.name, mFetchedData.shortName, mFetchedData.iconUrl,
|
| + mFetchedData.displayMode, mFetchedData.orientation,
|
| + mFetchedData.themeColor, mFetchedData.backgroundColor);
|
| return null;
|
| }
|
| };
|
| @@ -99,6 +112,13 @@ public class ManifestUpgradeDetectorTest {
|
| }
|
|
|
| @Override
|
| + protected void fetchHomescreenImage(
|
| + String imageUrl, ShortcutHelper.FetchHomescreenImageCallback callback) {
|
| + mRequestedIconUrl = imageUrl;
|
| + callback.onFetchedHomescreenImage(mFetchedData.icon, mFetchedData.iconMurmur2Hash);
|
| + }
|
| +
|
| + @Override
|
| protected void upgrade() {
|
| mIsUpgraded = true;
|
| }
|
| @@ -109,26 +129,47 @@ public class ManifestUpgradeDetectorTest {
|
| }
|
| }
|
|
|
| + /**
|
| + * Creates 1x1 bitmap.
|
| + * @param color The bitmap color.
|
| + */
|
| + private static Bitmap createBitmap(int color) {
|
| + int colors[] = { color };
|
| + return ShadowBitmap.createBitmap(colors, 1, 1, Bitmap.Config.ALPHA_8);
|
| + }
|
| +
|
| @Before
|
| public void setUp() {
|
| Context context = Robolectric.application;
|
| ContextUtils.initApplicationContextForTests(context);
|
| mPackageManager = (RobolectricPackageManager) context.getPackageManager();
|
| + }
|
|
|
| - setMetaData(WEBAPK_START_URL);
|
| + private TestManifestUpgradeDetector createDetectorWithFetchedData(Data fetchedData) {
|
| + return createDetector(new Data(), fetchedData);
|
| }
|
|
|
| - private TestManifestUpgradeDetector createDetector(FetchedData fetchedData) {
|
| - WebappInfo webappInfo = WebappInfo.create("", WEBAPK_START_URL, WEBAPK_SCOPE_URL, null,
|
| - WEBAPK_NAME, WEBAPK_SHORT_NAME, WEBAPK_DISPLAY_MODE, WEBAPK_ORIENTATION, 0,
|
| - WEBAPK_THEME_COLOR, WEBAPK_BACKGROUND_COLOR, false, WEBAPK_PACKAGE_NAME,
|
| + /**
|
| + * Creates ManifestUpgradeDetector.
|
| + * @param oldData Data used to create WebAPK. Potentially different from Web Manifest data at
|
| + * time that the WebAPK was generated.
|
| + * @param fetchedData Data fetched by ManifestUpgradeDetector.
|
| + */
|
| + private TestManifestUpgradeDetector createDetector(Data oldData, Data fetchedData) {
|
| + setMetaData(oldData.startUrl, oldData.iconUrl, oldData.iconMurmur2Hash);
|
| + WebappInfo webappInfo = WebappInfo.create("", oldData.startUrl, oldData.scopeUrl, null,
|
| + oldData.name, oldData.shortName, oldData.displayMode, oldData.orientation, 0,
|
| + oldData.themeColor, oldData.backgroundColor, false, WEBAPK_PACKAGE_NAME,
|
| WEBAPK_MANIFEST_URL);
|
| return new TestManifestUpgradeDetector(null, webappInfo, fetchedData);
|
| }
|
|
|
| - private void setMetaData(String startUrl) {
|
| + private void setMetaData(String startUrl, String iconUrl, long iconMurmur2Hash) {
|
| Bundle bundle = new Bundle();
|
| bundle.putString(ManifestUpgradeDetector.META_DATA_START_URL, startUrl);
|
| + bundle.putString(ManifestUpgradeDetector.META_DATA_ICON_URL, iconUrl);
|
| + bundle.putString(
|
| + ManifestUpgradeDetector.META_DATA_ICON_MURMUR2_HASH, iconMurmur2Hash + "L");
|
|
|
| ApplicationInfo appInfo = new ApplicationInfo();
|
| appInfo.metaData = bundle;
|
| @@ -141,7 +182,7 @@ public class ManifestUpgradeDetectorTest {
|
|
|
| @Test
|
| public void testManifestDoesNotUpgrade() {
|
| - TestManifestUpgradeDetector detector = createDetector(new FetchedData());
|
| + TestManifestUpgradeDetector detector = createDetectorWithFetchedData(new Data());
|
| detector.start();
|
| Assert.assertTrue(detector.mCompleted);
|
| Assert.assertFalse(detector.mIsUpgraded);
|
| @@ -149,11 +190,164 @@ public class ManifestUpgradeDetectorTest {
|
|
|
| @Test
|
| public void testStartUrlChangeShouldUpgrade() {
|
| - FetchedData fetchedData = new FetchedData();
|
| + Data fetchedData = new Data();
|
| fetchedData.startUrl = "/changed.html";
|
| - TestManifestUpgradeDetector detector = createDetector(fetchedData);
|
| + TestManifestUpgradeDetector detector = createDetectorWithFetchedData(fetchedData);
|
| + detector.start();
|
| + Assert.assertTrue(detector.mCompleted);
|
| + Assert.assertTrue(detector.mIsUpgraded);
|
| + }
|
| +
|
| + /**
|
| + * Test that an upgrade is requested when:
|
| + * - WebAPK was generated using icon at {@link WEBAPK_ICON_URL} from Web Manifest.
|
| + * - Bitmap at {@link WEBAPK_ICON_URL} has changed.
|
| + */
|
| + @Test
|
| + public void testHomescreenIconChangeShouldUpgrade() {
|
| + Data fetchedData = new Data();
|
| + fetchedData.iconMurmur2Hash = WEBAPK_ICON_MURMUR2_HASH + 1;
|
| + fetchedData.icon = createBitmap(Color.BLUE);
|
| + TestManifestUpgradeDetector detector = createDetectorWithFetchedData(fetchedData);
|
| +
|
| + detector.start();
|
| + Assert.assertTrue(detector.mCompleted);
|
| + Assert.assertEquals(WEBAPK_ICON_URL, detector.mRequestedIconUrl);
|
| + Assert.assertTrue(detector.mIsUpgraded);
|
| + }
|
| +
|
| + /**
|
| + * Test that an upgrade is requested when:
|
| + * - WebAPK is generated using icon at {@link WEBAPK_ICON_URL} from Web Manifest.
|
| + * - Web Manifest is updated to refer to different icon.
|
| + */
|
| + @Test
|
| + public void testHomescreenIconUrlChangeShouldUpgrade() {
|
| + Data fetchedData = new Data();
|
| + fetchedData.iconUrl = "/icon2.png";
|
| +
|
| + TestManifestUpgradeDetector detector = createDetectorWithFetchedData(fetchedData);
|
| + detector.start();
|
| + Assert.assertTrue(detector.mCompleted);
|
| + Assert.assertEquals("/icon2.png", detector.mRequestedIconUrl);
|
| + Assert.assertTrue(detector.mIsUpgraded);
|
| + }
|
| +
|
| + /**
|
| + * Test that an upgrade is requested when:
|
| + * - There were no icons in the Web Manifest when the WebAPK was generated. Page favicon
|
| + * at {@link faviconUrl} was used as the homescreen icon.
|
| + * - Bitmap at {@link faviconUrl} has changed.
|
| + */
|
| + @Test
|
| + public void testFaviconHomescreenIconChangeShouldUpgrade() {
|
| + final String faviconUrl = "/favicon.ico";
|
| +
|
| + Data oldData = new Data();
|
| + oldData.iconUrl = faviconUrl;
|
| + oldData.icon = createBitmap(Color.YELLOW);
|
| + oldData.iconMurmur2Hash = 5L;
|
| + Data fetchedData = new Data();
|
| + // Still no icon specified in Web Manifest.
|
| + fetchedData.iconUrl = "";
|
| + // Updated data at {@link WEBAPK_ICON_URL}.
|
| + fetchedData.iconMurmur2Hash = 6L;
|
| + fetchedData.icon = createBitmap(Color.BLUE);
|
| +
|
| + TestManifestUpgradeDetector detector = createDetector(oldData, fetchedData);
|
| detector.start();
|
| Assert.assertTrue(detector.mCompleted);
|
| + Assert.assertEquals(faviconUrl, detector.mRequestedIconUrl);
|
| Assert.assertTrue(detector.mIsUpgraded);
|
| }
|
| +
|
| + /**
|
| + * Test that an upgrade is not requested when:
|
| + * - There were no icons in the Web Manifest when the WebAPK was generated. Page favicon
|
| + * at {@link faviconUrl} was used as the homescreen icon.
|
| + * - Bitmap at {@link faviconUrl} has not changed.
|
| + */
|
| + @Test
|
| + public void testFaviconHomescreenIconShouldNotUpgrade() {
|
| + final String faviconUrl = "/favicon.ico";
|
| + final Bitmap faviconBitmap = createBitmap(Color.YELLOW);
|
| + final long faviconMurmur2Hash = 5L;
|
| +
|
| + Data oldData = new Data();
|
| + oldData.iconUrl = faviconUrl;
|
| + oldData.icon = faviconBitmap;
|
| + oldData.iconMurmur2Hash = faviconMurmur2Hash;
|
| + Data fetchedData = new Data();
|
| + // Still no icon specified in Web Manifest.
|
| + fetchedData.iconUrl = "";
|
| + fetchedData.icon = faviconBitmap;
|
| + fetchedData.iconMurmur2Hash = faviconMurmur2Hash;
|
| +
|
| + TestManifestUpgradeDetector detector = createDetector(oldData, fetchedData);
|
| + detector.start();
|
| + Assert.assertTrue(detector.mCompleted);
|
| + Assert.assertEquals(faviconUrl, detector.mRequestedIconUrl);
|
| + Assert.assertFalse(detector.mIsUpgraded);
|
| + }
|
| +
|
| + /**
|
| + * Test that an upgrade is not requested when:
|
| + * - WebAPK was generated using icon at {@link WEBAPK_ICON_URL} from Web Manifest.
|
| + * - Request for {@link WEBAPK_ICON_URL} returns HTTP 404 error.
|
| + */
|
| + @Test
|
| + public void test404HomescreenIconShouldNotUpgrade() {
|
| + Data fetchedData = new Data();
|
| + fetchedData.icon = null;
|
| + fetchedData.iconMurmur2Hash = 0L;
|
| +
|
| + TestManifestUpgradeDetector detector = createDetectorWithFetchedData(fetchedData);
|
| + detector.start();
|
| + Assert.assertTrue(detector.mCompleted);
|
| + Assert.assertEquals(WEBAPK_ICON_URL, detector.mRequestedIconUrl);
|
| + Assert.assertFalse(detector.mIsUpgraded);
|
| + }
|
| +
|
| + /**
|
| + * Test that an upgrade is requested when:
|
| + * - There were no icons in the Web Manifest when the WebAPK was generated. Icon was generated
|
| + * for use as the homescreen icon.
|
| + * - Web Manifest is updated to refer to {@link WEBAPK_ICON_URL}.
|
| + */
|
| + @Test
|
| + public void testNoIconToCustomIconShouldUpgrade() {
|
| + Data oldData = new Data();
|
| + oldData.iconUrl = "";
|
| + oldData.iconMurmur2Hash = 0L;
|
| + Data fetchedData = new Data();
|
| +
|
| + TestManifestUpgradeDetector detector = createDetector(oldData, fetchedData);
|
| + detector.start();
|
| + Assert.assertTrue(detector.mCompleted);
|
| + Assert.assertEquals(WEBAPK_ICON_URL, detector.mRequestedIconUrl);
|
| + Assert.assertTrue(detector.mIsUpgraded);
|
| + }
|
| +
|
| + /**
|
| + * Test that an upgrade is not requested when:
|
| + * - There were no icons in the Web Manifest when the WebAPK was generated. Icon was generated
|
| + * for use as the homescreen icon.
|
| + * - Web Manifest remains unchanged.
|
| + */
|
| + @Test
|
| + public void testNoIconShouldNotUpgrade() {
|
| + Data oldData = new Data();
|
| + oldData.iconUrl = "";
|
| + oldData.icon = null;
|
| + oldData.iconMurmur2Hash = 0L;
|
| + Data fetchedData = new Data();
|
| + fetchedData.iconUrl = "";
|
| +
|
| + TestManifestUpgradeDetector detector = createDetector(oldData, fetchedData);
|
| + detector.start();
|
| + Assert.assertTrue(detector.mCompleted);
|
| + // No icon should have been requested.
|
| + Assert.assertNull(detector.mRequestedIconUrl);
|
| + Assert.assertFalse(detector.mIsUpgraded);
|
| + }
|
| }
|
|
|