• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.location;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.arch.lifecycle.LifecycleOwner;
28 import android.content.Context;
29 import android.provider.Settings.Secure;
30 import android.support.v7.preference.PreferenceCategory;
31 import android.support.v7.preference.PreferenceScreen;
32 
33 import com.android.settings.testutils.SettingsRobolectricTestRunner;
34 import com.android.settings.widget.AppPreference;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.location.RecentLocationApps;
37 import com.android.settingslib.location.RecentLocationApps.Request;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.RuntimeEnvironment;
45 
46 import java.util.ArrayList;
47 import java.util.Collections;
48 
49 /** Unit tests for {@link RecentLocationRequestSeeAllPreferenceController} */
50 @RunWith(SettingsRobolectricTestRunner.class)
51 public class RecentLocationRequestSeeAllPreferenceControllerTest {
52 
53     @Mock
54     RecentLocationRequestSeeAllFragment mFragment;
55     @Mock
56     private PreferenceScreen mScreen;
57     @Mock
58     private PreferenceCategory mCategory;
59     @Mock
60     private RecentLocationApps mRecentLocationApps;
61 
62     private Context mContext;
63     private LifecycleOwner mLifecycleOwner;
64     private Lifecycle mLifecycle;
65     private RecentLocationRequestSeeAllPreferenceController mController;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         mContext = spy(RuntimeEnvironment.application);
71         mLifecycleOwner = () -> mLifecycle;
72         mLifecycle = new Lifecycle(mLifecycleOwner);
73         mController = spy(
74                 new RecentLocationRequestSeeAllPreferenceController(
75                         mContext, mLifecycle, mFragment, mRecentLocationApps));
76         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mCategory);
77         final String key = mController.getPreferenceKey();
78         when(mCategory.getKey()).thenReturn(key);
79         when(mCategory.getContext()).thenReturn(mContext);
80     }
81 
82     @Test
onLocationModeChanged_locationOn_shouldEnablePreference()83     public void onLocationModeChanged_locationOn_shouldEnablePreference() {
84         mController.displayPreference(mScreen);
85 
86         mController.onLocationModeChanged(Secure.LOCATION_MODE_HIGH_ACCURACY, false);
87 
88         verify(mCategory).setEnabled(true);
89     }
90 
91     @Test
onLocationModeChanged_locationOff_shouldDisablePreference()92     public void onLocationModeChanged_locationOff_shouldDisablePreference() {
93         mController.displayPreference(mScreen);
94 
95         mController.onLocationModeChanged(Secure.LOCATION_MODE_OFF, false);
96 
97         verify(mCategory).setEnabled(false);
98     }
99 
100     @Test
updateState_shouldRemoveAll()101     public void updateState_shouldRemoveAll() {
102         doReturn(Collections.EMPTY_LIST).when(mRecentLocationApps).getAppListSorted();
103 
104         mController.displayPreference(mScreen);
105         mController.updateState(mCategory);
106 
107         verify(mCategory).removeAll();
108     }
109 
110     @Test
updateState_hasRecentLocationRequest_shouldAddPreference()111     public void updateState_hasRecentLocationRequest_shouldAddPreference() {
112         Request request = mock(Request.class);
113         AppPreference appPreference = mock(AppPreference.class);
114         doReturn(appPreference)
115                 .when(mController).createAppPreference(any(Context.class), eq(request));
116         when(mRecentLocationApps.getAppListSorted())
117                 .thenReturn(new ArrayList<>(Collections.singletonList(request)));
118 
119         mController.displayPreference(mScreen);
120         mController.updateState(mCategory);
121 
122         verify(mCategory).removeAll();
123         verify(mCategory).addPreference(appPreference);
124     }
125 }
126