Update WebView implementation Dev Setting - new looks + descriptions.

Now that we are changing the WebView package mechanism so that a package
can only be used as WebView implementation if that package is enabled
for all users of the device we need to tell the user why a package can't
be chosen as WebView implementation.
With this CL we do so in the 'Set WebView Implementation' Developer
Setting.

We code for the WebView implementation Developer Setting is now based on
the same code as that of the Debug App Developer Setting.

Bug: 32894154
Test: Ensure WebView Implementation setting shows correct packages.
Test: Ensure WebView Implementation setting shows correct descriptions
for why a package is not usable - including the case where packages are
disabled/uninstalled for a second user.
Test: Ensure the summary for the WebView Implmentation setting is
updated after changing WebView package.
Test: Ensure the WebView package Activity
(Settings.ACTION_WEBVIEW_SETTINGS) is similar to the Dev Setting.
Test: ensure non-admin user cannot start WV-picker activity through
'adb shell am start -n com.android.settings/.WebViewImplementation'

Change-Id: Ia6e6e9e12ce8f8f45ec539807cd0c6479acb8ecb
This commit is contained in:
Gustav Sennton
2016-12-13 19:16:48 +00:00
parent d9d463be8b
commit 4d3334c50b
16 changed files with 922 additions and 188 deletions

View File

@@ -0,0 +1,149 @@
/*
* 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.webview;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.UserInfo;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WebViewAppListAdapterTest {
private Context mContext = RuntimeEnvironment.application;
private final static UserInfo FIRST_USER = new UserInfo(0, "FIRST_USER", 0);
private final static UserInfo SECOND_USER = new UserInfo(0, "SECOND_USER", 0);
private final static String DEFAULT_PACKAGE_NAME = "DEFAULT_PACKAGE_NAME";
@Test
public void testDisabledReasonNullIfPackagesOk() {
UserPackageWrapper packageForFirstUser = mock(UserPackageWrapper.class);
when(packageForFirstUser.isEnabledPackage()).thenReturn(true);
when(packageForFirstUser.isInstalledPackage()).thenReturn(true);
UserPackageWrapper packageForSecondUser = mock(UserPackageWrapper.class);
when(packageForSecondUser.isEnabledPackage()).thenReturn(true);
when(packageForSecondUser.isInstalledPackage()).thenReturn(true);
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
when(wvusWrapper.getPackageInfosAllUsers(
any(), eq(DEFAULT_PACKAGE_NAME))).thenReturn(
Arrays.asList(packageForFirstUser, packageForSecondUser));
assertThat(WebViewAppListAdapter.getDisabledReason(
wvusWrapper, mContext, DEFAULT_PACKAGE_NAME)).isNull();
}
@Test
public void testDisabledReasonForSingleUserDisabledPackage() {
UserPackageWrapper packageForFirstUser = mock(UserPackageWrapper.class);
when(packageForFirstUser.isEnabledPackage()).thenReturn(false);
when(packageForFirstUser.isInstalledPackage()).thenReturn(true);
when(packageForFirstUser.getUserInfo()).thenReturn(FIRST_USER);
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME)
)).thenReturn(Arrays.asList(packageForFirstUser));
assertThat(WebViewAppListAdapter.getDisabledReason(wvusWrapper, mContext,
DEFAULT_PACKAGE_NAME)).isEqualTo("Disabled for user " + FIRST_USER.name + "\n");
}
@Test
public void testDisabledReasonForSingleUserUninstalledPackage() {
UserPackageWrapper packageForFirstUser = mock(UserPackageWrapper.class);
when(packageForFirstUser.isEnabledPackage()).thenReturn(true);
when(packageForFirstUser.isInstalledPackage()).thenReturn(false);
when(packageForFirstUser.getUserInfo()).thenReturn(FIRST_USER);
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME)
)).thenReturn(Arrays.asList(packageForFirstUser));
assertThat(WebViewAppListAdapter.getDisabledReason(wvusWrapper, mContext,
DEFAULT_PACKAGE_NAME)).isEqualTo("Uninstalled for user " + FIRST_USER.name + "\n");
}
@Test
public void testDisabledReasonSeveralUsers() {
UserPackageWrapper packageForFirstUser = mock(UserPackageWrapper.class);
when(packageForFirstUser.isEnabledPackage()).thenReturn(false);
when(packageForFirstUser.isInstalledPackage()).thenReturn(true);
when(packageForFirstUser.getUserInfo()).thenReturn(FIRST_USER);
UserPackageWrapper packageForSecondUser = mock(UserPackageWrapper.class);
when(packageForSecondUser.isEnabledPackage()).thenReturn(true);
when(packageForSecondUser.isInstalledPackage()).thenReturn(false);
when(packageForSecondUser.getUserInfo()).thenReturn(SECOND_USER);
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME)
)).thenReturn(Arrays.asList(packageForFirstUser, packageForSecondUser));
final String EXPECTED_DISABLED_REASON = String.format(
"Disabled for user %s\nUninstalled for user %s\n",
FIRST_USER.name, SECOND_USER.name);
assertThat(WebViewAppListAdapter.getDisabledReason(
wvusWrapper, mContext,DEFAULT_PACKAGE_NAME)).isEqualTo(EXPECTED_DISABLED_REASON);
}
/**
* Ensure we only proclaim a package as uninstalled for a certain user if it's both uninstalled
* and disabled.
*/
@Test
public void testDisabledReasonUninstalledAndDisabled() {
UserPackageWrapper packageForFirstUser = mock(UserPackageWrapper.class);
when(packageForFirstUser.isEnabledPackage()).thenReturn(false);
when(packageForFirstUser.isInstalledPackage()).thenReturn(false);
when(packageForFirstUser.getUserInfo()).thenReturn(FIRST_USER);
UserPackageWrapper packageForSecondUser = mock(UserPackageWrapper.class);
when(packageForSecondUser.isEnabledPackage()).thenReturn(true);
when(packageForSecondUser.isInstalledPackage()).thenReturn(true);
when(packageForSecondUser.getUserInfo()).thenReturn(SECOND_USER);
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
when(wvusWrapper.getPackageInfosAllUsers(any(), eq(DEFAULT_PACKAGE_NAME)
)).thenReturn(Arrays.asList(packageForFirstUser, packageForSecondUser));
final String EXPECTED_DISABLED_REASON = String.format(
"Uninstalled for user %s\n", FIRST_USER.name);
assertThat(WebViewAppListAdapter.getDisabledReason(wvusWrapper, mContext,
DEFAULT_PACKAGE_NAME)).isEqualTo(EXPECTED_DISABLED_REASON);
}
}

View File

@@ -0,0 +1,109 @@
/*
* 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.webview;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.shadows.ShadowView.clickOn;
import static org.robolectric.Shadows.shadowOf;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.view.View;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
import org.robolectric.util.ActivityController;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WebViewAppPickerTest {
private static final String DEFAULT_PACKAGE_NAME = "DEFAULT_PACKAGE_NAME";
private static ApplicationInfo createApplicationInfo(String packageName) {
ApplicationInfo ai = new ApplicationInfo();
ai.packageName = packageName;
return ai;
}
@Test
public void testClickingItemChangesProvider() {
ActivityController<WebViewAppPicker> controller =
Robolectric.buildActivity(WebViewAppPicker.class);
WebViewAppPicker webviewAppPicker = controller.get();
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
when(wvusWrapper.getValidWebViewApplicationInfos(any())).thenReturn(
Arrays.asList(createApplicationInfo(DEFAULT_PACKAGE_NAME)));
when(wvusWrapper.setWebViewProvider(eq(DEFAULT_PACKAGE_NAME))).thenReturn(true);
webviewAppPicker.setWebViewUpdateServiceWrapper(wvusWrapper);
controller.create().start().postCreate(null).resume().visible();
WebViewApplicationInfo firstItem =
(WebViewApplicationInfo) webviewAppPicker.getListView().getItemAtPosition(0);
assertThat(firstItem.info.packageName).isEqualTo(DEFAULT_PACKAGE_NAME);
webviewAppPicker.onListItemClick(webviewAppPicker.getListView(), null, 0, 0);
verify(wvusWrapper, times(1)).setWebViewProvider(eq(DEFAULT_PACKAGE_NAME));
assertThat(shadowOf(webviewAppPicker).getResultCode()).isEqualTo(Activity.RESULT_OK);
verify(wvusWrapper, never()).showInvalidChoiceToast(any());
}
@Test
public void testFailingPackageChangeReturnsCancelled() {
ActivityController<WebViewAppPicker> controller =
Robolectric.buildActivity(WebViewAppPicker.class);
WebViewAppPicker webviewAppPicker = controller.get();
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
when(wvusWrapper.getValidWebViewApplicationInfos(any())).thenReturn(
Arrays.asList(createApplicationInfo(DEFAULT_PACKAGE_NAME)));
when(wvusWrapper.setWebViewProvider(eq(DEFAULT_PACKAGE_NAME))).thenReturn(false);
webviewAppPicker.setWebViewUpdateServiceWrapper(wvusWrapper);
controller.create().start().postCreate(null).resume().visible();
WebViewApplicationInfo firstItem =
(WebViewApplicationInfo) webviewAppPicker.getListView().getItemAtPosition(0);
assertThat(firstItem.info.packageName).isEqualTo(DEFAULT_PACKAGE_NAME);
webviewAppPicker.onListItemClick(webviewAppPicker.getListView(), null, 0, 0);
verify(wvusWrapper, times(1)).setWebViewProvider(eq(DEFAULT_PACKAGE_NAME));
assertThat(shadowOf(webviewAppPicker).getResultCode()).isEqualTo(Activity.RESULT_CANCELED);
verify(wvusWrapper, times(1)).showInvalidChoiceToast(any());
}
}

View File

@@ -0,0 +1,83 @@
/*
* 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.webview;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class WebViewAppPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private Preference mPreference;
private static final String DEFAULT_PACKAGE_NAME = "DEFAULT_PACKAGE_NAME";
@Before public void setUp() {
MockitoAnnotations.initMocks(this);
when(mPreferenceScreen.findPreference(any())).thenReturn(mPreference);
}
@Test public void testOnActivityResultUpdatesStateOnSuccess() {
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
WebViewAppPreferenceController controller =
spy(new WebViewAppPreferenceController(mContext, wvusWrapper));
controller.displayPreference(mPreferenceScreen); // Makes sure Preference is non-null
controller.onActivityResult(Activity.RESULT_OK, new Intent(DEFAULT_PACKAGE_NAME));
verify(controller, times(1)).updateState(any());
}
@Test public void testOnActivityResultWithFailureDoesNothing() {
WebViewUpdateServiceWrapper wvusWrapper = mock(WebViewUpdateServiceWrapper.class);
WebViewAppPreferenceController controller =
spy(new WebViewAppPreferenceController(mContext, wvusWrapper));
controller.displayPreference(mPreferenceScreen); // Makes sure Preference is non-null
controller.onActivityResult(Activity.RESULT_CANCELED, new Intent(DEFAULT_PACKAGE_NAME));
verify(controller, never()).updateState(any());
}
}