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