diff --git a/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java b/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java index 7a35aa6037c..de23ec00f1f 100644 --- a/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java +++ b/src/com/android/settings/dashboard/profileselector/ProfileSelectFragment.java @@ -17,8 +17,10 @@ package com.android.settings.dashboard.profileselector; import android.annotation.IntDef; +import android.app.Activity; import android.content.Context; import android.os.Bundle; +import android.os.UserManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -91,7 +93,22 @@ public abstract class ProfileSelectFragment extends DashboardFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContentView = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); - final int selected = getArguments().getInt(SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, 0); + final Activity activity = getActivity(); + final int intentUser = activity.getIntent().getContentUserHint(); + int selectedTab = 0; + + // Start intent from a specific user eg: adb shell --user 10 + if (intentUser > 0 && Utils.getManagedProfile(UserManager.get(activity)).getIdentifier() + == intentUser) { + selectedTab = WORK_TAB; + } + + // Set selected tab using fragment argument + final int extraTab = getArguments() != null ? getArguments().getInt( + SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, -1) : -1; + if (extraTab != -1) { + selectedTab = extraTab; + } final View tabContainer = mContentView.findViewById(R.id.tab_container); final ViewPager viewPager = tabContainer.findViewById(R.id.view_pager); @@ -99,7 +116,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment { final TabLayout tabs = tabContainer.findViewById(R.id.tabs); tabs.setupWithViewPager(viewPager); tabContainer.setVisibility(View.VISIBLE); - final TabLayout.Tab tab = tabs.getTabAt(selected); + final TabLayout.Tab tab = tabs.getTabAt(selectedTab); tab.select(); final FrameLayout listContainer = mContentView.findViewById(android.R.id.list_container); @@ -109,7 +126,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment { final RecyclerView recyclerView = getListView(); recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER); - Utils.setActionBarShadowAnimation(getActivity(), getSettingsLifecycle(), recyclerView); + Utils.setActionBarShadowAnimation(activity, getSettingsLifecycle(), recyclerView); return mContentView; } diff --git a/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java new file mode 100644 index 00000000000..8580b4ca85d --- /dev/null +++ b/tests/robotests/src/com/android/settings/dashboard/profileselector/ProfileSelectFragmentTest.java @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.dashboard.profileselector; + +import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.PERSONAL_TAB; +import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.WORK_TAB; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.preference.PreferenceFragmentCompat; +import androidx.recyclerview.widget.RecyclerView; + +import com.android.settings.R; +import com.android.settings.SettingsActivity; +import com.android.settings.SettingsPreferenceFragmentTest; + +import com.google.android.material.tabs.TabLayout; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.shadows.androidx.fragment.FragmentController; + +@RunWith(RobolectricTestRunner.class) +public class ProfileSelectFragmentTest { + + private Context mContext; + private TestProfileSelectFragment mFragment; + private FragmentActivity mActivity; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mActivity = spy(Robolectric.buildActivity(SettingsActivity.class).get()); + mFragment = spy(new TestProfileSelectFragment()); + when(mFragment.getContext()).thenReturn(mContext); + when(mFragment.getActivity()).thenReturn(mActivity); + } + + @Test + @Config(shadows = ShadowPreferenceFragmentCompat.class) + public void onCreateView_no_setCorrectTab() { + FragmentController.of(mFragment, new Intent()).create(0 /* containerViewId*/, + null /* bundle */).start().resume().visible().get(); + final TabLayout tabs = mFragment.getView().findViewById(R.id.tabs); + + assertThat(tabs.getSelectedTabPosition()).isEqualTo(PERSONAL_TAB); + } + + @Test + @Config(shadows = ShadowPreferenceFragmentCompat.class) + public void onCreateView_setArgument_setCorrectTab() { + final Bundle bundle = new Bundle(); + bundle.putInt(SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, WORK_TAB); + mFragment.setArguments(bundle); + + FragmentController.of(mFragment, new Intent()).create(0 /* containerViewId*/, + bundle).start().resume().visible().get(); + final TabLayout tabs = mFragment.getView().findViewById(R.id.tabs); + + assertThat(tabs.getSelectedTabPosition()).isEqualTo(WORK_TAB); + } + + public static class TestProfileSelectFragment extends ProfileSelectFragment { + + @Override + public Fragment[] getFragments() { + return new Fragment[]{ + new SettingsPreferenceFragmentTest.TestFragment(), //0 + new SettingsPreferenceFragmentTest.TestFragment() + }; + } + } + + @Implements(PreferenceFragmentCompat.class) + public static class ShadowPreferenceFragmentCompat { + + private Context mContext; + + @Implementation + protected View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + mContext = inflater.getContext(); + return inflater.inflate(R.layout.preference_list_fragment, container); + } + + @Implementation + protected RecyclerView getListView() { + final RecyclerView recyclerView = new RecyclerView(mContext); + recyclerView.setAdapter(new RecyclerView.Adapter() { + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, + int i) { + return null; + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { + + } + + @Override + public int getItemCount() { + return 0; + } + }); + return recyclerView; + } + } +}