Index user accounts.
Index user accounts in AccountDashboardFragment if there is no managed user profile. Fixes: 146478761 Test: manual & robotest Change-Id: I719dc6ee8030a11f1cbfacc8f24419e9e4c3ca18
This commit is contained in:
@@ -17,8 +17,13 @@ package com.android.settings.accounts;
|
|||||||
|
|
||||||
import static android.provider.Settings.EXTRA_AUTHORITIES;
|
import static android.provider.Settings.EXTRA_AUTHORITIES;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
|
import android.accounts.AccountManager;
|
||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
import android.os.UserManager;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
@@ -97,7 +102,26 @@ public class AccountDashboardFragment extends DashboardFragment {
|
|||||||
@Override
|
@Override
|
||||||
public List<SearchIndexableRaw> getDynamicRawDataToIndex(Context context,
|
public List<SearchIndexableRaw> getDynamicRawDataToIndex(Context context,
|
||||||
boolean enabled) {
|
boolean enabled) {
|
||||||
return null;
|
final List<SearchIndexableRaw> indexRaws = new ArrayList<>();
|
||||||
|
final UserManager userManager = (UserManager) context.getSystemService(
|
||||||
|
Context.USER_SERVICE);
|
||||||
|
final List<UserInfo> profiles = userManager.getProfiles(UserHandle.myUserId());
|
||||||
|
for (final UserInfo userInfo : profiles) {
|
||||||
|
if (userInfo.isManagedProfile()) {
|
||||||
|
return indexRaws;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final AccountManager accountManager = AccountManager.get(context);
|
||||||
|
final Account[] accounts = accountManager.getAccounts();
|
||||||
|
for (Account account : accounts) {
|
||||||
|
final SearchIndexableRaw raw = new SearchIndexableRaw(context);
|
||||||
|
raw.key = AccountTypePreference.buildKey(account);
|
||||||
|
raw.title = account.name;
|
||||||
|
indexRaws.add(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
return indexRaws;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -17,26 +17,55 @@ package com.android.settings.accounts;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
|
import android.accounts.AccountManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
|
import android.os.UserManager;
|
||||||
import android.provider.SearchIndexableResource;
|
import android.provider.SearchIndexableResource;
|
||||||
|
|
||||||
import com.android.settingslib.drawer.CategoryKey;
|
import com.android.settingslib.drawer.CategoryKey;
|
||||||
|
import com.android.settingslib.search.SearchIndexableRaw;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class AccountDashboardFragmentTest {
|
public class AccountDashboardFragmentTest {
|
||||||
|
|
||||||
|
private static final int PROFILE_ID = 10;
|
||||||
|
private static final String PROFILE_NAME = "User";
|
||||||
|
private static final String ACCOUNT_TYPE = "com.android.settings";
|
||||||
|
private static final String ACCOUNT_NAME = "test account";
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserManager mUserManager;
|
||||||
|
@Mock
|
||||||
|
private AccountManager mAccountManager;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
private AccountDashboardFragment mFragment;
|
private AccountDashboardFragment mFragment;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mFragment = new AccountDashboardFragment();
|
mFragment = new AccountDashboardFragment();
|
||||||
|
|
||||||
|
doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -45,7 +74,7 @@ public class AccountDashboardFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSearchIndexProvider_shouldIndexResource() {
|
public void searchIndexProvider_shouldIndexResource() {
|
||||||
final List<SearchIndexableResource> indexRes =
|
final List<SearchIndexableResource> indexRes =
|
||||||
AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
|
AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
|
||||||
.getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */);
|
.getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */);
|
||||||
@@ -53,4 +82,36 @@ public class AccountDashboardFragmentTest {
|
|||||||
assertThat(indexRes).isNotNull();
|
assertThat(indexRes).isNotNull();
|
||||||
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
|
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void searchIndexProvider_hasManagedProfile_shouldNotIndex() {
|
||||||
|
final List<UserInfo> infos = new ArrayList<>();
|
||||||
|
infos.add(new UserInfo(PROFILE_ID, PROFILE_NAME, UserInfo.FLAG_MANAGED_PROFILE));
|
||||||
|
doReturn(infos).when(mUserManager).getProfiles(anyInt());
|
||||||
|
|
||||||
|
final List<SearchIndexableRaw> indexRaws =
|
||||||
|
AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
|
||||||
|
.getDynamicRawDataToIndex(mContext, true /* enabled */);
|
||||||
|
|
||||||
|
assertThat(indexRaws).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void searchIndexProvider_hasAccounts_shouldIndex() {
|
||||||
|
final List<UserInfo> infos = new ArrayList<>();
|
||||||
|
infos.add(new UserInfo(PROFILE_ID, PROFILE_NAME, UserInfo.FLAG_PRIMARY));
|
||||||
|
doReturn(infos).when(mUserManager).getProfiles(anyInt());
|
||||||
|
|
||||||
|
final Account[] accounts = {
|
||||||
|
new Account(ACCOUNT_NAME, ACCOUNT_TYPE)
|
||||||
|
};
|
||||||
|
when(AccountManager.get(mContext)).thenReturn(mAccountManager);
|
||||||
|
doReturn(accounts).when(mAccountManager).getAccounts();
|
||||||
|
|
||||||
|
final List<SearchIndexableRaw> indexRaws =
|
||||||
|
AccountDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
|
||||||
|
.getDynamicRawDataToIndex(mContext, true /* enabled */);
|
||||||
|
|
||||||
|
assertThat(indexRaws).isNotEmpty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user