2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.android.settings.accounts;
18 import static com.android.settings.accounts.AccountDashboardFragmentTest
19 .ShadowAuthenticationHelper.LABELS;
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
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;
30 import com.android.settings.R;
31 import com.android.settings.TestConfig;
32 import com.android.settings.dashboard.SummaryLoader;
33 import com.android.settingslib.accounts.AuthenticatorHelper;
34 import com.android.settingslib.drawer.CategoryKey;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.Robolectric;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.annotation.Config;
44 import org.robolectric.annotation.Implementation;
45 import org.robolectric.annotation.Implements;
46 import org.robolectric.annotation.Resetter;
47 import org.robolectric.shadows.ShadowApplication;
49 import java.util.List;
51 @RunWith(RobolectricTestRunner.class)
52 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
53 public class AccountDashboardFragmentTest {
55 private AccountDashboardFragment mFragment;
59 MockitoAnnotations.initMocks(this);
60 mFragment = new AccountDashboardFragment();
64 public void tearDown() {
65 ShadowAuthenticationHelper.reset();
69 public void testCategory_isAccount() {
70 assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ACCOUNT);
75 ShadowAuthenticationHelper.class
77 public void updateSummary_hasAccount_shouldDisplayUpTo3AccountTypes() {
78 ShadowAuthenticationHelper.setHasAccount(true);
79 final SummaryLoader loader = mock(SummaryLoader.class);
80 final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
82 final SummaryLoader.SummaryProvider provider = mFragment.SUMMARY_PROVIDER_FACTORY
83 .createSummaryProvider(activity, loader);
84 provider.setListening(true);
86 verify(loader).setSummary(provider, LABELS[0] + ", " + LABELS[1] + ", " + LABELS[2]);
91 ShadowAuthenticationHelper.class
93 public void updateSummary_noAccount_shouldDisplayDefaultSummary() {
94 ShadowAuthenticationHelper.setHasAccount(false);
95 final SummaryLoader loader = mock(SummaryLoader.class);
96 final Activity activity = Robolectric.buildActivity(Activity.class).setup().get();
98 final SummaryLoader.SummaryProvider provider = mFragment.SUMMARY_PROVIDER_FACTORY
99 .createSummaryProvider(activity, loader);
100 provider.setListening(true);
102 verify(loader).setSummary(provider,
103 activity.getString(R.string.account_dashboard_default_summary));
107 public void testSearchIndexProvider_shouldIndexResource() {
108 final List<SearchIndexableResource> indexRes =
109 AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
110 ShadowApplication.getInstance().getApplicationContext(),
113 assertThat(indexRes).isNotNull();
114 assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
117 @Implements(AuthenticatorHelper.class)
118 public static class ShadowAuthenticationHelper {
120 static final String[] TYPES = new String[] {"type1", "type2", "type3", "type4"};
121 static final String[] LABELS = new String[] {"LABEL1", "LABEL2",
123 private static boolean sHasAccount = true;
125 public void __constructor__(Context context, UserHandle userHandle,
126 AuthenticatorHelper.OnAccountsUpdateListener listener) {
129 public static void setHasAccount(boolean hasAccount) {
130 sHasAccount = hasAccount;
134 public static void reset() {
139 public String[] getEnabledAccountTypes() {
140 return sHasAccount ? TYPES : null;
144 public CharSequence getLabelForType(Context context, final String accountType) {
145 if (TextUtils.equals(accountType, TYPES[0])) {
147 } else if (TextUtils.equals(accountType, TYPES[1])) {
149 } else if (TextUtils.equals(accountType, TYPES[2])) {
151 } else if (TextUtils.equals(accountType, TYPES[3])) {