Migrate ChooseAccountActivity to DashboardFragment

- Build a controller to generate/manage a list of preferences.
- Move some logics to the controller and add tests.
- Rename to ChooseAccountFragment.

Bug: 73899467
Test: make RunSettingsRoboTests -j
      atest UniquePreferenceTest SettingsGatewayTest
Change-Id: Id2906c4b922ef159d08c803b976671264c54665f
This commit is contained in:
Emily Chuang
2018-04-12 10:12:28 +08:00
committed by Fan Zhang
parent 8efbe6e255
commit b279b1c025
12 changed files with 712 additions and 153 deletions

View File

@@ -17,7 +17,6 @@ com.android.settings.applications.ManageDomainUrls
com.android.settings.applications.appinfo.WriteSettingsDetails
com.android.settings.applications.ProcessStatsSummary
com.android.settings.users.RestrictedProfileSettings
com.android.settings.accounts.ChooseAccountActivity
com.android.settings.accessibility.ToggleAutoclickPreferenceFragment
com.android.settings.applications.AppLaunchSettings
com.android.settings.applications.ProcessStatsUi

View File

@@ -0,0 +1,205 @@
/*
* Copyright (C) 2018 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.accounts;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import android.accounts.AuthenticatorDescription;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.SyncAdapterType;
import android.graphics.drawable.ColorDrawable;
import android.os.UserHandle;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowAccountManager;
import com.android.settings.testutils.shadow.ShadowContentResolver;
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class,
ShadowRestrictedLockUtils.class})
public class ChooseAccountPreferenceControllerTest {
private Context mContext;
private ChooseAccountPreferenceController mController;
private Activity mActivity;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mPreferenceScreen;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = spy(new ChooseAccountPreferenceController(mContext, "controller_key"));
mActivity = Robolectric.setupActivity(Activity.class);
mPreferenceManager = new PreferenceManager(mContext);
mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
}
@After
public void tearDown() {
ShadowContentResolver.reset();
ShadowAccountManager.resetAuthenticator();
ShadowRestrictedLockUtils.clearDisabledTypes();
}
@Test
public void getAvailabilityStatus_byDefault_shouldBeShown() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test
public void handlePreferenceTreeClick_notProviderPreference_shouldReturnFalse() {
final Preference preference = new Preference(mContext);
assertThat(mController.handlePreferenceTreeClick(preference)).isFalse();
}
@Test
public void handlePreferenceTreeClick_isProviderPreference_shouldFinishFragment() {
final ProviderPreference providerPreference = new ProviderPreference(mContext,
"account_type", null /* icon */, "provider_name");
mController.initialize(null, null, null, mActivity);
mController.handlePreferenceTreeClick(providerPreference);
assertThat(mActivity.isFinishing()).isTrue();
}
@Test
public void updateAuthDescriptions_oneProvider_shouldNotAddPreference() {
final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
"com.android.settings",
R.string.header_add_an_account, 0, 0, 0, false);
ShadowAccountManager.addAuthenticator(authDesc);
final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
"com.acct1" /* accountType */, false /* userVisible */,
true /* supportsUploading */)};
ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
ShadowRestrictedLockUtils.setHasSystemFeature(true);
ShadowRestrictedLockUtils.setDevicePolicyManager(mock(DevicePolicyManager.class));
ShadowRestrictedLockUtils.setDisabledTypes(new String[] {"test_type"});
doReturn("label").when(mController).getLabelForType(anyString());
mController.initialize(null, null, new UserHandle(3), mActivity);
mController.displayPreference(mPreferenceScreen);
assertThat(mActivity.isFinishing()).isTrue();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
}
@Test
public void updateAuthDescriptions_oneAdminDisabledProvider_shouldNotAddPreference() {
final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
"com.android.settings",
R.string.header_add_an_account, 0, 0, 0, false);
ShadowAccountManager.addAuthenticator(authDesc);
final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
"com.acct1" /* accountType */, false /* userVisible */,
true /* supportsUploading */)};
ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
ShadowRestrictedLockUtils.setHasSystemFeature(true);
ShadowRestrictedLockUtils.setDevicePolicyManager(mock(DevicePolicyManager.class));
ShadowRestrictedLockUtils.setDisabledTypes(new String[] {"com.acct1"});
doReturn("label").when(mController).getLabelForType(anyString());
mController.initialize(null, null, new UserHandle(3), mActivity);
mController.displayPreference(mPreferenceScreen);
assertThat(mActivity.isFinishing()).isTrue();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
}
@Test
public void updateAuthDescriptions_noProvider_shouldNotAddPreference() {
final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
"com.android.settings",
R.string.header_add_an_account, 0, 0, 0, false);
ShadowAccountManager.addAuthenticator(authDesc);
final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
"com.acct1" /* accountType */, false /* userVisible */,
true /* supportsUploading */)};
ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
doReturn("label").when(mController).getLabelForType(anyString());
mController.initialize(new String[] {"test_authoritiy1"}, null, new UserHandle(3),
mActivity);
mController.displayPreference(mPreferenceScreen);
assertThat(mActivity.isFinishing()).isTrue();
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
}
@Test
public void
updateAuthDescriptions_twoProvider_shouldAddTwoPreference() {
final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
"com.android.settings",
R.string.header_add_an_account, 0, 0, 0, false);
final AuthenticatorDescription authDesc2 = new AuthenticatorDescription("com.acct2",
"com.android.settings",
R.string.header_add_an_account, 0, 0, 0, false);
ShadowAccountManager.addAuthenticator(authDesc);
ShadowAccountManager.addAuthenticator(authDesc2);
final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
"com.acct1" /* accountType */, false /* userVisible */,
true /* supportsUploading */), new SyncAdapterType("authority2" /* authority */,
"com.acct2" /* accountType */, false /* userVisible */,
true /* supportsUploading */)};
ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
doReturn("label").when(mController).getLabelForType(anyString());
doReturn("label2").when(mController).getLabelForType(anyString());
doReturn(new ColorDrawable()).when(mController).getDrawableForType(anyString());
doReturn(new ColorDrawable()).when(mController).getDrawableForType(anyString());
mController.initialize(null, null, new UserHandle(3), mActivity);
mController.displayPreference(mPreferenceScreen);
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(2);
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright (C) 2018 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.accounts;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.widget.FooterPreferenceMixin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
public class EnterpriseDisclosurePreferenceControllerTest {
private static final String TEST_DISCLOSURE = "This is a test disclosure.";
private ChooseAccountFragment mFragment;
private Context mContext;
private EnterpriseDisclosurePreferenceController mController;
private FooterPreferenceMixin mFooterPreferenceMixin;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mPreferenceScreen;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = spy(new EnterpriseDisclosurePreferenceController(mContext));
mFragment = spy(new ChooseAccountFragment());
mFooterPreferenceMixin = new FooterPreferenceMixin(mFragment, mFragment.getLifecycle());
mPreferenceManager = new PreferenceManager(mContext);
mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
}
@Test
public void getAvailabilityStatus_hasDisclosure_shouldBeAvailable() {
doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_noDisclosure_shouldBeDisabled() {
doReturn(null).when(mController).getDisclosure();
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.DISABLED_UNSUPPORTED);
}
@Test
public void displayPreference_hasDisclosure_shouldSetTitle() {
doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
mController.setFooterPreferenceMixin(mFooterPreferenceMixin);
mController.displayPreference(mPreferenceScreen);
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(1);
assertThat(mPreferenceScreen.getPreference(0).getTitle()).isEqualTo(TEST_DISCLOSURE);
}
@Test
public void displayPreference_noDisclosure_shouldBeInvisible() {
doReturn(null).when(mController).getDisclosure();
mController.displayPreference(mPreferenceScreen);
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
}
}

View File

@@ -22,10 +22,24 @@ import android.accounts.AuthenticatorDescription;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.HashMap;
import java.util.Map;
@Implements(AccountManager.class)
public class ShadowAccountManager {
public class ShadowAccountManager{
private static final Map<String, AuthenticatorDescription> sAuthenticators = new HashMap<>();
@Implementation
public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
return null;
return sAuthenticators.values().toArray(new AuthenticatorDescription[sAuthenticators.size()]);
}
public static void addAuthenticator(AuthenticatorDescription authenticator) {
sAuthenticators.put(authenticator.type, authenticator);
}
public static void resetAuthenticator() {
sAuthenticators.clear();
}
}

View File

@@ -16,6 +16,7 @@
package com.android.settings.testutils.shadow;
import android.annotation.UserIdInt;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import com.android.internal.util.ArrayUtils;
@@ -28,26 +29,31 @@ import org.robolectric.annotation.Resetter;
@Implements(RestrictedLockUtils.class)
public class ShadowRestrictedLockUtils {
private static boolean isRestricted;
private static String[] restrictedPkgs;
private static boolean adminSupportDetailsIntentLaunched;
private static int keyguardDisabledFeatures;
private static boolean sIsRestricted;
private static boolean sAdminSupportDetailsIntentLaunched;
private static boolean sHasSystemFeature;
private static String[] sRestrictedPkgs;
private static DevicePolicyManager sDevicePolicyManager;
private static String[] sDisabledTypes;
private static int sKeyguardDisabledFeatures;
@Resetter
public static void reset() {
isRestricted = false;
restrictedPkgs = null;
adminSupportDetailsIntentLaunched = false;
keyguardDisabledFeatures = 0;
sIsRestricted = false;
sRestrictedPkgs = null;
sAdminSupportDetailsIntentLaunched = false;
sKeyguardDisabledFeatures = 0;
sDisabledTypes = new String[0];
}
@Implementation
public static EnforcedAdmin checkIfMeteredDataRestricted(Context context,
String packageName, int userId) {
if (isRestricted) {
if (sIsRestricted) {
return new EnforcedAdmin();
}
if (ArrayUtils.contains(restrictedPkgs, packageName)) {
if (ArrayUtils.contains(sRestrictedPkgs, packageName)) {
return new EnforcedAdmin();
}
return null;
@@ -55,32 +61,67 @@ public class ShadowRestrictedLockUtils {
@Implementation
public static void sendShowAdminSupportDetailsIntent(Context context, EnforcedAdmin admin) {
adminSupportDetailsIntentLaunched = true;
sAdminSupportDetailsIntentLaunched = true;
}
@Implementation
public static EnforcedAdmin checkIfAccountManagementDisabled(Context context,
String accountType, int userId) {
if (accountType == null) {
return null;
}
if (!sHasSystemFeature || sDevicePolicyManager == null) {
return null;
}
boolean isAccountTypeDisabled = false;
if (ArrayUtils.contains(sDisabledTypes, accountType)) {
isAccountTypeDisabled = true;
}
if (!isAccountTypeDisabled) {
return null;
}
return new EnforcedAdmin();
}
@Implementation
public static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
int features, final @UserIdInt int userId) {
return (keyguardDisabledFeatures & features) == 0 ? null : new EnforcedAdmin();
return (sKeyguardDisabledFeatures & features) == 0 ? null : new EnforcedAdmin();
}
public static boolean hasAdminSupportDetailsIntentLaunched() {
return adminSupportDetailsIntentLaunched;
return sAdminSupportDetailsIntentLaunched;
}
public static void clearAdminSupportDetailsIntentLaunch() {
adminSupportDetailsIntentLaunched = false;
sAdminSupportDetailsIntentLaunched = false;
}
public static void setRestricted(boolean restricted) {
isRestricted = restricted;
sIsRestricted = restricted;
}
public static void setRestrictedPkgs(String... pkgs) {
restrictedPkgs = pkgs;
sRestrictedPkgs = pkgs;
}
public static void setHasSystemFeature(boolean hasSystemFeature) {
sHasSystemFeature = hasSystemFeature;
}
public static void setDevicePolicyManager(DevicePolicyManager dpm) {
sDevicePolicyManager = dpm;
}
public static void setDisabledTypes(String[] disabledTypes) {
sDisabledTypes = disabledTypes;
}
public static void clearDisabledTypes() {
sDisabledTypes = new String[0];
}
public static void setKeyguardDisabledFeatures(int features) {
keyguardDisabledFeatures = features;
sKeyguardDisabledFeatures = features;
}
}