Merge changes from topic "role-replace" into qt-dev
* changes: Remove old default apps code and use roles instead. Move default payment app to special app access.
This commit is contained in:
committed by
Android (Google) Code Review
commit
0aa0d665c0
@@ -1,174 +0,0 @@
|
||||
/*
|
||||
* 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.settings.applications;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController;
|
||||
import com.android.settings.applications.defaultapps.DefaultPhonePreferenceController;
|
||||
import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController;
|
||||
import com.android.settings.dashboard.SummaryLoader;
|
||||
import com.android.settings.testutils.XmlTestUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultAppSettingsTest {
|
||||
|
||||
private Context mContext;
|
||||
|
||||
private DefaultAppSettings mFragment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mFragment = new DefaultAppSettings();
|
||||
mFragment.onAttach(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPreferenceScreenResId_shouldUseAppDefaultSettingPrefLayout() {
|
||||
assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(R.xml.app_default_settings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setListening_shouldUpdateSummary() {
|
||||
final SummaryLoader summaryLoader = mock(SummaryLoader.class);
|
||||
final DefaultAppSettings.SummaryProvider summaryProvider =
|
||||
new DefaultAppSettings.SummaryProvider(mContext, summaryLoader);
|
||||
final DefaultSmsPreferenceController defaultSms =
|
||||
mock(DefaultSmsPreferenceController.class);
|
||||
final DefaultBrowserPreferenceController defaultBrowser =
|
||||
mock(DefaultBrowserPreferenceController.class);
|
||||
final DefaultPhonePreferenceController defaultPhone =
|
||||
mock(DefaultPhonePreferenceController.class);
|
||||
ReflectionHelpers.setField(summaryProvider, "mDefaultSmsPreferenceController", defaultSms);
|
||||
ReflectionHelpers.setField(
|
||||
summaryProvider, "mDefaultBrowserPreferenceController", defaultBrowser);
|
||||
ReflectionHelpers.setField(
|
||||
summaryProvider, "mDefaultPhonePreferenceController", defaultPhone);
|
||||
|
||||
// all available
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader).setSummary(summaryProvider, "Browser1, Phone1, and Sms1");
|
||||
|
||||
// 2 available
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader).setSummary(summaryProvider, "Browser1 and Phone1");
|
||||
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader).setSummary(summaryProvider, "Phone1 and Sms1");
|
||||
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader).setSummary(summaryProvider, "Browser1 and Sms1");
|
||||
|
||||
// 1 available
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader).setSummary(summaryProvider, "Browser1");
|
||||
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader).setSummary(summaryProvider, "Sms1");
|
||||
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader).setSummary(summaryProvider, "Phone1");
|
||||
|
||||
// None available
|
||||
when(defaultSms.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
|
||||
when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
|
||||
summaryProvider.setListening(true);
|
||||
|
||||
verify(summaryLoader, never()).setSummary(summaryProvider, eq(anyString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonIndexableKeys_existInXmlLayout() {
|
||||
final Context context = spy(RuntimeEnvironment.application);
|
||||
when(context.getApplicationContext()).thenReturn(context);
|
||||
final UserManager userManager = mock(UserManager.class, RETURNS_DEEP_STUBS);
|
||||
|
||||
when(context.getSystemService(Context.USER_SERVICE))
|
||||
.thenReturn(userManager);
|
||||
when(userManager.getUserInfo(anyInt()).isRestricted()).thenReturn(true);
|
||||
|
||||
when(context.getSystemService(Context.TELEPHONY_SERVICE))
|
||||
.thenReturn(mock(TelephonyManager.class));
|
||||
when(context.getPackageManager())
|
||||
.thenReturn(mock(PackageManager.class));
|
||||
final List<String> niks = DefaultAppSettings.SEARCH_INDEX_DATA_PROVIDER
|
||||
.getNonIndexableKeys(context);
|
||||
|
||||
final int xmlId = new DefaultAppSettings().getPreferenceScreenResId();
|
||||
|
||||
final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
|
||||
|
||||
assertThat(keys).containsAllIn(niks);
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
* Copyright (C) 2019 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.
|
||||
@@ -11,36 +11,25 @@
|
||||
* 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.
|
||||
* limitations under the License
|
||||
*/
|
||||
|
||||
package com.android.settings.applications.defaultapps;
|
||||
package com.android.settings.applications;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.role.RoleManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
@@ -48,13 +37,9 @@ import org.robolectric.RobolectricTestRunner;
|
||||
import java.util.Collections;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class RolesPreferenceControllerTest {
|
||||
public class DefaultAppsPreferenceControllerTest {
|
||||
|
||||
private static final String PREFERENCE_KEY = "roles";
|
||||
private static final String DIFFERENT_PREFERENCE_KEY = "different";
|
||||
|
||||
private static final String PERMISSION_CONTROLLER_PACKAGE_NAME =
|
||||
"com.android.permissioncontroller";
|
||||
private static final String PREFERENCE_KEY = "DefaultApps";
|
||||
|
||||
private static final String BROWSER_PACKAGE_NAME = "com.example.browser1";
|
||||
private static final String DIALER_PACKAGE_NAME = "com.example.dialer1";
|
||||
@@ -73,6 +58,8 @@ public class RolesPreferenceControllerTest {
|
||||
@Mock
|
||||
private ApplicationInfo mSmsApplicationInfo;
|
||||
|
||||
private DefaultAppsPreferenceController mPreferenceController;
|
||||
|
||||
@Before
|
||||
public void setUp() throws PackageManager.NameNotFoundException {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
@@ -89,80 +76,13 @@ public class RolesPreferenceControllerTest {
|
||||
when(mSmsApplicationInfo.loadLabel(mPackageManager)).thenReturn("Sms1");
|
||||
when(mPackageManager.getApplicationInfo(eq(SMS_PACKAGE_NAME), anyInt())).thenReturn(
|
||||
mSmsApplicationInfo);
|
||||
|
||||
mPreferenceController = new DefaultAppsPreferenceController(mContext, PREFERENCE_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noPermissionController_shouldReturnUnsupportedOnDevice() {
|
||||
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(null);
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getAvailabilityStatus())
|
||||
.isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_hasPermissionController_shouldReturnAvailableUnsearchable() {
|
||||
when(mPackageManager.getPermissionControllerPackageName())
|
||||
.thenReturn(PERMISSION_CONTROLLER_PACKAGE_NAME);
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getAvailabilityStatus())
|
||||
.isEqualTo(BasePreferenceController.AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_differentKey_shouldReturnFalse() {
|
||||
when(mPackageManager.getPermissionControllerPackageName())
|
||||
.thenReturn(PERMISSION_CONTROLLER_PACKAGE_NAME);
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
Preference preference = mock(Preference.class);
|
||||
when(preference.getKey()).thenReturn(DIFFERENT_PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.handlePreferenceTreeClick(preference)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_sameKey_shouldReturnTrue() {
|
||||
when(mPackageManager.getPermissionControllerPackageName())
|
||||
.thenReturn(PERMISSION_CONTROLLER_PACKAGE_NAME);
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
Preference preference = mock(Preference.class);
|
||||
when(preference.getKey()).thenReturn(PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.handlePreferenceTreeClick(preference)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_noPermissionController_shouldNotStartActivity() {
|
||||
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(null);
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
Preference preference = mock(Preference.class);
|
||||
when(preference.getKey()).thenReturn(PREFERENCE_KEY);
|
||||
preferenceController.handlePreferenceTreeClick(preference);
|
||||
|
||||
verify(mContext, never()).startActivity(any(Intent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_hasPermissionController_shouldStartActivityWithIntent() {
|
||||
when(mPackageManager.getPermissionControllerPackageName())
|
||||
.thenReturn(PERMISSION_CONTROLLER_PACKAGE_NAME);
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
Preference preference = mock(Preference.class);
|
||||
when(preference.getKey()).thenReturn(PREFERENCE_KEY);
|
||||
preferenceController.handlePreferenceTreeClick(preference);
|
||||
ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class);
|
||||
|
||||
verify(mContext).startActivity(intent.capture());
|
||||
assertThat(intent.getValue().getAction())
|
||||
.isEqualTo(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
|
||||
assertThat(intent.getValue().getPackage()).isEqualTo(PERMISSION_CONTROLLER_PACKAGE_NAME);
|
||||
public void isAvailable_shouldReturnTrue() {
|
||||
assertThat(mPreferenceController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -173,10 +93,8 @@ public class RolesPreferenceControllerTest {
|
||||
Collections.singletonList(DIALER_PACKAGE_NAME));
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
|
||||
Collections.singletonList(SMS_PACKAGE_NAME));
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isEqualTo("Browser1, Phone1, and Sms1");
|
||||
assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1, Phone1, and Sms1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -186,10 +104,8 @@ public class RolesPreferenceControllerTest {
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
|
||||
Collections.singletonList(DIALER_PACKAGE_NAME));
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isEqualTo("Browser1 and Phone1");
|
||||
assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1 and Phone1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -200,10 +116,8 @@ public class RolesPreferenceControllerTest {
|
||||
Collections.emptyList());
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
|
||||
Collections.singletonList(SMS_PACKAGE_NAME));
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isEqualTo("Browser1 and Sms1");
|
||||
assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1 and Sms1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -214,10 +128,8 @@ public class RolesPreferenceControllerTest {
|
||||
Collections.singletonList(DIALER_PACKAGE_NAME));
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
|
||||
Collections.singletonList(SMS_PACKAGE_NAME));
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isEqualTo("Phone1 and Sms1");
|
||||
assertThat(mPreferenceController.getSummary()).isEqualTo("Phone1 and Sms1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -227,10 +139,8 @@ public class RolesPreferenceControllerTest {
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
|
||||
Collections.emptyList());
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isEqualTo("Browser1");
|
||||
assertThat(mPreferenceController.getSummary()).isEqualTo("Browser1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -240,10 +150,8 @@ public class RolesPreferenceControllerTest {
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
|
||||
Collections.singletonList(DIALER_PACKAGE_NAME));
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isEqualTo("Phone1");
|
||||
assertThat(mPreferenceController.getSummary()).isEqualTo("Phone1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -254,10 +162,8 @@ public class RolesPreferenceControllerTest {
|
||||
Collections.emptyList());
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(
|
||||
Collections.singletonList(SMS_PACKAGE_NAME));
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isEqualTo("Sms1");
|
||||
assertThat(mPreferenceController.getSummary()).isEqualTo("Sms1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -267,9 +173,7 @@ public class RolesPreferenceControllerTest {
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_DIALER)).thenReturn(
|
||||
Collections.emptyList());
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_SMS)).thenReturn(Collections.emptyList());
|
||||
RolesPreferenceController preferenceController = new RolesPreferenceController(mContext,
|
||||
PREFERENCE_KEY);
|
||||
|
||||
assertThat(preferenceController.getSummary()).isNull();
|
||||
assertThat(mPreferenceController.getSummary()).isNull();
|
||||
}
|
||||
}
|
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* 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.applications.defaultapps;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultBrowserPickerTest {
|
||||
|
||||
private static final String TEST_APP_KEY = "";
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Activity mActivity;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
private DefaultBrowserPicker mPicker;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
FakeFeatureFactory.setupForTest();
|
||||
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
|
||||
mPicker = new DefaultBrowserPicker();
|
||||
mPicker.onAttach(mActivity);
|
||||
|
||||
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultAppKey_shouldUpdateDefaultBrowser() {
|
||||
mPicker.setDefaultKey(TEST_APP_KEY);
|
||||
verify(mPackageManager).setDefaultBrowserPackageNameAsUser(eq(TEST_APP_KEY), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultAppKey_shouldReturnDefaultBrowser() {
|
||||
mPicker.getDefaultKey();
|
||||
verify(mPackageManager).getDefaultBrowserPackageNameAsUser(anyInt());
|
||||
}
|
||||
}
|
@@ -1,231 +0,0 @@
|
||||
/*
|
||||
* 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.applications.defaultapps;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.res.Resources;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultBrowserPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
private DefaultBrowserPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
|
||||
mController = new DefaultBrowserPreferenceController(mContext);
|
||||
ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_noBrowser_shouldReturnFalse() {
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(null);
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_hasBrowser_shouldReturnTrue() {
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(Collections.singletonList(createResolveInfo("com.test.pkg")));
|
||||
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSoleAppLabel_hasNoApp_shouldNotReturnLabel() {
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(null);
|
||||
final Preference pref = mock(Preference.class);
|
||||
|
||||
mController.updateState(pref);
|
||||
verify(pref).setSummary(R.string.app_list_preference_none);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultAppLabel_hasAppWithMultipleResolvedInfo_shouldReturnLabel()
|
||||
throws NameNotFoundException {
|
||||
DefaultBrowserPreferenceController spyController = spy(mController);
|
||||
doReturn(null).when(spyController).getDefaultAppIcon();
|
||||
final List<ResolveInfo> resolveInfos = new ArrayList<>();
|
||||
final CharSequence PACKAGE_NAME = "com.test.package";
|
||||
|
||||
// This ResolveInfo will return a non-null label from loadLabel.
|
||||
final ResolveInfo info1 = createResolveInfo(PACKAGE_NAME.toString());
|
||||
info1.nonLocalizedLabel = PACKAGE_NAME;
|
||||
resolveInfos.add(info1);
|
||||
|
||||
// This ResolveInfo will return a null label from loadLabel.
|
||||
final ResolveInfo info2 = createResolveInfo(PACKAGE_NAME.toString());
|
||||
resolveInfos.add(info2);
|
||||
|
||||
when(mPackageManager.getDefaultBrowserPackageNameAsUser(anyInt())).thenReturn(null);
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(resolveInfos);
|
||||
when(mPackageManager.getApplicationInfoAsUser(
|
||||
eq(PACKAGE_NAME.toString()), anyInt(), anyInt()))
|
||||
.thenReturn(createApplicationInfo(PACKAGE_NAME.toString()));
|
||||
|
||||
assertThat(spyController.getDefaultAppLabel()).isEqualTo(PACKAGE_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultApp_shouldGetDefaultBrowserPackage() {
|
||||
mController.getDefaultAppInfo();
|
||||
|
||||
verify(mPackageManager).getDefaultBrowserPackageNameAsUser(anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultApp_shouldGetApplicationInfoAsUser() throws NameNotFoundException {
|
||||
final String PACKAGE_NAME = "com.test.package";
|
||||
when(mPackageManager.getDefaultBrowserPackageNameAsUser(anyInt())).thenReturn(PACKAGE_NAME);
|
||||
|
||||
mController.getDefaultAppInfo();
|
||||
|
||||
verify(mPackageManager).getApplicationInfoAsUser(eq(PACKAGE_NAME), anyInt(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isBrowserDefault_onlyApp_shouldReturnTrue() {
|
||||
when(mPackageManager.getDefaultBrowserPackageNameAsUser(anyInt())).thenReturn(null);
|
||||
final List<ResolveInfo> resolveInfos = new ArrayList<>();
|
||||
final String PACKAGE_ONE = "pkg";
|
||||
resolveInfos.add(createResolveInfo(PACKAGE_ONE));
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(resolveInfos);
|
||||
|
||||
assertThat(mController.isBrowserDefault("pkg", 0)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCandidates_shouldNotIncludeDuplicatePackageName() throws NameNotFoundException {
|
||||
final List<ResolveInfo> resolveInfos = new ArrayList<>();
|
||||
final String PACKAGE_ONE = "com.first.package";
|
||||
final String PACKAGE_TWO = "com.second.package";
|
||||
resolveInfos.add(createResolveInfo(PACKAGE_ONE));
|
||||
resolveInfos.add(createResolveInfo(PACKAGE_TWO));
|
||||
resolveInfos.add(createResolveInfo(PACKAGE_ONE));
|
||||
resolveInfos.add(createResolveInfo(PACKAGE_TWO));
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(resolveInfos);
|
||||
when(mPackageManager.getApplicationInfoAsUser(eq(PACKAGE_ONE), anyInt(), anyInt()))
|
||||
.thenReturn(createApplicationInfo(PACKAGE_ONE));
|
||||
when(mPackageManager.getApplicationInfoAsUser(eq(PACKAGE_TWO), anyInt(), anyInt()))
|
||||
.thenReturn(createApplicationInfo(PACKAGE_TWO));
|
||||
|
||||
final List<ResolveInfo> defaultBrowserInfo =
|
||||
DefaultBrowserPreferenceController.getCandidates(mPackageManager, 0 /* userId */);
|
||||
|
||||
assertThat(defaultBrowserInfo.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCandidates_shouldQueryActivityWithMatchAll() {
|
||||
DefaultBrowserPreferenceController.getCandidates(mPackageManager, 0 /* userId */);
|
||||
|
||||
verify(mPackageManager).queryIntentActivitiesAsUser(
|
||||
any(Intent.class), eq(PackageManager.MATCH_ALL), eq(0) /* userId */);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getOnlyAppIcon_shouldGetApplicationInfoAsUser() throws NameNotFoundException {
|
||||
final List<ResolveInfo> resolveInfos = new ArrayList<>();
|
||||
final String PACKAGE_NAME = "com.test.package";
|
||||
resolveInfos.add(createResolveInfo(PACKAGE_NAME));
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(resolveInfos);
|
||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
when(mContext.getResources()).thenReturn(mock(Resources.class));
|
||||
|
||||
mController.getOnlyAppIcon();
|
||||
|
||||
verify(mPackageManager).getApplicationInfoAsUser(
|
||||
eq(PACKAGE_NAME), eq(0) /* flags */, eq(0) /* userId */);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasBrowserPreference_shouldQueryIntentActivitiesAsUser() {
|
||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||
|
||||
DefaultBrowserPreferenceController
|
||||
.hasBrowserPreference("com.test.package", mContext, 0 /* userId */);
|
||||
|
||||
verify(mPackageManager).queryIntentActivitiesAsUser(
|
||||
any(Intent.class), eq(0) /* flags */, eq(0) /* userId */);
|
||||
}
|
||||
|
||||
private ResolveInfo createResolveInfo(String packageName) {
|
||||
final ResolveInfo info = new ResolveInfo();
|
||||
info.handleAllWebDataURI = true;
|
||||
info.activityInfo = new ActivityInfo();
|
||||
info.activityInfo.packageName = packageName;
|
||||
info.activityInfo.applicationInfo = createApplicationInfo(packageName);
|
||||
return info;
|
||||
}
|
||||
|
||||
private ApplicationInfo createApplicationInfo(String packageName) {
|
||||
final ApplicationInfo info = new ApplicationInfo();
|
||||
info.packageName = packageName;
|
||||
return info;
|
||||
}
|
||||
}
|
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* 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.applications.defaultapps;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.role.RoleManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultEmergencyPickerTest {
|
||||
private static final String TAG = DefaultEmergencyPickerTest.class.getSimpleName();
|
||||
private static final String TEST_APP_KEY = "test_app";
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Activity mActivity;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private RoleManager mRoleManager;
|
||||
|
||||
private DefaultEmergencyPicker mPicker;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ShadowApplication shadowApplication = ShadowApplication.getInstance();
|
||||
shadowApplication.setSystemService(Context.ROLE_SERVICE, mRoleManager);
|
||||
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
|
||||
mPicker = spy(new DefaultEmergencyPicker());
|
||||
mPicker.onAttach(mActivity);
|
||||
|
||||
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
|
||||
when(mPicker.getContext()).thenReturn(RuntimeEnvironment.application);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultAppKey_shouldUpdateDefault() {
|
||||
mPicker.setDefaultKey(TEST_APP_KEY);
|
||||
verify(mRoleManager).addRoleHolderAsUser(
|
||||
eq(RoleManager.ROLE_EMERGENCY),
|
||||
eq(TEST_APP_KEY),
|
||||
eq(0),
|
||||
any(UserHandle.class),
|
||||
any(Executor.class),
|
||||
any(Consumer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultAppKey_shouldReturnDefault() {
|
||||
when(mRoleManager.getRoleHolders(RoleManager.ROLE_EMERGENCY))
|
||||
.thenReturn(Arrays.asList(TEST_APP_KEY));
|
||||
assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY);
|
||||
}
|
||||
}
|
@@ -1,168 +0,0 @@
|
||||
/*
|
||||
* 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.applications.defaultapps;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.Build;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.applications.DefaultAppInfo;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultHomePickerTest {
|
||||
|
||||
private static final String TEST_APP_KEY = "com.android.settings/DefaultEmergencyPickerTest";
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Activity mActivity;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private AppOpsManager mAppOpsManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
private Context mContext;
|
||||
private DefaultHomePicker mPicker;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
FakeFeatureFactory.setupForTest();
|
||||
|
||||
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
when(mActivity.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager);
|
||||
when(mActivity.getApplicationContext()).thenReturn(RuntimeEnvironment.application);
|
||||
|
||||
mPicker = spy(new DefaultHomePicker());
|
||||
mPicker.onAttach((Context) mActivity);
|
||||
|
||||
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
doReturn(mContext).when(mPicker).getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultAppKey_shouldUpdateDefault() {
|
||||
assertThat(mPicker.setDefaultKey(TEST_APP_KEY)).isTrue();
|
||||
|
||||
verify(mPackageManager).replacePreferredActivity(any(IntentFilter.class),
|
||||
anyInt(), any(ComponentName[].class), any(ComponentName.class));
|
||||
verify(mContext).startActivity(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultAppKey_shouldReturnDefault() {
|
||||
final ComponentName cn = mock(ComponentName.class);
|
||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(cn);
|
||||
mPicker.getDefaultKey();
|
||||
verify(cn).flattenToString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCandidates_allLaunchersAvailableIfNoManagedProfile() {
|
||||
addLaunchers();
|
||||
List<DefaultAppInfo> candidates = mPicker.getCandidates();
|
||||
assertThat(candidates.size()).isEqualTo(2);
|
||||
assertThat(candidates.get(0).summary).isNull();
|
||||
assertThat(candidates.get(0).enabled).isTrue();
|
||||
assertThat(candidates.get(1).summary).isNull();
|
||||
assertThat(candidates.get(1).enabled).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCandidates_onlyLollipopPlusLaunchersAvailableIfManagedProfile() {
|
||||
List<UserInfo> profiles = new ArrayList<>();
|
||||
profiles.add(new UserInfo(/*id=*/ 10, "TestUserName", UserInfo.FLAG_MANAGED_PROFILE));
|
||||
when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
|
||||
|
||||
addLaunchers();
|
||||
List<DefaultAppInfo> candidates = mPicker.getCandidates();
|
||||
assertThat(candidates.size()).isEqualTo(2);
|
||||
DefaultAppInfo lollipopPlusLauncher = candidates.get(0);
|
||||
assertThat(lollipopPlusLauncher.summary).isNull();
|
||||
assertThat(lollipopPlusLauncher.enabled).isTrue();
|
||||
|
||||
DefaultAppInfo preLollipopLauncher = candidates.get(1);
|
||||
assertThat(preLollipopLauncher.summary).isNotNull();
|
||||
assertThat(preLollipopLauncher.enabled).isFalse();
|
||||
}
|
||||
|
||||
private ResolveInfo createLauncher(
|
||||
String packageName, String className, int targetSdk) throws NameNotFoundException {
|
||||
ResolveInfo launcher = new ResolveInfo();
|
||||
launcher.activityInfo = new ActivityInfo();
|
||||
launcher.activityInfo.packageName = packageName;
|
||||
launcher.activityInfo.name = className;
|
||||
ApplicationInfo launcherAppInfo = new ApplicationInfo();
|
||||
launcherAppInfo.targetSdkVersion = targetSdk;
|
||||
when(mPackageManager.getApplicationInfo(eq(launcher.activityInfo.packageName), anyInt()))
|
||||
.thenReturn(launcherAppInfo);
|
||||
return launcher;
|
||||
}
|
||||
|
||||
private void addLaunchers() {
|
||||
doAnswer(invocation -> {
|
||||
// The result of this method is stored in the first parameter...
|
||||
List<ResolveInfo> parameter = (List<ResolveInfo>) invocation.getArguments()[0];
|
||||
parameter.add(createLauncher(
|
||||
"package.1", "LollipopPlusLauncher", Build.VERSION_CODES.LOLLIPOP));
|
||||
parameter.add(createLauncher(
|
||||
"package.2", "PreLollipopLauncher", Build.VERSION_CODES.KITKAT));
|
||||
return null;
|
||||
})
|
||||
.when(mPackageManager).getHomeActivities(anyList());
|
||||
}
|
||||
}
|
@@ -1,170 +0,0 @@
|
||||
/*
|
||||
* 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.applications.defaultapps;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultHomePreferenceControllerTest {
|
||||
|
||||
private static final String TEST_PACKAGE = "test.pkg";
|
||||
private static final String TEST_CLASS = "class";
|
||||
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
private Context mContext;
|
||||
private DefaultHomePreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
|
||||
mController = spy(new DefaultHomePreferenceController(mContext));
|
||||
ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultHome_byDefault_shouldBeShown() {
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void testDefaultHome_ifDisabled_shouldNotBeShown() {
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultApp_shouldGetDefaultBrowserPackage() {
|
||||
assertThat(mController.getDefaultAppInfo()).isNotNull();
|
||||
|
||||
verify(mPackageManager).getHomeActivities(anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultApp_noDefaultHome_shouldReturnNull() {
|
||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
|
||||
|
||||
assertThat(mController.getDefaultAppInfo()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_noDefaultApp_shouldAskPackageManagerForOnlyApp() {
|
||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
|
||||
mController.updateState(mock(Preference.class));
|
||||
|
||||
verify(mPackageManager, atLeastOnce()).getHomeActivities(anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsHomeDefault_noDefaultSet_shouldReturnTrue() {
|
||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
|
||||
assertThat(DefaultHomePreferenceController.isHomeDefault(TEST_PACKAGE, mPackageManager))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsHomeDefault_defaultSetToPkg_shouldReturnTrue() {
|
||||
final String pkgName = TEST_PACKAGE;
|
||||
final ComponentName defaultHome = new ComponentName(pkgName, TEST_CLASS);
|
||||
|
||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
|
||||
|
||||
assertThat(DefaultHomePreferenceController.isHomeDefault(pkgName, mPackageManager))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsHomeDefault_defaultSetToOtherPkg_shouldReturnFalse() {
|
||||
final String pkgName = TEST_PACKAGE;
|
||||
final ComponentName defaultHome = new ComponentName("not" + pkgName, TEST_CLASS);
|
||||
|
||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
|
||||
|
||||
assertThat(DefaultHomePreferenceController.isHomeDefault(pkgName, mPackageManager))
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSettingIntent_homeHasNoSetting_shouldNotReturnSettingIntent() {
|
||||
when(mPackageManager.getHomeActivities(anyList()))
|
||||
.thenReturn(new ComponentName(TEST_PACKAGE, TEST_CLASS));
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(null);
|
||||
|
||||
assertThat(mController.getSettingIntent(mController.getDefaultAppInfo())).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSettingIntent_homeHasOneSetting_shouldReturnSettingIntent() {
|
||||
when(mPackageManager.getHomeActivities(anyList()))
|
||||
.thenReturn(new ComponentName(TEST_PACKAGE, TEST_CLASS));
|
||||
final ResolveInfo info = mock(ResolveInfo.class);
|
||||
info.activityInfo = mock(ActivityInfo.class);
|
||||
info.activityInfo.name = TEST_CLASS;
|
||||
info.activityInfo.applicationInfo = mock(ApplicationInfo.class);
|
||||
info.activityInfo.applicationInfo.packageName = TEST_PACKAGE;
|
||||
when(mPackageManager.resolveActivity(any(Intent.class), anyInt()))
|
||||
.thenReturn(info);
|
||||
|
||||
Intent intent = mController.getSettingIntent(mController.getDefaultAppInfo());
|
||||
assertThat(intent).isNotNull();
|
||||
assertThat(intent.getPackage()).isEqualTo(TEST_PACKAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSettingIntent_noDefauldHome_shouldReturnNull() {
|
||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
|
||||
assertThat(mController.getSettingIntent(mController.getDefaultAppInfo())).isNull();
|
||||
}
|
||||
}
|
@@ -1,104 +0,0 @@
|
||||
/*
|
||||
* 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.applications.defaultapps;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
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.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultPhonePickerTest {
|
||||
|
||||
private static final String TEST_APP_KEY = "com.android.settings/PickerTest";
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Activity mActivity;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private DefaultPhonePicker.DefaultKeyUpdater mDefaultKeyUpdater;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private BatteryUtils mBatteryUtils;
|
||||
|
||||
private DefaultPhonePicker mPicker;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
when(mActivity.getSystemService(Context.TELECOM_SERVICE)).thenReturn(null);
|
||||
mPicker = spy(new DefaultPhonePicker());
|
||||
mPicker.onAttach(mActivity);
|
||||
|
||||
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
|
||||
ReflectionHelpers.setField(mPicker, "mDefaultKeyUpdater", mDefaultKeyUpdater);
|
||||
ReflectionHelpers.setField(mPicker, "mBatteryUtils", mBatteryUtils);
|
||||
doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSystemDefaultPackage_shouldAskDefaultKeyUpdater() {
|
||||
mPicker.getSystemDefaultKey();
|
||||
|
||||
verify(mDefaultKeyUpdater).getSystemDialerPackage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultAppKey_shouldUpdateDefault() {
|
||||
mPicker.setDefaultKey(TEST_APP_KEY);
|
||||
|
||||
verify(mDefaultKeyUpdater)
|
||||
.setDefaultDialerApplication(any(Context.class), eq(TEST_APP_KEY), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultAppKey_shouldReturnDefault() {
|
||||
mPicker.getDefaultKey();
|
||||
|
||||
verify(mDefaultKeyUpdater).getDefaultDialerApplication(any(Context.class), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultKey_shouldUnrestrictApp() {
|
||||
mPicker.setDefaultKey(TEST_APP_KEY);
|
||||
|
||||
verify(mBatteryUtils).clearForceAppStandby(TEST_APP_KEY);
|
||||
}
|
||||
}
|
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* 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.applications.defaultapps;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
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.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.settings.fuelgauge.BatteryUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class DefaultSmsPickerTest {
|
||||
|
||||
private static final String TEST_APP_KEY = "com.android.settings/PickerTest";
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Activity mActivity;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private DefaultSmsPicker.DefaultKeyUpdater mDefaultKeyUpdater;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private BatteryUtils mBatteryUtils;
|
||||
|
||||
private DefaultSmsPicker mPicker;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
mPicker = spy(new DefaultSmsPicker());
|
||||
mPicker.onAttach(mActivity);
|
||||
|
||||
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
|
||||
ReflectionHelpers.setField(mPicker, "mDefaultKeyUpdater", mDefaultKeyUpdater);
|
||||
ReflectionHelpers.setField(mPicker, "mBatteryUtils", mBatteryUtils);
|
||||
doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultAppKey_shouldUpdateDefault() {
|
||||
mPicker.setDefaultKey(TEST_APP_KEY);
|
||||
|
||||
verify(mDefaultKeyUpdater).setDefaultApplication(any(Context.class), eq(TEST_APP_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDefaultAppKey_shouldReturnDefault() {
|
||||
mPicker.getDefaultKey();
|
||||
|
||||
verify(mDefaultKeyUpdater).getDefaultApplication(any(Context.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultKey_shouldUnrestrictApp() {
|
||||
mPicker.setDefaultKey(TEST_APP_KEY);
|
||||
|
||||
verify(mBatteryUtils).clearForceAppStandby(TEST_APP_KEY);
|
||||
}
|
||||
}
|
@@ -14,13 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.applications.defaultapps;
|
||||
package com.android.settings.applications.specialaccess;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
@@ -28,17 +26,12 @@ import android.content.pm.PackageManager;
|
||||
import android.nfc.NfcAdapter;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.nfc.PaymentBackend;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@@ -52,11 +45,8 @@ public class DefaultPaymentSettingsPreferenceControllerTest {
|
||||
private UserManager mUserManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
private PaymentBackend mPaymentBackend;
|
||||
|
||||
private DefaultPaymentSettingsPreferenceController mController;
|
||||
private Preference mPreference;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -64,11 +54,9 @@ public class DefaultPaymentSettingsPreferenceControllerTest {
|
||||
|
||||
when(mContext.getApplicationContext()).thenReturn(mContext);
|
||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
mController = new DefaultPaymentSettingsPreferenceController(mContext);
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
mController = new DefaultPaymentSettingsPreferenceController(mContext, "key");
|
||||
ReflectionHelpers.setField(mController, "mNfcAdapter", mNfcAdapter);
|
||||
|
||||
mPreference = new Preference(RuntimeEnvironment.application);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,17 +76,4 @@ public class DefaultPaymentSettingsPreferenceControllerTest {
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_shouldSetSummaryToDefaultPaymentApp() {
|
||||
final PaymentBackend.PaymentAppInfo defaultApp = mock(PaymentBackend.PaymentAppInfo.class);
|
||||
defaultApp.label = "test_payment_app";
|
||||
when(mPaymentBackend.getDefaultApp()).thenReturn(defaultApp);
|
||||
ReflectionHelpers.setField(mController, "mPaymentBackend", mPaymentBackend);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPaymentBackend).refresh();
|
||||
assertThat(mPreference.getSummary()).isEqualTo(defaultApp.label);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user