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.when;
23 
24 import android.content.Context;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.widget.TextView;
28 
29 import androidx.preference.PreferenceCategory;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
35 import com.android.settingslib.applications.RecentAppOpsAccess;
36 
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Ignore;
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.RobolectricTestRunner;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.annotation.Config;
47 
48 import java.util.ArrayList;
49 
50 @RunWith(RobolectricTestRunner.class)
51 @Config(shadows = {ShadowDeviceConfig.class})
52 public class RecentLocationAccessPreferenceControllerTest {
53     private static final String PREFERENCE_KEY = "test_preference_key";
54     @Mock
55     private PreferenceCategory mLayoutPreference;
56     @Mock
57     private PreferenceScreen mScreen;
58     @Mock
59     private DashboardFragment mDashboardFragment;
60     @Mock
61     private RecentAppOpsAccess mRecentLocationApps;
62 
63     private Context mContext;
64     private RecentLocationAccessPreferenceController mController;
65     private View mAppEntitiesHeaderView;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         mContext = spy(RuntimeEnvironment.application);
71         mController = spy(
72                 new RecentLocationAccessPreferenceController(mContext, PREFERENCE_KEY,
73                         mRecentLocationApps));
74         mController.init(mDashboardFragment);
75         final String key = mController.getPreferenceKey();
76         mAppEntitiesHeaderView = LayoutInflater.from(mContext).inflate(
77                 R.layout.app_entities_header, null /* root */);
78         when(mScreen.findPreference(key)).thenReturn(mLayoutPreference);
79         when(mLayoutPreference.getKey()).thenReturn(key);
80         when(mLayoutPreference.getContext()).thenReturn(mContext);
81         when(mDashboardFragment.getContext()).thenReturn(mContext);
82     }
83 
84     @After
tearDown()85     public void tearDown() {
86         ShadowDeviceConfig.reset();
87     }
88 
89     @Test
isAvailable_shouldReturnTrue()90     public void isAvailable_shouldReturnTrue() {
91         assertThat(mController.isAvailable()).isEqualTo(true);
92     }
93 
94     /** Verifies the title text, details text are correct, and the click listener is set. */
95     @Test
96     @Ignore
updateState_whenAppListIsEmpty_shouldDisplayTitleTextAndDetailsText()97     public void updateState_whenAppListIsEmpty_shouldDisplayTitleTextAndDetailsText() {
98         doReturn(new ArrayList<>()).when(mRecentLocationApps).getAppListSorted(false);
99         mController.displayPreference(mScreen);
100         mController.updateState(mLayoutPreference);
101 
102         final TextView title = mAppEntitiesHeaderView.findViewById(R.id.header_title);
103         assertThat(title.getText()).isEqualTo(
104                 mContext.getText(R.string.location_category_recent_location_access));
105         final TextView details = mAppEntitiesHeaderView.findViewById(R.id.header_details);
106         assertThat(details.getText()).isEqualTo(
107                 mContext.getText(R.string.location_recent_location_access_view_details));
108         assertThat(details.hasOnClickListeners()).isTrue();
109     }
110 }
111