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 android.app.Activity; 19 import android.content.pm.UserInfo; 20 import android.os.Bundle; 21 import android.os.UserManager; 22 import android.provider.SearchIndexableResource; 23 24 import com.android.settings.R; 25 import com.android.settings.TestConfig; 26 import com.android.settings.dashboard.SummaryLoader; 27 import com.android.settingslib.drawer.CategoryKey; 28 import com.android.settingslib.drawer.Tile; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RobolectricTestRunner; 36 import org.robolectric.annotation.Config; 37 import org.robolectric.shadows.ShadowApplication; 38 39 import java.util.List; 40 41 import static com.google.common.truth.Truth.assertThat; 42 import static org.mockito.Matchers.anyInt; 43 import static org.mockito.Mockito.mock; 44 import static org.mockito.Mockito.verify; 45 import static org.mockito.Mockito.when; 46 47 @RunWith(RobolectricTestRunner.class) 48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 49 public class UserAndAccountDashboardFragmentTest { 50 51 private static final String METADATA_CATEGORY = "com.android.settings.category"; 52 private static final String METADATA_ACCOUNT_TYPE = "com.android.settings.ia.account"; 53 54 @Mock 55 private UserManager mUserManager; 56 private UserAndAccountDashboardFragment mFragment; 57 58 @Before setUp()59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 mFragment = new UserAndAccountDashboardFragment(); 62 } 63 64 @Test testCategory_isAccount()65 public void testCategory_isAccount() { 66 assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ACCOUNT); 67 } 68 69 @Test refreshDashboardTiles_HasAccountType_shouldNotDisplay()70 public void refreshDashboardTiles_HasAccountType_shouldNotDisplay() { 71 final Tile tile = new Tile(); 72 final Bundle metaData = new Bundle(); 73 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT); 74 metaData.putString(METADATA_ACCOUNT_TYPE, "com.abc"); 75 tile.metaData = metaData; 76 77 assertThat(mFragment.displayTile(tile)).isFalse(); 78 } 79 80 @Test refreshDashboardTiles_NoAccountType_shouldDisplay()81 public void refreshDashboardTiles_NoAccountType_shouldDisplay() { 82 final Tile tile = new Tile(); 83 final Bundle metaData = new Bundle(); 84 metaData.putString(METADATA_CATEGORY, CategoryKey.CATEGORY_ACCOUNT); 85 tile.metaData = metaData; 86 87 assertThat(mFragment.displayTile(tile)).isTrue(); 88 } 89 90 @Test updateSummary_shouldDisplaySignedInUser()91 public void updateSummary_shouldDisplaySignedInUser() { 92 final Activity activity = mock(Activity.class); 93 final SummaryLoader loader = mock(SummaryLoader.class); 94 final UserInfo userInfo = new UserInfo(); 95 userInfo.name = "test_name"; 96 97 when(activity.getSystemService(UserManager.class)).thenReturn(mUserManager); 98 when(mUserManager.getUserInfo(anyInt())).thenReturn(userInfo); 99 100 final SummaryLoader.SummaryProvider provider = mFragment.SUMMARY_PROVIDER_FACTORY 101 .createSummaryProvider(activity, loader); 102 provider.setListening(true); 103 104 verify(activity).getString(R.string.users_and_accounts_summary, 105 userInfo.name); 106 } 107 108 @Test testSearchIndexProvider_shouldIndexResource()109 public void testSearchIndexProvider_shouldIndexResource() { 110 final List<SearchIndexableResource> indexRes = 111 UserAndAccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex( 112 ShadowApplication.getInstance().getApplicationContext(), 113 true /* enabled */); 114 115 assertThat(indexRes).isNotNull(); 116 assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId()); 117 } 118 } 119