OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.test.suitebuilder.annotation.MediumTest; | 8 import android.test.suitebuilder.annotation.MediumTest; |
9 | 9 |
10 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
11 import org.chromium.base.test.util.CallbackHelper; | 11 import org.chromium.base.test.util.CallbackHelper; |
| 12 import org.chromium.base.test.util.CommandLineFlags; |
12 import org.chromium.base.test.util.Feature; | 13 import org.chromium.base.test.util.Feature; |
13 import org.chromium.blink_public.platform.WebDisplayMode; | 14 import org.chromium.blink_public.platform.WebDisplayMode; |
| 15 import org.chromium.chrome.browser.ChromeSwitches; |
14 import org.chromium.chrome.browser.tab.Tab; | 16 import org.chromium.chrome.browser.tab.Tab; |
15 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; | 17 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
16 import org.chromium.chrome.test.util.browser.WebappTestPage; | 18 import org.chromium.chrome.test.util.browser.WebappTestPage; |
17 import org.chromium.content_public.common.ScreenOrientationValues; | 19 import org.chromium.content_public.common.ScreenOrientationValues; |
18 import org.chromium.net.test.EmbeddedTestServer; | 20 import org.chromium.net.test.EmbeddedTestServer; |
| 21 import org.chromium.webapk.lib.client.WebApkVersion; |
19 | 22 |
20 import java.util.HashMap; | 23 import java.util.HashMap; |
21 import java.util.Map; | 24 import java.util.Map; |
22 | 25 |
23 /** | 26 /** |
24 * Tests ManifestUpgradeDetector. This class contains tests which cannot be done
as JUnit tests. | 27 * Tests WebApkUpdateManager. This class contains tests which cannot be done as
JUnit tests. |
25 */ | 28 */ |
26 public class ManifestUpgradeDetectorTest extends ChromeTabbedActivityTestBase { | 29 @CommandLineFlags.Add(ChromeSwitches.CHECK_FOR_WEB_MANIFEST_UPDATE_ON_STARTUP) |
| 30 public class WebApkUpdateManagerTest extends ChromeTabbedActivityTestBase { |
27 | 31 |
| 32 private static final String WEBAPK_PACKAGE = "test.package"; |
| 33 private static final String WEBAPK_ID = "webapk_id"; |
28 private static final String WEBAPK_MANIFEST_URL = | 34 private static final String WEBAPK_MANIFEST_URL = |
29 "/chrome/test/data/banners/manifest_one_icon.json"; | 35 "/chrome/test/data/banners/manifest_one_icon.json"; |
30 | 36 |
31 // Data contained in {@link WEBAPK_MANIFEST_URL}. | 37 // Data contained in {@link WEBAPK_MANIFEST_URL}. |
32 private static final String WEBAPK_START_URL = | 38 private static final String WEBAPK_START_URL = |
33 "/chrome/test/data/banners/manifest_test_page.html"; | 39 "/chrome/test/data/banners/manifest_test_page.html"; |
34 private static final String WEBAPK_SCOPE_URL = "/chrome/test/data/banners/"; | 40 private static final String WEBAPK_SCOPE_URL = "/chrome/test/data/banners/"; |
35 private static final String WEBAPK_NAME = "Manifest test app"; | 41 private static final String WEBAPK_NAME = "Manifest test app"; |
36 private static final String WEBAPK_SHORT_NAME = "Manifest test app"; | 42 private static final String WEBAPK_SHORT_NAME = "Manifest test app"; |
37 private static final String WEBAPK_ICON_URL = "/chrome/test/data/banners/ima
ge-512px.png"; | 43 private static final String WEBAPK_ICON_URL = "/chrome/test/data/banners/ima
ge-512px.png"; |
38 private static final String WEBAPK_ICON_MURMUR2_HASH = "7742433188808797392"
; | 44 private static final String WEBAPK_ICON_MURMUR2_HASH = "7742433188808797392"
; |
39 private static final int WEBAPK_DISPLAY_MODE = WebDisplayMode.Standalone; | 45 private static final int WEBAPK_DISPLAY_MODE = WebDisplayMode.Standalone; |
40 private static final int WEBAPK_ORIENTATION = ScreenOrientationValues.LANDSC
APE; | 46 private static final int WEBAPK_ORIENTATION = ScreenOrientationValues.LANDSC
APE; |
41 private static final long WEBAPK_THEME_COLOR = 2147483648L; | 47 private static final long WEBAPK_THEME_COLOR = 2147483648L; |
42 private static final long WEBAPK_BACKGROUND_COLOR = 2147483648L; | 48 private static final long WEBAPK_BACKGROUND_COLOR = 2147483648L; |
43 | 49 |
44 private EmbeddedTestServer mTestServer; | 50 private EmbeddedTestServer mTestServer; |
45 private Tab mTab; | 51 private Tab mTab; |
46 | 52 |
47 // CallbackHelper which blocks until the {@link ManifestUpgradeDetector.Call
back} callback is | 53 /** |
48 // called. | 54 * Subclass of {@link WebApkUpdateManager} which notifies the {@link Callbac
kHelper} passed to |
49 private static class CallbackWaiter | 55 * the constructor when it has been determined whether an update is needed. |
50 extends CallbackHelper implements ManifestUpgradeDetector.Callback { | 56 */ |
51 private String mName; | 57 private static class TestWebApkUpdateManager extends WebApkUpdateManager { |
52 private boolean mNeedsUpgrade; | 58 private CallbackHelper mWaiter; |
| 59 private boolean mNeedsUpdate = false; |
| 60 |
| 61 public TestWebApkUpdateManager(CallbackHelper waiter) { |
| 62 mWaiter = waiter; |
| 63 } |
53 | 64 |
54 @Override | 65 @Override |
55 public void onFinishedFetchingWebManifestForInitialUrl( | 66 public void onFinishedFetchingWebManifestForInitialUrl( |
56 boolean needsUpgrade, WebApkInfo info, String bestIconUrl) {} | 67 WebApkInfo fetchedInfo, String bestIconUrl) { |
57 | 68 super.onFinishedFetchingWebManifestForInitialUrl(fetchedInfo, bestIc
onUrl); |
58 public void onGotManifestData(boolean needsUpgrade, WebApkInfo info, Str
ing bestIconUrl) { | 69 mWaiter.notifyCalled(); |
59 mName = info.name(); | |
60 mNeedsUpgrade = needsUpgrade; | |
61 notifyCalled(); | |
62 } | 70 } |
63 | 71 |
64 public String name() { | 72 @Override |
65 return mName; | 73 public void onGotManifestData(WebApkInfo fetchedInfo, String bestIconUrl
) { |
| 74 super.onGotManifestData(fetchedInfo, bestIconUrl); |
| 75 mWaiter.notifyCalled(); |
66 } | 76 } |
67 | 77 |
68 public boolean needsUpgrade() { | 78 @Override |
69 return mNeedsUpgrade; | 79 protected void updateAsync(WebApkInfo fetchedInfo, String bestIconUrl) { |
| 80 mNeedsUpdate = true; |
| 81 } |
| 82 |
| 83 public boolean needsUpdate() { |
| 84 return mNeedsUpdate; |
70 } | 85 } |
71 } | 86 } |
72 | 87 |
73 private static class CreationData { | 88 private static class CreationData { |
74 public String manifestUrl; | 89 public String manifestUrl; |
75 public String startUrl; | 90 public String startUrl; |
76 public String scope; | 91 public String scope; |
77 public String name; | 92 public String name; |
78 public String shortName; | 93 public String shortName; |
79 public Map<String, String> iconUrlToMurmur2HashMap; | 94 public Map<String, String> iconUrlToMurmur2HashMap; |
(...skipping 21 matching lines...) Expand all Loading... |
101 creationData.backgroundColor = WEBAPK_BACKGROUND_COLOR; | 116 creationData.backgroundColor = WEBAPK_BACKGROUND_COLOR; |
102 return creationData; | 117 return creationData; |
103 } | 118 } |
104 | 119 |
105 @Override | 120 @Override |
106 protected void setUp() throws Exception { | 121 protected void setUp() throws Exception { |
107 super.setUp(); | 122 super.setUp(); |
108 Context context = getInstrumentation().getTargetContext(); | 123 Context context = getInstrumentation().getTargetContext(); |
109 mTestServer = EmbeddedTestServer.createAndStartServer(context); | 124 mTestServer = EmbeddedTestServer.createAndStartServer(context); |
110 mTab = getActivity().getActivityTab(); | 125 mTab = getActivity().getActivityTab(); |
| 126 |
| 127 TestFetchStorageCallback callback = new TestFetchStorageCallback(); |
| 128 WebappRegistry.getInstance().register(WEBAPK_ID, callback); |
| 129 callback.waitForCallback(0); |
111 } | 130 } |
112 | 131 |
113 @Override | 132 @Override |
114 protected void tearDown() throws Exception { | 133 protected void tearDown() throws Exception { |
115 mTestServer.stopAndDestroyServer(); | 134 mTestServer.stopAndDestroyServer(); |
116 super.tearDown(); | 135 super.tearDown(); |
117 } | 136 } |
118 | 137 |
119 @Override | 138 @Override |
120 public void startMainActivity() throws InterruptedException { | 139 public void startMainActivity() throws InterruptedException { |
121 startMainActivityOnBlankPage(); | 140 startMainActivityOnBlankPage(); |
122 } | 141 } |
123 | 142 |
124 /** | 143 /** Checks whether a WebAPK update is needed. */ |
125 * Starts a ManifestUpgradeDetector. Calls {@link callback} once the detecto
r has fetched the | 144 private boolean checkUpdateNeeded(final CreationData creationData) throws Ex
ception { |
126 * Web Manifest and determined whether the WebAPK needs to be upgraded. | 145 CallbackHelper waiter = new CallbackHelper(); |
127 */ | 146 final TestWebApkUpdateManager updateManager = new TestWebApkUpdateManage
r(waiter); |
128 private void startManifestUpgradeDetector( | 147 |
129 CreationData creationData, final ManifestUpgradeDetector.Callback ca
llback) { | |
130 WebApkInfo info = WebApkInfo.create("", "", creationData.scope, null, cr
eationData.name, | |
131 creationData.shortName, creationData.displayMode, creationData.o
rientation, 0, | |
132 creationData.themeColor, creationData.backgroundColor, "", 0, | |
133 creationData.manifestUrl, creationData.startUrl, | |
134 creationData.iconUrlToMurmur2HashMap); | |
135 final ManifestUpgradeDetector detector = new ManifestUpgradeDetector(mTa
b, info, callback); | |
136 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 148 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
137 @Override | 149 @Override |
138 public void run() { | 150 public void run() { |
139 detector.start(); | 151 WebApkInfo info = WebApkInfo.create(WEBAPK_ID, "", creationData.
scope, null, |
| 152 creationData.name, creationData.shortName, creationData.
displayMode, |
| 153 creationData.orientation, 0, creationData.themeColor, |
| 154 creationData.backgroundColor, "", WebApkVersion.CURRENT_
SHELL_APK_VERSION, |
| 155 creationData.manifestUrl, creationData.startUrl, |
| 156 creationData.iconUrlToMurmur2HashMap); |
| 157 updateManager.updateIfNeeded(mTab, info); |
140 } | 158 } |
141 }); | 159 }); |
| 160 waiter.waitForCallback(0); |
| 161 |
| 162 return updateManager.needsUpdate(); |
142 } | 163 } |
143 | 164 |
144 /** | 165 /** |
145 * Test that the canonicalized URLs are used in determining whether the fetc
hed Web Manifest | 166 * Test that the canonicalized URLs are used in determining whether the fetc
hed Web Manifest |
146 * data differs from the metadata in the WebAPK's Android Manifest. This is
important because | 167 * data differs from the metadata in the WebAPK's Android Manifest. This is
important because |
147 * the URLs in the Web Manifest have been modified by the WebAPK server prio
r to being stored in | 168 * the URLs in the Web Manifest have been modified by the WebAPK server prio
r to being stored in |
148 * the WebAPK Android Manifest. Chrome and the WebAPK server parse URLs diff
erently. | 169 * the WebAPK Android Manifest. Chrome and the WebAPK server parse URLs diff
erently. |
149 */ | 170 */ |
150 @MediumTest | 171 @MediumTest |
151 @Feature({"WebApk"}) | 172 @Feature({"WebApk"}) |
152 public void testCanonicalUrlsIdenticalShouldNotUpgrade() throws Exception { | 173 public void testCanonicalUrlsIdenticalShouldNotUpgrade() throws Exception { |
153 CallbackWaiter waiter = new CallbackWaiter(); | |
154 | |
155 // URL canonicalization should replace "%74" with 't'. | 174 // URL canonicalization should replace "%74" with 't'. |
156 CreationData creationData = defaultCreationData(mTestServer); | 175 CreationData creationData = defaultCreationData(mTestServer); |
157 creationData.startUrl = mTestServer.getURL( | 176 creationData.startUrl = mTestServer.getURL( |
158 "/chrome/test/data/banners/manifest_%74est_page.html"); | 177 "/chrome/test/data/banners/manifest_%74est_page.html"); |
159 startManifestUpgradeDetector(creationData, waiter); | |
160 | 178 |
161 WebappTestPage.navigateToPageWithServiceWorkerAndManifest( | 179 WebappTestPage.navigateToPageWithServiceWorkerAndManifest( |
162 mTestServer, mTab, WEBAPK_MANIFEST_URL); | 180 mTestServer, mTab, WEBAPK_MANIFEST_URL); |
163 waiter.waitForCallback(0); | 181 assertFalse(checkUpdateNeeded(creationData)); |
164 | |
165 assertEquals(WEBAPK_NAME, waiter.name()); | |
166 assertFalse(waiter.needsUpgrade()); | |
167 } | 182 } |
168 | 183 |
169 /** | 184 /** |
170 * Test that an upgraded WebAPK is requested if the canonicalized "start URL
s" are different. | 185 * Test that an upgraded WebAPK is requested if the canonicalized "start URL
s" are different. |
171 */ | 186 */ |
172 @MediumTest | 187 @MediumTest |
173 @Feature({"WebApk"}) | 188 @Feature({"WebApk"}) |
174 public void testCanonicalUrlsDifferentShouldUpgrade() throws Exception { | 189 public void testCanonicalUrlsDifferentShouldUpgrade() throws Exception { |
175 CallbackWaiter waiter = new CallbackWaiter(); | |
176 | |
177 // URL canonicalization should replace "%62" with 'b'. | 190 // URL canonicalization should replace "%62" with 'b'. |
178 CreationData creationData = defaultCreationData(mTestServer); | 191 CreationData creationData = defaultCreationData(mTestServer); |
179 creationData.startUrl = mTestServer.getURL( | 192 creationData.startUrl = mTestServer.getURL( |
180 "/chrome/test/data/banners/manifest_%62est_page.html"); | 193 "/chrome/test/data/banners/manifest_%62est_page.html"); |
181 startManifestUpgradeDetector(creationData, waiter); | |
182 | 194 |
183 WebappTestPage.navigateToPageWithServiceWorkerAndManifest( | 195 WebappTestPage.navigateToPageWithServiceWorkerAndManifest( |
184 mTestServer, mTab, WEBAPK_MANIFEST_URL); | 196 mTestServer, mTab, WEBAPK_MANIFEST_URL); |
185 waiter.waitForCallback(0); | 197 assertTrue(checkUpdateNeeded(creationData)); |
186 | |
187 assertEquals(WEBAPK_NAME, waiter.name()); | |
188 assertTrue(waiter.needsUpgrade()); | |
189 } | 198 } |
190 } | 199 } |
OLD | NEW |