• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.Intent.EXTRA_USER;
19 
20 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYHINT;
21 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_TITLE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 
33 import android.accounts.Account;
34 import android.content.Context;
35 import android.content.Intent;
36 import android.content.pm.ActivityInfo;
37 import android.content.pm.PackageManager;
38 import android.content.pm.ResolveInfo;
39 import android.os.Bundle;
40 import android.os.UserHandle;
41 import android.os.UserManager;
42 
43 import androidx.fragment.app.FragmentActivity;
44 import androidx.preference.Preference;
45 
46 import com.android.settings.dashboard.DashboardFeatureProviderImpl;
47 import com.android.settings.testutils.shadow.ShadowAccountManager;
48 import com.android.settings.testutils.shadow.ShadowUserManager;
49 import com.android.settingslib.drawer.ActivityTile;
50 import com.android.settingslib.drawer.CategoryKey;
51 import com.android.settingslib.drawer.Tile;
52 
53 import org.junit.After;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.robolectric.Robolectric;
58 import org.robolectric.RobolectricTestRunner;
59 import org.robolectric.RuntimeEnvironment;
60 import org.robolectric.Shadows;
61 import org.robolectric.annotation.Config;
62 import org.robolectric.shadow.api.Shadow;
63 import org.robolectric.util.ReflectionHelpers;
64 
65 @RunWith(RobolectricTestRunner.class)
66 @Config(shadows = {
67         ShadowAccountManager.class,
68         ShadowUserManager.class,
69         com.android.settings.testutils.shadow.ShadowDashboardFragment.class,
70         com.android.settings.testutils.shadow.ShadowFragment.class,
71 })
72 public class AccountDetailDashboardFragmentTest {
73 
74     private static final String METADATA_CATEGORY = "com.android.settings.category";
75     private static final String METADATA_ACCOUNT_TYPE = "com.android.settings.ia.account";
76     private static final String METADATA_USER_HANDLE = "user_handle";
77 
78     private AccountDetailDashboardFragment mFragment;
79     private Context mContext;
80     private ActivityInfo mActivityInfo;
81 
82     @Before
setUp()83     public void setUp() {
84         mContext = RuntimeEnvironment.application;
85         mActivityInfo = new ActivityInfo();
86         mActivityInfo.packageName = mContext.getPackageName();
87         mActivityInfo.name = "clazz";
88         mActivityInfo.metaData = new Bundle();
89 
90         final Bundle args = new Bundle();
91         args.putParcelable(METADATA_USER_HANDLE, UserHandle.CURRENT);
92 
93         mFragment = spy(new AccountDetailDashboardFragment());
94         mFragment.setArguments(args);
95         mFragment.mAccountType = "com.abc";
96         mFragment.mAccount = new Account("name1@abc.com", "com.abc");
97         when(mFragment.getContext()).thenReturn(mContext);
98     }
99 
100     @After
tearDown()101     public void tearDown() {
102         ShadowAccountManager.reset();
103     }
104 
105     @Test
testCategory_isAccountDetail()106     public void testCategory_isAccountDetail() {
107         assertThat(new AccountDetailDashboardFragment().getCategoryKey())
108                 .isEqualTo(CategoryKey.CATEGORY_ACCOUNT_DETAIL);
109     }
110 
111     @Test
refreshDashboardTiles_HasAccountType_shouldDisplay()112     public void refreshDashboardTiles_HasAccountType_shouldDisplay() {
113         final Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
114         mActivityInfo.metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
115         mActivityInfo.metaData.putString(METADATA_ACCOUNT_TYPE, "com.abc");
116 
117         assertThat(mFragment.displayTile(tile)).isTrue();
118     }
119 
120     @Test
refreshDashboardTiles_NoAccountType_shouldNotDisplay()121     public void refreshDashboardTiles_NoAccountType_shouldNotDisplay() {
122         final Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
123         mActivityInfo.metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
124 
125         assertThat(mFragment.displayTile(tile)).isFalse();
126     }
127 
128     @Test
refreshDashboardTiles_OtherAccountType_shouldNotDisplay()129     public void refreshDashboardTiles_OtherAccountType_shouldNotDisplay() {
130         final Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
131         mActivityInfo.metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
132         mActivityInfo.metaData.putString(METADATA_ACCOUNT_TYPE, "com.other");
133 
134         assertThat(mFragment.displayTile(tile)).isFalse();
135     }
136 
137     @Test
refreshDashboardTiles_HasAccountType_shouldAddAccountNameToIntent()138     public void refreshDashboardTiles_HasAccountType_shouldAddAccountNameToIntent() {
139         final DashboardFeatureProviderImpl dashboardFeatureProvider =
140                 new DashboardFeatureProviderImpl(mContext);
141         final PackageManager packageManager = mock(PackageManager.class);
142         ReflectionHelpers.setField(dashboardFeatureProvider, "mPackageManager", packageManager);
143         when(packageManager.resolveActivity(any(Intent.class), anyInt()))
144                 .thenReturn(mock(ResolveInfo.class));
145 
146         final Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
147         mActivityInfo.metaData.putString(META_DATA_PREFERENCE_KEYHINT, "key");
148         mActivityInfo.metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT);
149         mActivityInfo.metaData.putString(METADATA_ACCOUNT_TYPE, "com.abc");
150         mActivityInfo.metaData.putString(META_DATA_PREFERENCE_TITLE, "summary");
151         mActivityInfo.metaData.putString("com.android.settings.intent.action",
152                 Intent.ACTION_ASSIST);
153         tile.userHandle = null;
154         mFragment.displayTile(tile);
155 
156         final FragmentActivity activity = Robolectric.setupActivity(FragmentActivity.class);
157         final Preference preference = new Preference(mContext);
158         dashboardFeatureProvider.bindPreferenceToTileAndGetObservers(activity, mFragment,
159                 false /* forceRoundedIcon */, preference, tile, null /* key */,
160                 Preference.DEFAULT_ORDER);
161 
162         assertThat(preference.getKey()).isEqualTo(tile.getKey(mContext));
163         preference.performClick();
164 
165         final Intent intent = Shadows.shadowOf(activity).getNextStartedActivityForResult().intent;
166 
167         assertThat(intent.getStringExtra("extra.accountName")).isEqualTo("name1@abc.com");
168     }
169 
170     @Test
displayTile_shouldAddUserHandleToTileIntent()171     public void displayTile_shouldAddUserHandleToTileIntent() {
172         mFragment.mUserHandle = new UserHandle(1);
173 
174         final Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_ACCOUNT_DETAIL);
175         mActivityInfo.metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT);
176         mActivityInfo.metaData.putString(METADATA_ACCOUNT_TYPE, "com.abc");
177 
178         mFragment.displayTile(tile);
179 
180         final UserHandle userHandle = tile.getIntent().getParcelableExtra(EXTRA_USER);
181         assertThat(userHandle.getIdentifier()).isEqualTo(1);
182     }
183 
184     @Test
onResume_accountMissing_shouldFinish()185     public void onResume_accountMissing_shouldFinish() {
186         ShadowUserManager userManager =
187             Shadow.extract(mContext.getSystemService(UserManager.class));
188 
189         userManager.addUserProfile(new UserHandle(1));
190         ShadowAccountManager.addAccountForUser(1, new Account("test@test.com", "com.test"));
191 
192         mFragment.finishIfAccountMissing();
193         verify(mFragment).finish();
194     }
195 
196     @Test
onResume_accountPresentOneProfile_shouldNotFinish()197     public void onResume_accountPresentOneProfile_shouldNotFinish() {
198         ShadowUserManager userManager =
199             Shadow.extract(mContext.getSystemService(UserManager.class));
200 
201         userManager.addUserProfile(new UserHandle(1));
202         ShadowAccountManager.addAccountForUser(1, mFragment.mAccount);
203 
204         mFragment.finishIfAccountMissing();
205         verify(mFragment, never()).finish();
206     }
207 
208     @Test
onResume_accountPresentTwoProfiles_shouldNotFinish()209     public void onResume_accountPresentTwoProfiles_shouldNotFinish() {
210         ShadowUserManager userManager =
211             Shadow.extract(mContext.getSystemService(UserManager.class));
212 
213         userManager.addUserProfile(new UserHandle(1));
214         userManager.addUserProfile(new UserHandle(2));
215         ShadowAccountManager.addAccountForUser(1, new Account("test@test.com", "com.test"));
216         ShadowAccountManager.addAccountForUser(2, mFragment.mAccount);
217 
218         mFragment.finishIfAccountMissing();
219         verify(mFragment, never()).finish();
220     }
221 }
222