• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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.car.settings.accounts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.when;
23 import static org.robolectric.RuntimeEnvironment.application;
24 import static org.testng.Assert.assertThrows;
25 
26 import android.accounts.Account;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.os.Bundle;
30 
31 import androidx.lifecycle.Lifecycle;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceGroup;
34 
35 import com.android.car.settings.CarSettingsRobolectricTestRunner;
36 import com.android.car.settings.common.ExtraSettingsLoader;
37 import com.android.car.settings.common.LogicalPreferenceGroup;
38 import com.android.car.settings.common.PreferenceControllerTestHelper;
39 import com.android.car.settings.testutils.ShadowAccountManager;
40 import com.android.car.settings.testutils.ShadowApplicationPackageManager;
41 import com.android.car.settings.testutils.ShadowContentResolver;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.RuntimeEnvironment;
49 import org.robolectric.annotation.Config;
50 
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
54 
55 /**
56  * Unit test for {@link AccountDetailsSettingController}.
57  */
58 @RunWith(CarSettingsRobolectricTestRunner.class)
59 @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class,
60         ShadowApplicationPackageManager.class})
61 public class AccountDetailsSettingControllerTest {
62 
63     private static final String ACCOUNT_NAME = "account_name";
64     private static final String MATCHING_ACCOUNT_TYPE = "account_type";
65     private static final String ACCOUNT_ACCESS_ID = "account_access_id";
66     private static final String METADATA_IA_ACCOUNT = "com.android.settings.ia.account";
67     private static final String NOT_MATCHING_ACCOUNT_TYPE = "com.android.settings.ia.account";
68 
69     private Context mContext;
70     private PreferenceGroup mPreference;
71     @Mock
72     private ExtraSettingsLoader mExtraSettingsLoader;
73     private AccountDetailsSettingController mAccountDetailsSettingController;
74     private Account mAccount = new Account(ACCOUNT_NAME, MATCHING_ACCOUNT_TYPE, ACCOUNT_ACCESS_ID);
75     private List<Preference> mPreferenceList;
76     private HashMap<Preference, Bundle> mPreferenceBundleMap;
77     PreferenceControllerTestHelper<AccountDetailsSettingController> mHelper;
78 
79     @Before
setUp()80     public void setUp() {
81         MockitoAnnotations.initMocks(this);
82         mContext = RuntimeEnvironment.application;
83 
84         mHelper = new PreferenceControllerTestHelper<>(application,
85                 AccountDetailsSettingController.class);
86 
87         mAccountDetailsSettingController = mHelper.getController();
88         mAccountDetailsSettingController.setAccount(mAccount);
89 
90         mPreferenceList = new ArrayList<>();
91         mPreferenceBundleMap = new HashMap<>();
92         mPreference = new LogicalPreferenceGroup(application);
93         mPreference.setIntent(new Intent());
94         mHelper.setPreference(mPreference);
95     }
96 
97     @Test
checkInitialized_accountSetAndUserHandleSet_doesNothing()98     public void checkInitialized_accountSetAndUserHandleSet_doesNothing() {
99         mAccountDetailsSettingController = new PreferenceControllerTestHelper<>(application,
100                 AccountDetailsSettingController.class).getController();
101         mAccountDetailsSettingController.setAccount(mAccount);
102 
103         mAccountDetailsSettingController.checkInitialized();
104     }
105 
106     @Test
checkInitialized_nullAccount_throwsIllegalStateException()107     public void checkInitialized_nullAccount_throwsIllegalStateException() {
108         assertThrows(IllegalStateException.class,
109                 () -> new PreferenceControllerTestHelper<>(mContext,
110                         AccountDetailsSettingController.class,
111                         new LogicalPreferenceGroup(mContext)));
112     }
113 
114     @Test
addExtraSettings_preferenceEmpty_shouldNotAddAnyPreferences()115     public void addExtraSettings_preferenceEmpty_shouldNotAddAnyPreferences() {
116         mHelper.markState(Lifecycle.State.STARTED);
117 
118         setupMockSettingLoaderAndRefreshUI();
119 
120         assertThat(mPreference.getPreferenceCount()).isEqualTo(0);
121     }
122 
123     @Test
addExtraSettings_preferenceEmpty_isNotVisible()124     public void addExtraSettings_preferenceEmpty_isNotVisible() {
125         mHelper.markState(Lifecycle.State.STARTED);
126 
127         setupMockSettingLoaderAndRefreshUI();
128 
129         assertThat(mPreference.isVisible()).isFalse();
130     }
131 
132     @Test
addExtraSettings_accountTypeNotEqual_shouldNotAddAnyPreferences()133     public void addExtraSettings_accountTypeNotEqual_shouldNotAddAnyPreferences() {
134         mHelper.markState(Lifecycle.State.STARTED);
135         Preference preference = new Preference(mContext);
136         mPreferenceList.add(preference);
137         Bundle bundle = new Bundle();
138         bundle.putString(METADATA_IA_ACCOUNT, NOT_MATCHING_ACCOUNT_TYPE);
139         mPreferenceBundleMap.put(preference, bundle);
140 
141         setupMockSettingLoaderAndRefreshUI();
142 
143         assertThat(mPreference.getPreferenceCount()).isEqualTo(0);
144     }
145 
146     @Test
addExtraSettings_accountTypeNotEqual_isNotVisible()147     public void addExtraSettings_accountTypeNotEqual_isNotVisible() {
148         mHelper.markState(Lifecycle.State.STARTED);
149         Preference preference = new Preference(mContext);
150         mPreferenceList.add(preference);
151         Bundle bundle = new Bundle();
152         bundle.putString(METADATA_IA_ACCOUNT, NOT_MATCHING_ACCOUNT_TYPE);
153         mPreferenceBundleMap.put(preference, bundle);
154 
155         setupMockSettingLoaderAndRefreshUI();
156 
157         assertThat(mPreference.isVisible()).isFalse();
158     }
159 
160     @Test
addExtraSettings_accountTypeEqual_shouldAddPreferences()161     public void addExtraSettings_accountTypeEqual_shouldAddPreferences() {
162         Preference preference = new Preference(mContext);
163         mPreferenceList.add(preference);
164         Bundle bundle = new Bundle();
165         bundle.putString(METADATA_IA_ACCOUNT, MATCHING_ACCOUNT_TYPE);
166         mPreferenceBundleMap.put(preference, bundle);
167 
168         setupMockSettingLoaderAndRefreshUI();
169         mHelper.sendLifecycleEvent(Lifecycle.Event.ON_CREATE);
170 
171         assertThat(mPreference.getPreferenceCount()).isEqualTo(1);
172     }
173 
174     @Test
addExtraSettings_accountTypeEqual_isVisible()175     public void addExtraSettings_accountTypeEqual_isVisible() {
176         Preference preference = new Preference(mContext);
177         mPreferenceList.add(preference);
178         Bundle bundle = new Bundle();
179         bundle.putString(METADATA_IA_ACCOUNT, MATCHING_ACCOUNT_TYPE);
180         mPreferenceBundleMap.put(preference, bundle);
181 
182         setupMockSettingLoaderAndRefreshUI();
183         mHelper.sendLifecycleEvent(Lifecycle.Event.ON_CREATE);
184 
185         assertThat(mPreference.isVisible()).isTrue();
186     }
187 
setupMockSettingLoaderAndRefreshUI()188     private void setupMockSettingLoaderAndRefreshUI() {
189         when(mExtraSettingsLoader.loadPreferences(any())).thenReturn(mPreferenceBundleMap);
190 
191         mAccountDetailsSettingController.setExtraSettingsLoader(mExtraSettingsLoader);
192         mAccountDetailsSettingController.refreshUi();
193     }
194 }
195