When Remove Account is disallowed, show dialog

When DISALLOW_MODIFY_ACCOUNT is set by a device admin, and
the user wants to remove an account, show the support dialog
instead of showing the confirm dialog.

To use the O-API DPM.createAdminSupportIntent(), a wrapper
needs to be used, because existing Robolectric tests otherwise
don't compile.

Bug: 37413849
Test: make RunSettingsRoboTests -j40 ROBOTEST_FILTER=*RemoveAccountPreferenceControllerTest

Change-Id: If23ea304ab8b9df1748e6acf3032310843b04780
This commit is contained in:
phweiss
2017-06-02 19:38:50 +02:00
parent d5612408af
commit 7a34e9c28e
4 changed files with 56 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
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.verify;
import static org.mockito.Mockito.when;
@@ -33,17 +34,20 @@ import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.PreferenceScreen;
import android.widget.Button;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowAccountManager;
import com.android.settings.testutils.shadow.ShadowContentResolver;
@@ -64,6 +68,8 @@ public class RemoveAccountPreferenceControllerTest {
@Mock(answer = RETURNS_DEEP_STUBS)
private AccountManager mAccountManager;
@Mock
private DevicePolicyManagerWrapper mDevicePolicyManager;
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceFragment mFragment;
@Mock
@@ -92,7 +98,8 @@ public class RemoveAccountPreferenceControllerTest {
when(mAccountManager.getAuthenticatorTypesAsUser(anyInt())).thenReturn(
new AuthenticatorDescription[0]);
when(mAccountManager.getAccountsAsUser(anyInt())).thenReturn(new Account[0]);
mController = new RemoveAccountPreferenceController(mContext, mFragment);
mController = new RemoveAccountPreferenceController(mContext, mFragment,
mDevicePolicyManager);
}
@Test
@@ -116,6 +123,18 @@ public class RemoveAccountPreferenceControllerTest {
eq(TAG_REMOVE_ACCOUNT_DIALOG));
}
@Test
public void onClick_shouldNotStartConfirmDialogWhenModifyAccountsIsDisallowed() {
when(mFragment.isAdded()).thenReturn(true);
when(mDevicePolicyManager.createAdminSupportIntent(UserManager.DISALLOW_MODIFY_ACCOUNTS))
.thenReturn(new Intent());
mController.onClick(null);
verify(mFragmentTransaction, never()).add(
any(RemoveAccountPreferenceController.ConfirmRemoveAccountDialog.class),
eq(TAG_REMOVE_ACCOUNT_DIALOG));
}
@Test
@Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class})
public void confirmRemove_shouldRemoveAccount() {
@@ -134,4 +153,4 @@ public class RemoveAccountPreferenceControllerTest {
verify(mAccountManager).removeAccountAsUser(eq(account), nullable(Activity.class),
nullable(AccountManagerCallback.class), nullable(Handler.class), eq(userHandle));
}
}
}