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.development; 18 19 import static com.android.settings.development.DevelopmentOptionsActivityRequestCodes.REQUEST_CODE_DEBUG_APP; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.app.Activity; 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.provider.Settings; 31 import android.support.v7.preference.Preference; 32 import android.support.v7.preference.PreferenceScreen; 33 34 import com.android.settings.R; 35 import com.android.settings.testutils.SettingsRobolectricTestRunner; 36 import com.android.settingslib.wrapper.PackageManagerWrapper; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RuntimeEnvironment; 44 import org.robolectric.util.ReflectionHelpers; 45 46 @RunWith(SettingsRobolectricTestRunner.class) 47 public class SelectDebugAppPreferenceControllerTest { 48 49 @Mock 50 private Preference mPreference; 51 @Mock 52 private PreferenceScreen mPreferenceScreen; 53 @Mock 54 private DevelopmentSettingsDashboardFragment mFragment; 55 @Mock 56 private PackageManagerWrapper mPackageManagerWrapper; 57 58 private Context mContext; 59 private SelectDebugAppPreferenceController mController; 60 61 @Before setup()62 public void setup() { 63 MockitoAnnotations.initMocks(this); 64 mContext = RuntimeEnvironment.application; 65 mController = spy(new SelectDebugAppPreferenceController(mContext, mFragment)); 66 ReflectionHelpers 67 .setField(mController, "mPackageManager" /* field name */, mPackageManagerWrapper); 68 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 69 .thenReturn(mPreference); 70 mController.displayPreference(mPreferenceScreen); 71 } 72 73 @Test handlePreferenceTreeClick_preferenceClicked_launchActivity()74 public void handlePreferenceTreeClick_preferenceClicked_launchActivity() { 75 final Intent activityStartIntent = new Intent(mContext, AppPicker.class); 76 final String preferenceKey = mController.getPreferenceKey(); 77 doReturn(activityStartIntent).when(mController).getActivityStartIntent(); 78 when(mPreference.getKey()).thenReturn(preferenceKey); 79 mController.handlePreferenceTreeClick(mPreference); 80 81 verify(mFragment).startActivityForResult(activityStartIntent, REQUEST_CODE_DEBUG_APP); 82 } 83 84 @Test updateState_foobarAppSelected_shouldUpdateSummaryWithDebugAppLabel()85 public void updateState_foobarAppSelected_shouldUpdateSummaryWithDebugAppLabel() { 86 final String debugApp = "foobar"; 87 final ContentResolver contentResolver = mContext.getContentResolver(); 88 Settings.Global.putString(contentResolver, Settings.Global.DEBUG_APP, debugApp); 89 mController.updateState(mPreference); 90 91 verify(mPreference).setSummary(mContext.getString(R.string.debug_app_set, debugApp)); 92 } 93 94 @Test updateState_noAppSelected_shouldUpdateSummaryWithNoAppSelected()95 public void updateState_noAppSelected_shouldUpdateSummaryWithNoAppSelected() { 96 final String debugApp = null; 97 final ContentResolver contentResolver = mContext.getContentResolver(); 98 Settings.Global.putString(contentResolver, Settings.Global.DEBUG_APP, debugApp); 99 mController.updateState(mPreference); 100 101 verify(mPreference).setSummary(mContext.getString(R.string.debug_app_not_set)); 102 } 103 104 @Test onActivityResult_foobarAppSelected_shouldUpdateSummaryWithDebugLabel()105 public void onActivityResult_foobarAppSelected_shouldUpdateSummaryWithDebugLabel() { 106 Intent activityResultIntent = new Intent(mContext, AppPicker.class); 107 final String appLabel = "foobar"; 108 activityResultIntent.setAction(appLabel); 109 final boolean result = mController 110 .onActivityResult(REQUEST_CODE_DEBUG_APP, Activity.RESULT_OK, activityResultIntent); 111 112 assertThat(result).isTrue(); 113 verify(mPreference).setSummary(mContext.getString(R.string.debug_app_set, appLabel)); 114 } 115 116 @Test onActivityResult_badRequestCode_shouldReturnFalse()117 public void onActivityResult_badRequestCode_shouldReturnFalse() { 118 assertThat(mController.onActivityResult( 119 -1 /* requestCode */, -1 /* resultCode */, null /* intent */)).isFalse(); 120 } 121 122 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()123 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 124 mController.onDeveloperOptionsSwitchDisabled(); 125 126 verify(mPreference).setEnabled(false); 127 verify(mPreference).setSummary(mContext.getString(R.string.debug_app_not_set)); 128 } 129 } 130