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.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.accounts.Account; 26 import android.accounts.AccountManager; 27 import android.content.Context; 28 import android.content.pm.UserInfo; 29 import android.os.UserManager; 30 import android.provider.SearchIndexableResource; 31 32 import com.android.settingslib.drawer.CategoryKey; 33 import com.android.settingslib.search.SearchIndexableRaw; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 43 import java.util.ArrayList; 44 import java.util.List; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class AccountDashboardFragmentTest { 48 49 private static final int PROFILE_ID = 10; 50 private static final String PROFILE_NAME = "User"; 51 private static final String ACCOUNT_TYPE = "com.android.settings"; 52 private static final String ACCOUNT_NAME = "test account"; 53 54 @Mock 55 private UserManager mUserManager; 56 @Mock 57 private AccountManager mAccountManager; 58 59 private Context mContext; 60 private AccountDashboardFragment mFragment; 61 62 @Before setUp()63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mContext = spy(RuntimeEnvironment.application); 66 mFragment = new AccountDashboardFragment(); 67 68 doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE); 69 } 70 71 @Test testCategory_isAccount()72 public void testCategory_isAccount() { 73 assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_ACCOUNT); 74 } 75 76 @Test searchIndexProvider_shouldIndexResource()77 public void searchIndexProvider_shouldIndexResource() { 78 final List<SearchIndexableResource> indexRes = 79 AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 80 .getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */); 81 82 assertThat(indexRes).isNotNull(); 83 assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId()); 84 } 85 86 @Test searchIndexProvider_hasManagedProfile_shouldNotIndex()87 public void searchIndexProvider_hasManagedProfile_shouldNotIndex() { 88 final List<UserInfo> infos = new ArrayList<>(); 89 infos.add(new UserInfo(PROFILE_ID, PROFILE_NAME, UserInfo.FLAG_MANAGED_PROFILE)); 90 doReturn(infos).when(mUserManager).getProfiles(anyInt()); 91 92 final List<SearchIndexableRaw> indexRaws = 93 AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 94 .getDynamicRawDataToIndex(mContext, true /* enabled */); 95 96 assertThat(indexRaws).isEmpty(); 97 } 98 99 @Test searchIndexProvider_hasAccounts_shouldIndex()100 public void searchIndexProvider_hasAccounts_shouldIndex() { 101 final List<UserInfo> infos = new ArrayList<>(); 102 infos.add(new UserInfo(PROFILE_ID, PROFILE_NAME, UserInfo.FLAG_PRIMARY)); 103 doReturn(infos).when(mUserManager).getProfiles(anyInt()); 104 105 final Account[] accounts = { 106 new Account(ACCOUNT_NAME, ACCOUNT_TYPE) 107 }; 108 when(AccountManager.get(mContext)).thenReturn(mAccountManager); 109 doReturn(accounts).when(mAccountManager).getAccounts(); 110 111 final List<SearchIndexableRaw> indexRaws = 112 AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 113 .getDynamicRawDataToIndex(mContext, true /* enabled */); 114 115 assertThat(indexRaws).isNotEmpty(); 116 } 117 118 @Test shouldSkipForInitialSUW_returnTrue()119 public void shouldSkipForInitialSUW_returnTrue() { 120 assertThat(mFragment.shouldSkipForInitialSUW()).isTrue(); 121 } 122 } 123