OSDN Git Service

Merge "Zen Condition text and primary click changes"
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / accounts / AccountDashboardFragmentTest.java
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
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;
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.TestConfig;
32 import com.android.settings.dashboard.SummaryLoader;
33 import com.android.settingslib.accounts.AuthenticatorHelper;
34 import com.android.settingslib.drawer.CategoryKey;
35
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;
48
49 import java.util.List;
50
51 @RunWith(RobolectricTestRunner.class)
52 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
53 public class AccountDashboardFragmentTest {
54
55     private AccountDashboardFragment mFragment;
56
57     @Before
58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mFragment = new AccountDashboardFragment();
61     }
62
63     @After
64     public void tearDown() {
65         ShadowAuthenticationHelper.reset();
66     }
67
68     @Test
69     public void testCategory_isAccount() {
70         assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ACCOUNT);
71     }
72
73     @Test
74     @Config(shadows = {
75             ShadowAuthenticationHelper.class
76     })
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();
81
82         final SummaryLoader.SummaryProvider provider = mFragment.SUMMARY_PROVIDER_FACTORY
83                 .createSummaryProvider(activity, loader);
84         provider.setListening(true);
85
86         verify(loader).setSummary(provider, LABELS[0] + ", " + LABELS[1] + ", " + LABELS[2]);
87     }
88
89     @Test
90     @Config(shadows = {
91             ShadowAuthenticationHelper.class
92     })
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();
97
98         final SummaryLoader.SummaryProvider provider = mFragment.SUMMARY_PROVIDER_FACTORY
99                 .createSummaryProvider(activity, loader);
100         provider.setListening(true);
101
102         verify(loader).setSummary(provider,
103                 activity.getString(R.string.account_dashboard_default_summary));
104     }
105
106     @Test
107     public void testSearchIndexProvider_shouldIndexResource() {
108         final List<SearchIndexableResource> indexRes =
109                 AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
110                         ShadowApplication.getInstance().getApplicationContext(),
111                         true /* enabled */);
112
113         assertThat(indexRes).isNotNull();
114         assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
115     }
116
117     @Implements(AuthenticatorHelper.class)
118     public static class ShadowAuthenticationHelper {
119
120         static final String[] TYPES = new String[] {"type1", "type2", "type3", "type4"};
121         static final String[] LABELS = new String[] {"LABEL1", "LABEL2",
122                 "LABEL3", "LABEL4"};
123         private static boolean sHasAccount = true;
124
125         public void __constructor__(Context context, UserHandle userHandle,
126                 AuthenticatorHelper.OnAccountsUpdateListener listener) {
127         }
128
129         public static void setHasAccount(boolean hasAccount) {
130             sHasAccount = hasAccount;
131         }
132
133         @Resetter
134         public static void reset() {
135             sHasAccount = true;
136         }
137
138         @Implementation
139         public String[] getEnabledAccountTypes() {
140             return sHasAccount ? TYPES : null;
141         }
142
143         @Implementation
144         public CharSequence getLabelForType(Context context, final String accountType) {
145             if (TextUtils.equals(accountType, TYPES[0])) {
146                 return LABELS[0];
147             } else if (TextUtils.equals(accountType, TYPES[1])) {
148                 return LABELS[1];
149             } else if (TextUtils.equals(accountType, TYPES[2])) {
150                 return LABELS[2];
151             } else if (TextUtils.equals(accountType, TYPES[3])) {
152                 return LABELS[3];
153             }
154             return "no_label";
155         }
156     }
157 }