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:
@@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
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.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.TestConfig;
|
||||
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.location.RecentLocationApps;
|
||||
import com.android.settingslib.location.RecentLocationApps.Request;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -56,9 +57,6 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class RecentLocationRequestPreferenceControllerTest {
|
||||
@@ -71,6 +69,8 @@ public class RecentLocationRequestPreferenceControllerTest {
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private RecentLocationApps mRecentLocationApps;
|
||||
@Mock
|
||||
private Preference mSeeAllButton;
|
||||
|
||||
private Context mContext;
|
||||
private RecentLocationRequestPreferenceController mController;
|
||||
@@ -86,6 +86,7 @@ public class RecentLocationRequestPreferenceControllerTest {
|
||||
mController = spy(new RecentLocationRequestPreferenceController(
|
||||
mContext, mFragment, mLifecycle, mRecentLocationApps));
|
||||
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory);
|
||||
when(mScreen.findPreference(mController.KEY_SEE_ALL)).thenReturn(mSeeAllButton);
|
||||
final String key = mController.getPreferenceKey();
|
||||
when(mCategory.getKey()).thenReturn(key);
|
||||
when(mCategory.getContext()).thenReturn(mContext);
|
||||
@@ -123,38 +124,43 @@ public class RecentLocationRequestPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void updateState_hasRecentRequest_shouldRemoveAllAndAddInjectedSettings() {
|
||||
final List<RecentLocationApps.Request> requests = new ArrayList<>();
|
||||
final Request req1 = mock(Request.class);
|
||||
final Request req2 = mock(Request.class);
|
||||
requests.add(req1);
|
||||
requests.add(req2);
|
||||
List<Request> requests = createMockRequests(2);
|
||||
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.updateState(mCategory);
|
||||
|
||||
verify(mCategory).removeAll();
|
||||
// Verifies two preferences are added in original order
|
||||
InOrder inOrder = Mockito.inOrder(mCategory);
|
||||
inOrder.verify(mCategory).addPreference(argThat(titleMatches(title1)));
|
||||
inOrder.verify(mCategory).addPreference(argThat(titleMatches(title2)));
|
||||
inOrder.verify(mCategory).addPreference(argThat(titleMatches("appTitle0")));
|
||||
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
|
||||
public void createAppPreference_shouldAddClickListener() {
|
||||
final Request request = mock(Request.class);
|
||||
final AppPreference preference = mock(AppPreference.class);
|
||||
doReturn(preference).when(mController)
|
||||
.createAppPreference(any(Context.class));
|
||||
doReturn(preference).when(mController).createAppPreference(any(Context.class));
|
||||
|
||||
mController.createAppPreference(mContext, request);
|
||||
|
||||
@@ -190,4 +196,19 @@ public class RecentLocationRequestPreferenceControllerTest {
|
||||
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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user