• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settings.accounts;
17 
18 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
19 import static org.mockito.ArgumentMatchers.nullable;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.accounts.Account;
30 import android.accounts.AccountManager;
31 import android.accounts.AccountManagerCallback;
32 import android.accounts.AuthenticatorDescription;
33 import android.app.Activity;
34 import android.app.FragmentManager;
35 import android.app.FragmentTransaction;
36 import android.content.Context;
37 import android.content.Intent;
38 import android.os.Bundle;
39 import android.os.Handler;
40 import android.os.UserHandle;
41 import android.os.UserManager;
42 import android.support.v14.preference.PreferenceFragment;
43 import android.support.v7.preference.PreferenceScreen;
44 import android.widget.Button;
45 
46 import com.android.settings.R;
47 import com.android.settings.TestConfig;
48 import com.android.settings.applications.LayoutPreference;
49 import com.android.settings.enterprise.DevicePolicyManagerWrapper;
50 import com.android.settings.testutils.SettingsRobolectricTestRunner;
51 import com.android.settings.testutils.shadow.ShadowAccountManager;
52 import com.android.settings.testutils.shadow.ShadowContentResolver;
53 
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.Mock;
58 import org.mockito.MockitoAnnotations;
59 import org.robolectric.annotation.Config;
60 import org.robolectric.shadows.ShadowApplication;
61 
62 @RunWith(SettingsRobolectricTestRunner.class)
63 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
64 public class RemoveAccountPreferenceControllerTest {
65 
66     private static final String KEY_REMOVE_ACCOUNT = "remove_account";
67     private static final String TAG_REMOVE_ACCOUNT_DIALOG = "confirmRemoveAccount";
68 
69     @Mock(answer = RETURNS_DEEP_STUBS)
70     private AccountManager mAccountManager;
71     @Mock
72     private DevicePolicyManagerWrapper mDevicePolicyManager;
73     @Mock(answer = RETURNS_DEEP_STUBS)
74     private PreferenceFragment mFragment;
75     @Mock
76     private PreferenceScreen mScreen;
77     @Mock
78     private FragmentManager mFragmentManager;
79     @Mock
80     private FragmentTransaction mFragmentTransaction;
81     @Mock
82     private LayoutPreference mPreference;
83 
84     private Context mContext;
85     private RemoveAccountPreferenceController mController;
86 
87     @Before
setUp()88     public void setUp() {
89         MockitoAnnotations.initMocks(this);
90         ShadowApplication shadowContext = ShadowApplication.getInstance();
91         shadowContext.setSystemService(Context.ACCOUNT_SERVICE, mAccountManager);
92         mContext = spy(shadowContext.getApplicationContext());
93 
94         when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
95         when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
96         when(mFragment.getFragmentManager()).thenReturn(mFragmentManager);
97         when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
98         when(mAccountManager.getAuthenticatorTypesAsUser(anyInt())).thenReturn(
99             new AuthenticatorDescription[0]);
100         when(mAccountManager.getAccountsAsUser(anyInt())).thenReturn(new Account[0]);
101         mController = new RemoveAccountPreferenceController(mContext, mFragment,
102             mDevicePolicyManager);
103     }
104 
105     @Test
displayPreference_shouldAddClickListener()106     public void displayPreference_shouldAddClickListener() {
107         when(mScreen.findPreference(KEY_REMOVE_ACCOUNT)).thenReturn(mPreference);
108         final Button button = mock(Button.class);
109         when(mPreference.findViewById(R.id.button)).thenReturn(button);
110 
111         mController.displayPreference(mScreen);
112 
113         verify(button).setOnClickListener(mController);
114     }
115 
116     @Test
onClick_shouldStartConfirmDialog()117     public void onClick_shouldStartConfirmDialog() {
118         when(mFragment.isAdded()).thenReturn(true);
119         mController.onClick(null);
120 
121         verify(mFragmentTransaction).add(
122             any(RemoveAccountPreferenceController.ConfirmRemoveAccountDialog.class),
123             eq(TAG_REMOVE_ACCOUNT_DIALOG));
124     }
125 
126     @Test
onClick_shouldNotStartConfirmDialogWhenModifyAccountsIsDisallowed()127     public void onClick_shouldNotStartConfirmDialogWhenModifyAccountsIsDisallowed() {
128         when(mFragment.isAdded()).thenReturn(true);
129         when(mDevicePolicyManager.createAdminSupportIntent(UserManager.DISALLOW_MODIFY_ACCOUNTS))
130             .thenReturn(new Intent());
131         mController.onClick(null);
132 
133         verify(mFragmentTransaction, never()).add(
134             any(RemoveAccountPreferenceController.ConfirmRemoveAccountDialog.class),
135             eq(TAG_REMOVE_ACCOUNT_DIALOG));
136     }
137 
138     @Test
139     @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class})
confirmRemove_shouldRemoveAccount()140     public void confirmRemove_shouldRemoveAccount() {
141         when(mFragment.isAdded()).thenReturn(true);
142         Activity activity = mock(Activity.class);
143         when(activity.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mAccountManager);
144         when(mFragment.getActivity()).thenReturn(activity);
145 
146         Account account = new Account("Account11", "com.acct1");
147         UserHandle userHandle = new UserHandle(10);
148         RemoveAccountPreferenceController.ConfirmRemoveAccountDialog dialog =
149             RemoveAccountPreferenceController.ConfirmRemoveAccountDialog.show(
150                     mFragment, account, userHandle);
151         dialog.onCreate(new Bundle());
152         dialog.onClick(null, 0);
153         verify(mAccountManager).removeAccountAsUser(eq(account), nullable(Activity.class),
154             nullable(AccountManagerCallback.class), nullable(Handler.class), eq(userHandle));
155     }
156 }
157