Introduce WebViewAppPreferenceControllerV2

- Create new WebViewAppPreferenceControllerV2
 - Deprecate WebViewAppPreferenceController
 - Create controller inside the DashboardFragment
 - Port logic from DevelopmentSettings into the controller

Bug: 34203528
Test: make RunSettingsRoboTests -j40
Change-Id: I45bb7beb98ef9c7a998f64e81d180613c795ed3a
This commit is contained in:
jeffreyhuang
2017-10-02 18:18:28 -07:00
parent 498e883d52
commit aa4bf8c266
6 changed files with 227 additions and 2 deletions

View File

@@ -246,7 +246,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

View File

@@ -0,0 +1,101 @@
/*
* 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();
}
}

View File

@@ -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";

View File

@@ -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() {}

View File

@@ -0,0 +1,116 @@
/*
* 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);
}
}

View File

@@ -30,6 +30,10 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
/**
* Deprecated in favor of {@link com.android.settings.development.WebViewAppPreferenceControllerV2}
*/
@Deprecated
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WebViewAppPreferenceControllerTest {