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.mock; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.graphics.drawable.Drawable; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.widget.TextView; 30 31 import androidx.preference.PreferenceCategory; 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settings.R; 35 import com.android.settings.dashboard.DashboardFragment; 36 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 37 import com.android.settingslib.location.RecentLocationAccesses; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Ignore; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.RuntimeEnvironment; 48 import org.robolectric.annotation.Config; 49 50 import java.util.ArrayList; 51 import java.util.List; 52 53 @RunWith(RobolectricTestRunner.class) 54 @Config(shadows = {ShadowDeviceConfig.class}) 55 public class RecentLocationAccessPreferenceControllerTest { 56 private static final String PREFERENCE_KEY = "test_preference_key"; 57 @Mock 58 private PreferenceCategory mLayoutPreference; 59 @Mock 60 private PreferenceScreen mScreen; 61 @Mock 62 private DashboardFragment mDashboardFragment; 63 @Mock 64 private RecentLocationAccesses mRecentLocationApps; 65 66 private Context mContext; 67 private RecentLocationAccessPreferenceController mController; 68 private View mAppEntitiesHeaderView; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 mContext = spy(RuntimeEnvironment.application); 74 mController = spy( 75 new RecentLocationAccessPreferenceController(mContext, PREFERENCE_KEY, 76 mRecentLocationApps)); 77 mController.init(mDashboardFragment); 78 final String key = mController.getPreferenceKey(); 79 mAppEntitiesHeaderView = LayoutInflater.from(mContext).inflate( 80 R.layout.app_entities_header, null /* root */); 81 when(mScreen.findPreference(key)).thenReturn(mLayoutPreference); 82 when(mLayoutPreference.getKey()).thenReturn(key); 83 when(mLayoutPreference.getContext()).thenReturn(mContext); 84 when(mDashboardFragment.getContext()).thenReturn(mContext); 85 } 86 87 @After tearDown()88 public void tearDown() { 89 ShadowDeviceConfig.reset(); 90 } 91 92 @Test isAvailable_shouldReturnTrue()93 public void isAvailable_shouldReturnTrue() { 94 assertThat(mController.isAvailable()).isEqualTo(true); 95 } 96 97 /** Verifies the title text, details text are correct, and the click listener is set. */ 98 @Test 99 @Ignore updateState_whenAppListIsEmpty_shouldDisplayTitleTextAndDetailsText()100 public void updateState_whenAppListIsEmpty_shouldDisplayTitleTextAndDetailsText() { 101 doReturn(new ArrayList<>()).when(mRecentLocationApps).getAppListSorted(false); 102 mController.displayPreference(mScreen); 103 mController.updateState(mLayoutPreference); 104 105 final TextView title = mAppEntitiesHeaderView.findViewById(R.id.header_title); 106 assertThat(title.getText()).isEqualTo( 107 mContext.getText(R.string.location_category_recent_location_access)); 108 final TextView details = mAppEntitiesHeaderView.findViewById(R.id.header_details); 109 assertThat(details.getText()).isEqualTo( 110 mContext.getText(R.string.location_recent_location_access_view_details)); 111 assertThat(details.hasOnClickListeners()).isTrue(); 112 } 113 createMockAccesses(int count)114 private List<RecentLocationAccesses.Access> createMockAccesses(int count) { 115 final List<RecentLocationAccesses.Access> accesses = new ArrayList<>(); 116 for (int i = 0; i < count; i++) { 117 final Drawable icon = mock(Drawable.class); 118 // Add mock accesses 119 final RecentLocationAccesses.Access access = new RecentLocationAccesses.Access( 120 "packageName", android.os.Process.myUserHandle(), icon, 121 "appTitle" + i, "appSummary" + i, 1000 - i); 122 accesses.add(access); 123 } 124 return accesses; 125 } 126 } 127