Under "recent location requests", add "See all"

Under Settings -> Security & Location -> Location, add "See all" button
under recent location requests

On location settings page, display at most 3 recent location requests.
If there are more than 3, show a "see all" button.
When user clicks "See all", take them to a new fragment with all recent
location requests.

Test: Robo
Test: Manual
Bug: 70350519
Change-Id: Id1f9a8da1593814a8f30f8e6ec8ac75fb10f6672
This commit is contained in:
Maggie
2018-01-26 16:30:17 -08:00
parent 45520fce49
commit 51fc2889f8
10 changed files with 418 additions and 41 deletions

View File

@@ -3371,6 +3371,8 @@
<string name="location_app_level_permissions">App-level permissions</string> <string name="location_app_level_permissions">App-level permissions</string>
<!-- [CHAR LIMIT=42] Location settings screen, sub category for recent location requests --> <!-- [CHAR LIMIT=42] Location settings screen, sub category for recent location requests -->
<string name="location_category_recent_location_requests">Recent location requests</string> <string name="location_category_recent_location_requests">Recent location requests</string>
<!-- Location settings screen, displayed when there're more than three recent location requests -->
<string name="location_recent_location_requests_see_all">See all</string>
<!-- Location settings screen, displayed when there's no recent app accessing location --> <!-- Location settings screen, displayed when there's no recent app accessing location -->
<string name="location_no_recent_apps">No apps have requested location recently</string> <string name="location_no_recent_apps">No apps have requested location recently</string>
<!-- [CHAR LIMIT=30] Location settings screen, sub category for location services --> <!-- [CHAR LIMIT=30] Location settings screen, sub category for location services -->

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/location_category_recent_location_requests"
android:key="recent_location_requests_see_all">
<PreferenceCategory
android:key="all_recent_location_requests"/>
</PreferenceScreen>

View File

@@ -24,6 +24,13 @@
android:key="recent_location_requests" android:key="recent_location_requests"
android:title="@string/location_category_recent_location_requests"/> android:title="@string/location_category_recent_location_requests"/>
<Preference
android:key="recent_location_requests_see_all"
android:title="@string/location_recent_location_requests_see_all"
android:icon="@drawable/ic_chevron_right_24dp"
android:selectable="true"
android:fragment="com.android.settings.location.RecentLocationRequestSeeAllFragment"/>
<!-- This preference category gets removed if new_recent_location_ui is disabled --> <!-- This preference category gets removed if new_recent_location_ui is disabled -->
<Preference <Preference
android:key="app_level_permissions" android:key="app_level_permissions"

View File

@@ -70,8 +70,10 @@ public class LocationSettings extends DashboardFragment {
public int getInitialExpandedChildCount() { public int getInitialExpandedChildCount() {
final RecentLocationApps recentLocationApps = new RecentLocationApps(getActivity()); final RecentLocationApps recentLocationApps = new RecentLocationApps(getActivity());
final int locationRequestsApps = recentLocationApps.getAppList().size(); final int locationRequestsApps = recentLocationApps.getAppList().size();
final int locationRequestsPrefs = locationRequestsApps == 0 ? 1 : locationRequestsApps; final int locationRequestsPrefs =
return locationRequestsPrefs + 2; locationRequestsApps == 0
? 1 : (locationRequestsApps > 3 ? 4 : locationRequestsApps);
return locationRequestsPrefs + 1;
} }
@Override @Override

View File

@@ -20,32 +20,33 @@ import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.applications.appinfo.AppInfoDashboardFragment; import com.android.settings.applications.appinfo.AppInfoDashboardFragment;
import com.android.settings.core.SubSettingLauncher; import com.android.settings.core.SubSettingLauncher;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.AppPreference; import com.android.settings.widget.AppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps; import com.android.settingslib.location.RecentLocationApps;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController { public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController {
/** Key for preference category "Recent location requests" */ /** Key for preference category "Recent location requests" */
private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests"; private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests";
@VisibleForTesting
static final String KEY_SEE_ALL = "recent_location_requests_see_all";
private final LocationSettings mFragment; private final LocationSettings mFragment;
private final RecentLocationApps mRecentLocationApps; private final RecentLocationApps mRecentLocationApps;
private PreferenceCategory mCategoryRecentLocationRequests; private PreferenceCategory mCategoryRecentLocationRequests;
private Preference mSeeAllButton;
@VisibleForTesting /** Used in this class and {@link RecentLocationRequestSeeAllPreferenceController}*/
static class PackageEntryClickedListener implements Preference.OnPreferenceClickListener { static class PackageEntryClickedListener implements Preference.OnPreferenceClickListener {
private final LocationSettings mFragment; private final DashboardFragment mFragment;
private final String mPackage; private final String mPackage;
private final UserHandle mUserHandle; private final UserHandle mUserHandle;
public PackageEntryClickedListener(LocationSettings fragment, String packageName, public PackageEntryClickedListener(DashboardFragment fragment, String packageName,
UserHandle userHandle) { UserHandle userHandle) {
mFragment = fragment; mFragment = fragment;
mPackage = packageName; mPackage = packageName;
@@ -92,24 +93,32 @@ public class RecentLocationRequestPreferenceController extends LocationBasePrefe
super.displayPreference(screen); super.displayPreference(screen);
mCategoryRecentLocationRequests = mCategoryRecentLocationRequests =
(PreferenceCategory) screen.findPreference(KEY_RECENT_LOCATION_REQUESTS); (PreferenceCategory) screen.findPreference(KEY_RECENT_LOCATION_REQUESTS);
mSeeAllButton = screen.findPreference(KEY_SEE_ALL);
} }
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
mCategoryRecentLocationRequests.removeAll(); mCategoryRecentLocationRequests.removeAll();
mSeeAllButton.setVisible(false);
final Context prefContext = preference.getContext(); final Context prefContext = preference.getContext();
final List<RecentLocationApps.Request> recentLocationRequests = final List<RecentLocationApps.Request> recentLocationRequests =
mRecentLocationApps.getAppListSorted(); mRecentLocationApps.getAppListSorted();
final List<Preference> recentLocationPrefs = new ArrayList<>(recentLocationRequests.size()); if (recentLocationRequests.size() > 3) {
for (final RecentLocationApps.Request request : recentLocationRequests) { // Display the top 3 preferences to container in original order.
recentLocationPrefs.add(createAppPreference(prefContext, request)); for (int i = 0; i < 3; i ++) {
} mCategoryRecentLocationRequests.addPreference(
if (recentLocationRequests.size() > 0) { createAppPreference(prefContext, recentLocationRequests.get(i)));
}
// Display a button to list all requests
mSeeAllButton.setVisible(true);
} else if (recentLocationRequests.size() > 0) {
// Add preferences to container in original order (already sorted by recency). // Add preferences to container in original order (already sorted by recency).
for (Preference entry : recentLocationPrefs) { for (RecentLocationApps.Request request : recentLocationRequests) {
mCategoryRecentLocationRequests.addPreference(entry); mCategoryRecentLocationRequests.addPreference(
createAppPreference(prefContext, request));
} }
} else { } else {
// If there's no item to display, add a "No recent apps" item. // If there's no item to display, add a "No recent apps" item.
@@ -132,7 +141,7 @@ public class RecentLocationRequestPreferenceController extends LocationBasePrefe
@VisibleForTesting @VisibleForTesting
AppPreference createAppPreference(Context prefContext, RecentLocationApps.Request request) { AppPreference createAppPreference(Context prefContext, RecentLocationApps.Request request) {
final AppPreference pref = createAppPreference(prefContext); final AppPreference pref = createAppPreference(prefContext);
pref.setSummary(request.contentDescription); pref.setSummary(request.contentDescription);
pref.setIcon(request.icon); pref.setIcon(request.icon);
pref.setTitle(request.label); pref.setTitle(request.label);

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2018 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 android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** Dashboard Fragment to display all recent location requests, sorted by recency. */
public class RecentLocationRequestSeeAllFragment extends DashboardFragment {
private static final String TAG = "RecentLocationReqAll";
public static final String PATH =
"com.android.settings.location.RecentLocationRequestSeeAllFragment";
@Override
public int getMetricsCategory() {
return MetricsEvent.RECENT_LOCATION_REQUESTS_ALL;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.location_recent_requests_see_all;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getLifecycle(), this);
}
private static List<AbstractPreferenceController> buildPreferenceControllers(
Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(
new RecentLocationRequestSeeAllPreferenceController(context, lifecycle, fragment));
return controllers;
}
/**
* For Search.
*/
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(
Context context, boolean enabled) {
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.location_recent_requests_see_all;
return Arrays.asList(sir);
}
@Override
public List<AbstractPreferenceController> getPreferenceControllers(Context
context) {
return buildPreferenceControllers(
context, /* lifecycle = */ null, /* fragment = */ null);
}
};
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2018 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 android.content.Context;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.widget.AppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps;
import java.util.List;
/** Preference controller for preference category displaying all recent location requests. */
public class RecentLocationRequestSeeAllPreferenceController
extends LocationBasePreferenceController {
/** Key for preference category "All recent location requests" */
private static final String KEY_ALL_RECENT_LOCATION_REQUESTS = "all_recent_location_requests";
private final RecentLocationRequestSeeAllFragment mFragment;
private PreferenceCategory mCategoryAllRecentLocationRequests;
private RecentLocationApps mRecentLocationApps;
public RecentLocationRequestSeeAllPreferenceController(
Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) {
this(context, lifecycle, fragment, new RecentLocationApps(context));
}
@VisibleForTesting
RecentLocationRequestSeeAllPreferenceController(
Context context,
Lifecycle lifecycle,
RecentLocationRequestSeeAllFragment fragment,
RecentLocationApps recentLocationApps) {
super(context, lifecycle);
mFragment = fragment;
mRecentLocationApps = recentLocationApps;
}
@Override
public String getPreferenceKey() {
return KEY_ALL_RECENT_LOCATION_REQUESTS;
}
@Override
public void onLocationModeChanged(int mode, boolean restricted) {
mCategoryAllRecentLocationRequests.setEnabled(mLocationEnabler.isEnabled(mode));
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mCategoryAllRecentLocationRequests =
(PreferenceCategory) screen.findPreference(KEY_ALL_RECENT_LOCATION_REQUESTS);
}
@Override
public void updateState(Preference preference) {
mCategoryAllRecentLocationRequests.removeAll();
List<RecentLocationApps.Request> requests = mRecentLocationApps.getAppListSorted();
for (RecentLocationApps.Request request : requests) {
Preference appPreference = createAppPreference(preference.getContext(), request);
mCategoryAllRecentLocationRequests.addPreference(appPreference);
}
}
@VisibleForTesting
AppPreference createAppPreference(
Context prefContext, RecentLocationApps.Request request) {
final AppPreference pref = new AppPreference(prefContext);
pref.setSummary(request.contentDescription);
pref.setIcon(request.icon);
pref.setTitle(request.label);
pref.setOnPreferenceClickListener(
new RecentLocationRequestPreferenceController.PackageEntryClickedListener(
mFragment, request.packageName, request.userHandle));
return pref;
}
}

View File

@@ -65,6 +65,7 @@ import com.android.settings.inputmethod.PhysicalKeyboardFragment;
import com.android.settings.inputmethod.VirtualKeyboardFragment; import com.android.settings.inputmethod.VirtualKeyboardFragment;
import com.android.settings.language.LanguageAndInputSettings; import com.android.settings.language.LanguageAndInputSettings;
import com.android.settings.location.LocationSettings; import com.android.settings.location.LocationSettings;
import com.android.settings.location.RecentLocationRequestSeeAllFragment;
import com.android.settings.location.ScanningSettings; import com.android.settings.location.ScanningSettings;
import com.android.settings.network.NetworkDashboardFragment; import com.android.settings.network.NetworkDashboardFragment;
import com.android.settings.nfc.PaymentSettings; import com.android.settings.nfc.PaymentSettings;
@@ -177,6 +178,7 @@ public class SearchIndexableResourcesImpl implements SearchIndexableResources {
addIndex(SmartBatterySettings.class); addIndex(SmartBatterySettings.class);
addIndex(MyDeviceInfoFragment.class); addIndex(MyDeviceInfoFragment.class);
addIndex(VibrationSettings.class); addIndex(VibrationSettings.class);
addIndex(RecentLocationRequestSeeAllFragment.class);
} }
@Override @Override

View File

@@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -34,7 +35,6 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.applications.appinfo.AppInfoDashboardFragment; import com.android.settings.applications.appinfo.AppInfoDashboardFragment;
@@ -43,7 +43,8 @@ import com.android.settings.widget.AppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps; import com.android.settingslib.location.RecentLocationApps;
import com.android.settingslib.location.RecentLocationApps.Request; import com.android.settingslib.location.RecentLocationApps.Request;
import java.util.ArrayList;
import java.util.List;
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;
@@ -56,9 +57,6 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class RecentLocationRequestPreferenceControllerTest { public class RecentLocationRequestPreferenceControllerTest {
@@ -71,6 +69,8 @@ public class RecentLocationRequestPreferenceControllerTest {
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
@Mock @Mock
private RecentLocationApps mRecentLocationApps; private RecentLocationApps mRecentLocationApps;
@Mock
private Preference mSeeAllButton;
private Context mContext; private Context mContext;
private RecentLocationRequestPreferenceController mController; private RecentLocationRequestPreferenceController mController;
@@ -86,6 +86,7 @@ public class RecentLocationRequestPreferenceControllerTest {
mController = spy(new RecentLocationRequestPreferenceController( mController = spy(new RecentLocationRequestPreferenceController(
mContext, mFragment, mLifecycle, mRecentLocationApps)); mContext, mFragment, mLifecycle, mRecentLocationApps));
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory);
when(mScreen.findPreference(mController.KEY_SEE_ALL)).thenReturn(mSeeAllButton);
final String key = mController.getPreferenceKey(); final String key = mController.getPreferenceKey();
when(mCategory.getKey()).thenReturn(key); when(mCategory.getKey()).thenReturn(key);
when(mCategory.getContext()).thenReturn(mContext); when(mCategory.getContext()).thenReturn(mContext);
@@ -123,38 +124,43 @@ public class RecentLocationRequestPreferenceControllerTest {
@Test @Test
public void updateState_hasRecentRequest_shouldRemoveAllAndAddInjectedSettings() { public void updateState_hasRecentRequest_shouldRemoveAllAndAddInjectedSettings() {
final List<RecentLocationApps.Request> requests = new ArrayList<>(); List<Request> requests = createMockRequests(2);
final Request req1 = mock(Request.class);
final Request req2 = mock(Request.class);
requests.add(req1);
requests.add(req2);
doReturn(requests).when(mRecentLocationApps).getAppListSorted(); doReturn(requests).when(mRecentLocationApps).getAppListSorted();
final String title1 = "testTitle1";
final String title2 = "testTitle2";
final AppPreference preference1 = mock(AppPreference.class);
final AppPreference preference2 = mock(AppPreference.class);
when(preference1.getTitle()).thenReturn(title1);
when(preference2.getTitle()).thenReturn(title2);
doReturn(preference1).when(mController)
.createAppPreference(any(Context.class), eq(req1));
doReturn(preference2).when(mController)
.createAppPreference(any(Context.class), eq(req2));
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.updateState(mCategory); mController.updateState(mCategory);
verify(mCategory).removeAll(); verify(mCategory).removeAll();
// Verifies two preferences are added in original order // Verifies two preferences are added in original order
InOrder inOrder = Mockito.inOrder(mCategory); InOrder inOrder = Mockito.inOrder(mCategory);
inOrder.verify(mCategory).addPreference(argThat(titleMatches(title1))); inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle0")));
inOrder.verify(mCategory).addPreference(argThat(titleMatches(title2))); inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle1")));
}
@Test
public void updateState_hasOverThreeRequests_shouldDisplaySeeAllButton() {
List<Request> requests = createMockRequests(6);
when(mRecentLocationApps.getAppListSorted()).thenReturn(requests);
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mCategory).removeAll();
// Verifies the first three preferences are added
InOrder inOrder = Mockito.inOrder(mCategory);
inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle0")));
inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle1")));
inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle2")));
verify(mCategory, never()).addPreference(argThat(titleMatches("appTitle3")));
// Verifies the "See all" preference is visible
verify(mSeeAllButton).setVisible(true);
} }
@Test @Test
public void createAppPreference_shouldAddClickListener() { public void createAppPreference_shouldAddClickListener() {
final Request request = mock(Request.class); final Request request = mock(Request.class);
final AppPreference preference = mock(AppPreference.class); final AppPreference preference = mock(AppPreference.class);
doReturn(preference).when(mController) doReturn(preference).when(mController).createAppPreference(any(Context.class));
.createAppPreference(any(Context.class));
mController.createAppPreference(mContext, request); mController.createAppPreference(mContext, request);
@@ -190,4 +196,19 @@ public class RecentLocationRequestPreferenceControllerTest {
return preference -> TextUtils.equals(expected, preference.getTitle()); return preference -> TextUtils.equals(expected, preference.getTitle());
} }
private List<RecentLocationApps.Request> createMockRequests(int count) {
List<RecentLocationApps.Request> requests = new ArrayList<>();
for (int i = 0; i < count; i++) {
// Add mock requests
Request req = mock(Request.class, "request" + i);
requests.add(req);
// Map mock AppPreferences with mock requests
String title = "appTitle" + i;
AppPreference appPreference = mock(AppPreference.class, "AppPreference" + i);
doReturn(title).when(appPreference).getTitle();
doReturn(appPreference)
.when(mController).createAppPreference(any(Context.class), eq(req));
}
return requests;
}
} }

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2018 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.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.arch.lifecycle.LifecycleOwner;
import android.content.Context;
import android.provider.Settings.Secure;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.AppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps;
import com.android.settingslib.location.RecentLocationApps.Request;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
/** Unit tests for {@link RecentLocationRequestSeeAllPreferenceController} */
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class RecentLocationRequestSeeAllPreferenceControllerTest {
@Mock
RecentLocationRequestSeeAllFragment mFragment;
@Mock
private PreferenceScreen mScreen;
@Mock
private PreferenceCategory mCategory;
@Mock
private RecentLocationApps mRecentLocationApps;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private RecentLocationRequestSeeAllPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mController = spy(
new RecentLocationRequestSeeAllPreferenceController(
mContext, mLifecycle, mFragment, mRecentLocationApps));
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory);
final String key = mController.getPreferenceKey();
when(mCategory.getKey()).thenReturn(key);
when(mCategory.getContext()).thenReturn(mContext);
}
@Test
public void onLocationModeChanged_locationOn_shouldEnablePreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Secure.LOCATION_MODE_HIGH_ACCURACY, false);
verify(mCategory).setEnabled(true);
}
@Test
public void onLocationModeChanged_locationOff_shouldDisablePreference() {
mController.displayPreference(mScreen);
mController.onLocationModeChanged(Secure.LOCATION_MODE_OFF, false);
verify(mCategory).setEnabled(false);
}
@Test
public void updateState_shouldRemoveAll() {
doReturn(Collections.EMPTY_LIST).when(mRecentLocationApps).getAppListSorted();
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mCategory).removeAll();
}
@Test
public void updateState_hasRecentLocationRequest_shouldAddPreference() {
Request request = mock(Request.class);
AppPreference appPreference = mock(AppPreference.class);
doReturn(appPreference)
.when(mController).createAppPreference(any(Context.class), eq(request));
when(mRecentLocationApps.getAppListSorted())
.thenReturn(new ArrayList<>(Arrays.asList(request)));
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mCategory).removeAll();
verify(mCategory).addPreference(appPreference);
}
}