Fix UI issue in LocationSettings
- Add WorkPreferenceController to support directly use work profile related feature in xml - Get only work/personal infos in RecentLocationRequestPreferenceController and RecentLocationRequestSeeAllPreferenceController - Remove ProfileSelectStorageFragment Bug: 141601408 Fixes: 146080649 Test: manual, robolectric Change-Id: Ide39c7a3796e16421f3a5690309c3d746a956de8
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.android.settings.dashboard.profileselector;
|
||||
|
||||
import static android.content.Intent.EXTRA_USER_ID;
|
||||
|
||||
import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.PERSONAL_TAB;
|
||||
import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.WORK_TAB;
|
||||
|
||||
@@ -27,22 +29,13 @@ 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 com.android.settings.testutils.shadow.ShadowUserManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -52,16 +45,18 @@ 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;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowUserManager.class})
|
||||
public class ProfileSelectFragmentTest {
|
||||
|
||||
private Context mContext;
|
||||
private TestProfileSelectFragment mFragment;
|
||||
private FragmentActivity mActivity;
|
||||
private ShadowUserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -71,30 +66,51 @@ public class ProfileSelectFragmentTest {
|
||||
mFragment = spy(new TestProfileSelectFragment());
|
||||
when(mFragment.getContext()).thenReturn(mContext);
|
||||
when(mFragment.getActivity()).thenReturn(mActivity);
|
||||
mUserManager = ShadowUserManager.getShadow();
|
||||
}
|
||||
|
||||
@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);
|
||||
public void getTabId_no_setCorrectTab() {
|
||||
assertThat(mFragment.getTabId(mActivity, null)).isEqualTo(PERSONAL_TAB);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = ShadowPreferenceFragmentCompat.class)
|
||||
public void onCreateView_setArgument_setCorrectTab() {
|
||||
public void getTabId_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(mFragment.getTabId(mActivity, bundle)).isEqualTo(WORK_TAB);
|
||||
}
|
||||
|
||||
assertThat(tabs.getSelectedTabPosition()).isEqualTo(WORK_TAB);
|
||||
@Test
|
||||
public void getTabId_setWorkId_getCorrectTab() {
|
||||
final Bundle bundle = new Bundle();
|
||||
bundle.putInt(EXTRA_USER_ID, 10);
|
||||
final Set<Integer> profileIds = new HashSet<>();
|
||||
profileIds.add(10);
|
||||
mUserManager.setManagedProfiles(profileIds);
|
||||
|
||||
assertThat(mFragment.getTabId(mActivity, bundle)).isEqualTo(WORK_TAB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTabId_setPersonalId_getCorrectTab() {
|
||||
final Bundle bundle = new Bundle();
|
||||
bundle.putInt(EXTRA_USER_ID, 0);
|
||||
|
||||
assertThat(mFragment.getTabId(mActivity, bundle)).isEqualTo(PERSONAL_TAB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTabId_setPersonalIdByIntent_getCorrectTab() {
|
||||
final Set<Integer> profileIds = new HashSet<>();
|
||||
profileIds.add(10);
|
||||
mUserManager.setManagedProfiles(profileIds);
|
||||
final Intent intent = spy(new Intent());
|
||||
when(intent.getContentUserHint()).thenReturn(10);
|
||||
when(mActivity.getIntent()).thenReturn(intent);
|
||||
|
||||
assertThat(mFragment.getTabId(mActivity, null)).isEqualTo(WORK_TAB);
|
||||
}
|
||||
|
||||
public static class TestProfileSelectFragment extends ProfileSelectFragment {
|
||||
@@ -107,41 +123,4 @@ public class ProfileSelectFragmentTest {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.EXTRA_PROFILE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ProfileSelectLocationFragmentTest {
|
||||
|
||||
private ProfileSelectLocationFragment mProfileSelectLocationFragment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mProfileSelectLocationFragment = new ProfileSelectLocationFragment();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFragments_containsCorrectBundle() {
|
||||
assertThat(mProfileSelectLocationFragment.getFragments().length).isEqualTo(2);
|
||||
assertThat(mProfileSelectLocationFragment.getFragments()[0].getArguments().getInt(
|
||||
EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.PERSONAL);
|
||||
assertThat(mProfileSelectLocationFragment.getFragments()[1].getArguments().getInt(
|
||||
EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.WORK);
|
||||
}
|
||||
}
|
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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.location;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.UserHandle;
|
||||
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
|
||||
import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||
import com.android.settingslib.location.RecentLocationApps;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowUserManager.class})
|
||||
public class RecentLocationRequestPreferenceControllerTest {
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private PreferenceCategory mCategory;
|
||||
private Context mContext;
|
||||
private RecentLocationRequestPreferenceController mController;
|
||||
private ShadowUserManager mUserManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = spy(
|
||||
new RecentLocationRequestPreferenceController(mContext, "key"));
|
||||
when(mCategory.getContext()).thenReturn(mContext);
|
||||
when(mScreen.findPreference("key")).thenReturn(mCategory);
|
||||
mUserManager = ShadowUserManager.getShadow();
|
||||
mController.mRecentLocationApps = spy(new RecentLocationApps(mContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_whenAppListMoreThanThree_shouldDisplayTopThreeApps() {
|
||||
final List<RecentLocationApps.Request> requests = createMockRequest(6);
|
||||
when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
verify(mCategory, times(3)).addPreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_workProfile_shouldShowOnlyWorkProfileApps() {
|
||||
final List<RecentLocationApps.Request> requests = createMockRequest(6);
|
||||
when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests);
|
||||
mController.setProfileType(ProfileSelectFragment.ProfileType.WORK);
|
||||
final Set<Integer> profileIds = new HashSet<>();
|
||||
profileIds.add(4);
|
||||
profileIds.add(5);
|
||||
mUserManager.setManagedProfiles(profileIds);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
// contains userId 4 and userId 5
|
||||
verify(mCategory, times(2)).addPreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_Personal_shouldShowOnlyPersonalApps() {
|
||||
final List<RecentLocationApps.Request> requests = createMockRequest(6);
|
||||
when(mController.mRecentLocationApps.getAppListSorted(false)).thenReturn(requests);
|
||||
mController.setProfileType(ProfileSelectFragment.ProfileType.PERSONAL);
|
||||
final Set<Integer> profileIds = new HashSet<>();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
profileIds.add(i);
|
||||
}
|
||||
mUserManager.setManagedProfiles(profileIds);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
|
||||
// contains userId 4 and userId 5
|
||||
verify(mCategory, times(2)).addPreference(any());
|
||||
}
|
||||
|
||||
private List<RecentLocationApps.Request> createMockRequest(int count) {
|
||||
final List<RecentLocationApps.Request> requests = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
final Drawable icon = mock(Drawable.class);
|
||||
// Add mock accesses
|
||||
final RecentLocationApps.Request request = new RecentLocationApps.Request(
|
||||
"packageName", UserHandle.of(i), icon,
|
||||
"appTitle" + i, false, "appSummary" + i, 1000 - i);
|
||||
requests.add(request);
|
||||
}
|
||||
return requests;
|
||||
}
|
||||
}
|
@@ -27,8 +27,8 @@ import com.google.android.collect.Maps;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
import org.robolectric.annotation.Resetter;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -132,6 +132,16 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
|
||||
return mUserSwitchEnabled;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
protected boolean isManagedProfile(int userId) {
|
||||
return mManagedProfiles.contains(userId);
|
||||
}
|
||||
|
||||
public void setManagedProfiles(Set<Integer> profileIds) {
|
||||
mManagedProfiles.clear();
|
||||
mManagedProfiles.addAll(profileIds);
|
||||
}
|
||||
|
||||
public void setUserSwitcherEnabled(boolean userSwitchEnabled) {
|
||||
mUserSwitchEnabled = userSwitchEnabled;
|
||||
}
|
||||
|
Reference in New Issue
Block a user