Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 53b78241 authored by Gustav Sennton's avatar Gustav Sennton
Browse files

Add initial unit tests for WebViewUpdateService.

The logic in the WebViewUpdateService is now more complex and there
are lots of edge cases that should be tested to make sure we don't
regress anything.

Also refrain from running fallback logic on boot if fallback logic not
enabled (bug found through a failing test, yeeah buddy!).

Catch uncaught MissingWebViewException (very timing-dependent!) and add
test for this.

Add tests for:
Some package setups (single package, non-default vs. default packages,
    valid vs. invalid packages).
Ensure correct error codes used
Switching provider through settings-method
Switching provider through adding more prioritized provider
Switching provider in the middle of already running webview preparation
Ensure fallback logic is run when enabled and not run when disabled (at
    boot, packageStateChanged, and when adding a new user).

Bug: 27635535

Change-Id: I275ecb0f6129f71258da0fa053e7f7b36a675886
parent b026b6a4
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -119,6 +119,8 @@ public class WebViewUpdateServiceImpl {
    }

    private void updateFallbackStateOnBoot() {
        if (!mSystemInterface.isFallbackLogicEnabled()) return;

        WebViewProviderInfo[] webviewProviders = mSystemInterface.getWebViewPackages();
        updateFallbackState(webviewProviders, true);
    }
@@ -497,8 +499,15 @@ public class WebViewUpdateServiceImpl {
                    mWebViewPackageDirty = false;
                    // If we have changed provider since we started the relro creation we need to
                    // redo the whole process using the new package instead.
                    try {
                        PackageInfo newPackage = findPreferredWebViewPackage();
                        onWebViewProviderChanged(newPackage);
                    } catch (WebViewFactory.MissingWebViewPackageException e) {
                        // If we can't find any valid WebView package we are now in a state where
                        // mAnyWebViewInstalled is false, so loading WebView will be blocked and we
                        // should simply wait until we receive an intent declaring a new package was
                        // installed.
                    }
                } else {
                    mLock.notifyAll();
                }
+112 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.webkit;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.webkit.WebViewProviderInfo;

import java.util.HashMap;

public class TestSystemImpl implements SystemInterface {
    private String mUserProvider = "";
    private final WebViewProviderInfo[] mPackageConfigs;
    HashMap<String, PackageInfo> mPackages = new HashMap();
    private boolean mFallbackLogicEnabled;
    private final int mNumRelros;
    private final boolean mIsDebuggable;

    public TestSystemImpl(WebViewProviderInfo[] packageConfigs, boolean fallbackLogicEnabled,
            int numRelros, boolean isDebuggable) {
        mPackageConfigs = packageConfigs;
        mFallbackLogicEnabled = fallbackLogicEnabled;
        mNumRelros = numRelros;
        mIsDebuggable = isDebuggable;
    }

    @Override
    public WebViewProviderInfo[] getWebViewPackages() {
        return mPackageConfigs;
    }

    @Override
    public int onWebViewProviderChanged(PackageInfo packageInfo) {
        return mNumRelros;
    }

    @Override
    public String getUserChosenWebViewProvider(Context context) { return mUserProvider; }

    @Override
    public void updateUserSetting(Context context, String newProviderName) {
        mUserProvider = newProviderName;
    }

    @Override
    public void killPackageDependents(String packageName) {}

    @Override
    public boolean isFallbackLogicEnabled() {
        return mFallbackLogicEnabled;
    }

    @Override
    public void enableFallbackLogic(boolean enable) {
        mFallbackLogicEnabled = enable;
    }

    @Override
    public void uninstallAndDisablePackageForAllUsers(Context context, String packageName) {
        enablePackageForAllUsers(context, packageName, false);
    }

    @Override
    public void enablePackageForAllUsers(Context context, String packageName, boolean enable) {
        enablePackageForUser(packageName, enable, 0);
    }

    @Override
    public void enablePackageForUser(String packageName, boolean enable, int userId) {
        PackageInfo packageInfo = mPackages.get(packageName);
        if (packageInfo == null) {
            throw new IllegalArgumentException("There is no package called " + packageName);
        }
        packageInfo.applicationInfo.enabled = enable;
        setPackageInfo(packageInfo);
    }

    @Override
    public boolean systemIsDebuggable() { return mIsDebuggable; }

    @Override
    public PackageInfo getPackageInfoForProvider(WebViewProviderInfo info) throws
            NameNotFoundException {
        PackageInfo ret = mPackages.get(info.packageName);
        if (ret == null) throw new NameNotFoundException(info.packageName);
        return ret;
    }

    public void setPackageInfo(PackageInfo pi) {
        mPackages.put(pi.packageName, pi);
    }

    @Override
    public int getFactoryPackageVersion(String packageName) {
        return 0;
    }
}
+613 −0

File added.

Preview size limit exceeded, changes collapsed.