1 /* 2 * Copyright (C) 2017 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.applications.appinfo; 18 19 import static com.android.settings.applications.appinfo.AppInfoDashboardFragment.SUB_INFO_FRAGMENT; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.ArgumentMatchers.eq; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Intent; 27 import android.content.pm.ApplicationInfo; 28 import android.os.Bundle; 29 import android.support.v7.preference.Preference; 30 import android.support.v7.preference.PreferenceScreen; 31 32 import com.android.settings.SettingsActivity; 33 import com.android.settings.SettingsPreferenceFragment; 34 import com.android.settings.core.BasePreferenceController; 35 import com.android.settings.notification.AppNotificationSettings; 36 import com.android.settings.testutils.SettingsRobolectricTestRunner; 37 import com.android.settingslib.applications.ApplicationsState; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.ArgumentCaptor; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RuntimeEnvironment; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 public class AppInfoPreferenceControllerBaseTest { 49 50 @Mock 51 private SettingsActivity mActivity; 52 @Mock 53 private AppInfoDashboardFragment mFragment; 54 @Mock 55 private PreferenceScreen mScreen; 56 @Mock 57 private Preference mPreference; 58 59 private TestPreferenceController mController; 60 61 @Before 62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 mController = new TestPreferenceController(mFragment); 65 final String key = mController.getPreferenceKey(); 66 when(mScreen.findPreference(key)).thenReturn(mPreference); 67 when(mPreference.getKey()).thenReturn(key); 68 when(mFragment.getContext()).thenReturn(mActivity); 69 when(mFragment.getActivity()).thenReturn(mActivity); 70 } 71 72 @Test 73 public void getAvailabilityStatus_shouldReturnAvailable() { 74 assertThat(mController.getAvailabilityStatus()) 75 .isEqualTo(BasePreferenceController.AVAILABLE); 76 } 77 78 @Test 79 public void refreshUi_shouldUpdatePreference() { 80 mController.displayPreference(mScreen); 81 mController.refreshUi(); 82 83 assertThat(mController.preferenceUpdated).isTrue(); 84 } 85 86 @Test 87 public void handlePreferenceTreeClick_shouldStartDetailFragmentClass() { 88 final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class); 89 appEntry.info = new ApplicationInfo(); 90 when(mFragment.getAppEntry()).thenReturn(appEntry); 91 92 mController.handlePreferenceTreeClick(mPreference); 93 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 94 95 verify(mFragment).startActivityForResult(intentCaptor.capture(), eq(SUB_INFO_FRAGMENT)); 96 assertThat(intentCaptor.getValue().getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)) 97 .isEqualTo(mController.getDetailFragmentClass().getName()); 98 } 99 100 private class TestPreferenceController extends AppInfoPreferenceControllerBase { 101 102 private boolean preferenceUpdated; 103 104 private TestPreferenceController(AppInfoDashboardFragment parent) { 105 super(RuntimeEnvironment.application, "TestKey"); 106 setParentFragment(parent); 107 } 108 109 @Override 110 public void updateState(Preference preference) { 111 preferenceUpdated = true; 112 } 113 114 @Override 115 public Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() { 116 return AppNotificationSettings.class; 117 } 118 119 @Override 120 protected Bundle getArguments() { 121 Bundle bundle = new Bundle(); 122 bundle.putString("test", "test"); 123 return bundle; 124 } 125 } 126 } 127