1 /*
2  * Copyright (C) 2019 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 package com.android.settings.location;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.os.UserHandle;
27 import android.provider.Settings;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.TextView;
31 
32 import androidx.preference.PreferenceCategory;
33 import androidx.preference.PreferenceScreen;
34 
35 import com.android.settings.R;
36 import com.android.settings.dashboard.DashboardFragment;
37 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
38 import com.android.settingslib.applications.RecentAppOpsAccess;
39 
40 import com.google.common.collect.ImmutableList;
41 
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Ignore;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.MockitoAnnotations;
50 import org.robolectric.RobolectricTestRunner;
51 import org.robolectric.RuntimeEnvironment;
52 import org.robolectric.annotation.Config;
53 
54 import java.util.ArrayList;
55 
56 @RunWith(RobolectricTestRunner.class)
57 @Config(shadows = {ShadowDeviceConfig.class})
58 public class RecentLocationAccessPreferenceControllerTest {
59     private static final String PREFERENCE_KEY = "test_preference_key";
60     @Mock
61     private PreferenceCategory mLayoutPreference;
62     @Mock
63     private PreferenceScreen mScreen;
64     @Mock
65     private DashboardFragment mDashboardFragment;
66     @Mock
67     private RecentAppOpsAccess mRecentLocationApps;
68 
69     private Context mContext;
70     private RecentLocationAccessPreferenceController mController;
71     private View mAppEntitiesHeaderView;
72 
73     @Before
setUp()74     public void setUp() {
75         MockitoAnnotations.initMocks(this);
76         mContext = spy(RuntimeEnvironment.application);
77         mController = spy(
78                 new RecentLocationAccessPreferenceController(mContext, PREFERENCE_KEY,
79                         mRecentLocationApps));
80         mController.init(mDashboardFragment);
81         final String key = mController.getPreferenceKey();
82         mAppEntitiesHeaderView = LayoutInflater.from(mContext).inflate(
83                 com.android.settingslib.widget.entityheader.R.layout.app_entities_header, null /* root */);
84         when(mScreen.findPreference(key)).thenReturn(mLayoutPreference);
85         when(mLayoutPreference.getKey()).thenReturn(key);
86         when(mLayoutPreference.getContext()).thenReturn(mContext);
87         when(mDashboardFragment.getContext()).thenReturn(mContext);
88     }
89 
90     @After
tearDown()91     public void tearDown() {
92         ShadowDeviceConfig.reset();
93     }
94 
95     @Test
isAvailable_shouldReturnTrue()96     public void isAvailable_shouldReturnTrue() {
97         assertThat(mController.isAvailable()).isEqualTo(true);
98     }
99 
100     /** Verifies the title text, details text are correct, and the click listener is set. */
101     @Test
102     @Ignore
updateState_whenAppListIsEmpty_shouldDisplayTitleTextAndDetailsText()103     public void updateState_whenAppListIsEmpty_shouldDisplayTitleTextAndDetailsText() {
104         doReturn(new ArrayList<>()).when(mRecentLocationApps).getAppListSorted(false);
105         mController.displayPreference(mScreen);
106         mController.updateState(mLayoutPreference);
107 
108         final TextView title = mAppEntitiesHeaderView.findViewById(R.id.header_title);
109         assertThat(title.getText()).isEqualTo(
110                 mContext.getText(R.string.location_category_recent_location_access));
111         final TextView details = mAppEntitiesHeaderView
112                 .findViewById(com.android.settingslib.widget.entityheader.R.id.header_details);
113         assertThat(details.getText()).isEqualTo(
114                 mContext.getText(R.string.location_recent_location_access_view_details));
115         assertThat(details.hasOnClickListeners()).isTrue();
116     }
117 
118     /** Verifies the title text, details text are correct, and the click listener is set. */
119     @Test
updateState_showSystemAccess()120     public void updateState_showSystemAccess() {
121         doReturn(ImmutableList.of(
122                 new RecentAppOpsAccess.Access("app", UserHandle.CURRENT, null, "app", "", 0)))
123                 .when(mRecentLocationApps).getAppListSorted(false);
124         doReturn(new ArrayList<>()).when(mRecentLocationApps).getAppListSorted(true);
125         mController.displayPreference(mScreen);
126         mController.updateState(mLayoutPreference);
127         verify(mLayoutPreference).addPreference(Mockito.any());
128 
129         Settings.Secure.putInt(
130                 mContext.getContentResolver(), Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 1);
131         verify(mLayoutPreference, Mockito.times(1)).addPreference(Mockito.any());
132     }
133 }
134