153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton/*
253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * Copyright (C) 2016 The Android Open Source Project
353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton *
453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * Licensed under the Apache License, Version 2.0 (the "License");
553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * you may not use this file except in compliance with the License.
653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * You may obtain a copy of the License at
753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton *
853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton *      http://www.apache.org/licenses/LICENSE-2.0
953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton *
1053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * Unless required by applicable law or agreed to in writing, software
1153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * distributed under the License is distributed on an "AS IS" BASIS,
1253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * See the License for the specific language governing permissions and
1453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * limitations under the License.
1553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton */
1653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
1753b78241ffc32e61d12afc6245633266e592db88Gustav Senntonpackage com.android.server.webkit;
1853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
19564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Senntonimport static org.junit.Assert.assertArrayEquals;
2086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonimport static org.junit.Assert.assertEquals;
2186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonimport static org.junit.Assert.assertFalse;
22dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)import static org.junit.Assert.assertTrue;
2386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton
2453b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.content.Context;
2553b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.content.pm.ApplicationInfo;
2653b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.content.pm.PackageInfo;
2753b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.content.pm.Signature;
28564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Senntonimport android.os.Build;
2953b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.os.Bundle;
3086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonimport android.support.test.InstrumentationRegistry;
3186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonimport android.support.test.runner.AndroidJUnit4;
32148cb3eb64fa81f7d4b2e0f149153edb4924039fGustav Senntonimport android.test.suitebuilder.annotation.MediumTest;
3353b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.util.Base64;
3453b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.webkit.WebViewFactory;
3553b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.webkit.WebViewProviderInfo;
3653b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport android.webkit.WebViewProviderResponse;
3753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
3886f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonimport org.junit.Test;
3986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonimport org.junit.runner.RunWith;
4086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton
4153b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport org.mockito.Mockito;
4253b78241ffc32e61d12afc6245633266e592db88Gustav Senntonimport org.mockito.Matchers;
43192bb0bc54f6bb418f5778fe26eb2e68514290fbPaul Duffinimport org.mockito.compat.ArgumentMatcher;
4453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
45b265016b0c02fde89e392b4637a70317b0a63545Gustav Senntonimport java.lang.Integer;
4686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonimport java.util.concurrent.CountDownLatch;
4786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton
4853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
4953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton/**
5053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton * Tests for WebViewUpdateService
51148cb3eb64fa81f7d4b2e0f149153edb4924039fGustav Sennton runtest --path frameworks/base/services/tests/servicestests/ \
52148cb3eb64fa81f7d4b2e0f149153edb4924039fGustav Sennton     -c com.android.server.webkit.WebViewUpdateServiceTest
5353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton */
54148cb3eb64fa81f7d4b2e0f149153edb4924039fGustav Sennton// Use MediumTest instead of SmallTest as the implementation of WebViewUpdateService
55148cb3eb64fa81f7d4b2e0f149153edb4924039fGustav Sennton// is intended to work on several threads and uses at least one sleep/wait-statement.
5686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton@RunWith(AndroidJUnit4.class)
57148cb3eb64fa81f7d4b2e0f149153edb4924039fGustav Sennton@MediumTest
5886f7bbe134a274a4936b73e2fc2287482ac0157eGustav Senntonpublic class WebViewUpdateServiceTest {
5953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private final static String TAG = WebViewUpdateServiceTest.class.getSimpleName();
6053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
6153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private WebViewUpdateServiceImpl mWebViewUpdateServiceImpl;
6253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private TestSystemImpl mTestSystemImpl;
6353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
6453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private static final String WEBVIEW_LIBRARY_FLAG = "com.android.webview.WebViewLibrary";
6553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
6653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    /**
6753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     * Creates a new instance.
6853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     */
6953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public WebViewUpdateServiceTest() {
7053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
7153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
7253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void setupWithPackages(WebViewProviderInfo[] packages) {
7353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, true);
7453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
7553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
7653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void setupWithPackages(WebViewProviderInfo[] packages,
7753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            boolean fallbackLogicEnabled) {
7853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, fallbackLogicEnabled, 1);
7953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
8053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
8153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void setupWithPackages(WebViewProviderInfo[] packages,
8253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            boolean fallbackLogicEnabled, int numRelros) {
8353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, fallbackLogicEnabled, numRelros,
8453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                true /* isDebuggable == true -> don't check package signatures */);
8553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
8653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
8753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void setupWithPackages(WebViewProviderInfo[] packages,
8853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            boolean fallbackLogicEnabled, int numRelros, boolean isDebuggable) {
89dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        setupWithPackages(packages, fallbackLogicEnabled, numRelros, isDebuggable,
90dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                false /* multiProcessDefault */);
91dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)    }
92dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
93dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)    private void setupWithPackages(WebViewProviderInfo[] packages,
94dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)            boolean fallbackLogicEnabled, int numRelros, boolean isDebuggable,
95dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)            boolean multiProcessDefault) {
9653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        TestSystemImpl testing = new TestSystemImpl(packages, fallbackLogicEnabled, numRelros,
97dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                isDebuggable, multiProcessDefault);
9853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl = Mockito.spy(testing);
9953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl =
10053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewUpdateServiceImpl(null /*Context*/, mTestSystemImpl);
10153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
10253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
10353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void setEnabledAndValidPackageInfos(WebViewProviderInfo[] providers) {
104364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Set package infos for the primary user (user 0).
105364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, providers);
106364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
107364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
108364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    private void setEnabledAndValidPackageInfosForUser(int userId,
109364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            WebViewProviderInfo[] providers) {
11053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        for(WebViewProviderInfo wpi : providers) {
111364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfoForUser(userId, createPackageInfo(wpi.packageName,
112364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    true /* enabled */, true /* valid */, true /* installed */));
11353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
11453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
11553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
116a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    private void checkCertainPackageUsedAfterWebViewBootPreparation(String expectedProviderName,
11753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            WebViewProviderInfo[] webviewPackages) {
118a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(
119a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                expectedProviderName, webviewPackages, 1);
12053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
12153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
122a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    private void checkCertainPackageUsedAfterWebViewBootPreparation(String expectedProviderName,
12353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            WebViewProviderInfo[] webviewPackages, int numRelros) {
12453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(webviewPackages, true, numRelros);
12553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Add (enabled and valid) package infos for each provider
12653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setEnabledAndValidPackageInfos(webviewPackages);
12753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
12886f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
12953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
13053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).onWebViewProviderChanged(
13153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(expectedProviderName)));
13253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
13353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        for (int n = 0; n < numRelros; n++) {
13453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            mWebViewUpdateServiceImpl.notifyRelroCreationCompleted();
13553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
13653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
13753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
13853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status);
13953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(expectedProviderName, response.packageInfo.packageName);
14053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
14153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
14253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // For matching the package name of a PackageInfo
14353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private class IsPackageInfoWithName extends ArgumentMatcher<PackageInfo> {
14453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        private final String mPackageName;
14553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
14653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        IsPackageInfoWithName(String name) {
14753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            mPackageName = name;
14853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
14953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
15053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        @Override
151192bb0bc54f6bb418f5778fe26eb2e68514290fbPaul Duffin        public boolean matchesObject(Object p) {
15253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            return ((PackageInfo) p).packageName.equals(mPackageName);
15353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
15453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
15553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        @Override
156192bb0bc54f6bb418f5778fe26eb2e68514290fbPaul Duffin        public String toString() {
157192bb0bc54f6bb418f5778fe26eb2e68514290fbPaul Duffin            return String.format("PackageInfo with name '%s'", mPackageName);
15853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
15953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
16053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
16153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private static PackageInfo createPackageInfo(
1620df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            String packageName, boolean enabled, boolean valid, boolean installed) {
16353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        PackageInfo p = new PackageInfo();
16453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        p.packageName = packageName;
16553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        p.applicationInfo = new ApplicationInfo();
16653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        p.applicationInfo.enabled = enabled;
16753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        p.applicationInfo.metaData = new Bundle();
1680df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        if (installed) {
1690df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            p.applicationInfo.flags |= ApplicationInfo.FLAG_INSTALLED;
1700df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        } else {
1710df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            p.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
1720df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        }
17353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        if (valid) {
17453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            // no flag means invalid
17553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            p.applicationInfo.metaData.putString(WEBVIEW_LIBRARY_FLAG, "blah");
17653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
177564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        // Default to this package being valid in terms of targetSdkVersion.
178564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        p.applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
17953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        return p;
18053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
18153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
1820df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    private static PackageInfo createPackageInfo(String packageName, boolean enabled, boolean valid,
1830df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            boolean installed, Signature[] signatures, long updateTime) {
1840df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        PackageInfo p = createPackageInfo(packageName, enabled, valid, installed);
18553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        p.signatures = signatures;
1860df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        p.lastUpdateTime = updateTime;
1870df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        return p;
1880df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
1890df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
1900df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    private static PackageInfo createPackageInfo(String packageName, boolean enabled, boolean valid,
1910df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            boolean installed, Signature[] signatures, long updateTime, boolean hidden) {
1920df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        PackageInfo p =
1930df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            createPackageInfo(packageName, enabled, valid, installed, signatures, updateTime);
1940df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        if (hidden) {
1950df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            p.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_HIDDEN;
1960df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        } else {
1970df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            p.applicationInfo.privateFlags &= ~ApplicationInfo.PRIVATE_FLAG_HIDDEN;
1980df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        }
19953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        return p;
20053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
20153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
20218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    private static PackageInfo createPackageInfo(String packageName, boolean enabled, boolean valid,
203fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton            boolean installed, Signature[] signatures, long updateTime, boolean hidden,
204fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton            int versionCode, boolean isSystemApp) {
205fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton        PackageInfo p = createPackageInfo(packageName, enabled, valid, installed, signatures,
206fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton                updateTime, hidden);
20718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        p.versionCode = versionCode;
20818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        p.applicationInfo.versionCode = versionCode;
20918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        if (isSystemApp) p.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
21018c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        return p;
21118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
21218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
213a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    private void checkPreparationPhasesForPackage(String expectedPackage, int numPreparation) {
214a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Verify that onWebViewProviderChanged was called for the numPreparation'th time for the
215a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // expected package
216a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.times(numPreparation)).onWebViewProviderChanged(
217a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(expectedPackage)));
218a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
219a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mWebViewUpdateServiceImpl.notifyRelroCreationCompleted();
220a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
221a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
222a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status);
223a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        assertEquals(expectedPackage, response.packageInfo.packageName);
224a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    }
225a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
22686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    /**
22786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton     * The WebView preparation boot phase is run on the main thread (especially on a thread with a
22886f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton     * looper) so to avoid bugs where our tests fail because a looper hasn't been attached to the
22986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton     * thread running prepareWebViewInSystemServer we run it on the main thread.
23086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton     */
23186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    private void runWebViewBootPreparationOnMainSync() {
23286f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
23386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton            @Override
23486f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton            public void run() {
23586f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton                mWebViewUpdateServiceImpl.prepareWebViewInSystemServer();
23686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton            }
23786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        });
23886f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    }
23986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton
24053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
24153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // ****************
24253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // Tests
24353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // ****************
24453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
24553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
24686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
24753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testWithSinglePackage() {
24853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String testPackageName = "test.package.name";
249a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(testPackageName,
25053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                new WebViewProviderInfo[] {
25153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    new WebViewProviderInfo(testPackageName, "",
25253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                            true /*default available*/, false /* fallback */, null)});
25353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
25453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
25586f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
25653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testDefaultPackageUsedOverNonDefault() {
25753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String defaultPackage = "defaultPackage";
25853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String nonDefaultPackage = "nonDefaultPackage";
25953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
26053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(nonDefaultPackage, "", false, false, null),
26153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(defaultPackage, "", true, false, null)};
262a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(defaultPackage, packages);
26353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
26453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
26586f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
26653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testSeveralRelros() {
26753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String singlePackage = "singlePackage";
268a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(
26953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                singlePackage,
27053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                new WebViewProviderInfo[] {
27153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    new WebViewProviderInfo(singlePackage, "", true /*def av*/, false, null)},
27253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                2);
27353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
27453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
27553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // Ensure that package with valid signatures is chosen rather than package with invalid
27653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // signatures.
27786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
27853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testWithSignatures() {
27953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String validPackage = "valid package";
28053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String invalidPackage = "invalid package";
28153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
28253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Signature validSignature = new Signature("11");
28353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Signature invalidExpectedSignature = new Signature("22");
28453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Signature invalidPackageSignature = new Signature("33");
28553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
28653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
28753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(invalidPackage, "", true, false, new String[]{
28853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                        Base64.encodeToString(
28953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                                invalidExpectedSignature.toByteArray(), Base64.DEFAULT)}),
29053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(validPackage, "", true, false, new String[]{
29153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                        Base64.encodeToString(
29253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                                validSignature.toByteArray(), Base64.DEFAULT)})
29353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        };
29453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, true /* fallback logic enabled */, 1 /* numRelros */,
29553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                false /* isDebuggable */);
29653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(invalidPackage, true /* enabled */,
2970df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */, new Signature[]{invalidPackageSignature}
2980df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    , 0 /* updateTime */));
29953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(validPackage, true /* enabled */,
3000df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */, new Signature[]{validSignature}
3010df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    , 0 /* updateTime */));
30253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
30386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
30453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
30553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
306a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(validPackage, 1 /* first preparation for this package */);
30753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
30853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] validPackages = mWebViewUpdateServiceImpl.getValidWebViewPackages();
30953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(1, validPackages.length);
31053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(validPackage, validPackages[0].packageName);
31153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
31253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
31386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
31453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testFailWaitingForRelro() {
31553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
31653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo("packagename", "", true, true, null)};
31753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages);
31853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setEnabledAndValidPackageInfos(packages);
31953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
32086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
32153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
32253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).onWebViewProviderChanged(
32353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(packages[0].packageName)));
32453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
32553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Never call notifyRelroCreation()
32653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
32753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
32853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_FAILED_WAITING_FOR_RELRO, response.status);
32953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
33053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
33186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
33253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testFailListingEmptyWebviewPackages() {
33353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[0];
33453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages);
33553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setEnabledAndValidPackageInfos(packages);
33653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
33786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
33853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
33953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.never()).onWebViewProviderChanged(
34053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Matchers.anyObject());
34153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
34253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
34353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status);
344bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(null, mWebViewUpdateServiceImpl.getCurrentWebViewPackage());
345bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
346bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        // Now install a package
347bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        String singlePackage = "singlePackage";
348bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        packages = new WebViewProviderInfo[]{
349bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton            new WebViewProviderInfo(singlePackage, "", true, false, null)};
350bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        setupWithPackages(packages);
351bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        setEnabledAndValidPackageInfos(packages);
352bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
353bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(singlePackage,
354364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID);
355bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
356bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        checkPreparationPhasesForPackage(singlePackage, 1 /* number of finished preparations */);
357bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(singlePackage,
358bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName);
359bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
360bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        // Remove the package again
361bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        mTestSystemImpl.removePackageInfo(singlePackage);
362bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(singlePackage,
363364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID);
364bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
365bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        // Package removed - ensure our interface states that there is no package
366bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
367bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status);
368bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(null, mWebViewUpdateServiceImpl.getCurrentWebViewPackage());
36953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
37053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
37186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
37253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testFailListingInvalidWebviewPackage() {
37395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        WebViewProviderInfo wpi = new WebViewProviderInfo("package", "", true, true, null);
37453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {wpi};
37553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages);
37695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mTestSystemImpl.setPackageInfo(
3770df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                createPackageInfo(wpi.packageName, true /* enabled */, false /* valid */,
3780df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* installed */));
37953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
38086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
38195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
38295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.never()).onWebViewProviderChanged(
38395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                Matchers.anyObject());
38495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
38553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
38653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status);
38795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
38895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Verify that we can recover from failing to list webview packages.
38995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mTestSystemImpl.setPackageInfo(
3900df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                createPackageInfo(wpi.packageName, true /* enabled */, true /* valid */,
3910df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* installed */));
39295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(wpi.packageName,
393364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID);
39495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
39595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(wpi.packageName, 1);
39653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
39753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
39853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // Test that switching provider using changeProviderAndSetting works.
39986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
40053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testSwitchingProvider() {
40153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String firstPackage = "first";
40253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String secondPackage = "second";
40353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
40453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true, false, null),
40553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true, false, null)};
40653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkSwitchingProvider(packages, firstPackage, secondPackage);
40753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
40853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
40986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
41053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testSwitchingProviderToNonDefault() {
41153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String defaultPackage = "defaultPackage";
41253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String nonDefaultPackage = "nonDefaultPackage";
41353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
41453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(defaultPackage, "", true, false, null),
41553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(nonDefaultPackage, "", false, false, null)};
41653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkSwitchingProvider(packages, defaultPackage, nonDefaultPackage);
41753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
41853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
41953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void checkSwitchingProvider(WebViewProviderInfo[] packages, String initialPackage,
42053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            String finalPackage) {
421a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(initialPackage, packages);
42253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
42353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.changeProviderAndSetting(finalPackage);
424a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(finalPackage, 1 /* first preparation for this package */);
42553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
42653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(initialPackage));
42753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
42853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
42953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // Change provider during relro creation by using changeProviderAndSetting
43086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
43153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testSwitchingProviderDuringRelroCreation() {
43253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkChangingProviderDuringRelroCreation(true);
43353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
43453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
43553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    // Change provider during relro creation by enabling a provider
43686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
43753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testChangingProviderThroughEnablingDuringRelroCreation() {
43853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkChangingProviderDuringRelroCreation(false);
43953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
44053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
44153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void checkChangingProviderDuringRelroCreation(boolean settingsChange) {
44253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String firstPackage = "first";
44353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String secondPackage = "second";
44453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
44553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true, false, null),
44653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true, false, null)};
44753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages);
448364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Have all packages be enabled, so that we can change provider however we want to
449364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfos(packages);
45053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
45153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        CountDownLatch countdown = new CountDownLatch(1);
45253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
45386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
45453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
45553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).onWebViewProviderChanged(
45653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(firstPackage)));
45753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
458bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage,
459bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName);
46053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
46153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        new Thread(new Runnable() {
46253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            @Override
46353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            public void run() {
46453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                WebViewProviderResponse threadResponse =
46553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    mWebViewUpdateServiceImpl.waitForAndGetProvider();
46653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                assertEquals(WebViewFactory.LIBLOAD_SUCCESS, threadResponse.status);
46753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                assertEquals(secondPackage, threadResponse.packageInfo.packageName);
468364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                // Verify that we killed the first package if we performed a settings change -
469364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                // otherwise we had to disable the first package, in which case its dependents
470364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                // should have been killed by the framework.
471364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                if (settingsChange) {
472364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(firstPackage));
473364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                }
47453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                countdown.countDown();
47553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            }
47653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }).start();
47753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        try {
4780df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            Thread.sleep(500); // Let the new thread run / be blocked
47953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        } catch (InterruptedException e) {
48053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
48153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
48253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        if (settingsChange) {
48353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage);
48453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        } else {
485364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Enable the second provider
48653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */,
4870df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                        true /* valid */, true /* installed */));
48853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            mWebViewUpdateServiceImpl.packageStateChanged(
489364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    secondPackage, WebViewUpdateService.PACKAGE_CHANGED, TestSystemImpl.PRIMARY_USER_ID);
490364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
491364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Ensure we haven't changed package yet.
492364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            assertEquals(firstPackage,
493364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName);
494364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
495364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Switch provider by disabling the first one
496364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, false /* enabled */,
497364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                        true /* valid */, true /* installed */));
498364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mWebViewUpdateServiceImpl.packageStateChanged(
499364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    firstPackage, WebViewUpdateService.PACKAGE_CHANGED, TestSystemImpl.PRIMARY_USER_ID);
50053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
50153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.notifyRelroCreationCompleted();
50253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // first package done, should start on second
50353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
50453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).onWebViewProviderChanged(
50553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(secondPackage)));
50653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
50753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.notifyRelroCreationCompleted();
50853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // second package done, the other thread should now be unblocked
50953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        try {
51053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            countdown.await();
51153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        } catch (InterruptedException e) {
51253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
51353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
51453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
51586f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
51653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testRunFallbackLogicIfEnabled() {
51753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkFallbackLogicBeingRun(true);
51853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
51953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
52086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
52153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testDontRunFallbackLogicIfDisabled() {
52253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkFallbackLogicBeingRun(false);
52353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
52453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
52553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    private void checkFallbackLogicBeingRun(boolean fallbackLogicEnabled) {
52653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String primaryPackage = "primary";
52753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String fallbackPackage = "fallback";
52853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
52953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
53053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    primaryPackage, "", true /* default available */, false /* fallback */, null),
53153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
53253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    fallbackPackage, "", true /* default available */, true /* fallback */, null)};
53353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, fallbackLogicEnabled);
53453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setEnabledAndValidPackageInfos(packages);
53553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
53686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
53753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Verify that we disable the fallback package if fallback logic enabled, and don't disable
53853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // the fallback package if that logic is disabled
53953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        if (fallbackLogicEnabled) {
54053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            Mockito.verify(mTestSystemImpl).uninstallAndDisablePackageForAllUsers(
54153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Matchers.anyObject(), Mockito.eq(fallbackPackage));
54253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        } else {
54353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            Mockito.verify(mTestSystemImpl, Mockito.never()).uninstallAndDisablePackageForAllUsers(
54453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Matchers.anyObject(), Matchers.anyObject());
54553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
54653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).onWebViewProviderChanged(
54753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(primaryPackage)));
54853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
54953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Enable fallback package
55053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(fallbackPackage, true /* enabled */,
5510df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                        true /* valid */, true /* installed */));
55253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(
553364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                fallbackPackage, WebViewUpdateService.PACKAGE_CHANGED, TestSystemImpl.PRIMARY_USER_ID);
55453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
55553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        if (fallbackLogicEnabled) {
55653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            // Check that we have now disabled the fallback package twice
55753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            Mockito.verify(mTestSystemImpl, Mockito.times(2)).uninstallAndDisablePackageForAllUsers(
55853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Matchers.anyObject(), Mockito.eq(fallbackPackage));
55953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        } else {
56053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            // Check that we still haven't disabled any package
56153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            Mockito.verify(mTestSystemImpl, Mockito.never()).uninstallAndDisablePackageForAllUsers(
56253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Matchers.anyObject(), Matchers.anyObject());
56353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
56453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
56553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
56653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    /**
56753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     * Scenario for installing primary package when fallback enabled.
56853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     * 1. Start with only fallback installed
56953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     * 2. Install non-fallback
57053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     * 3. Fallback should be disabled
57153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     */
57286f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
57353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testInstallingNonFallbackPackage() {
57453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String primaryPackage = "primary";
57553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String fallbackPackage = "fallback";
57653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
57753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
57853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    primaryPackage, "", true /* default available */, false /* fallback */, null),
57953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
58053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    fallbackPackage, "", true /* default available */, true /* fallback */, null)};
58153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, true /* isFallbackLogicEnabled */);
58253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(
5830df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                createPackageInfo(fallbackPackage, true /* enabled */ , true /* valid */,
5840df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* installed */));
58553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
58686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
58753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.never()).uninstallAndDisablePackageForAllUsers(
58853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Matchers.anyObject(), Matchers.anyObject());
58953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
590a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(fallbackPackage,
591a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                1 /* first preparation for this package*/);
59253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
59353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Install primary package
59453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(
5950df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                createPackageInfo(primaryPackage, true /* enabled */ , true /* valid */,
5960df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* installed */));
59753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
598364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID);
59953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
600a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Verify fallback disabled, primary package used as provider, and fallback package killed
60153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).uninstallAndDisablePackageForAllUsers(
60253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Matchers.anyObject(), Mockito.eq(fallbackPackage));
603a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation for this package*/);
60453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(fallbackPackage));
60553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
60653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
60786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
608364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    public void testFallbackChangesEnabledStateSingleUser() {
609364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        for (PackageRemovalType removalType : REMOVAL_TYPES) {
610364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            checkFallbackChangesEnabledState(false /* multiUser */, removalType);
611364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        }
612364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
613364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
614364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    @Test
615364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    public void testFallbackChangesEnabledStateMultiUser() {
616364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        for (PackageRemovalType removalType : REMOVAL_TYPES) {
617364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            checkFallbackChangesEnabledState(true /* multiUser */, removalType);
618364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        }
619364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
620364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
621364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    /**
622364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * Represents how to remove a package during a tests (disabling it / uninstalling it / hiding
623364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * it).
624364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     */
625364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    private enum PackageRemovalType {
626364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        UNINSTALL, DISABLE, HIDE
627364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
628364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
629364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    private PackageRemovalType[] REMOVAL_TYPES = PackageRemovalType.class.getEnumConstants();
630364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
631364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    public void checkFallbackChangesEnabledState(boolean multiUser,
632364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            PackageRemovalType removalType) {
63353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String primaryPackage = "primary";
63453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String fallbackPackage = "fallback";
63553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
63653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
63753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    primaryPackage, "", true /* default available */, false /* fallback */, null),
63853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
63953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    fallbackPackage, "", true /* default available */, true /* fallback */, null)};
64053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, true /* fallbackLogicEnabled */);
641364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        int secondaryUserId = 10;
642364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        int userIdToChangePackageFor = multiUser ? secondaryUserId : TestSystemImpl.PRIMARY_USER_ID;
643364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        if (multiUser) {
644364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.addUser(secondaryUserId);
645364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            setEnabledAndValidPackageInfosForUser(secondaryUserId, packages);
646364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        }
647364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, packages);
64853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
64986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
65053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
65153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Verify fallback disabled at boot when primary package enabled
652364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkEnablePackageForUserCalled(fallbackPackage, false, multiUser
653364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                ? new int[] {TestSystemImpl.PRIMARY_USER_ID, secondaryUserId}
654364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                : new int[] {TestSystemImpl.PRIMARY_USER_ID}, 1 /* numUsages */);
65553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
65695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(primaryPackage, 1);
65795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
658364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        boolean enabled = !(removalType == PackageRemovalType.DISABLE);
659364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        boolean installed = !(removalType == PackageRemovalType.UNINSTALL);
660364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        boolean hidden = (removalType == PackageRemovalType.HIDE);
66195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Disable primary package and ensure fallback becomes enabled and used
662364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(userIdToChangePackageFor,
663364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                createPackageInfo(primaryPackage, enabled /* enabled */, true /* valid */,
664364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    installed /* installed */, null /* signature */, 0 /* updateTime */,
665364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    hidden /* hidden */));
66653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
667364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                removalType == PackageRemovalType.DISABLE
668364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                ? WebViewUpdateService.PACKAGE_CHANGED : WebViewUpdateService.PACKAGE_REMOVED,
669364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                userIdToChangePackageFor); // USER ID
67053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
671364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkEnablePackageForUserCalled(fallbackPackage, true, multiUser
672364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                ? new int[] {TestSystemImpl.PRIMARY_USER_ID, secondaryUserId}
673364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                : new int[] {TestSystemImpl.PRIMARY_USER_ID}, 1 /* numUsages */);
67453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
67595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(fallbackPackage, 1);
67695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
67795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
67895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Again enable primary package and verify primary is used and fallback becomes disabled
679364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(userIdToChangePackageFor,
6800df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                createPackageInfo(primaryPackage, true /* enabled */, true /* valid */,
6810df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* installed */));
68253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
683364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                removalType == PackageRemovalType.DISABLE
684364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                ? WebViewUpdateService.PACKAGE_CHANGED : WebViewUpdateService.PACKAGE_ADDED,
685364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                userIdToChangePackageFor);
68653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
68753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Verify fallback is disabled a second time when primary package becomes enabled
688364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkEnablePackageForUserCalled(fallbackPackage, false, multiUser
689364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                ? new int[] {TestSystemImpl.PRIMARY_USER_ID, secondaryUserId}
690364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                : new int[] {TestSystemImpl.PRIMARY_USER_ID}, 2 /* numUsages */);
69195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
69295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(primaryPackage, 2);
69353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
69453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
695364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    private void checkEnablePackageForUserCalled(String packageName, boolean expectEnabled,
696364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            int[] userIds, int numUsages) {
697364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        for (int userId : userIds) {
698364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            Mockito.verify(mTestSystemImpl, Mockito.times(numUsages)).enablePackageForUser(
699364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    Mockito.eq(packageName), Mockito.eq(expectEnabled), Mockito.eq(userId));
700364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        }
701364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
702364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
70386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
70453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testAddUserWhenFallbackLogicEnabled() {
70553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkAddingNewUser(true);
70653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
70753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
70886f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
70953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testAddUserWhenFallbackLogicDisabled() {
71053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        checkAddingNewUser(false);
71153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
71253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
71353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void checkAddingNewUser(boolean fallbackLogicEnabled) {
71453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String primaryPackage = "primary";
71553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String fallbackPackage = "fallback";
71653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
71753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
71853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    primaryPackage, "", true /* default available */, false /* fallback */, null),
71953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(
72053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    fallbackPackage, "", true /* default available */, true /* fallback */, null)};
72153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages, fallbackLogicEnabled);
722364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, packages);
72353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        int newUser = 100;
724364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.addUser(newUser);
725364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfosForUser(newUser, packages);
72653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.handleNewUser(newUser);
72753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        if (fallbackLogicEnabled) {
72853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            // Verify fallback package becomes disabled for new user
72953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            Mockito.verify(mTestSystemImpl).enablePackageForUser(
73053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Mockito.eq(fallbackPackage), Mockito.eq(false) /* enable */,
73153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Mockito.eq(newUser));
73253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        } else {
73353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            // Verify that we don't disable fallback for new user
73453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            Mockito.verify(mTestSystemImpl, Mockito.never()).enablePackageForUser(
73553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Mockito.anyObject(), Matchers.anyBoolean() /* enable */,
73653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    Matchers.anyInt() /* user */);
73753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        }
73853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
73953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
74053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    /**
741364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * Ensures that adding a new user for which the current WebView package is uninstalled causes a
742364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * change of WebView provider.
743364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     */
744364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    @Test
745364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    public void testAddingNewUserWithUninstalledPackage() {
746364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        String primaryPackage = "primary";
747364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        String fallbackPackage = "fallback";
748364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
749364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            new WebViewProviderInfo(
750364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    primaryPackage, "", true /* default available */, false /* fallback */, null),
751364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            new WebViewProviderInfo(
752364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    fallbackPackage, "", true /* default available */, true /* fallback */, null)};
753364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setupWithPackages(packages, true /* fallbackLogicEnabled */);
754364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, packages);
755364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        int newUser = 100;
756364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.addUser(newUser);
757364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Let the primary package be uninstalled for the new user
758364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(newUser,
759364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                createPackageInfo(primaryPackage, true /* enabled */, true /* valid */,
760364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                        false /* installed */));
761364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(newUser,
762364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                createPackageInfo(fallbackPackage, false /* enabled */, true /* valid */,
763364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                        true /* installed */));
764364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mWebViewUpdateServiceImpl.handleNewUser(newUser);
765364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Verify fallback package doesn't become disabled for the primary user.
766364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.never()).enablePackageForUser(
767364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                Mockito.anyObject(), Mockito.eq(false) /* enable */,
768364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                Mockito.eq(TestSystemImpl.PRIMARY_USER_ID) /* user */);
769364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Verify that we enable the fallback package for the secondary user.
770364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.times(1)).enablePackageForUser(
771364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                Mockito.eq(fallbackPackage), Mockito.eq(true) /* enable */,
772364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                Mockito.eq(newUser) /* user */);
773364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkPreparationPhasesForPackage(fallbackPackage, 1 /* numRelros */);
774364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
775364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
776364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    /**
77753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     * Timing dependent test where we verify that the list of valid webview packages becoming empty
77853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     * at a certain point doesn't crash us or break our state.
77953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton     */
78086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
78153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    public void testNotifyRelroDoesntCrashIfNoPackages() {
78253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String firstPackage = "first";
78353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        String secondPackage = "second";
78453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
78553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true /* default available */,
78653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    false /* fallback */, null),
78753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true /* default available */,
78853b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                    false /* fallback */, null)};
78953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setupWithPackages(packages);
79053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Add (enabled and valid) package infos for each provider
79153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        setEnabledAndValidPackageInfos(packages);
79253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
79386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
79453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
79553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        Mockito.verify(mTestSystemImpl).onWebViewProviderChanged(
79653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(firstPackage)));
79753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
79895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Change provider during relro creation to enter a state where we are
79995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // waiting for relro creation to complete just to re-run relro creation.
80095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // (so that in next notifyRelroCreationCompleted() call we have to list webview packages)
80153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage);
80253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
80353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Make packages invalid to cause exception to be thrown
80453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, true /* enabled */,
8050df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* valid */, true /* installed */, null /* signatures */,
8060df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    0 /* updateTime */));
80753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */,
8080df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* valid */, true /* installed */));
80953b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
81053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // This shouldn't throw an exception!
81153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.notifyRelroCreationCompleted();
81253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
81353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
81453b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status);
81553b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
81653b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        // Now make a package valid again and verify that we can switch back to that
81753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, true /* enabled */,
8180df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */, null /* signatures */,
8190df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    1 /* updateTime */ ));
82053b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
82153b78241ffc32e61d12afc6245633266e592db88Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(firstPackage,
822364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID);
82353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
824a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Ensure we use firstPackage
825a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(firstPackage, 2 /* second preparation for this package */);
826a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    }
82753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
828a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    /**
829a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton     * Verify that even if a user-chosen package is removed temporarily we start using it again when
830a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton     * it is added back.
831a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton     */
83286f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
833a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    public void testTempRemovePackageDoesntSwitchProviderPermanently() {
834a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        String firstPackage = "first";
835a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        String secondPackage = "second";
836a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
837a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true /* default available */,
838a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                    false /* fallback */, null),
839a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true /* default available */,
840a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                    false /* fallback */, null)};
841a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages);
84253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
843a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Explicitly use the second package
844a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage);
845a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(secondPackage, 1 /* first time for this package */);
846a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
847a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Remove second package (invalidate it) and verify that first package is used
848a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */,
8490df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* valid */, true /* installed */));
850a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(secondPackage,
851364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID);
852a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(firstPackage, 2 /* second time for this package */);
853a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
854a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Now make the second package valid again and verify that it is used again
855a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */,
8560df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */));
857a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(secondPackage,
858364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED, TestSystemImpl.PRIMARY_USER_ID);
859a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(secondPackage, 2 /* second time for this package */);
860a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    }
861a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
862a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    /**
863a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton     * Ensure that we update the user-chosen setting across boots if the chosen package is no
864a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton     * longer installed and valid.
865a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton     */
86686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
867a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton    public void testProviderSettingChangedDuringBootIfProviderNotAvailable() {
868a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        String chosenPackage = "chosenPackage";
869a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        String nonChosenPackage = "non-chosenPackage";
870a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
871a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton            new WebViewProviderInfo(chosenPackage, "", true /* default available */,
872a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                    false /* fallback */, null),
873a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton            new WebViewProviderInfo(nonChosenPackage, "", true /* default available */,
874a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                    false /* fallback */, null)};
875a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
876a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        setupWithPackages(packages);
877a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Only 'install' nonChosenPackage
878a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mTestSystemImpl.setPackageInfo(
8790df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                createPackageInfo(nonChosenPackage, true /* enabled */, true /* valid */, true /* installed */));
880a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
881a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Set user-chosen package
882a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        mTestSystemImpl.updateUserSetting(null, chosenPackage);
883a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
88486f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
885a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
886a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        // Verify that we switch the setting to point to the current package
887a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        Mockito.verify(mTestSystemImpl).updateUserSetting(
888a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton                Mockito.anyObject(), Mockito.eq(nonChosenPackage));
889a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        assertEquals(nonChosenPackage, mTestSystemImpl.getUserChosenWebViewProvider(null));
890a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton
891a9159040c86d94f7a3d45bdc7fe6c4ea24f58456Gustav Sennton        checkPreparationPhasesForPackage(nonChosenPackage, 1);
89253b78241ffc32e61d12afc6245633266e592db88Gustav Sennton    }
89353b78241ffc32e61d12afc6245633266e592db88Gustav Sennton
89486f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
89595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void testRecoverFailedListingWebViewPackagesSettingsChange() {
89695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkRecoverAfterFailListingWebviewPackages(true);
89795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
89895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
89986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
90095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void testRecoverFailedListingWebViewPackagesAddedPackage() {
90195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkRecoverAfterFailListingWebviewPackages(false);
90295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
90395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
90495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    /**
90595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton     * Test that we can recover correctly from failing to list WebView packages.
90695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton     * settingsChange: whether to fail during changeProviderAndSetting or packageStateChanged
90795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton     */
90895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void checkRecoverAfterFailListingWebviewPackages(boolean settingsChange) {
90995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String firstPackage = "first";
91095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String secondPackage = "second";
91195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
91295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true /* default available */,
91395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null),
91495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true /* default available */,
91595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null)};
91695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages);
91795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
91895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Make both packages invalid so that we fail listing WebView packages
91995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(firstPackage, true /* enabled */,
9200df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* valid */, true /* installed */));
92195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */,
9220df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* valid */, true /* installed */));
92395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
92495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Change package to hit the webview packages listing problem.
92595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        if (settingsChange) {
92695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage);
92795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        } else {
92895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            mWebViewUpdateServiceImpl.packageStateChanged(secondPackage,
929364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID);
93095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        }
93195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
93295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
93395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status);
93495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
93595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Make second package valid and verify that we can load it again
93695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(secondPackage, true /* enabled */,
9370df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */));
93895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
93995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(secondPackage,
940364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID);
94195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
94295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
94395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(secondPackage, 1);
94495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
94595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
94686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
94795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void testDontKillIfPackageReplaced() {
94895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkDontKillIfPackageRemoved(true);
94995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
95095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
95186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
95295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void testDontKillIfPackageRemoved() {
95395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkDontKillIfPackageRemoved(false);
95495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
95595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
95695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void checkDontKillIfPackageRemoved(boolean replaced) {
95795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String firstPackage = "first";
95895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String secondPackage = "second";
95995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
96095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true /* default available */,
96195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null),
96295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true /* default available */,
96395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null)};
96495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages);
96595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
96695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Replace or remove the current webview package
96795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        if (replaced) {
96895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            mTestSystemImpl.setPackageInfo(
9690df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    createPackageInfo(firstPackage, true /* enabled */, false /* valid */,
9700df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                        true /* installed */));
97195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            mWebViewUpdateServiceImpl.packageStateChanged(firstPackage,
972364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    WebViewUpdateService.PACKAGE_ADDED_REPLACED, TestSystemImpl.PRIMARY_USER_ID);
97395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        } else {
97495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            mTestSystemImpl.removePackageInfo(firstPackage);
97595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            mWebViewUpdateServiceImpl.packageStateChanged(firstPackage,
976364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    WebViewUpdateService.PACKAGE_REMOVED, TestSystemImpl.PRIMARY_USER_ID);
97795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        }
97895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
97995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(secondPackage, 1);
98095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
98195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.never()).killPackageDependents(
98295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                Mockito.anyObject());
98395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
98495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
98586f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
98695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void testKillIfSettingChanged() {
98795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String firstPackage = "first";
98895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String secondPackage = "second";
98995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
99095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true /* default available */,
99195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null),
99295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true /* default available */,
99395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null)};
99495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkCertainPackageUsedAfterWebViewBootPreparation(firstPackage, packages);
99595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
99695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage);
99795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
99895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(secondPackage, 1);
99995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
100095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(firstPackage));
100195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
100295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
100395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    /**
100495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton     * Test that we kill apps using an old provider when we change the provider setting, even if the
100595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton     * new provider is not the one we intended to change to.
100695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton     */
100786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
100895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    public void testKillIfChangeProviderIncorrectly() {
100995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String firstPackage = "first";
101095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String secondPackage = "second";
101195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        String thirdPackage = "third";
101295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
101395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(firstPackage, "", true /* default available */,
101495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null),
101595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(secondPackage, "", true /* default available */,
101695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null),
101795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton            new WebViewProviderInfo(thirdPackage, "", true /* default available */,
101895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                    false /* fallback */, null)};
101995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        setupWithPackages(packages);
102095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        setEnabledAndValidPackageInfos(packages);
102195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
102295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Start with the setting pointing to the third package
102395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mTestSystemImpl.updateUserSetting(null, thirdPackage);
102495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
102586f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
102695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(thirdPackage, 1);
102795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
102895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        mTestSystemImpl.setPackageInfo(
10290df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                createPackageInfo(secondPackage, true /* enabled */, false /* valid */, true /* installed */));
103095f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
103195f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // Try to switch to the invalid second package, this should result in switching to the first
103295f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        // package, since that is more preferred than the third one.
103395f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        assertEquals(firstPackage,
103495f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton                mWebViewUpdateServiceImpl.changeProviderAndSetting(secondPackage));
103595f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
103695f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        checkPreparationPhasesForPackage(firstPackage, 1);
103795f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton
103895f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton        Mockito.verify(mTestSystemImpl).killPackageDependents(Mockito.eq(thirdPackage));
103995f7e8e06dd0003a813f7a4b3c718c68d0fa4a01Gustav Sennton    }
104018c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1041ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
104218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    public void testLowerPackageVersionNotValid() {
1043826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{200000} /* system version */, 100000/* candidate version */,
1044826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                false /* expected validity */);
104518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
104618c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1047ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
104818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    public void testEqualPackageVersionValid() {
1049826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{100000} /* system version */, 100000 /* candidate version */,
1050826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                true /* expected validity */);
105118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
105218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1053ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
105418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    public void testGreaterPackageVersionValid() {
1055826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{100000} /* system versions */, 200000 /* candidate version */,
1056826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                true /* expected validity */);
105718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
105818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1059ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
1060826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    public void testLastFiveDigitsIgnored() {
1061826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{654321} /* system version */, 612345 /* candidate version */,
1062826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                true /* expected validity */);
106318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
106418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1065ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
1066826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    public void testMinimumSystemVersionUsedTwoDefaultsCandidateValid() {
1067826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{300000, 100000} /* system versions */,
1068826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                200000 /* candidate version */, true /* expected validity */);
106918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
107018c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1071ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
1072826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    public void testMinimumSystemVersionUsedTwoDefaultsCandidateInvalid() {
1073826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{300000, 200000} /* system versions */,
1074826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                 100000 /* candidate version */, false /* expected validity */);
107518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
107618c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1077ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
1078826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    public void testMinimumSystemVersionUsedSeveralDefaultsCandidateValid() {
1079826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{100000, 200000, 300000, 400000, 500000} /* system versions */,
1080826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                100000 /* candidate version */, true /* expected validity */);
1081826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    }
1082826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu
1083ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
1084826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    public void testMinimumSystemVersionUsedSeveralDefaultsCandidateInvalid() {
1085826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{200000, 300000, 400000, 500000, 600000} /* system versions */,
1086826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                100000 /* candidate version */, false /* expected validity */);
108718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
108818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1089ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
109018c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    public void testMinimumSystemVersionUsedFallbackIgnored() {
1091826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{300000, 400000, 500000, 600000, 700000} /* system versions */,
1092826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                200000 /* candidate version */, false /* expected validity */, true /* add fallback */,
1093826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                100000 /* fallback version */, false /* expected validity of fallback */);
109418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
109518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1096ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
1097826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    public void testFallbackValid() {
1098826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(new int[]{300000, 400000, 500000, 600000, 700000} /* system versions */,
1099826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                200000/* candidate version */, false /* expected validity */, true /* add fallback */,
1100826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                300000 /* fallback version */, true /* expected validity of fallback */);
1101826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    }
1102826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu
1103826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    private void checkPackageVersions(int[] systemVersions, int candidateVersion,
1104826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            boolean candidateShouldBeValid) {
1105826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        checkPackageVersions(systemVersions, candidateVersion, candidateShouldBeValid,
1106826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                false, 0, false);
110718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
110818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
110918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    /**
111018c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton     * Utility method for checking that package version restriction works as it should.
111118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton     * I.e. that a package with lower version than the system-default is not valid and that a
111218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton     * package with greater than or equal version code is considered valid.
111318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton     */
1114826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu    private void checkPackageVersions(int[] systemVersions, int candidateVersion,
1115826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            boolean candidateShouldBeValid, boolean addFallback, int fallbackVersion,
1116826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            boolean fallbackShouldBeValid) {
111718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        int numSystemPackages = systemVersions.length;
111818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        int numFallbackPackages = (addFallback ? 1 : 0);
111918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        int numPackages = systemVersions.length + 1 + numFallbackPackages;
1120826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        String candidatePackage = "candidatePackage";
112118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        String systemPackage = "systemPackage";
112218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        String fallbackPackage = "fallbackPackage";
112318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
112418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        // Each package needs a valid signature since we set isDebuggable to false
112518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        Signature signature = new Signature("11");
112618c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        String encodedSignatureString =
112718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            Base64.encodeToString(signature.toByteArray(), Base64.DEFAULT);
112818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
112918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        // Set up config
1130826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        // 1. candidatePackage
113118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        // 2-N. default available non-fallback packages
113218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        // N+1. default available fallback package
113318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[numPackages];
1134826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        packages[0] = new WebViewProviderInfo(candidatePackage, "",
113518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                false /* available by default */, false /* fallback */,
113618c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                new String[]{encodedSignatureString});
113718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        for(int n = 1; n < numSystemPackages + 1; n++) {
113818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            packages[n] = new WebViewProviderInfo(systemPackage + n, "",
113918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                    true /* available by default */, false /* fallback */,
114018c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                    new String[]{encodedSignatureString});
114118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        }
114218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        if (addFallback) {
114318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            packages[packages.length-1] = new WebViewProviderInfo(fallbackPackage, "",
114418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                    true /* available by default */, true /* fallback */,
114518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                    new String[]{encodedSignatureString});
114618c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        }
114718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
114818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        setupWithPackages(packages, true /* fallback logic enabled */, 1 /* numRelros */,
114918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                false /* isDebuggable */);
115018c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
115118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        // Set package infos
115218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        mTestSystemImpl.setPackageInfo(
1153826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                createPackageInfo(candidatePackage, true /* enabled */, true /* valid */,
1154fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton                    true /* installed */, new Signature[]{signature}, 0 /* updateTime */,
1155fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton                    false /* hidden */, candidateVersion, false /* isSystemApp */));
115618c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        for(int n = 1; n < numSystemPackages + 1; n++) {
115718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            mTestSystemImpl.setPackageInfo(
115818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                    createPackageInfo(systemPackage + n, true /* enabled */, true /* valid */,
1159fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton                        true /* installed */, new Signature[]{signature}, 0 /* updateTime */,
1160fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton                        false /* hidden */, systemVersions[n-1], true /* isSystemApp */));
116118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        }
116218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        if (addFallback) {
116318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            mTestSystemImpl.setPackageInfo(
116418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton                    createPackageInfo(fallbackPackage, true /* enabled */, true /* valid */,
1165fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton                        true /* installed */, new Signature[]{signature}, 0 /* updateTime */,
1166fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton                        false /* hidden */, fallbackVersion, true /* isSystemApp */));
116718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        }
116818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
116918c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        WebViewProviderInfo[] validPackages = mWebViewUpdateServiceImpl.getValidWebViewPackages();
1170826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        int expectedNumValidPackages = numSystemPackages;
1171826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        if (candidateShouldBeValid) {
1172826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            expectedNumValidPackages++;
1173826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        } else {
1174826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            // Ensure the candidate package is not one of the valid packages
1175826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            for(int n = 0; n < validPackages.length; n++) {
1176826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                assertFalse(candidatePackage.equals(validPackages[n].packageName));
1177826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            }
1178826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        }
1179826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu
1180826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        if (fallbackShouldBeValid) {
1181826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            expectedNumValidPackages += numFallbackPackages;
118218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        } else {
1183826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            // Ensure the fallback package is not one of the valid packages
118418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            for(int n = 0; n < validPackages.length; n++) {
1185826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu                assertFalse(fallbackPackage.equals(validPackages[n].packageName));
118618c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            }
118718c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        }
118818c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1189826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        assertEquals(expectedNumValidPackages, validPackages.length);
1190826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu
1191ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)        runWebViewBootPreparationOnMainSync();
119218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
119318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        // The non-system package is not available by default so it shouldn't be used here
119418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        checkPreparationPhasesForPackage(systemPackage + "1", 1);
119518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton
1196826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        // Try explicitly switching to the candidate package
1197826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        String packageChange = mWebViewUpdateServiceImpl.changeProviderAndSetting(candidatePackage);
1198826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu        if (candidateShouldBeValid) {
1199826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            assertEquals(candidatePackage, packageChange);
1200826a24b2961901bff8d53b4f448efe07a8db2646Hui Shu            checkPreparationPhasesForPackage(candidatePackage, 1);
120118c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        } else {
120218c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            assertEquals(systemPackage + "1", packageChange);
120318c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton            // We didn't change package so the webview preparation won't run here
120418c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton        }
120518c9e151fdf8676a8ad8f66a0bde592c0c1644d3Gustav Sennton    }
1206fd222fc2f8c27ebea1bad050f6fd61ab3b954ea2Gustav Sennton
1207364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    /**
1208364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * Ensure that the update service does use an uninstalled package when that is the only
1209364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * package available.
1210364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     */
121186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
12120df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testWithSingleUninstalledPackage() {
12130df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String testPackageName = "test.package.name";
12140df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] {
12150df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                new WebViewProviderInfo(testPackageName, "",
12160df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                        true /*default available*/, false /* fallback */, null)};
12170df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        setupWithPackages(webviewPackages, true /* fallback logic enabled */, 1 /* numRelros */);
12180df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(testPackageName, true /* enabled */,
12190df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, false /* installed */));
12200df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
122186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
12220df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
1223963dbbbdb616646240dde9a4b2b7b7592ddfefaaGustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.never()).onWebViewProviderChanged(
1224963dbbbdb616646240dde9a4b2b7b7592ddfefaaGustav Sennton                Matchers.anyObject());
1225963dbbbdb616646240dde9a4b2b7b7592ddfefaaGustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
1226963dbbbdb616646240dde9a4b2b7b7592ddfefaaGustav Sennton        assertEquals(WebViewFactory.LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES, response.status);
1227963dbbbdb616646240dde9a4b2b7b7592ddfefaaGustav Sennton        assertEquals(null, mWebViewUpdateServiceImpl.getCurrentWebViewPackage());
12280df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
12290df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
123086f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
12310df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testNonhiddenPackageUserOverHidden() {
1232364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkVisiblePackageUserOverNonVisible(false /* multiUser*/, PackageRemovalType.HIDE);
1233364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkVisiblePackageUserOverNonVisible(true /* multiUser*/, PackageRemovalType.HIDE);
12340df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
12350df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
123686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
12370df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testInstalledPackageUsedOverUninstalled() {
1238364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkVisiblePackageUserOverNonVisible(false /* multiUser*/, PackageRemovalType.UNINSTALL);
1239364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkVisiblePackageUserOverNonVisible(true /* multiUser*/, PackageRemovalType.UNINSTALL);
12400df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
12410df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
1242364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    private void checkVisiblePackageUserOverNonVisible(boolean multiUser,
1243364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            PackageRemovalType removalType) {
1244364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        assert removalType != PackageRemovalType.DISABLE;
1245364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        boolean testUninstalled = removalType == PackageRemovalType.UNINSTALL;
1246364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        boolean testHidden = removalType == PackageRemovalType.HIDE;
12470df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String installedPackage = "installedPackage";
12480df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String uninstalledPackage = "uninstalledPackage";
12490df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] {
12500df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(uninstalledPackage, "", true /* available by default */,
12510df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* fallback */, null),
12520df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(installedPackage, "", true /* available by default */,
12530df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* fallback */, null)};
12540df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
12550df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        setupWithPackages(webviewPackages, true /* fallback logic enabled */, 1 /* numRelros */);
1256364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        int secondaryUserId = 5;
1257364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        if (multiUser) {
1258364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.addUser(secondaryUserId);
1259364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Install all packages for the primary user.
1260364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, webviewPackages);
1261364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(
1262364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    installedPackage, true /* enabled */, true /* valid */, true /* installed */));
1263364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Hide or uninstall the primary package for the second user
1264364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfo(createPackageInfo(uninstalledPackage, true /* enabled */,
1265364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    true /* valid */, (testUninstalled ? false : true) /* installed */,
1266364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    null /* signatures */, 0 /* updateTime */, (testHidden ? true : false)));
1267364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        } else {
1268364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfo(createPackageInfo(installedPackage, true /* enabled */,
12690df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */));
1270364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Hide or uninstall the primary package
1271364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfo(createPackageInfo(uninstalledPackage, true /* enabled */,
12720df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, (testUninstalled ? false : true) /* installed */,
12730df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    null /* signatures */, 0 /* updateTime */, (testHidden ? true : false)));
1274364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        }
12750df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
127686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
12770df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
12780df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkPreparationPhasesForPackage(installedPackage, 1 /* first preparation phase */);
12790df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
12800df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
128186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
12820df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testCantSwitchToHiddenPackage () {
12830df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkCantSwitchToNonVisiblePackage(false /* true == uninstalled, false == hidden */);
12840df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
12850df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
12860df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
128786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
12880df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testCantSwitchToUninstalledPackage () {
12890df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkCantSwitchToNonVisiblePackage(true /* true == uninstalled, false == hidden */);
12900df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
12910df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
12920df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    /**
1293364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * Ensure that we won't prioritize an uninstalled (or hidden) package even if it is user-chosen.
12940df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton     */
12950df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    private void checkCantSwitchToNonVisiblePackage(boolean uninstalledNotHidden) {
12960df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        boolean testUninstalled = uninstalledNotHidden;
12970df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        boolean testHidden = !uninstalledNotHidden;
12980df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String installedPackage = "installedPackage";
12990df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String uninstalledPackage = "uninstalledPackage";
13000df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] {
13010df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(uninstalledPackage, "", true /* available by default */,
13020df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* fallback */, null),
13030df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(installedPackage, "", true /* available by default */,
13040df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* fallback */, null)};
13050df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
13060df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        setupWithPackages(webviewPackages, true /* fallback logic enabled */, 1 /* numRelros */);
1307364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        int secondaryUserId = 412;
1308364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.addUser(secondaryUserId);
1309364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
1310364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Let all packages be installed and enabled for the primary user.
1311364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, webviewPackages);
1312364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Only uninstall the 'uninstalled package' for the secondary user.
1313364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(installedPackage,
1314364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                true /* enabled */, true /* valid */, true /* installed */));
1315364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(uninstalledPackage,
1316364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                true /* enabled */, true /* valid */, !testUninstalled /* installed */,
1317364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                null /* signatures */, 0 /* updateTime */, testHidden /* hidden */));
13180df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
131986f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
13200df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
13210df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkPreparationPhasesForPackage(installedPackage, 1 /* first preparation phase */);
13220df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
13230df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // ensure that we don't switch to the uninstalled package (it will be used if it becomes
13240df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // installed later)
13250df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        assertEquals(installedPackage,
13260df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                mWebViewUpdateServiceImpl.changeProviderAndSetting(uninstalledPackage));
13270df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
1328364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        // Ensure both packages are considered valid.
1329364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        assertEquals(2, mWebViewUpdateServiceImpl.getValidWebViewPackages().length);
1330364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
1331364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
13320df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // We should only have called onWebViewProviderChanged once (before calling
13330df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // changeProviderAndSetting
13340df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.times(1)).onWebViewProviderChanged(
13350df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(installedPackage)));
13360df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
13370df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
133886f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
13390df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testHiddenPackageNotPrioritizedEvenIfChosen() {
13400df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkNonvisiblePackageNotPrioritizedEvenIfChosen(
13410df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                false /* true == uninstalled, false == hidden */);
13420df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
13430df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
134486f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
13450df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testUninstalledPackageNotPrioritizedEvenIfChosen() {
13460df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkNonvisiblePackageNotPrioritizedEvenIfChosen(
13470df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                true /* true == uninstalled, false == hidden */);
13480df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
13490df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
13500df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void checkNonvisiblePackageNotPrioritizedEvenIfChosen(boolean uninstalledNotHidden) {
13510df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        boolean testUninstalled = uninstalledNotHidden;
13520df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        boolean testHidden = !uninstalledNotHidden;
13530df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String installedPackage = "installedPackage";
13540df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String uninstalledPackage = "uninstalledPackage";
13550df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        WebViewProviderInfo[] webviewPackages = new WebViewProviderInfo[] {
13560df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(uninstalledPackage, "", true /* available by default */,
13570df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* fallback */, null),
13580df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(installedPackage, "", true /* available by default */,
13590df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    false /* fallback */, null)};
13600df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
13610df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        setupWithPackages(webviewPackages, true /* fallback logic enabled */, 1 /* numRelros */);
1362364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        int secondaryUserId = 4;
1363364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.addUser(secondaryUserId);
1364364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
1365364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, webviewPackages);
1366364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(installedPackage,
1367364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                true /* enabled */, true /* valid */, true /* installed */));
1368364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        mTestSystemImpl.setPackageInfoForUser(secondaryUserId, createPackageInfo(uninstalledPackage,
1369364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                true /* enabled */, true /* valid */,
1370364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                (testUninstalled ? false : true) /* installed */, null /* signatures */,
1371364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                0 /* updateTime */, (testHidden ? true : false) /* hidden */));
13720df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
13730df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // Start with the setting pointing to the uninstalled package
13740df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mTestSystemImpl.updateUserSetting(null, uninstalledPackage);
13750df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
137686f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
13770df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
13780df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkPreparationPhasesForPackage(installedPackage, 1 /* first preparation phase */);
13790df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
13800df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
1381364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    @Test
1382364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    public void testFallbackEnabledIfPrimaryUninstalledSingleUser() {
1383364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkFallbackEnabledIfPrimaryUninstalled(false /* multiUser */);
1384364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
1385364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
1386364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    @Test
1387364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    public void testFallbackEnabledIfPrimaryUninstalledMultiUser() {
1388364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        checkFallbackEnabledIfPrimaryUninstalled(true /* multiUser */);
1389364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    }
1390364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton
13910df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    /**
1392364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton     * Ensures that fallback becomes enabled at boot if the primary package is uninstalled for some
13930df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton     * user.
13940df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton     */
1395364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton    private void checkFallbackEnabledIfPrimaryUninstalled(boolean multiUser) {
13960df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String primaryPackage = "primary";
13970df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String fallbackPackage = "fallback";
13980df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
13990df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(
14000df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    primaryPackage, "", true /* default available */, false /* fallback */, null),
14010df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(
14020df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    fallbackPackage, "", true /* default available */, true /* fallback */, null)};
14030df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        setupWithPackages(packages, true /* fallback logic enabled */);
1404364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        int secondaryUserId = 5;
1405364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        if (multiUser) {
1406364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.addUser(secondaryUserId);
1407364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Install all packages for the primary user.
1408364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            setEnabledAndValidPackageInfosForUser(TestSystemImpl.PRIMARY_USER_ID, packages);
1409364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            // Only install fallback package for secondary user.
1410364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfoForUser(secondaryUserId,
1411364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    createPackageInfo(primaryPackage, true /* enabled */,
1412364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                            true /* valid */, false /* installed */));
1413364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfoForUser(secondaryUserId,
1414364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                    createPackageInfo(fallbackPackage, false /* enabled */,
1415364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton                            true /* valid */, true /* installed */));
1416364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        } else {
1417364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */,
14180df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, false /* installed */));
1419364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton            mTestSystemImpl.setPackageInfo(createPackageInfo(fallbackPackage, false /* enabled */,
14200df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */));
1421364e16029017a4e16ed727a5e501f70363d04e5aGustav Sennton        }
14220df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
142386f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
14240df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // Verify that we enable the fallback package
14250df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        Mockito.verify(mTestSystemImpl).enablePackageForAllUsers(
14260df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                Mockito.anyObject(), Mockito.eq(fallbackPackage), Mockito.eq(true) /* enable */);
14270df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14280df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkPreparationPhasesForPackage(fallbackPackage, 1 /* first preparation phase */);
14290df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
14300df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
143186f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton    @Test
14320df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    public void testPreparationRunsIffNewPackage() {
14330df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String primaryPackage = "primary";
14340df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        String fallbackPackage = "fallback";
14350df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
14360df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(
14370df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    primaryPackage, "", true /* default available */, false /* fallback */, null),
14380df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton            new WebViewProviderInfo(
14390df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    fallbackPackage, "", true /* default available */, true /* fallback */, null)};
14400df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        setupWithPackages(packages, true /* fallback logic enabled */);
14410df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */,
14420df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */, null /* signatures */,
14430df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    10 /* lastUpdateTime*/ ));
14440df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(fallbackPackage, true /* enabled */,
14450df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */));
14460df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
144786f7bbe134a274a4936b73e2fc2287482ac0157eGustav Sennton        runWebViewBootPreparationOnMainSync();
14480df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14490df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation phase */);
14500df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.times(1)).enablePackageForUser(
14510df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                Mockito.eq(fallbackPackage), Mockito.eq(false) /* enable */,
14520df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                Matchers.anyInt() /* user */);
14530df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14540df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14550df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
14560df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, 0 /* userId */);
14570df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
14580df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, 1 /* userId */);
14590df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
14600df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, 2 /* userId */);
14610df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // package still has the same update-time so we shouldn't run preparation here
14620df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.times(1)).onWebViewProviderChanged(
14630df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(primaryPackage)));
14640df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        Mockito.verify(mTestSystemImpl, Mockito.times(1)).enablePackageForUser(
14650df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                Mockito.eq(fallbackPackage), Mockito.eq(false) /* enable */,
14660df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                Matchers.anyInt() /* user */);
14670df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14680df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // Ensure we can still load the package
14690df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
14700df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status);
14710df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        assertEquals(primaryPackage, response.packageInfo.packageName);
14720df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14730df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14740df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */,
14750df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */, null /* signatures */,
14760df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    20 /* lastUpdateTime*/ ));
14770df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
14780df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, 0);
14790df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // The package has now changed - ensure that we have run the preparation phase a second time
14800df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkPreparationPhasesForPackage(primaryPackage, 2 /* second preparation phase */);
14810df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14820df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14830df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */,
14840df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    true /* valid */, true /* installed */, null /* signatures */,
14850df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                    50 /* lastUpdateTime*/ ));
14860df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        // Receive intent for different user
14870df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        mWebViewUpdateServiceImpl.packageStateChanged(primaryPackage,
14880df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton                WebViewUpdateService.PACKAGE_ADDED_REPLACED, 2);
14890df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton
14900df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton        checkPreparationPhasesForPackage(primaryPackage, 3 /* third preparation phase */);
14910df2c5566a9d7d71ca84cb072114f051bbe7a344Gustav Sennton    }
1492bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
1493ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)    @Test
1494bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton    public void testGetCurrentWebViewPackage() {
1495bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        PackageInfo firstPackage = createPackageInfo("first", true /* enabled */,
1496bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                        true /* valid */, true /* installed */);
1497bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        firstPackage.versionCode = 100;
1498bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        firstPackage.versionName = "first package version";
1499bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
1500bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton            new WebViewProviderInfo(firstPackage.packageName, "", true, false, null)};
1501bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        setupWithPackages(packages, true);
1502bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        mTestSystemImpl.setPackageInfo(firstPackage);
1503bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
1504ab5d0ba22ee2dcd90c6ba12b1c5ce94b7783bacbTorne (Richard Coles)        runWebViewBootPreparationOnMainSync();
1505bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
1506bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        Mockito.verify(mTestSystemImpl).onWebViewProviderChanged(
1507bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                Mockito.argThat(new IsPackageInfoWithName(firstPackage.packageName)));
1508bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
1509bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        mWebViewUpdateServiceImpl.notifyRelroCreationCompleted();
1510bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
1511bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        // Ensure the API is correct before running waitForAndGetProvider
1512bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage.packageName,
1513bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName);
1514bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage.versionCode,
1515bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().versionCode);
1516bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage.versionName,
1517bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().versionName);
1518bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
1519bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        WebViewProviderResponse response = mWebViewUpdateServiceImpl.waitForAndGetProvider();
1520bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(WebViewFactory.LIBLOAD_SUCCESS, response.status);
1521bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage.packageName, response.packageInfo.packageName);
1522bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton
1523bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        // Ensure the API is still correct after running waitForAndGetProvider
1524bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage.packageName,
1525bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().packageName);
1526bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage.versionCode,
1527bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().versionCode);
1528bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton        assertEquals(firstPackage.versionName,
1529bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton                mWebViewUpdateServiceImpl.getCurrentWebViewPackage().versionName);
1530bf683e07c350ed7cd2c8e877b447e1dd41863a94Gustav Sennton    }
1531dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1532dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)    @Test
1533dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)    public void testMultiProcessEnabledByDefault() {
1534b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefault(true /* enabledByDefault */);
1535b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    }
1536b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton
1537b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    @Test
1538b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    public void testMultiProcessDisabledByDefault() {
1539b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefault(false /* enabledByDefault */);
1540b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    }
1541b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton
1542b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    private void testMultiProcessByDefault(boolean enabledByDefault) {
1543dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        String primaryPackage = "primary";
1544dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
1545dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)            new WebViewProviderInfo(
1546dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    primaryPackage, "", true /* default available */, false /* fallback */, null)};
1547dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        setupWithPackages(packages, true /* fallback logic enabled */, 1 /* numRelros */,
1548b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                          true /* debuggable */,
1549b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                          enabledByDefault /* not multiprocess by default */);
1550dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */,
1551dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    true /* valid */, true /* installed */, null /* signatures */,
1552dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    10 /* lastUpdateTime*/, false /* not hidden */, 1000 /* versionCode */,
1553dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    false /* isSystemApp */));
1554dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1555dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        runWebViewBootPreparationOnMainSync();
1556dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation phase */);
1557dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1558b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        // Check it's off by default
1559b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        assertEquals(enabledByDefault, mWebViewUpdateServiceImpl.isMultiProcessEnabled());
1560dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1561dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        // Test toggling it
1562b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        mWebViewUpdateServiceImpl.enableMultiProcess(!enabledByDefault);
1563b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        assertEquals(!enabledByDefault, mWebViewUpdateServiceImpl.isMultiProcessEnabled());
1564b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        mWebViewUpdateServiceImpl.enableMultiProcess(enabledByDefault);
1565b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        assertEquals(enabledByDefault, mWebViewUpdateServiceImpl.isMultiProcessEnabled());
1566b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    }
1567dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1568b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    @Test
1569b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    public void testMultiProcessEnabledByDefaultWithSettingsValue() {
1570b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1571b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                true /* enabledByDefault */, Integer.MIN_VALUE, false /* expectEnabled */);
1572b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1573b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                true /* enabledByDefault */, -999999, true /* expectEnabled */);
1574b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1575b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                true /* enabledByDefault */, 0, true /* expectEnabled */);
1576b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1577b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                true /* enabledByDefault */, 999999, true /* expectEnabled */);
1578dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)    }
1579dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1580dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)    @Test
1581b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    public void testMultiProcessDisabledByDefaultWithSettingsValue() {
1582b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1583b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                false /* enabledByDefault */, Integer.MIN_VALUE, false /* expectEnabled */);
1584b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1585b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                false /* enabledByDefault */, 0, false /* expectEnabled */);
1586b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1587b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                false /* enabledByDefault */, 999999, false /* expectEnabled */);
1588b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        testMultiProcessByDefaultWithSettingsValue(
1589b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                false /* enabledByDefault */, Integer.MAX_VALUE, true /* expectEnabled */);
1590b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    }
1591b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton
1592b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    /**
1593b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton     * Test the logic of the multiprocess setting depending on whether multiprocess is enabled by
1594b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton     * default, and what the setting is set to.
1595b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton     * @param enabledByDefault whether multiprocess is enabled by default.
1596b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton     * @param settingValue value of the multiprocess setting.
1597b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton     */
1598b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton    private void testMultiProcessByDefaultWithSettingsValue(
1599b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton            boolean enabledByDefault, int settingValue, boolean expectEnabled) {
1600dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        String primaryPackage = "primary";
1601dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
1602dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)            new WebViewProviderInfo(
1603dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    primaryPackage, "", true /* default available */, false /* fallback */, null)};
1604dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        setupWithPackages(packages, true /* fallback logic enabled */, 1 /* numRelros */,
1605b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton                          true /* debuggable */, enabledByDefault /* multiprocess by default */);
1606dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        mTestSystemImpl.setPackageInfo(createPackageInfo(primaryPackage, true /* enabled */,
1607dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    true /* valid */, true /* installed */, null /* signatures */,
1608dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    10 /* lastUpdateTime*/, false /* not hidden */, 1000 /* versionCode */,
1609dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)                    false /* isSystemApp */));
1610dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1611dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        runWebViewBootPreparationOnMainSync();
1612dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)        checkPreparationPhasesForPackage(primaryPackage, 1 /* first preparation phase */);
1613dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1614b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        mTestSystemImpl.setMultiProcessSetting(null /* context */, settingValue);
1615dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)
1616b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton        assertEquals(expectEnabled, mWebViewUpdateServiceImpl.isMultiProcessEnabled());
1617dc375072c4fc95d612ccd3c60ff0519eb75b804cTorne (Richard Coles)    }
1618564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton
1619b265016b0c02fde89e392b4637a70317b0a63545Gustav Sennton
1620564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton    /**
1621564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton     * Ensure that packages with a targetSdkVersion targeting the current platform are valid, and
1622564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton     * that packages targeting an older version are not valid.
1623564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton     */
1624564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton    @Test
1625564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton    public void testTargetSdkVersionValidity() {
1626564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        PackageInfo newSdkPackage = createPackageInfo("newTargetSdkPackage",
1627564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton            true /* enabled */, true /* valid */, true /* installed */);
1628564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        newSdkPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
1629564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        PackageInfo currentSdkPackage = createPackageInfo("currentTargetSdkPackage",
1630564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton            true /* enabled */, true /* valid */, true /* installed */);
1631564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        currentSdkPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.N_MR1+1;
1632564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        PackageInfo oldSdkPackage = createPackageInfo("oldTargetSdkPackage",
1633564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton            true /* enabled */, true /* valid */, true /* installed */);
1634564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        oldSdkPackage.applicationInfo.targetSdkVersion = Build.VERSION_CODES.N_MR1;
1635564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton
1636564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        WebViewProviderInfo newSdkProviderInfo =
1637564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton                new WebViewProviderInfo(newSdkPackage.packageName, "", true, false, null);
1638564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        WebViewProviderInfo currentSdkProviderInfo =
1639564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton                new WebViewProviderInfo(currentSdkPackage.packageName, "", true, false, null);
1640564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        WebViewProviderInfo[] packages = new WebViewProviderInfo[] {
1641564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton            new WebViewProviderInfo(oldSdkPackage.packageName, "", true, false, null),
1642564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton            currentSdkProviderInfo, newSdkProviderInfo};
1643564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        setupWithPackages(packages, true);
1644564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton;
1645564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        mTestSystemImpl.setPackageInfo(newSdkPackage);
1646564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        mTestSystemImpl.setPackageInfo(currentSdkPackage);
1647564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        mTestSystemImpl.setPackageInfo(oldSdkPackage);
1648564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton
1649564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        assertArrayEquals(new WebViewProviderInfo[]{currentSdkProviderInfo, newSdkProviderInfo},
1650564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton                mWebViewUpdateServiceImpl.getValidWebViewPackages());
1651564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton
1652564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        runWebViewBootPreparationOnMainSync();
1653564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton
1654564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton        checkPreparationPhasesForPackage(currentSdkPackage.packageName,
1655564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton                1 /* first preparation phase */);
1656564c2fd5db707dbe3e10df3e69780c0ab6ce7a51Gustav Sennton    }
165753b78241ffc32e61d12afc6245633266e592db88Gustav Sennton}
1658