Merge "Return the correct position for the profile tab" into main

This commit is contained in:
Manish Singh
2024-03-06 19:33:41 +00:00
committed by Android (Google) Code Review
2 changed files with 41 additions and 6 deletions

View File

@@ -230,7 +230,8 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
if (bundle != null) { if (bundle != null) {
final int extraTab = bundle.getInt(SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, -1); final int extraTab = bundle.getInt(SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, -1);
if (extraTab != -1) { if (extraTab != -1) {
return ((ViewPagerAdapter) mViewPager.getAdapter()).getTabForPosition(extraTab); return ((ViewPagerAdapter) mViewPager.getAdapter())
.getPositionForProfileTab(extraTab);
} }
final int userId = bundle.getInt(EXTRA_USER_ID, UserHandle.SYSTEM.getIdentifier()); final int userId = bundle.getInt(EXTRA_USER_ID, UserHandle.SYSTEM.getIdentifier());
final boolean isWorkProfile = UserManager.get(activity).isManagedProfile(userId); final boolean isWorkProfile = UserManager.get(activity).isManagedProfile(userId);
@@ -410,7 +411,22 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
} }
@ProfileType @ProfileType
int profileType = mChildFragments[position].getArguments().getInt(EXTRA_PROFILE); int profileType = mChildFragments[position].getArguments().getInt(EXTRA_PROFILE);
return profileTypeToTab(profileType);
}
private int getPositionForProfileTab(int profileTab) {
for (int i = 0; i < mChildFragments.length; ++i) {
Bundle arguments = mChildFragments[i].getArguments();
if (arguments != null
&& profileTypeToTab(arguments.getInt(EXTRA_PROFILE)) == profileTab) {
return i;
}
}
Log.e(TAG, "position requested for an unknown profile tab " + profileTab);
return 0;
}
private int profileTypeToTab(@ProfileType int profileType) {
if (profileType == ProfileType.WORK) { if (profileType == ProfileType.WORK) {
return WORK_TAB; return WORK_TAB;
} }

View File

@@ -118,7 +118,9 @@ public class ProfileSelectFragmentTest {
profileSelectFragment.setViewPager(viewPager); profileSelectFragment.setViewPager(viewPager);
mFragmentManager.beginTransaction().add(profileSelectFragment, "tag"); mFragmentManager.beginTransaction().add(profileSelectFragment, "tag");
assertThat(mFragment.getTabId(mActivity, bundle)).isEqualTo(WORK_TAB); // The expected position '2' comes from the order in which fragments are added in
// TestProfileSelectFragment#getFragments()
assertThat(mFragment.getTabId(mActivity, bundle)).isEqualTo(2);
} }
@Test @Test
@@ -136,7 +138,9 @@ public class ProfileSelectFragmentTest {
profileSelectFragment.setViewPager(viewPager); profileSelectFragment.setViewPager(viewPager);
mFragmentManager.beginTransaction().add(profileSelectFragment, "tag"); mFragmentManager.beginTransaction().add(profileSelectFragment, "tag");
assertThat(mFragment.getTabId(mActivity, bundle)).isEqualTo(PRIVATE_TAB); // The expected position '1' comes from the order in which fragments are added in
// TestProfileSelectFragment#getFragments()
assertThat(mFragment.getTabId(mActivity, bundle)).isEqualTo(1);
} }
@Test @Test
@@ -343,10 +347,25 @@ public class ProfileSelectFragmentTest {
@Override @Override
public Fragment[] getFragments() { public Fragment[] getFragments() {
Fragment personalFragment = new SettingsPreferenceFragmentTest.TestFragment();
Bundle personalBundle = new Bundle();
personalBundle.putInt(EXTRA_PROFILE, ProfileType.PERSONAL);
personalFragment.setArguments(personalBundle);
Fragment workFragment = new SettingsPreferenceFragmentTest.TestFragment();
Bundle workBundle = new Bundle();
workBundle.putInt(EXTRA_PROFILE, ProfileType.WORK);
workFragment.setArguments(workBundle);
Fragment privateFragment = new SettingsPreferenceFragmentTest.TestFragment();
Bundle privateBundle = new Bundle();
privateBundle.putInt(EXTRA_PROFILE, ProfileType.PRIVATE);
privateFragment.setArguments(privateBundle);
return new Fragment[]{ return new Fragment[]{
new SettingsPreferenceFragmentTest.TestFragment(), //0 personalFragment, //0
new SettingsPreferenceFragmentTest.TestFragment(), privateFragment,
new SettingsPreferenceFragmentTest.TestFragment() workFragment
}; };
} }
} }