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 android.accounts.Account; 19 import android.accounts.AccountManager; 20 import android.accounts.AccountManagerCallback; 21 import android.accounts.AuthenticatorDescription; 22 import android.app.Activity; 23 import android.app.FragmentManager; 24 import android.app.FragmentTransaction; 25 import android.content.Context; 26 import android.os.Bundle; 27 import android.os.Handler; 28 import android.os.UserHandle; 29 import android.support.v7.preference.PreferenceScreen; 30 import android.support.v14.preference.PreferenceFragment; 31 import android.widget.Button; 32 33 import com.android.settings.AccessiblePreferenceCategory; 34 import com.android.settings.R; 35 import com.android.settings.SettingsRobolectricTestRunner; 36 import com.android.settings.TestConfig; 37 import com.android.settings.applications.LayoutPreference; 38 import com.android.settings.search.SearchIndexableRaw; 39 import com.android.settings.testutils.FakeFeatureFactory; 40 import com.android.settings.testutils.shadow.ShadowAccountManager; 41 import com.android.settings.testutils.shadow.ShadowContentResolver; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.annotation.Config; 49 import org.robolectric.shadows.ShadowApplication; 50 51 import static com.google.common.truth.Truth.assertThat; 52 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 53 import static org.mockito.Matchers.any; 54 import static org.mockito.Matchers.anyInt; 55 import static org.mockito.Matchers.eq; 56 import static org.mockito.Mockito.mock; 57 import static org.mockito.Mockito.spy; 58 import static org.mockito.Mockito.verify; 59 import static org.mockito.Mockito.when; 60 61 @RunWith(SettingsRobolectricTestRunner.class) 62 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 63 public class RemoveAccountPreferenceControllerTest { 64 65 private static final String KEY_REMOVE_ACCOUNT = "remove_account"; 66 private static final String TAG_REMOVE_ACCOUNT_DIALOG = "confirmRemoveAccount"; 67 68 @Mock(answer = RETURNS_DEEP_STUBS) 69 private AccountManager mAccountManager; 70 @Mock(answer = RETURNS_DEEP_STUBS) 71 private PreferenceFragment mFragment; 72 @Mock 73 private PreferenceScreen mScreen; 74 @Mock 75 private FragmentManager mFragmentManager; 76 @Mock 77 private FragmentTransaction mFragmentTransaction; 78 @Mock 79 private LayoutPreference mPreference; 80 81 private Context mContext; 82 private RemoveAccountPreferenceController mController; 83 84 @Before setUp()85 public void setUp() { 86 MockitoAnnotations.initMocks(this); 87 ShadowApplication shadowContext = ShadowApplication.getInstance(); 88 shadowContext.setSystemService(Context.ACCOUNT_SERVICE, mAccountManager); 89 mContext = spy(shadowContext.getApplicationContext()); 90 91 when(mFragment.getPreferenceScreen()).thenReturn(mScreen); 92 when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext); 93 when(mFragment.getFragmentManager()).thenReturn(mFragmentManager); 94 when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction); 95 when(mAccountManager.getAuthenticatorTypesAsUser(anyInt())).thenReturn( 96 new AuthenticatorDescription[0]); 97 when(mAccountManager.getAccountsAsUser(anyInt())).thenReturn(new Account[0]); 98 mController = new RemoveAccountPreferenceController(mContext, mFragment); 99 } 100 101 @Test displayPreference_shouldAddClickListener()102 public void displayPreference_shouldAddClickListener() { 103 when(mScreen.findPreference(KEY_REMOVE_ACCOUNT)).thenReturn(mPreference); 104 final Button button = mock(Button.class); 105 when(mPreference.findViewById(R.id.button)).thenReturn(button); 106 107 mController.displayPreference(mScreen); 108 109 verify(button).setOnClickListener(mController); 110 } 111 112 @Test onClick_shouldStartConfirmDialog()113 public void onClick_shouldStartConfirmDialog() { 114 when(mFragment.isAdded()).thenReturn(true); 115 mController.onClick(null); 116 117 verify(mFragmentTransaction).add( 118 any(RemoveAccountPreferenceController.ConfirmRemoveAccountDialog.class), 119 eq(TAG_REMOVE_ACCOUNT_DIALOG)); 120 } 121 122 @Test 123 @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class}) confirmRemove_shouldRemoveAccount()124 public void confirmRemove_shouldRemoveAccount() { 125 when(mFragment.isAdded()).thenReturn(true); 126 Activity activity = mock(Activity.class); 127 when(activity.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mAccountManager); 128 when(mFragment.getActivity()).thenReturn(activity); 129 130 Account account = new Account("Account11", "com.acct1"); 131 UserHandle userHandle = new UserHandle(10); 132 RemoveAccountPreferenceController.ConfirmRemoveAccountDialog dialog = 133 RemoveAccountPreferenceController.ConfirmRemoveAccountDialog.show( 134 mFragment, account, userHandle); 135 dialog.onCreate(new Bundle()); 136 dialog.onClick(null, 0); 137 verify(mAccountManager).removeAccountAsUser(eq(account), any(Activity.class), 138 any(AccountManagerCallback.class), any(Handler.class), eq(userHandle)); 139 } 140 }