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

Commit d0b5c017 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Introduce WebViewAppPreferenceControllerV2"

parents a8ecc390 aa4bf8c2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -254,7 +254,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        // running services
        // convert to file encryption
        controllers.add(new PictureColorModePreferenceController(context, lifecycle));
        // webview implementation
        controllers.add(new WebViewAppPreferenceControllerV2(context));
        controllers.add(new CoolColorTemperaturePreferenceController(context));
        controllers.add(new DisableAutomaticUpdatesPreferenceController(context));
        // system ui demo mode
+101 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.settings.development;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import android.util.Log;

import com.android.settings.R;
import com.android.settings.applications.defaultapps.DefaultAppInfo;
import com.android.settings.webview.WebViewUpdateServiceWrapper;
import com.android.settingslib.wrapper.PackageManagerWrapper;

public class WebViewAppPreferenceControllerV2 extends DeveloperOptionsPreferenceController {

    private static final String TAG = "WebViewAppPrefCtrl";
    private static final String WEBVIEW_APP_KEY = "select_webview_provider";

    private final PackageManagerWrapper mPackageManager;
    private final WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;

    private Preference mPreference;

    public WebViewAppPreferenceControllerV2(Context context) {
        super(context);

        mPackageManager = new PackageManagerWrapper(context.getPackageManager());
        mWebViewUpdateServiceWrapper = new WebViewUpdateServiceWrapper();
    }

    @Override
    public String getPreferenceKey() {
        return WEBVIEW_APP_KEY;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);

        mPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public void updateState(Preference preference) {
        final CharSequence defaultAppLabel = getDefaultAppLabel();
        if (!TextUtils.isEmpty(defaultAppLabel)) {
            mPreference.setSummary(defaultAppLabel);
            mPreference.setIcon(getDefaultAppIcon());
        } else {
            Log.d(TAG, "No default app");
            mPreference.setSummary(R.string.app_list_preference_none);
            mPreference.setIcon(null);
        }
    }

    @Override
    protected void onDeveloperOptionsSwitchEnabled() {
        mPreference.setEnabled(true);
    }

    @Override
    protected void onDeveloperOptionsSwitchDisabled() {
        mPreference.setEnabled(false);
    }

    @VisibleForTesting
    DefaultAppInfo getDefaultAppInfo() {
        final PackageInfo currentPackage = mWebViewUpdateServiceWrapper.getCurrentWebViewPackage();
        return new DefaultAppInfo(mPackageManager,
                currentPackage == null ? null : currentPackage.applicationInfo);
    }

    private Drawable getDefaultAppIcon() {
        final DefaultAppInfo app = getDefaultAppInfo();
        return app.loadIcon();
    }

    private CharSequence getDefaultAppLabel() {
        final DefaultAppInfo app = getDefaultAppInfo();
        return app.loadLabel();
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -22,6 +22,10 @@ import android.support.v7.preference.PreferenceScreen;
import com.android.settings.applications.defaultapps.DefaultAppInfo;
import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;

/**
 * Deprecated in favor of {@link com.android.settings.development.WebViewAppPreferenceControllerV2}
 */
@Deprecated
public class WebViewAppPreferenceController extends DefaultAppPreferenceController {

    private static final String WEBVIEW_APP_KEY = "select_webview_provider";
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ import com.android.settings.wrapper.UserPackageWrapperImpl;
import java.util.ArrayList;
import java.util.List;

class WebViewUpdateServiceWrapper {
public class WebViewUpdateServiceWrapper {
    private static final String TAG = "WVUSWrapper";

    public WebViewUpdateServiceWrapper() {}
+116 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.settings.development;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.applications.defaultapps.DefaultAppInfo;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.webview.WebViewUpdateServiceWrapper;
import com.android.settingslib.wrapper.PackageManagerWrapper;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WebViewAppPreferenceControllerV2Test {

    @Mock
    private PreferenceScreen mPreferenceScreen;
    @Mock
    private PackageManagerWrapper mPackageManager;
    @Mock
    private WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
    @Mock
    private Preference mPreference;
    @Mock
    private DefaultAppInfo mAppInfo;
    @Mock
    private Drawable mDrawable;

    private Context mContext;
    private WebViewAppPreferenceControllerV2 mController;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mController = spy(new WebViewAppPreferenceControllerV2(mContext));
        ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
        ReflectionHelpers.setField(mController, "mWebViewUpdateServiceWrapper",
                mWebViewUpdateServiceWrapper);
        doReturn(mAppInfo).when(mController).getDefaultAppInfo();
        when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
                mPreference);
        mController.displayPreference(mPreferenceScreen);
    }

    @Test
    public void updateState_hasAppLabel_shouldSetAppLabelAndIcon() {
        final String appLabel = "SomeRandomAppLabel!!!";
        when(mAppInfo.loadLabel()).thenReturn(appLabel);
        when(mAppInfo.loadIcon()).thenReturn(mDrawable);

        mController.updateState(mPreference);

        verify(mPreference).setSummary(appLabel);
        verify(mPreference).setIcon(mDrawable);
    }

    @Test
    public void updateState_noAppLabel_shouldSetAppDefaultLabelAndNullIcon() {
        final String appLabel = null;
        when(mAppInfo.loadLabel()).thenReturn(appLabel);
        when(mAppInfo.loadIcon()).thenReturn(mDrawable);

        mController.updateState(mPreference);

        verify(mPreference).setSummary(R.string.app_list_preference_none);
        verify(mPreference).setIcon(null);
    }

    @Test
    public void onDeveloperOptionsSwitchDisabled_preferenceShouldBeDisabled() {
        mController.onDeveloperOptionsSwitchDisabled();

        verify(mPreference).setEnabled(false);
    }

    @Test
    public void onDeveloperOptionsSwitchEnabled_preferenceShouldBeEnabled() {
        mController.onDeveloperOptionsSwitchEnabled();

        verify(mPreference).setEnabled(true);
    }
}
Loading