1 /* 2 * Copyright (C) 2016 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.AuthenticatorDescription; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.os.UserHandle; 24 import android.support.v7.preference.Preference; 25 import android.support.v7.preference.PreferenceScreen; 26 27 import com.android.settings.SettingsRobolectricTestRunner; 28 import com.android.settings.TestConfig; 29 import com.android.settings.testutils.shadow.ShadowAccountManager; 30 import com.android.settings.testutils.shadow.ShadowContentResolver; 31 import com.android.settingslib.drawer.CategoryKey; 32 import com.android.settingslib.drawer.Tile; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.annotation.Config; 40 import org.robolectric.shadows.ShadowApplication; 41 42 import static com.google.common.truth.Truth.assertThat; 43 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 44 import static org.mockito.Matchers.anyInt; 45 import static org.mockito.Mockito.doReturn; 46 import static org.mockito.Mockito.spy; 47 import static org.mockito.Mockito.verify; 48 import static org.mockito.Mockito.when; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 52 public class AccountDetailDashboardFragmentTest { 53 54 private static final String METADATA_CATEGORY = "com.android.settings.category"; 55 private static final String METADATA_ACCOUNT_TYPE = "com.android.settings.ia.account"; 56 private static final String METADATA_USER_HANDLE = "user_handle"; 57 private static final String PREF_ACCOUNT_HEADER = "account_header"; 58 59 @Mock(answer = RETURNS_DEEP_STUBS) 60 private AccountManager mAccountManager; 61 @Mock 62 private Preference mPreference; 63 @Mock 64 private PreferenceScreen mScreen; 65 66 private AccountDetailDashboardFragment mFragment; 67 private Context mContext; 68 69 @Before setUp()70 public void setUp() { 71 MockitoAnnotations.initMocks(this); 72 ShadowApplication shadowContext = ShadowApplication.getInstance(); 73 shadowContext.setSystemService(Context.ACCOUNT_SERVICE, mAccountManager); 74 mContext = spy(shadowContext.getApplicationContext()); 75 76 mFragment = spy(new AccountDetailDashboardFragment()); 77 final Bundle args = new Bundle(); 78 args.putParcelable(METADATA_USER_HANDLE, UserHandle.CURRENT); 79 mFragment.setArguments(args); 80 mFragment.mAccountType = "com.abc"; 81 mFragment.mAccount = new Account("name1@abc.com", "com.abc"); 82 } 83 84 @Test testCategory_isAccount()85 public void testCategory_isAccount() { 86 assertThat(new AccountDetailDashboardFragment().getCategoryKey()) 87 .isEqualTo(CategoryKey.CATEGORY_ACCOUNT); 88 } 89 90 @Test refreshDashboardTiles_HasAccountType_shouldDisplay()91 public void refreshDashboardTiles_HasAccountType_shouldDisplay() { 92 final Tile tile = new Tile(); 93 final Bundle metaData = new Bundle(); 94 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT); 95 metaData.putString(METADATA_ACCOUNT_TYPE, "com.abc"); 96 tile.metaData = metaData; 97 98 assertThat(mFragment.displayTile(tile)).isTrue(); 99 } 100 101 @Test refreshDashboardTiles_NoAccountType_shouldNotDisplay()102 public void refreshDashboardTiles_NoAccountType_shouldNotDisplay() { 103 final Tile tile = new Tile(); 104 final Bundle metaData = new Bundle(); 105 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT); 106 tile.metaData = metaData; 107 108 assertThat(mFragment.displayTile(tile)).isFalse(); 109 } 110 111 @Test refreshDashboardTiles_OtherAccountType_shouldNotDisplay()112 public void refreshDashboardTiles_OtherAccountType_shouldNotDisplay() { 113 final Tile tile = new Tile(); 114 final Bundle metaData = new Bundle(); 115 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT); 116 metaData.putString(METADATA_ACCOUNT_TYPE, "com.other"); 117 tile.metaData = metaData; 118 119 assertThat(mFragment.displayTile(tile)).isFalse(); 120 } 121 122 @Test 123 @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class}) updateAccountHeader_shouldShowAccountName()124 public void updateAccountHeader_shouldShowAccountName() throws Exception { 125 when(mAccountManager.getAuthenticatorTypesAsUser(anyInt())).thenReturn( 126 new AuthenticatorDescription[0]); 127 when(mAccountManager.getAccountsAsUser(anyInt())).thenReturn(new Account[0]); 128 when(mFragment.getContext()).thenReturn(mContext); 129 doReturn(mScreen).when(mFragment).getPreferenceScreen(); 130 doReturn(mPreference).when(mFragment).findPreference(PREF_ACCOUNT_HEADER); 131 132 mFragment.updateUi(); 133 134 verify(mPreference).setTitle("name1@abc.com"); 135 } 136 137 } 138