Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.webkit; |
| 18 | |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 19 | import static org.junit.Assert.assertArrayEquals; |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertFalse; |
| 22 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageInfo; |
| 25 | import android.content.pm.Signature; |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 26 | import android.os.Build; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 27 | import android.os.Bundle; |
Gustav Sennton | 148cb3e | 2016-09-02 15:58:38 +0100 | [diff] [blame] | 28 | import android.test.suitebuilder.annotation.MediumTest; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 29 | import android.util.Base64; |
Gustav Sennton | e5468f7 | 2017-11-20 19:42:43 +0000 | [diff] [blame] | 30 | import android.webkit.UserPackage; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 31 | import android.webkit.WebViewFactory; |
| 32 | import android.webkit.WebViewProviderInfo; |
| 33 | import android.webkit.WebViewProviderResponse; |
| 34 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 35 | import androidx.test.InstrumentationRegistry; |
| 36 | import androidx.test.runner.AndroidJUnit4; |
| 37 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
Philip P. Moltmann | 6836607 | 2017-11-27 12:29:20 -0800 | [diff] [blame] | 40 | import org.mockito.ArgumentMatcher; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 41 | import org.mockito.Matchers; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 42 | import org.mockito.Mockito; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 43 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 44 | import java.util.concurrent.CountDownLatch; |
| 45 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 46 | /** |
| 47 | * Tests for WebViewUpdateService |
Gustav Sennton | 148cb3e | 2016-09-02 15:58:38 +0100 | [diff] [blame] | 48 | runtest --path frameworks/base/services/tests/servicestests/ \ |
| 49 | -c com.android.server.webkit.WebViewUpdateServiceTest |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 50 | */ |
Gustav Sennton | 148cb3e | 2016-09-02 15:58:38 +0100 | [diff] [blame] | 51 | // Use MediumTest instead of SmallTest as the implementation of WebViewUpdateService |
| 52 | // is intended to work on several threads and uses at least one sleep/wait-statement. |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 53 | @RunWith(AndroidJUnit4.class) |
Gustav Sennton | 148cb3e | 2016-09-02 15:58:38 +0100 | [diff] [blame] | 54 | @MediumTest |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 55 | public class WebViewUpdateServiceTest { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 56 | private final static String TAG = WebViewUpdateServiceTest.class.getSimpleName(); |
| 57 | |
| 58 | private WebViewUpdateServiceImpl mWebViewUpdateServiceImpl; |
| 59 | private TestSystemImpl mTestSystemImpl; |
| 60 | |
| 61 | private static final String WEBVIEW_LIBRARY_FLAG = "com.android.webview.WebViewLibrary"; |
| 62 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 63 | /** |
| 64 | * Creates a new instance. |
| 65 | */ |
| 66 | public WebViewUpdateServiceTest() { |
| 67 | } |
| 68 | |
| 69 | private void setupWithPackages(WebViewProviderInfo[] packages) { |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 70 | setupWithAllParameters(packages, false /* fallbackLogicEnabled */, 1 /* numRelros */, |
| 71 | true /* isDebuggable */, false /* multiProcessDefault */); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 74 | private void setupWithPackagesAndFallbackLogic(WebViewProviderInfo[] packages) { |
| 75 | setupWithAllParameters(packages, true /* fallbackLogicEnabled */, 1 /* numRelros */, |
| 76 | true /* isDebuggable */, false /* multiProcessDefault */); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 79 | private void setupWithPackagesAndRelroCount(WebViewProviderInfo[] packages, int numRelros) { |
| 80 | setupWithAllParameters(packages, false /* fallbackLogicEnabled */, numRelros, |
| 81 | true /* isDebuggable */, false /* multiProcessDefault */); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 84 | private void setupWithPackagesNonDebuggable(WebViewProviderInfo[] packages) { |
| 85 | setupWithAllParameters(packages, false /* fallbackLogicEnabled */, 1 /* numRelros */, |
| 86 | false /* isDebuggable */, false /* multiProcessDefault */); |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 89 | private void setupWithPackagesAndMultiProcess(WebViewProviderInfo[] packages, |
| 90 | boolean multiProcessDefault) { |
| 91 | setupWithAllParameters(packages, false /* fallbackLogicEnabled */, 1 /* numRelros */, |
| 92 | true /* isDebuggable */, multiProcessDefault); |
| 93 | } |
| 94 | |
| 95 | private void setupWithAllParameters(WebViewProviderInfo[] packages, |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 96 | boolean fallbackLogicEnabled, int numRelros, boolean isDebuggable, |
| 97 | boolean multiProcessDefault) { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 98 | TestSystemImpl testing = new TestSystemImpl(packages, fallbackLogicEnabled, numRelros, |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 99 | isDebuggable, multiProcessDefault); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 100 | mTestSystemImpl = Mockito.spy(testing); |
| 101 | mWebViewUpdateServiceImpl = |
| 102 | new WebViewUpdateServiceImpl(null /*Context*/, mTestSystemImpl); |
| 103 | } |
| 104 | |
| 105 | private void setEnabledAndValidPackageInfos(WebViewProviderInfo[] providers) { |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 106 | // Set package infos for the primary user (user 0). |
| 107 | setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, providers); |
| 108 | } |
| 109 | |
| 110 | private void setEnabledAndValidPackageInfosForUser(int userId, |
| 111 | WebViewProviderInfo[] providers) { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 112 | for(WebViewProviderInfo wpi : providers) { |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 113 | mTestSystemImpl.setPackageInfoForUser(userId, createPackageInfo(wpi.packageName, |
| 114 | true /* enabled */, true /* valid */, true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 118 | private void checkCertainPackageUsedAfterWebViewBootPreparation(String expectedProviderName, |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 119 | WebViewProviderInfo[] webviewPackages) { |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 120 | checkCertainPackageUsedAfterWebViewBootPreparation( |
| 121 | expectedProviderName, webviewPackages, 1); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 124 | private void checkCertainPackageUsedAfterWebViewBootPreparation(String expectedProviderName, |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 125 | WebViewProviderInfo[] webviewPackages, int numRelros) { |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 126 | setupWithPackagesAndRelroCount(webviewPackages, numRelros); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 127 | // Add (enabled and valid) package infos for each provider |
| 128 | setEnabledAndValidPackageInfos(webviewPackages); |
| 129 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 130 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 131 | |
| 132 | Mockito.verify(mTestSystemImpl).onWebViewProviderChanged( |
| 133 | Mockito.argThat(new IsPackageInfoWithName(expectedProviderName))); |
| 134 | |
| 135 | for (int n = 0; n < numRelros; n++) { |
| 136 | mWebViewUpdateServiceImpl.notifyRelroCreationCompleted(); |
| 137 | } |
| 138 | |
| 139 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 140 | assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status); |
| 141 | assertEquals(expectedProviderName, response.packageInfo.packageName); |
| 142 | } |
| 143 | |
| 144 | // For matching the package name of a PackageInfo |
Philip P. Moltmann | 6836607 | 2017-11-27 12:29:20 -0800 | [diff] [blame] | 145 | private class IsPackageInfoWithName implements ArgumentMatcher<PackageInfo> { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 146 | private final String mPackageName; |
| 147 | |
| 148 | IsPackageInfoWithName(String name) { |
| 149 | mPackageName = name; |
| 150 | } |
| 151 | |
| 152 | @Override |
Philip P. Moltmann | 6836607 | 2017-11-27 12:29:20 -0800 | [diff] [blame] | 153 | public boolean matches(PackageInfo p) { |
| 154 | return p.packageName.equals(mPackageName); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 157 | @Override |
Paul Duffin | 192bb0b | 2017-03-09 18:49:41 +0000 | [diff] [blame] | 158 | public String toString() { |
| 159 | return String.format("PackageInfo with name '%s'", mPackageName); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | private static PackageInfo createPackageInfo( |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 164 | String packageName, boolean enabled, boolean valid, boolean installed) { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 165 | PackageInfo p = new PackageInfo(); |
| 166 | p.packageName = packageName; |
| 167 | p.applicationInfo = new ApplicationInfo(); |
| 168 | p.applicationInfo.enabled = enabled; |
| 169 | p.applicationInfo.metaData = new Bundle(); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 170 | if (installed) { |
| 171 | p.applicationInfo.flags |= ApplicationInfo.FLAG_INSTALLED; |
| 172 | } else { |
| 173 | p.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED; |
| 174 | } |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 175 | if (valid) { |
| 176 | // no flag means invalid |
| 177 | p.applicationInfo.metaData.putString(WEBVIEW_LIBRARY_FLAG, "blah"); |
| 178 | } |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 179 | // Default to this package being valid in terms of targetSdkVersion. |
| 180 | p.applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 181 | return p; |
| 182 | } |
| 183 | |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 184 | private static PackageInfo createPackageInfo(String packageName, boolean enabled, boolean valid, |
| 185 | boolean installed, Signature[] signatures, long updateTime) { |
| 186 | PackageInfo p = createPackageInfo(packageName, enabled, valid, installed); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 187 | p.signatures = signatures; |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 188 | p.lastUpdateTime = updateTime; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 189 | return p; |
| 190 | } |
| 191 | |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 192 | private static PackageInfo createPackageInfo(String packageName, boolean enabled, boolean valid, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 193 | boolean installed, Signature[] signatures, long updateTime, boolean hidden) { |
| 194 | PackageInfo p = |
| 195 | createPackageInfo(packageName, enabled, valid, installed, signatures, updateTime); |
| 196 | if (hidden) { |
| 197 | p.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_HIDDEN; |
| 198 | } else { |
| 199 | p.applicationInfo.privateFlags &= ~ApplicationInfo.PRIVATE_FLAG_HIDDEN; |
| 200 | } |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 201 | return p; |
| 202 | } |
| 203 | |
Gustav Sennton | fd222fc2 | 2016-06-16 20:24:23 +0100 | [diff] [blame] | 204 | private static PackageInfo createPackageInfo(String packageName, boolean enabled, boolean valid, |
| 205 | boolean installed, Signature[] signatures, long updateTime, boolean hidden, |
Dianne Hackborn | 891b74c | 2017-12-11 16:54:27 -0800 | [diff] [blame] | 206 | long versionCode, boolean isSystemApp) { |
Gustav Sennton | fd222fc2 | 2016-06-16 20:24:23 +0100 | [diff] [blame] | 207 | PackageInfo p = createPackageInfo(packageName, enabled, valid, installed, signatures, |
| 208 | updateTime, hidden); |
Dianne Hackborn | 891b74c | 2017-12-11 16:54:27 -0800 | [diff] [blame] | 209 | p.setLongVersionCode(versionCode); |
Patrick Baumann | c2def58 | 2018-04-04 12:14:15 -0700 | [diff] [blame] | 210 | p.applicationInfo.setVersionCode(versionCode); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 211 | if (isSystemApp) p.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 212 | return p; |
| 213 | } |
| 214 | |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 215 | private void checkPreparationPhasesForPackage(String expectedPackage, int numPreparation) { |
| 216 | // Verify that onWebViewProviderChanged was called for the numPreparation'th time for the |
| 217 | // expected package |
| 218 | Mockito.verify(mTestSystemImpl, Mockito.times(numPreparation)).onWebViewProviderChanged( |
| 219 | Mockito.argThat(new IsPackageInfoWithName(expectedPackage))); |
| 220 | |
| 221 | mWebViewUpdateServiceImpl.notifyRelroCreationCompleted(); |
| 222 | |
| 223 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 224 | assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status); |
| 225 | assertEquals(expectedPackage, response.packageInfo.packageName); |
| 226 | } |
| 227 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 228 | /** |
| 229 | * The WebView preparation boot phase is run on the main thread (especially on a thread with a |
| 230 | * looper) so to avoid bugs where our tests fail because a looper hasn't been attached to the |
| 231 | * thread running prepareWebViewInSystemServer we run it on the main thread. |
| 232 | */ |
| 233 | private void runWebViewBootPreparationOnMainSync() { |
| 234 | InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { |
| 235 | @Override |
| 236 | public void run() { |
| 237 | mWebViewUpdateServiceImpl.prepareWebViewInSystemServer(); |
| 238 | } |
| 239 | }); |
| 240 | } |
| 241 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 242 | |
| 243 | // **************** |
| 244 | // Tests |
| 245 | // **************** |
| 246 | |
| 247 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 248 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 249 | public void testWithSinglePackage() { |
| 250 | String testPackageName = "test.package.name"; |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 251 | checkCertainPackageUsedAfterWebViewBootPreparation(testPackageName, |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 252 | new WebViewProviderInfo[] { |
| 253 | new WebViewProviderInfo(testPackageName, "", |
| 254 | true /*default available*/, false /* fallback */, null)}); |
| 255 | } |
| 256 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 257 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 258 | public void testDefaultPackageUsedOverNonDefault() { |
| 259 | String defaultPackage = "defaultPackage"; |
| 260 | String nonDefaultPackage = "nonDefaultPackage"; |
| 261 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 262 | new WebViewProviderInfo(nonDefaultPackage, "", false, false, null), |
| 263 | new WebViewProviderInfo(defaultPackage, "", true, false, null)}; |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 264 | checkCertainPackageUsedAfterWebViewBootPreparation(defaultPackage, packages); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 267 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 268 | public void testSeveralRelros() { |
| 269 | String singlePackage = "singlePackage"; |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 270 | checkCertainPackageUsedAfterWebViewBootPreparation( |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 271 | singlePackage, |
| 272 | new WebViewProviderInfo[] { |
| 273 | new WebViewProviderInfo(singlePackage, "", true /*def av*/, false, null)}, |
| 274 | 2); |
| 275 | } |
| 276 | |
| 277 | // Ensure that package with valid signatures is chosen rather than package with invalid |
| 278 | // signatures. |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 279 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 280 | public void testWithSignatures() { |
| 281 | String validPackage = "valid package"; |
| 282 | String invalidPackage = "invalid package"; |
| 283 | |
| 284 | Signature validSignature = new Signature("11"); |
| 285 | Signature invalidExpectedSignature = new Signature("22"); |
| 286 | Signature invalidPackageSignature = new Signature("33"); |
| 287 | |
| 288 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 289 | new WebViewProviderInfo(invalidPackage, "", true, false, new String[]{ |
| 290 | Base64.encodeToString( |
| 291 | invalidExpectedSignature.toByteArray(), Base64.DEFAULT)}), |
| 292 | new WebViewProviderInfo(validPackage, "", true, false, new String[]{ |
| 293 | Base64.encodeToString( |
| 294 | validSignature.toByteArray(), Base64.DEFAULT)}) |
| 295 | }; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 296 | setupWithPackagesNonDebuggable(packages); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 297 | mTestSystemImpl.setPackageInfo(createPackageInfo(invalidPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 298 | true /* valid */, true /* installed */, new Signature[]{invalidPackageSignature} |
| 299 | , 0 /* updateTime */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 300 | mTestSystemImpl.setPackageInfo(createPackageInfo(validPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 301 | true /* valid */, true /* installed */, new Signature[]{validSignature} |
| 302 | , 0 /* updateTime */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 303 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 304 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 305 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 306 | |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 307 | checkPreparationPhasesForPackage(validPackage, 1 /* first preparation for this package */); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 308 | |
| 309 | WebViewProviderInfo[] validPackages = mWebViewUpdateServiceImpl.getValidWebViewPackages(); |
| 310 | assertEquals(1, validPackages.length); |
| 311 | assertEquals(validPackage, validPackages[0].packageName); |
| 312 | } |
| 313 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 314 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 315 | public void testFailWaitingForRelro() { |
| 316 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 317 | new WebViewProviderInfo("packagename", "", true, true, null)}; |
| 318 | setupWithPackages(packages); |
| 319 | setEnabledAndValidPackageInfos(packages); |
| 320 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 321 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 322 | |
| 323 | Mockito.verify(mTestSystemImpl).onWebViewProviderChanged( |
| 324 | Mockito.argThat(new IsPackageInfoWithName(packages[0].packageName))); |
| 325 | |
| 326 | // Never call notifyRelroCreation() |
| 327 | |
| 328 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 329 | assertEquals(WebViewFactory.LIBLOAD_FAILED_WAITING_FOR_RELRO, response.status); |
| 330 | } |
| 331 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 332 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 333 | public void testFailListingEmptyWebviewPackages() { |
| 334 | WebViewProviderInfo[] packages = new WebViewProviderInfo[0]; |
| 335 | setupWithPackages(packages); |
| 336 | setEnabledAndValidPackageInfos(packages); |
| 337 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 338 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 339 | |
| 340 | Mockito.verify(mTestSystemImpl, Mockito.never()).onWebViewProviderChanged( |
| 341 | Matchers.anyObject()); |
| 342 | |
| 343 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 344 | assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 345 | assertEquals(null, mWebViewUpdateServiceImpl.getCurrentWebViewPackage()); |
| 346 | |
| 347 | // Now install a package |
| 348 | String singlePackage = "singlePackage"; |
| 349 | packages = new WebViewProviderInfo[]{ |
| 350 | new WebViewProviderInfo(singlePackage, "", true, false, null)}; |
| 351 | setupWithPackages(packages); |
| 352 | setEnabledAndValidPackageInfos(packages); |
| 353 | |
| 354 | mWebViewUpdateServiceImpl.packageStateChanged(singlePackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 355 | WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 356 | |
| 357 | checkPreparationPhasesForPackage(singlePackage, 1 /* number of finished preparations */); |
| 358 | assertEquals(singlePackage, |
| 359 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName); |
| 360 | |
| 361 | // Remove the package again |
| 362 | mTestSystemImpl.removePackageInfo(singlePackage); |
| 363 | mWebViewUpdateServiceImpl.packageStateChanged(singlePackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 364 | WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 365 | |
| 366 | // Package removed - ensure our interface states that there is no package |
| 367 | response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 368 | assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status); |
| 369 | assertEquals(null, mWebViewUpdateServiceImpl.getCurrentWebViewPackage()); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 372 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 373 | public void testFailListingInvalidWebviewPackage() { |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 374 | WebViewProviderInfo wpi = new WebViewProviderInfo("package", "", true, true, null); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 375 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] {wpi}; |
| 376 | setupWithPackages(packages); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 377 | mTestSystemImpl.setPackageInfo( |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 378 | createPackageInfo(wpi.packageName, true /* enabled */, false /* valid */, |
| 379 | true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 380 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 381 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 382 | |
| 383 | Mockito.verify(mTestSystemImpl, Mockito.never()).onWebViewProviderChanged( |
| 384 | Matchers.anyObject()); |
| 385 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 386 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 387 | assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 388 | |
| 389 | // Verify that we can recover from failing to list webview packages. |
| 390 | mTestSystemImpl.setPackageInfo( |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 391 | createPackageInfo(wpi.packageName, true /* enabled */, true /* valid */, |
| 392 | true /* installed */)); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 393 | mWebViewUpdateServiceImpl.packageStateChanged(wpi.packageName, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 394 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 395 | |
| 396 | checkPreparationPhasesForPackage(wpi.packageName, 1); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Test that switching provider using changeProviderAndSetting works. |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 400 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 401 | public void testSwitchingProvider() { |
| 402 | String firstPackage = "first"; |
| 403 | String secondPackage = "second"; |
| 404 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 405 | new WebViewProviderInfo(firstPackage, "", true, false, null), |
| 406 | new WebViewProviderInfo(secondPackage, "", true, false, null)}; |
| 407 | checkSwitchingProvider(packages, firstPackage, secondPackage); |
| 408 | } |
| 409 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 410 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 411 | public void testSwitchingProviderToNonDefault() { |
| 412 | String defaultPackage = "defaultPackage"; |
| 413 | String nonDefaultPackage = "nonDefaultPackage"; |
| 414 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 415 | new WebViewProviderInfo(defaultPackage, "", true, false, null), |
| 416 | new WebViewProviderInfo(nonDefaultPackage, "", false, false, null)}; |
| 417 | checkSwitchingProvider(packages, defaultPackage, nonDefaultPackage); |
| 418 | } |
| 419 | |
| 420 | private void checkSwitchingProvider(WebViewProviderInfo[] packages, String initialPackage, |
| 421 | String finalPackage) { |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 422 | checkCertainPackageUsedAfterWebViewBootPreparation(initialPackage, packages); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 423 | |
| 424 | mWebViewUpdateServiceImpl.changeProviderAndSetting(finalPackage); |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 425 | checkPreparationPhasesForPackage(finalPackage, 1 /* first preparation for this package */); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 426 | |
| 427 | Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(initialPackage)); |
| 428 | } |
| 429 | |
| 430 | // Change provider during relro creation by using changeProviderAndSetting |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 431 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 432 | public void testSwitchingProviderDuringRelroCreation() { |
| 433 | checkChangingProviderDuringRelroCreation(true); |
| 434 | } |
| 435 | |
| 436 | // Change provider during relro creation by enabling a provider |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 437 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 438 | public void testChangingProviderThroughEnablingDuringRelroCreation() { |
| 439 | checkChangingProviderDuringRelroCreation(false); |
| 440 | } |
| 441 | |
| 442 | private void checkChangingProviderDuringRelroCreation(boolean settingsChange) { |
| 443 | String firstPackage = "first"; |
| 444 | String secondPackage = "second"; |
| 445 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 446 | new WebViewProviderInfo(firstPackage, "", true, false, null), |
| 447 | new WebViewProviderInfo(secondPackage, "", true, false, null)}; |
| 448 | setupWithPackages(packages); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 449 | // Have all packages be enabled, so that we can change provider however we want to |
| 450 | setEnabledAndValidPackageInfos(packages); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 451 | |
| 452 | CountDownLatch countdown = new CountDownLatch(1); |
| 453 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 454 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 455 | |
| 456 | Mockito.verify(mTestSystemImpl).onWebViewProviderChanged( |
| 457 | Mockito.argThat(new IsPackageInfoWithName(firstPackage))); |
| 458 | |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 459 | assertEquals(firstPackage, |
| 460 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 461 | |
| 462 | new Thread(new Runnable() { |
| 463 | @Override |
| 464 | public void run() { |
| 465 | WebViewProviderResponse threadResponse = |
| 466 | mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 467 | assertEquals(WebViewFactory.LIBLOAD_SUCCESS, threadResponse.status); |
| 468 | assertEquals(secondPackage, threadResponse.packageInfo.packageName); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 469 | // Verify that we killed the first package if we performed a settings change - |
| 470 | // otherwise we had to disable the first package, in which case its dependents |
| 471 | // should have been killed by the framework. |
| 472 | if (settingsChange) { |
| 473 | Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(firstPackage)); |
| 474 | } |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 475 | countdown.countDown(); |
| 476 | } |
| 477 | }).start(); |
| 478 | try { |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 479 | Thread.sleep(500); // Let the new thread run / be blocked |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 480 | } catch (InterruptedException e) { |
| 481 | } |
| 482 | |
| 483 | if (settingsChange) { |
| 484 | mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage); |
| 485 | } else { |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 486 | // Enable the second provider |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 487 | mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 488 | true /* valid */, true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 489 | mWebViewUpdateServiceImpl.packageStateChanged( |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 490 | secondPackage, WebViewUpdateService.PACKAGE_CHANGED, TestSystemImpl.PRIMARY_USER_ID); |
| 491 | |
| 492 | // Ensure we haven't changed package yet. |
| 493 | assertEquals(firstPackage, |
| 494 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName); |
| 495 | |
| 496 | // Switch provider by disabling the first one |
| 497 | mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, false /* enabled */, |
| 498 | true /* valid */, true /* installed */)); |
| 499 | mWebViewUpdateServiceImpl.packageStateChanged( |
| 500 | firstPackage, WebViewUpdateService.PACKAGE_CHANGED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 501 | } |
| 502 | mWebViewUpdateServiceImpl.notifyRelroCreationCompleted(); |
| 503 | // first package done, should start on second |
| 504 | |
| 505 | Mockito.verify(mTestSystemImpl).onWebViewProviderChanged( |
| 506 | Mockito.argThat(new IsPackageInfoWithName(secondPackage))); |
| 507 | |
| 508 | mWebViewUpdateServiceImpl.notifyRelroCreationCompleted(); |
| 509 | // second package done, the other thread should now be unblocked |
| 510 | try { |
| 511 | countdown.await(); |
| 512 | } catch (InterruptedException e) { |
| 513 | } |
| 514 | } |
| 515 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 516 | /** |
| 517 | * Scenario for testing migrating away from the fallback logic. |
| 518 | * We start with a primary package that's a disabled fallback, and an enabled secondary, |
| 519 | * so that the fallback being re-enabled will cause a provider switch, as that covers |
| 520 | * the most complex case. |
| 521 | */ |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 522 | @Test |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 523 | public void testFallbackLogicMigration() { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 524 | String primaryPackage = "primary"; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 525 | String secondaryPackage = "secondary"; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 526 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 527 | new WebViewProviderInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 528 | primaryPackage, "", true /* default available */, true /* fallback */, null), |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 529 | new WebViewProviderInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 530 | secondaryPackage, "", true /* default available */, false /* fallback */, |
| 531 | null)}; |
| 532 | setupWithPackagesAndFallbackLogic(packages); |
| 533 | mTestSystemImpl.setPackageInfo( |
| 534 | createPackageInfo(primaryPackage, false /* enabled */ , true /* valid */, |
| 535 | true /* installed */)); |
| 536 | mTestSystemImpl.setPackageInfo( |
| 537 | createPackageInfo(secondaryPackage, true /* enabled */ , true /* valid */, |
| 538 | true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 539 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 540 | // Check that the boot time logic re-enables and chooses the primary, and disables the |
| 541 | // fallback logic. |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 542 | runWebViewBootPreparationOnMainSync(); |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 543 | Mockito.verify(mTestSystemImpl).enablePackageForAllUsers( |
| 544 | Matchers.anyObject(), Mockito.eq(primaryPackage), Mockito.eq(true)); |
| 545 | checkPreparationPhasesForPackage(primaryPackage, 1); |
| 546 | assertFalse(mTestSystemImpl.isFallbackLogicEnabled()); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 547 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 548 | // Disable primary again |
| 549 | mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, false /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 550 | true /* valid */, true /* installed */)); |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 551 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
| 552 | WebViewUpdateService.PACKAGE_CHANGED, TestSystemImpl.PRIMARY_USER_ID); |
| 553 | checkPreparationPhasesForPackage(secondaryPackage, 1); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 554 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 555 | // Run boot logic again and check that we didn't re-enable the primary a second time. |
| 556 | runWebViewBootPreparationOnMainSync(); |
| 557 | Mockito.verify(mTestSystemImpl, Mockito.times(1)).enablePackageForAllUsers( |
| 558 | Matchers.anyObject(), Mockito.eq(primaryPackage), Mockito.eq(true)); |
| 559 | checkPreparationPhasesForPackage(secondaryPackage, 2); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /** |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 563 | * Scenario for installing primary package when secondary in use. |
| 564 | * 1. Start with only secondary installed |
| 565 | * 2. Install primary |
| 566 | * 3. Primary should be used |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 567 | */ |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 568 | @Test |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 569 | public void testInstallingPrimaryPackage() { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 570 | String primaryPackage = "primary"; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 571 | String secondaryPackage = "secondary"; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 572 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 573 | new WebViewProviderInfo( |
| 574 | primaryPackage, "", true /* default available */, false /* fallback */, null), |
| 575 | new WebViewProviderInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 576 | secondaryPackage, "", true /* default available */, false /* fallback */, |
| 577 | null)}; |
| 578 | setupWithPackages(packages); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 579 | mTestSystemImpl.setPackageInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 580 | createPackageInfo(secondaryPackage, true /* enabled */ , true /* valid */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 581 | true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 582 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 583 | runWebViewBootPreparationOnMainSync(); |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 584 | checkPreparationPhasesForPackage(secondaryPackage, |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 585 | 1 /* first preparation for this package*/); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 586 | |
| 587 | // Install primary package |
| 588 | mTestSystemImpl.setPackageInfo( |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 589 | createPackageInfo(primaryPackage, true /* enabled */ , true /* valid */, |
| 590 | true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 591 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 592 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 593 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 594 | // Verify primary package used as provider, and secondary package killed |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 595 | checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation for this package*/); |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 596 | Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(secondaryPackage)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 599 | @Test |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 600 | public void testRemovingPrimarySelectsSecondarySingleUser() { |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 601 | for (PackageRemovalType removalType : REMOVAL_TYPES) { |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 602 | checkRemovingPrimarySelectsSecondary(false /* multiUser */, removalType); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | @Test |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 607 | public void testRemovingPrimarySelectsSecondaryMultiUser() { |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 608 | for (PackageRemovalType removalType : REMOVAL_TYPES) { |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 609 | checkRemovingPrimarySelectsSecondary(true /* multiUser */, removalType); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Represents how to remove a package during a tests (disabling it / uninstalling it / hiding |
| 615 | * it). |
| 616 | */ |
| 617 | private enum PackageRemovalType { |
| 618 | UNINSTALL, DISABLE, HIDE |
| 619 | } |
| 620 | |
| 621 | private PackageRemovalType[] REMOVAL_TYPES = PackageRemovalType.class.getEnumConstants(); |
| 622 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 623 | public void checkRemovingPrimarySelectsSecondary(boolean multiUser, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 624 | PackageRemovalType removalType) { |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 625 | String primaryPackage = "primary"; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 626 | String secondaryPackage = "secondary"; |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 627 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 628 | new WebViewProviderInfo( |
| 629 | primaryPackage, "", true /* default available */, false /* fallback */, null), |
| 630 | new WebViewProviderInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 631 | secondaryPackage, "", true /* default available */, false /* fallback */, |
| 632 | null)}; |
| 633 | setupWithPackages(packages); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 634 | int secondaryUserId = 10; |
| 635 | int userIdToChangePackageFor = multiUser ? secondaryUserId : TestSystemImpl.PRIMARY_USER_ID; |
| 636 | if (multiUser) { |
| 637 | mTestSystemImpl.addUser(secondaryUserId); |
| 638 | setEnabledAndValidPackageInfosForUser(secondaryUserId, packages); |
| 639 | } |
| 640 | setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, packages); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 641 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 642 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 643 | checkPreparationPhasesForPackage(primaryPackage, 1); |
| 644 | |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 645 | boolean enabled = !(removalType == PackageRemovalType.DISABLE); |
| 646 | boolean installed = !(removalType == PackageRemovalType.UNINSTALL); |
| 647 | boolean hidden = (removalType == PackageRemovalType.HIDE); |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 648 | // Disable primary package and ensure secondary becomes used |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 649 | mTestSystemImpl.setPackageInfoForUser(userIdToChangePackageFor, |
| 650 | createPackageInfo(primaryPackage, enabled /* enabled */, true /* valid */, |
| 651 | installed /* installed */, null /* signature */, 0 /* updateTime */, |
| 652 | hidden /* hidden */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 653 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 654 | removalType == PackageRemovalType.DISABLE |
| 655 | ? WebViewUpdateService.PACKAGE_CHANGED : WebViewUpdateService.PACKAGE_REMOVED, |
| 656 | userIdToChangePackageFor); // USER ID |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 657 | checkPreparationPhasesForPackage(secondaryPackage, 1); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 658 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 659 | // Again enable primary package and verify primary is used |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 660 | mTestSystemImpl.setPackageInfoForUser(userIdToChangePackageFor, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 661 | createPackageInfo(primaryPackage, true /* enabled */, true /* valid */, |
| 662 | true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 663 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 664 | removalType == PackageRemovalType.DISABLE |
| 665 | ? WebViewUpdateService.PACKAGE_CHANGED : WebViewUpdateService.PACKAGE_ADDED, |
| 666 | userIdToChangePackageFor); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 667 | checkPreparationPhasesForPackage(primaryPackage, 2); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 668 | } |
| 669 | |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 670 | /** |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 671 | * Ensures that adding a new user for which the current WebView package is uninstalled causes a |
| 672 | * change of WebView provider. |
| 673 | */ |
| 674 | @Test |
| 675 | public void testAddingNewUserWithUninstalledPackage() { |
| 676 | String primaryPackage = "primary"; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 677 | String secondaryPackage = "secondary"; |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 678 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 679 | new WebViewProviderInfo( |
| 680 | primaryPackage, "", true /* default available */, false /* fallback */, null), |
| 681 | new WebViewProviderInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 682 | secondaryPackage, "", true /* default available */, false /* fallback */, |
| 683 | null)}; |
| 684 | setupWithPackages(packages); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 685 | setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, packages); |
| 686 | int newUser = 100; |
| 687 | mTestSystemImpl.addUser(newUser); |
| 688 | // Let the primary package be uninstalled for the new user |
| 689 | mTestSystemImpl.setPackageInfoForUser(newUser, |
| 690 | createPackageInfo(primaryPackage, true /* enabled */, true /* valid */, |
| 691 | false /* installed */)); |
| 692 | mTestSystemImpl.setPackageInfoForUser(newUser, |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 693 | createPackageInfo(secondaryPackage, true /* enabled */, true /* valid */, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 694 | true /* installed */)); |
| 695 | mWebViewUpdateServiceImpl.handleNewUser(newUser); |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 696 | checkPreparationPhasesForPackage(secondaryPackage, 1 /* numRelros */); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 700 | * Timing dependent test where we verify that the list of valid webview packages becoming empty |
| 701 | * at a certain point doesn't crash us or break our state. |
| 702 | */ |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 703 | @Test |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 704 | public void testNotifyRelroDoesntCrashIfNoPackages() { |
| 705 | String firstPackage = "first"; |
| 706 | String secondPackage = "second"; |
| 707 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 708 | new WebViewProviderInfo(firstPackage, "", true /* default available */, |
| 709 | false /* fallback */, null), |
| 710 | new WebViewProviderInfo(secondPackage, "", true /* default available */, |
| 711 | false /* fallback */, null)}; |
| 712 | setupWithPackages(packages); |
| 713 | // Add (enabled and valid) package infos for each provider |
| 714 | setEnabledAndValidPackageInfos(packages); |
| 715 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 716 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 717 | |
| 718 | Mockito.verify(mTestSystemImpl).onWebViewProviderChanged( |
| 719 | Mockito.argThat(new IsPackageInfoWithName(firstPackage))); |
| 720 | |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 721 | // Change provider during relro creation to enter a state where we are |
| 722 | // waiting for relro creation to complete just to re-run relro creation. |
| 723 | // (so that in next notifyRelroCreationCompleted() call we have to list webview packages) |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 724 | mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage); |
| 725 | |
| 726 | // Make packages invalid to cause exception to be thrown |
| 727 | mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 728 | false /* valid */, true /* installed */, null /* signatures */, |
| 729 | 0 /* updateTime */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 730 | mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 731 | false /* valid */, true /* installed */)); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 732 | |
| 733 | // This shouldn't throw an exception! |
| 734 | mWebViewUpdateServiceImpl.notifyRelroCreationCompleted(); |
| 735 | |
| 736 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 737 | assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status); |
| 738 | |
| 739 | // Now make a package valid again and verify that we can switch back to that |
| 740 | mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 741 | true /* valid */, true /* installed */, null /* signatures */, |
| 742 | 1 /* updateTime */ )); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 743 | |
| 744 | mWebViewUpdateServiceImpl.packageStateChanged(firstPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 745 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 746 | |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 747 | // Ensure we use firstPackage |
| 748 | checkPreparationPhasesForPackage(firstPackage, 2 /* second preparation for this package */); |
| 749 | } |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 750 | |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 751 | /** |
| 752 | * Verify that even if a user-chosen package is removed temporarily we start using it again when |
| 753 | * it is added back. |
| 754 | */ |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 755 | @Test |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 756 | public void testTempRemovePackageDoesntSwitchProviderPermanently() { |
| 757 | String firstPackage = "first"; |
| 758 | String secondPackage = "second"; |
| 759 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 760 | new WebViewProviderInfo(firstPackage, "", true /* default available */, |
| 761 | false /* fallback */, null), |
| 762 | new WebViewProviderInfo(secondPackage, "", true /* default available */, |
| 763 | false /* fallback */, null)}; |
| 764 | checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 765 | |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 766 | // Explicitly use the second package |
| 767 | mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage); |
| 768 | checkPreparationPhasesForPackage(secondPackage, 1 /* first time for this package */); |
| 769 | |
| 770 | // Remove second package (invalidate it) and verify that first package is used |
| 771 | mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 772 | false /* valid */, true /* installed */)); |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 773 | mWebViewUpdateServiceImpl.packageStateChanged(secondPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 774 | WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 775 | checkPreparationPhasesForPackage(firstPackage, 2 /* second time for this package */); |
| 776 | |
| 777 | // Now make the second package valid again and verify that it is used again |
| 778 | mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 779 | true /* valid */, true /* installed */)); |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 780 | mWebViewUpdateServiceImpl.packageStateChanged(secondPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 781 | WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 782 | checkPreparationPhasesForPackage(secondPackage, 2 /* second time for this package */); |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Ensure that we update the user-chosen setting across boots if the chosen package is no |
| 787 | * longer installed and valid. |
| 788 | */ |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 789 | @Test |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 790 | public void testProviderSettingChangedDuringBootIfProviderNotAvailable() { |
| 791 | String chosenPackage = "chosenPackage"; |
| 792 | String nonChosenPackage = "non-chosenPackage"; |
| 793 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 794 | new WebViewProviderInfo(chosenPackage, "", true /* default available */, |
| 795 | false /* fallback */, null), |
| 796 | new WebViewProviderInfo(nonChosenPackage, "", true /* default available */, |
| 797 | false /* fallback */, null)}; |
| 798 | |
| 799 | setupWithPackages(packages); |
| 800 | // Only 'install' nonChosenPackage |
| 801 | mTestSystemImpl.setPackageInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 802 | createPackageInfo(nonChosenPackage, true /* enabled */, true /* valid */, |
| 803 | true /* installed */)); |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 804 | |
| 805 | // Set user-chosen package |
| 806 | mTestSystemImpl.updateUserSetting(null, chosenPackage); |
| 807 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 808 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | a915904 | 2016-04-11 16:32:52 +0100 | [diff] [blame] | 809 | |
| 810 | // Verify that we switch the setting to point to the current package |
| 811 | Mockito.verify(mTestSystemImpl).updateUserSetting( |
| 812 | Mockito.anyObject(), Mockito.eq(nonChosenPackage)); |
| 813 | assertEquals(nonChosenPackage, mTestSystemImpl.getUserChosenWebViewProvider(null)); |
| 814 | |
| 815 | checkPreparationPhasesForPackage(nonChosenPackage, 1); |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 818 | @Test |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 819 | public void testRecoverFailedListingWebViewPackagesSettingsChange() { |
| 820 | checkRecoverAfterFailListingWebviewPackages(true); |
| 821 | } |
| 822 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 823 | @Test |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 824 | public void testRecoverFailedListingWebViewPackagesAddedPackage() { |
| 825 | checkRecoverAfterFailListingWebviewPackages(false); |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Test that we can recover correctly from failing to list WebView packages. |
| 830 | * settingsChange: whether to fail during changeProviderAndSetting or packageStateChanged |
| 831 | */ |
| 832 | public void checkRecoverAfterFailListingWebviewPackages(boolean settingsChange) { |
| 833 | String firstPackage = "first"; |
| 834 | String secondPackage = "second"; |
| 835 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 836 | new WebViewProviderInfo(firstPackage, "", true /* default available */, |
| 837 | false /* fallback */, null), |
| 838 | new WebViewProviderInfo(secondPackage, "", true /* default available */, |
| 839 | false /* fallback */, null)}; |
| 840 | checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages); |
| 841 | |
| 842 | // Make both packages invalid so that we fail listing WebView packages |
| 843 | mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 844 | false /* valid */, true /* installed */)); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 845 | mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 846 | false /* valid */, true /* installed */)); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 847 | |
| 848 | // Change package to hit the webview packages listing problem. |
| 849 | if (settingsChange) { |
| 850 | mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage); |
| 851 | } else { |
| 852 | mWebViewUpdateServiceImpl.packageStateChanged(secondPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 853 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 857 | assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status); |
| 858 | |
| 859 | // Make second package valid and verify that we can load it again |
| 860 | mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 861 | true /* valid */, true /* installed */)); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 862 | |
| 863 | mWebViewUpdateServiceImpl.packageStateChanged(secondPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 864 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 865 | |
| 866 | |
| 867 | checkPreparationPhasesForPackage(secondPackage, 1); |
| 868 | } |
| 869 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 870 | @Test |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 871 | public void testDontKillIfPackageReplaced() { |
| 872 | checkDontKillIfPackageRemoved(true); |
| 873 | } |
| 874 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 875 | @Test |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 876 | public void testDontKillIfPackageRemoved() { |
| 877 | checkDontKillIfPackageRemoved(false); |
| 878 | } |
| 879 | |
| 880 | public void checkDontKillIfPackageRemoved(boolean replaced) { |
| 881 | String firstPackage = "first"; |
| 882 | String secondPackage = "second"; |
| 883 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 884 | new WebViewProviderInfo(firstPackage, "", true /* default available */, |
| 885 | false /* fallback */, null), |
| 886 | new WebViewProviderInfo(secondPackage, "", true /* default available */, |
| 887 | false /* fallback */, null)}; |
| 888 | checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages); |
| 889 | |
| 890 | // Replace or remove the current webview package |
| 891 | if (replaced) { |
| 892 | mTestSystemImpl.setPackageInfo( |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 893 | createPackageInfo(firstPackage, true /* enabled */, false /* valid */, |
| 894 | true /* installed */)); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 895 | mWebViewUpdateServiceImpl.packageStateChanged(firstPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 896 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 897 | } else { |
| 898 | mTestSystemImpl.removePackageInfo(firstPackage); |
| 899 | mWebViewUpdateServiceImpl.packageStateChanged(firstPackage, |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 900 | WebViewUpdateService.PACKAGE_REMOVED, TestSystemImpl.PRIMARY_USER_ID); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | checkPreparationPhasesForPackage(secondPackage, 1); |
| 904 | |
| 905 | Mockito.verify(mTestSystemImpl, Mockito.never()).killPackageDependents( |
| 906 | Mockito.anyObject()); |
| 907 | } |
| 908 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 909 | @Test |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 910 | public void testKillIfSettingChanged() { |
| 911 | String firstPackage = "first"; |
| 912 | String secondPackage = "second"; |
| 913 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 914 | new WebViewProviderInfo(firstPackage, "", true /* default available */, |
| 915 | false /* fallback */, null), |
| 916 | new WebViewProviderInfo(secondPackage, "", true /* default available */, |
| 917 | false /* fallback */, null)}; |
| 918 | checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages); |
| 919 | |
| 920 | mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage); |
| 921 | |
| 922 | checkPreparationPhasesForPackage(secondPackage, 1); |
| 923 | |
| 924 | Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(firstPackage)); |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * Test that we kill apps using an old provider when we change the provider setting, even if the |
| 929 | * new provider is not the one we intended to change to. |
| 930 | */ |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 931 | @Test |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 932 | public void testKillIfChangeProviderIncorrectly() { |
| 933 | String firstPackage = "first"; |
| 934 | String secondPackage = "second"; |
| 935 | String thirdPackage = "third"; |
| 936 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 937 | new WebViewProviderInfo(firstPackage, "", true /* default available */, |
| 938 | false /* fallback */, null), |
| 939 | new WebViewProviderInfo(secondPackage, "", true /* default available */, |
| 940 | false /* fallback */, null), |
| 941 | new WebViewProviderInfo(thirdPackage, "", true /* default available */, |
| 942 | false /* fallback */, null)}; |
| 943 | setupWithPackages(packages); |
| 944 | setEnabledAndValidPackageInfos(packages); |
| 945 | |
| 946 | // Start with the setting pointing to the third package |
| 947 | mTestSystemImpl.updateUserSetting(null, thirdPackage); |
| 948 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 949 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 950 | checkPreparationPhasesForPackage(thirdPackage, 1); |
| 951 | |
| 952 | mTestSystemImpl.setPackageInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 953 | createPackageInfo(secondPackage, true /* enabled */, false /* valid */, |
| 954 | true /* installed */)); |
Gustav Sennton | 95f7e8e | 2016-04-14 15:07:09 +0100 | [diff] [blame] | 955 | |
| 956 | // Try to switch to the invalid second package, this should result in switching to the first |
| 957 | // package, since that is more preferred than the third one. |
| 958 | assertEquals(firstPackage, |
| 959 | mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage)); |
| 960 | |
| 961 | checkPreparationPhasesForPackage(firstPackage, 1); |
| 962 | |
| 963 | Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(thirdPackage)); |
| 964 | } |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 965 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 966 | @Test |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 967 | public void testLowerPackageVersionNotValid() { |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 968 | checkPackageVersions(new int[]{200000} /* system version */, 100000/* candidate version */, |
| 969 | false /* expected validity */); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 970 | } |
| 971 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 972 | @Test |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 973 | public void testEqualPackageVersionValid() { |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 974 | checkPackageVersions(new int[]{100000} /* system version */, 100000 /* candidate version */, |
| 975 | true /* expected validity */); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 978 | @Test |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 979 | public void testGreaterPackageVersionValid() { |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 980 | checkPackageVersions(new int[]{100000} /* system versions */, 200000 /* candidate version */, |
| 981 | true /* expected validity */); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 984 | @Test |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 985 | public void testLastFiveDigitsIgnored() { |
| 986 | checkPackageVersions(new int[]{654321} /* system version */, 612345 /* candidate version */, |
| 987 | true /* expected validity */); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 990 | @Test |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 991 | public void testMinimumSystemVersionUsedTwoDefaultsCandidateValid() { |
| 992 | checkPackageVersions(new int[]{300000, 100000} /* system versions */, |
| 993 | 200000 /* candidate version */, true /* expected validity */); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 994 | } |
| 995 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 996 | @Test |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 997 | public void testMinimumSystemVersionUsedTwoDefaultsCandidateInvalid() { |
| 998 | checkPackageVersions(new int[]{300000, 200000} /* system versions */, |
| 999 | 100000 /* candidate version */, false /* expected validity */); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 1002 | @Test |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1003 | public void testMinimumSystemVersionUsedSeveralDefaultsCandidateValid() { |
| 1004 | checkPackageVersions(new int[]{100000, 200000, 300000, 400000, 500000} /* system versions */, |
| 1005 | 100000 /* candidate version */, true /* expected validity */); |
| 1006 | } |
| 1007 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 1008 | @Test |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1009 | public void testMinimumSystemVersionUsedSeveralDefaultsCandidateInvalid() { |
| 1010 | checkPackageVersions(new int[]{200000, 300000, 400000, 500000, 600000} /* system versions */, |
| 1011 | 100000 /* candidate version */, false /* expected validity */); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1014 | /** |
| 1015 | * Utility method for checking that package version restriction works as it should. |
| 1016 | * I.e. that a package with lower version than the system-default is not valid and that a |
| 1017 | * package with greater than or equal version code is considered valid. |
| 1018 | */ |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1019 | private void checkPackageVersions(int[] systemVersions, int candidateVersion, |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1020 | boolean candidateShouldBeValid) { |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1021 | int numSystemPackages = systemVersions.length; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1022 | int numPackages = systemVersions.length + 1; |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1023 | String candidatePackage = "candidatePackage"; |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1024 | String systemPackage = "systemPackage"; |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1025 | |
| 1026 | // Each package needs a valid signature since we set isDebuggable to false |
| 1027 | Signature signature = new Signature("11"); |
| 1028 | String encodedSignatureString = |
| 1029 | Base64.encodeToString(signature.toByteArray(), Base64.DEFAULT); |
| 1030 | |
| 1031 | // Set up config |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1032 | // 1. candidatePackage |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1033 | // 2-N. default available packages |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1034 | WebViewProviderInfo[] packages = new WebViewProviderInfo[numPackages]; |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1035 | packages[0] = new WebViewProviderInfo(candidatePackage, "", |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1036 | false /* available by default */, false /* fallback */, |
| 1037 | new String[]{encodedSignatureString}); |
| 1038 | for(int n = 1; n < numSystemPackages + 1; n++) { |
| 1039 | packages[n] = new WebViewProviderInfo(systemPackage + n, "", |
| 1040 | true /* available by default */, false /* fallback */, |
| 1041 | new String[]{encodedSignatureString}); |
| 1042 | } |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1043 | setupWithPackagesNonDebuggable(packages); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1044 | |
| 1045 | // Set package infos |
| 1046 | mTestSystemImpl.setPackageInfo( |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1047 | createPackageInfo(candidatePackage, true /* enabled */, true /* valid */, |
Gustav Sennton | fd222fc2 | 2016-06-16 20:24:23 +0100 | [diff] [blame] | 1048 | true /* installed */, new Signature[]{signature}, 0 /* updateTime */, |
| 1049 | false /* hidden */, candidateVersion, false /* isSystemApp */)); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1050 | for(int n = 1; n < numSystemPackages + 1; n++) { |
| 1051 | mTestSystemImpl.setPackageInfo( |
| 1052 | createPackageInfo(systemPackage + n, true /* enabled */, true /* valid */, |
Gustav Sennton | fd222fc2 | 2016-06-16 20:24:23 +0100 | [diff] [blame] | 1053 | true /* installed */, new Signature[]{signature}, 0 /* updateTime */, |
| 1054 | false /* hidden */, systemVersions[n-1], true /* isSystemApp */)); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1055 | } |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1056 | |
| 1057 | WebViewProviderInfo[] validPackages = mWebViewUpdateServiceImpl.getValidWebViewPackages(); |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1058 | int expectedNumValidPackages = numSystemPackages; |
| 1059 | if (candidateShouldBeValid) { |
| 1060 | expectedNumValidPackages++; |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1061 | } else { |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1062 | // Ensure the candidate package is not one of the valid packages |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1063 | for(int n = 0; n < validPackages.length; n++) { |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1064 | assertFalse(candidatePackage.equals(validPackages[n].packageName)); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1068 | assertEquals(expectedNumValidPackages, validPackages.length); |
| 1069 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 1070 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1071 | |
| 1072 | // The non-system package is not available by default so it shouldn't be used here |
| 1073 | checkPreparationPhasesForPackage(systemPackage + "1", 1); |
| 1074 | |
Hui Shu | 826a24b | 2016-04-26 14:53:40 -0700 | [diff] [blame] | 1075 | // Try explicitly switching to the candidate package |
| 1076 | String packageChange = mWebViewUpdateServiceImpl.changeProviderAndSetting(candidatePackage); |
| 1077 | if (candidateShouldBeValid) { |
| 1078 | assertEquals(candidatePackage, packageChange); |
| 1079 | checkPreparationPhasesForPackage(candidatePackage, 1); |
Gustav Sennton | 18c9e15 | 2016-04-15 15:24:53 +0100 | [diff] [blame] | 1080 | } else { |
| 1081 | assertEquals(systemPackage + "1", packageChange); |
| 1082 | // We didn't change package so the webview preparation won't run here |
| 1083 | } |
| 1084 | } |
Gustav Sennton | fd222fc2 | 2016-06-16 20:24:23 +0100 | [diff] [blame] | 1085 | |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1086 | /** |
| 1087 | * Ensure that the update service does use an uninstalled package when that is the only |
| 1088 | * package available. |
| 1089 | */ |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1090 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1091 | public void testWithSingleUninstalledPackage() { |
| 1092 | String testPackageName = "test.package.name"; |
| 1093 | WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] { |
| 1094 | new WebViewProviderInfo(testPackageName, "", |
| 1095 | true /*default available*/, false /* fallback */, null)}; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1096 | setupWithPackages(webviewPackages); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1097 | mTestSystemImpl.setPackageInfo(createPackageInfo(testPackageName, true /* enabled */, |
| 1098 | true /* valid */, false /* installed */)); |
| 1099 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1100 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1101 | |
Gustav Sennton | 963dbbb | 2017-04-06 16:18:02 +0100 | [diff] [blame] | 1102 | Mockito.verify(mTestSystemImpl, Mockito.never()).onWebViewProviderChanged( |
| 1103 | Matchers.anyObject()); |
| 1104 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 1105 | assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status); |
| 1106 | assertEquals(null, mWebViewUpdateServiceImpl.getCurrentWebViewPackage()); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1109 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1110 | public void testNonhiddenPackageUserOverHidden() { |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1111 | checkVisiblePackageUserOverNonVisible(false /* multiUser*/, PackageRemovalType.HIDE); |
| 1112 | checkVisiblePackageUserOverNonVisible(true /* multiUser*/, PackageRemovalType.HIDE); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1115 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1116 | public void testInstalledPackageUsedOverUninstalled() { |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1117 | checkVisiblePackageUserOverNonVisible(false /* multiUser*/, PackageRemovalType.UNINSTALL); |
| 1118 | checkVisiblePackageUserOverNonVisible(true /* multiUser*/, PackageRemovalType.UNINSTALL); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1121 | private void checkVisiblePackageUserOverNonVisible(boolean multiUser, |
| 1122 | PackageRemovalType removalType) { |
| 1123 | assert removalType != PackageRemovalType.DISABLE; |
| 1124 | boolean testUninstalled = removalType == PackageRemovalType.UNINSTALL; |
| 1125 | boolean testHidden = removalType == PackageRemovalType.HIDE; |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1126 | String installedPackage = "installedPackage"; |
| 1127 | String uninstalledPackage = "uninstalledPackage"; |
| 1128 | WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] { |
| 1129 | new WebViewProviderInfo(uninstalledPackage, "", true /* available by default */, |
| 1130 | false /* fallback */, null), |
| 1131 | new WebViewProviderInfo(installedPackage, "", true /* available by default */, |
| 1132 | false /* fallback */, null)}; |
| 1133 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1134 | setupWithPackages(webviewPackages); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1135 | int secondaryUserId = 5; |
| 1136 | if (multiUser) { |
| 1137 | mTestSystemImpl.addUser(secondaryUserId); |
| 1138 | // Install all packages for the primary user. |
| 1139 | setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, webviewPackages); |
| 1140 | mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo( |
| 1141 | installedPackage, true /* enabled */, true /* valid */, true /* installed */)); |
| 1142 | // Hide or uninstall the primary package for the second user |
| 1143 | mTestSystemImpl.setPackageInfo(createPackageInfo(uninstalledPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1144 | true /* valid */, (testUninstalled ? false : true) /* installed */, |
| 1145 | null /* signatures */, 0 /* updateTime */, (testHidden ? true : false))); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1146 | } else { |
| 1147 | mTestSystemImpl.setPackageInfo(createPackageInfo(installedPackage, true /* enabled */, |
| 1148 | true /* valid */, true /* installed */)); |
| 1149 | // Hide or uninstall the primary package |
| 1150 | mTestSystemImpl.setPackageInfo(createPackageInfo(uninstalledPackage, true /* enabled */, |
| 1151 | true /* valid */, (testUninstalled ? false : true) /* installed */, |
| 1152 | null /* signatures */, 0 /* updateTime */, (testHidden ? true : false))); |
| 1153 | } |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1154 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1155 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1156 | |
| 1157 | checkPreparationPhasesForPackage(installedPackage, 1 /* first preparation phase */); |
| 1158 | } |
| 1159 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1160 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1161 | public void testCantSwitchToHiddenPackage () { |
| 1162 | checkCantSwitchToNonVisiblePackage(false /* true == uninstalled, false == hidden */); |
| 1163 | } |
| 1164 | |
| 1165 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1166 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1167 | public void testCantSwitchToUninstalledPackage () { |
| 1168 | checkCantSwitchToNonVisiblePackage(true /* true == uninstalled, false == hidden */); |
| 1169 | } |
| 1170 | |
| 1171 | /** |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1172 | * Ensure that we won't prioritize an uninstalled (or hidden) package even if it is user-chosen. |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1173 | */ |
| 1174 | private void checkCantSwitchToNonVisiblePackage(boolean uninstalledNotHidden) { |
| 1175 | boolean testUninstalled = uninstalledNotHidden; |
| 1176 | boolean testHidden = !uninstalledNotHidden; |
| 1177 | String installedPackage = "installedPackage"; |
| 1178 | String uninstalledPackage = "uninstalledPackage"; |
| 1179 | WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] { |
| 1180 | new WebViewProviderInfo(uninstalledPackage, "", true /* available by default */, |
| 1181 | false /* fallback */, null), |
| 1182 | new WebViewProviderInfo(installedPackage, "", true /* available by default */, |
| 1183 | false /* fallback */, null)}; |
| 1184 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1185 | setupWithPackages(webviewPackages); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1186 | int secondaryUserId = 412; |
| 1187 | mTestSystemImpl.addUser(secondaryUserId); |
| 1188 | |
| 1189 | // Let all packages be installed and enabled for the primary user. |
| 1190 | setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, webviewPackages); |
| 1191 | // Only uninstall the 'uninstalled package' for the secondary user. |
| 1192 | mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(installedPackage, |
| 1193 | true /* enabled */, true /* valid */, true /* installed */)); |
| 1194 | mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(uninstalledPackage, |
| 1195 | true /* enabled */, true /* valid */, !testUninstalled /* installed */, |
| 1196 | null /* signatures */, 0 /* updateTime */, testHidden /* hidden */)); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1197 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1198 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1199 | |
| 1200 | checkPreparationPhasesForPackage(installedPackage, 1 /* first preparation phase */); |
| 1201 | |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1202 | // ensure that we don't switch to the uninstalled package (it will be used if it becomes |
| 1203 | // installed later) |
| 1204 | assertEquals(installedPackage, |
| 1205 | mWebViewUpdateServiceImpl.changeProviderAndSetting(uninstalledPackage)); |
| 1206 | |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1207 | // Ensure both packages are considered valid. |
| 1208 | assertEquals(2, mWebViewUpdateServiceImpl.getValidWebViewPackages().length); |
| 1209 | |
| 1210 | |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1211 | // We should only have called onWebViewProviderChanged once (before calling |
| 1212 | // changeProviderAndSetting |
| 1213 | Mockito.verify(mTestSystemImpl, Mockito.times(1)).onWebViewProviderChanged( |
| 1214 | Mockito.argThat(new IsPackageInfoWithName(installedPackage))); |
| 1215 | } |
| 1216 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1217 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1218 | public void testHiddenPackageNotPrioritizedEvenIfChosen() { |
| 1219 | checkNonvisiblePackageNotPrioritizedEvenIfChosen( |
| 1220 | false /* true == uninstalled, false == hidden */); |
| 1221 | } |
| 1222 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1223 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1224 | public void testUninstalledPackageNotPrioritizedEvenIfChosen() { |
| 1225 | checkNonvisiblePackageNotPrioritizedEvenIfChosen( |
| 1226 | true /* true == uninstalled, false == hidden */); |
| 1227 | } |
| 1228 | |
| 1229 | public void checkNonvisiblePackageNotPrioritizedEvenIfChosen(boolean uninstalledNotHidden) { |
| 1230 | boolean testUninstalled = uninstalledNotHidden; |
| 1231 | boolean testHidden = !uninstalledNotHidden; |
| 1232 | String installedPackage = "installedPackage"; |
| 1233 | String uninstalledPackage = "uninstalledPackage"; |
| 1234 | WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] { |
| 1235 | new WebViewProviderInfo(uninstalledPackage, "", true /* available by default */, |
| 1236 | false /* fallback */, null), |
| 1237 | new WebViewProviderInfo(installedPackage, "", true /* available by default */, |
| 1238 | false /* fallback */, null)}; |
| 1239 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1240 | setupWithPackages(webviewPackages); |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1241 | int secondaryUserId = 4; |
| 1242 | mTestSystemImpl.addUser(secondaryUserId); |
| 1243 | |
| 1244 | setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, webviewPackages); |
| 1245 | mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(installedPackage, |
| 1246 | true /* enabled */, true /* valid */, true /* installed */)); |
| 1247 | mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(uninstalledPackage, |
| 1248 | true /* enabled */, true /* valid */, |
| 1249 | (testUninstalled ? false : true) /* installed */, null /* signatures */, |
| 1250 | 0 /* updateTime */, (testHidden ? true : false) /* hidden */)); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1251 | |
| 1252 | // Start with the setting pointing to the uninstalled package |
| 1253 | mTestSystemImpl.updateUserSetting(null, uninstalledPackage); |
| 1254 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1255 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1256 | |
| 1257 | checkPreparationPhasesForPackage(installedPackage, 1 /* first preparation phase */); |
| 1258 | } |
| 1259 | |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 1260 | @Test |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1261 | public void testPreparationRunsIffNewPackage() { |
| 1262 | String primaryPackage = "primary"; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1263 | String secondaryPackage = "secondary"; |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1264 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 1265 | new WebViewProviderInfo( |
| 1266 | primaryPackage, "", true /* default available */, false /* fallback */, null), |
| 1267 | new WebViewProviderInfo( |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1268 | secondaryPackage, "", true /* default available */, false /* fallback */, |
| 1269 | null)}; |
| 1270 | setupWithPackages(packages); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1271 | mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */, |
| 1272 | true /* valid */, true /* installed */, null /* signatures */, |
| 1273 | 10 /* lastUpdateTime*/ )); |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1274 | mTestSystemImpl.setPackageInfo(createPackageInfo(secondaryPackage, true /* enabled */, |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1275 | true /* valid */, true /* installed */)); |
| 1276 | |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 1277 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1278 | |
| 1279 | checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation phase */); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1280 | |
| 1281 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
| 1282 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, 0 /* userId */); |
| 1283 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
| 1284 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, 1 /* userId */); |
| 1285 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
| 1286 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, 2 /* userId */); |
| 1287 | // package still has the same update-time so we shouldn't run preparation here |
| 1288 | Mockito.verify(mTestSystemImpl, Mockito.times(1)).onWebViewProviderChanged( |
| 1289 | Mockito.argThat(new IsPackageInfoWithName(primaryPackage))); |
Gustav Sennton | 0df2c55 | 2016-06-14 15:32:19 +0100 | [diff] [blame] | 1290 | |
| 1291 | // Ensure we can still load the package |
| 1292 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 1293 | assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status); |
| 1294 | assertEquals(primaryPackage, response.packageInfo.packageName); |
| 1295 | |
| 1296 | |
| 1297 | mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */, |
| 1298 | true /* valid */, true /* installed */, null /* signatures */, |
| 1299 | 20 /* lastUpdateTime*/ )); |
| 1300 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
| 1301 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, 0); |
| 1302 | // The package has now changed - ensure that we have run the preparation phase a second time |
| 1303 | checkPreparationPhasesForPackage(primaryPackage, 2 /* second preparation phase */); |
| 1304 | |
| 1305 | |
| 1306 | mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */, |
| 1307 | true /* valid */, true /* installed */, null /* signatures */, |
| 1308 | 50 /* lastUpdateTime*/ )); |
| 1309 | // Receive intent for different user |
| 1310 | mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage, |
| 1311 | WebViewUpdateService.PACKAGE_ADDED_REPLACED, 2); |
| 1312 | |
| 1313 | checkPreparationPhasesForPackage(primaryPackage, 3 /* third preparation phase */); |
| 1314 | } |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 1315 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 1316 | @Test |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 1317 | public void testGetCurrentWebViewPackage() { |
| 1318 | PackageInfo firstPackage = createPackageInfo("first", true /* enabled */, |
| 1319 | true /* valid */, true /* installed */); |
Dianne Hackborn | 891b74c | 2017-12-11 16:54:27 -0800 | [diff] [blame] | 1320 | firstPackage.setLongVersionCode(100); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 1321 | firstPackage.versionName = "first package version"; |
| 1322 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 1323 | new WebViewProviderInfo(firstPackage.packageName, "", true, false, null)}; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1324 | setupWithPackages(packages); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 1325 | mTestSystemImpl.setPackageInfo(firstPackage); |
| 1326 | |
Torne (Richard Coles) | ab5d0ba | 2016-11-03 15:09:34 +0000 | [diff] [blame] | 1327 | runWebViewBootPreparationOnMainSync(); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 1328 | |
| 1329 | Mockito.verify(mTestSystemImpl).onWebViewProviderChanged( |
| 1330 | Mockito.argThat(new IsPackageInfoWithName(firstPackage.packageName))); |
| 1331 | |
| 1332 | mWebViewUpdateServiceImpl.notifyRelroCreationCompleted(); |
| 1333 | |
| 1334 | // Ensure the API is correct before running waitForAndGetProvider |
| 1335 | assertEquals(firstPackage.packageName, |
| 1336 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 1337 | assertEquals(firstPackage.getLongVersionCode(), |
| 1338 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().getLongVersionCode()); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 1339 | assertEquals(firstPackage.versionName, |
| 1340 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().versionName); |
| 1341 | |
| 1342 | WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider(); |
| 1343 | assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status); |
| 1344 | assertEquals(firstPackage.packageName, response.packageInfo.packageName); |
| 1345 | |
| 1346 | // Ensure the API is still correct after running waitForAndGetProvider |
| 1347 | assertEquals(firstPackage.packageName, |
| 1348 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 1349 | assertEquals(firstPackage.getLongVersionCode(), |
| 1350 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().getLongVersionCode()); |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 1351 | assertEquals(firstPackage.versionName, |
| 1352 | mWebViewUpdateServiceImpl.getCurrentWebViewPackage().versionName); |
| 1353 | } |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 1354 | |
| 1355 | @Test |
| 1356 | public void testMultiProcessEnabledByDefault() { |
Gustav Sennton | b265016 | 2017-04-07 14:41:38 +0100 | [diff] [blame] | 1357 | testMultiProcessByDefault(true /* enabledByDefault */); |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | @Test |
| 1361 | public void testMultiProcessDisabledByDefault() { |
Gustav Sennton | b265016 | 2017-04-07 14:41:38 +0100 | [diff] [blame] | 1362 | testMultiProcessByDefault(false /* enabledByDefault */); |
| 1363 | } |
| 1364 | |
| 1365 | private void testMultiProcessByDefault(boolean enabledByDefault) { |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 1366 | String primaryPackage = "primary"; |
| 1367 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 1368 | new WebViewProviderInfo( |
| 1369 | primaryPackage, "", true /* default available */, false /* fallback */, null)}; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1370 | setupWithPackagesAndMultiProcess(packages, enabledByDefault); |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 1371 | mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */, |
| 1372 | true /* valid */, true /* installed */, null /* signatures */, |
| 1373 | 10 /* lastUpdateTime*/, false /* not hidden */, 1000 /* versionCode */, |
| 1374 | false /* isSystemApp */)); |
| 1375 | |
| 1376 | runWebViewBootPreparationOnMainSync(); |
| 1377 | checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation phase */); |
| 1378 | |
| 1379 | // Check it's off by default |
Gustav Sennton | b265016 | 2017-04-07 14:41:38 +0100 | [diff] [blame] | 1380 | assertEquals(enabledByDefault, mWebViewUpdateServiceImpl.isMultiProcessEnabled()); |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 1381 | |
| 1382 | // Test toggling it |
Gustav Sennton | b265016 | 2017-04-07 14:41:38 +0100 | [diff] [blame] | 1383 | mWebViewUpdateServiceImpl.enableMultiProcess(!enabledByDefault); |
| 1384 | assertEquals(!enabledByDefault, mWebViewUpdateServiceImpl.isMultiProcessEnabled()); |
| 1385 | mWebViewUpdateServiceImpl.enableMultiProcess(enabledByDefault); |
| 1386 | assertEquals(enabledByDefault, mWebViewUpdateServiceImpl.isMultiProcessEnabled()); |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 1387 | } |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 1388 | |
Gustav Sennton | b265016 | 2017-04-07 14:41:38 +0100 | [diff] [blame] | 1389 | @Test |
| 1390 | public void testMultiProcessEnabledByDefaultWithSettingsValue() { |
| 1391 | testMultiProcessByDefaultWithSettingsValue( |
| 1392 | true /* enabledByDefault */, Integer.MIN_VALUE, false /* expectEnabled */); |
| 1393 | testMultiProcessByDefaultWithSettingsValue( |
| 1394 | true /* enabledByDefault */, -999999, true /* expectEnabled */); |
| 1395 | testMultiProcessByDefaultWithSettingsValue( |
| 1396 | true /* enabledByDefault */, 0, true /* expectEnabled */); |
| 1397 | testMultiProcessByDefaultWithSettingsValue( |
| 1398 | true /* enabledByDefault */, 999999, true /* expectEnabled */); |
| 1399 | } |
| 1400 | |
| 1401 | @Test |
| 1402 | public void testMultiProcessDisabledByDefaultWithSettingsValue() { |
| 1403 | testMultiProcessByDefaultWithSettingsValue( |
| 1404 | false /* enabledByDefault */, Integer.MIN_VALUE, false /* expectEnabled */); |
| 1405 | testMultiProcessByDefaultWithSettingsValue( |
| 1406 | false /* enabledByDefault */, 0, false /* expectEnabled */); |
| 1407 | testMultiProcessByDefaultWithSettingsValue( |
| 1408 | false /* enabledByDefault */, 999999, false /* expectEnabled */); |
| 1409 | testMultiProcessByDefaultWithSettingsValue( |
| 1410 | false /* enabledByDefault */, Integer.MAX_VALUE, true /* expectEnabled */); |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * Test the logic of the multiprocess setting depending on whether multiprocess is enabled by |
| 1415 | * default, and what the setting is set to. |
| 1416 | * @param enabledByDefault whether multiprocess is enabled by default. |
| 1417 | * @param settingValue value of the multiprocess setting. |
| 1418 | */ |
| 1419 | private void testMultiProcessByDefaultWithSettingsValue( |
| 1420 | boolean enabledByDefault, int settingValue, boolean expectEnabled) { |
| 1421 | String primaryPackage = "primary"; |
| 1422 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 1423 | new WebViewProviderInfo( |
| 1424 | primaryPackage, "", true /* default available */, false /* fallback */, null)}; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1425 | setupWithPackagesAndMultiProcess(packages, enabledByDefault); |
Gustav Sennton | b265016 | 2017-04-07 14:41:38 +0100 | [diff] [blame] | 1426 | mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */, |
| 1427 | true /* valid */, true /* installed */, null /* signatures */, |
| 1428 | 10 /* lastUpdateTime*/, false /* not hidden */, 1000 /* versionCode */, |
| 1429 | false /* isSystemApp */)); |
| 1430 | |
| 1431 | runWebViewBootPreparationOnMainSync(); |
| 1432 | checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation phase */); |
| 1433 | |
| 1434 | mTestSystemImpl.setMultiProcessSetting(null /* context */, settingValue); |
| 1435 | |
| 1436 | assertEquals(expectEnabled, mWebViewUpdateServiceImpl.isMultiProcessEnabled()); |
| 1437 | } |
| 1438 | |
| 1439 | |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 1440 | /** |
| 1441 | * Ensure that packages with a targetSdkVersion targeting the current platform are valid, and |
| 1442 | * that packages targeting an older version are not valid. |
| 1443 | */ |
| 1444 | @Test |
| 1445 | public void testTargetSdkVersionValidity() { |
| 1446 | PackageInfo newSdkPackage = createPackageInfo("newTargetSdkPackage", |
| 1447 | true /* enabled */, true /* valid */, true /* installed */); |
| 1448 | newSdkPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 1449 | PackageInfo currentSdkPackage = createPackageInfo("currentTargetSdkPackage", |
| 1450 | true /* enabled */, true /* valid */, true /* installed */); |
Gustav Sennton | e5468f7 | 2017-11-20 19:42:43 +0000 | [diff] [blame] | 1451 | currentSdkPackage.applicationInfo.targetSdkVersion = UserPackage.MINIMUM_SUPPORTED_SDK; |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 1452 | PackageInfo oldSdkPackage = createPackageInfo("oldTargetSdkPackage", |
| 1453 | true /* enabled */, true /* valid */, true /* installed */); |
Gustav Sennton | e5468f7 | 2017-11-20 19:42:43 +0000 | [diff] [blame] | 1454 | oldSdkPackage.applicationInfo.targetSdkVersion = UserPackage.MINIMUM_SUPPORTED_SDK - 1; |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 1455 | |
| 1456 | WebViewProviderInfo newSdkProviderInfo = |
| 1457 | new WebViewProviderInfo(newSdkPackage.packageName, "", true, false, null); |
| 1458 | WebViewProviderInfo currentSdkProviderInfo = |
| 1459 | new WebViewProviderInfo(currentSdkPackage.packageName, "", true, false, null); |
| 1460 | WebViewProviderInfo[] packages = new WebViewProviderInfo[] { |
| 1461 | new WebViewProviderInfo(oldSdkPackage.packageName, "", true, false, null), |
| 1462 | currentSdkProviderInfo, newSdkProviderInfo}; |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 1463 | setupWithPackages(packages); |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 1464 | ; |
| 1465 | mTestSystemImpl.setPackageInfo(newSdkPackage); |
| 1466 | mTestSystemImpl.setPackageInfo(currentSdkPackage); |
| 1467 | mTestSystemImpl.setPackageInfo(oldSdkPackage); |
| 1468 | |
| 1469 | assertArrayEquals(new WebViewProviderInfo[]{currentSdkProviderInfo, newSdkProviderInfo}, |
| 1470 | mWebViewUpdateServiceImpl.getValidWebViewPackages()); |
| 1471 | |
| 1472 | runWebViewBootPreparationOnMainSync(); |
| 1473 | |
| 1474 | checkPreparationPhasesForPackage(currentSdkPackage.packageName, |
| 1475 | 1 /* first preparation phase */); |
| 1476 | } |
Gustav Sennton | 53b7824 | 2016-04-07 15:56:10 +0100 | [diff] [blame] | 1477 | } |