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 static com.android.settings.accounts.AccountDashboardFragmentTest.ShadowAuthenticationHelper.LABELS; 19 import static com.android.settings.accounts.AccountDashboardFragmentTest.ShadowAuthenticationHelper.TYPES; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 24 import android.app.Activity; 25 import android.content.Context; 26 import android.os.UserHandle; 27 import android.provider.SearchIndexableResource; 28 import android.text.TextUtils; 29 30 import com.android.settings.R; 31 import com.android.settings.dashboard.SummaryLoader; 32 import com.android.settingslib.accounts.AuthenticatorHelper; 33 import com.android.settingslib.drawer.CategoryKey; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.robolectric.Robolectric; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 import org.robolectric.annotation.Config; 43 import org.robolectric.annotation.Implementation; 44 import org.robolectric.annotation.Implements; 45 import org.robolectric.annotation.Resetter; 46 47 import java.util.List; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class AccountDashboardFragmentTest { 51 52 private AccountDashboardFragment mFragment; 53 54 @Before setUp()55 public void setUp() { 56 mFragment = new AccountDashboardFragment(); 57 } 58 59 @After tearDown()60 public void tearDown() { 61 ShadowAuthenticationHelper.reset(); 62 } 63 64 @Test testCategory_isAccount()65 public void testCategory_isAccount() { 66 assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ACCOUNT); 67 } 68 69 @Test 70 @Config(shadows = { 71 ShadowAuthenticationHelper.class 72 }) updateSummary_hasAccount_shouldDisplayUpTo3AccountTypes()73 public void updateSummary_hasAccount_shouldDisplayUpTo3AccountTypes() { 74 final SummaryLoader loader = mock(SummaryLoader.class); 75 final Activity activity = Robolectric.buildActivity(Activity.class).setup().get(); 76 77 final SummaryLoader.SummaryProvider provider = 78 AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity, loader); 79 provider.setListening(true); 80 81 verify(loader).setSummary(provider, LABELS[0] + ", " + LABELS[1] + ", " + LABELS[2]); 82 } 83 84 @Test 85 @Config(shadows = ShadowAuthenticationHelper.class) updateSummary_noAccount_shouldDisplayDefaultSummary()86 public void updateSummary_noAccount_shouldDisplayDefaultSummary() { 87 ShadowAuthenticationHelper.setEnabledAccount(null); 88 final SummaryLoader loader = mock(SummaryLoader.class); 89 final Activity activity = Robolectric.buildActivity(Activity.class).setup().get(); 90 91 final SummaryLoader.SummaryProvider provider = 92 AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity, loader); 93 provider.setListening(true); 94 95 verify(loader).setSummary(provider, 96 activity.getString(R.string.account_dashboard_default_summary)); 97 } 98 99 @Test 100 @Config(shadows = ShadowAuthenticationHelper.class) updateSummary_noAccountTypeLabel_shouldNotDisplayNullEntry()101 public void updateSummary_noAccountTypeLabel_shouldNotDisplayNullEntry() { 102 final SummaryLoader loader = mock(SummaryLoader.class); 103 final Activity activity = Robolectric.buildActivity(Activity.class).setup().get(); 104 final String[] enabledAccounts = {TYPES[0], "unlabeled_account_type", TYPES[1]}; 105 ShadowAuthenticationHelper.setEnabledAccount(enabledAccounts); 106 107 final SummaryLoader.SummaryProvider provider = 108 AccountDashboardFragment.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(activity, loader); 109 provider.setListening(true); 110 111 // should only show the 2 accounts with labels 112 verify(loader).setSummary(provider, LABELS[0] + ", " + LABELS[1]); 113 } 114 115 @Test testSearchIndexProvider_shouldIndexResource()116 public void testSearchIndexProvider_shouldIndexResource() { 117 final List<SearchIndexableResource> indexRes = 118 AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 119 .getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */); 120 121 assertThat(indexRes).isNotNull(); 122 assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId()); 123 } 124 125 @Implements(AuthenticatorHelper.class) 126 public static class ShadowAuthenticationHelper { 127 128 static final String[] TYPES = {"type1", "type2", "type3", "type4"}; 129 static final String[] LABELS = {"LABEL1", "LABEL2", "LABEL3", "LABEL4"}; 130 private static String[] sEnabledAccount = TYPES; 131 __constructor__(Context context, UserHandle userHandle, AuthenticatorHelper.OnAccountsUpdateListener listener)132 public void __constructor__(Context context, UserHandle userHandle, 133 AuthenticatorHelper.OnAccountsUpdateListener listener) { 134 } 135 setEnabledAccount(String[] enabledAccount)136 private static void setEnabledAccount(String[] enabledAccount) { 137 sEnabledAccount = enabledAccount; 138 } 139 140 @Resetter reset()141 public static void reset() { 142 sEnabledAccount = TYPES; 143 } 144 145 @Implementation getEnabledAccountTypes()146 public String[] getEnabledAccountTypes() { 147 return sEnabledAccount; 148 } 149 150 @Implementation getLabelForType(Context context, final String accountType)151 public CharSequence getLabelForType(Context context, final String accountType) { 152 if (TextUtils.equals(accountType, TYPES[0])) { 153 return LABELS[0]; 154 } else if (TextUtils.equals(accountType, TYPES[1])) { 155 return LABELS[1]; 156 } else if (TextUtils.equals(accountType, TYPES[2])) { 157 return LABELS[2]; 158 } else if (TextUtils.equals(accountType, TYPES[3])) { 159 return LABELS[3]; 160 } 161 return null; 162 } 163 } 164 } 165