1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings.accounts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 
23 import com.android.settings.R;
24 import com.android.settings.testutils.shadow.ShadowAuthenticationHelper;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.robolectric.RobolectricTestRunner;
31 import org.robolectric.RuntimeEnvironment;
32 import org.robolectric.annotation.Config;
33 
34 @RunWith(RobolectricTestRunner.class)
35 @Config(shadows = {ShadowAuthenticationHelper.class})
36 public class TopLevelAccountEntryPreferenceControllerTest {
37 
38     private TopLevelAccountEntryPreferenceController mController;
39     private Context mContext;
40     private String[] LABELS;
41     private String[] TYPES;
42 
43     @Before
setUp()44     public void setUp() {
45         mContext = RuntimeEnvironment.application;
46         mController = new TopLevelAccountEntryPreferenceController(mContext, "test_key");
47         LABELS = ShadowAuthenticationHelper.getLabels();
48         TYPES = ShadowAuthenticationHelper.getTypes();
49     }
50 
51     @After
tearDown()52     public void tearDown() {
53         ShadowAuthenticationHelper.reset();
54     }
55 
56     @Test
updateSummary_hasAccount_shouldDisplayUpTo3AccountTypes()57     public void updateSummary_hasAccount_shouldDisplayUpTo3AccountTypes() {
58         assertThat(mController.getSummary())
59                 .isEqualTo(LABELS[0] + ", " + LABELS[1] + ", and " + LABELS[2]);
60     }
61 
62     @Test
updateSummary_noAccount_shouldDisplayDefaultSummary()63     public void updateSummary_noAccount_shouldDisplayDefaultSummary() {
64         ShadowAuthenticationHelper.setEnabledAccount(null);
65 
66         assertThat(mController.getSummary()).isEqualTo(
67                 mContext.getText(R.string.account_dashboard_default_summary));
68     }
69 
70     @Test
updateSummary_noAccountTypeLabel_shouldNotDisplayNullEntry()71     public void updateSummary_noAccountTypeLabel_shouldNotDisplayNullEntry() {
72         final String[] enabledAccounts = {TYPES[0], "unlabeled_account_type", TYPES[1]};
73         ShadowAuthenticationHelper.setEnabledAccount(enabledAccounts);
74 
75 
76         // should only show the 2 accounts with labels
77         assertThat(mController.getSummary()).isEqualTo(LABELS[0] + " and " + LABELS[1]);
78     }
79 }
80