1 /* 2 * Copyright (C) 2016 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.deviceinfo; 18 19 import android.app.Activity; 20 import android.app.Fragment; 21 import android.content.Context; 22 import android.os.Build; 23 import android.os.UserManager; 24 import android.provider.Settings; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.internal.logging.nano.MetricsProto; 29 import com.android.settings.development.DevelopmentSettings; 30 import com.android.settings.SettingsRobolectricTestRunner; 31 import com.android.settings.TestConfig; 32 import com.android.settings.core.lifecycle.Lifecycle; 33 import com.android.settings.search2.DatabaseIndexingManager; 34 import com.android.settings.testutils.FakeFeatureFactory; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Answers; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.annotation.Config; 44 import org.robolectric.shadows.ShadowApplication; 45 import org.robolectric.util.ReflectionHelpers; 46 47 import static com.google.common.truth.Truth.assertThat; 48 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 49 import static org.mockito.Matchers.any; 50 import static org.mockito.Matchers.eq; 51 import static org.mockito.Mockito.mock; 52 import static org.mockito.Mockito.never; 53 import static org.mockito.Mockito.spy; 54 import static org.mockito.Mockito.verify; 55 import static org.mockito.Mockito.when; 56 57 @RunWith(SettingsRobolectricTestRunner.class) 58 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 59 public class BuildNumberPreferenceControllerTest { 60 61 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 62 private Context mContext; 63 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 64 private Activity mActivity; 65 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 66 private Fragment mFragment; 67 @Mock(answer = RETURNS_DEEP_STUBS) 68 private PreferenceScreen mScreen; 69 @Mock 70 private UserManager mUserManager; 71 72 private Lifecycle mLifecycle; 73 private FakeFeatureFactory mFactory; 74 private Preference mPreference; 75 private BuildNumberPreferenceController mController; 76 77 @Before setUp()78 public void setUp() { 79 MockitoAnnotations.initMocks(this); 80 FakeFeatureFactory.setupForTest(mContext); 81 mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 82 mLifecycle = new Lifecycle(); 83 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 84 mController = new BuildNumberPreferenceController( 85 mContext, mActivity, mFragment, mLifecycle); 86 87 mPreference = new Preference(RuntimeEnvironment.application); 88 mPreference.setKey(mController.getPreferenceKey()); 89 } 90 91 @Test displayPref_shouldAlwaysDisplay()92 public void displayPref_shouldAlwaysDisplay() { 93 mController.displayPreference(mScreen); 94 95 verify(mScreen.findPreference(mController.getPreferenceKey())).setSummary(Build.DISPLAY); 96 verify(mScreen, never()).removePreference(any(Preference.class)); 97 } 98 99 @Test handlePrefTreeClick_onlyHandleBuildNumberPref()100 public void handlePrefTreeClick_onlyHandleBuildNumberPref() { 101 assertThat(mController.handlePreferenceTreeClick(mock(Preference.class))).isFalse(); 102 } 103 104 @Test handlePrefTreeClick_notAdminUser_doNothing()105 public void handlePrefTreeClick_notAdminUser_doNothing() { 106 when(mUserManager.isAdminUser()).thenReturn(false); 107 108 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 109 } 110 111 @Test handlePrefTreeClick_deviceNotProvisioned_doNothing()112 public void handlePrefTreeClick_deviceNotProvisioned_doNothing() { 113 when(mUserManager.isAdminUser()).thenReturn(true); 114 final Context context = RuntimeEnvironment.application; 115 Settings.Global.putInt(context.getContentResolver(), 116 Settings.Global.DEVICE_PROVISIONED, 0); 117 118 mController = new BuildNumberPreferenceController( 119 context, mActivity, mFragment, mLifecycle); 120 ReflectionHelpers.setField(mController, "mContext", context); 121 ReflectionHelpers.setField(mController, "mUm", mUserManager); 122 123 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 124 verify(mFactory.metricsFeatureProvider).action( 125 any(Context.class), 126 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_BUILD_NUMBER_PREF)); 127 } 128 129 @Test handlePrefTreeClick_userHasRestriction_doNothing()130 public void handlePrefTreeClick_userHasRestriction_doNothing() { 131 final Context context = spy(RuntimeEnvironment.application); 132 Settings.Global.putInt(context.getContentResolver(), 133 Settings.Global.DEVICE_PROVISIONED, 1); 134 135 when(mUserManager.isAdminUser()).thenReturn(true); 136 when(mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)) 137 .thenReturn(true); 138 139 mController = new BuildNumberPreferenceController( 140 mContext, mActivity, mFragment, mLifecycle); 141 ReflectionHelpers.setField(mController, "mContext", context); 142 143 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 144 verify(mFactory.metricsFeatureProvider).action( 145 any(Context.class), 146 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_BUILD_NUMBER_PREF)); 147 } 148 149 @Test onActivityResult_notConfirmPasswordRequest_doNothing()150 public void onActivityResult_notConfirmPasswordRequest_doNothing() { 151 final boolean activityResultHandled = mController.onActivityResult( 152 BuildNumberPreferenceController.REQUEST_CONFIRM_PASSWORD_FOR_DEV_PREF + 1, 153 Activity.RESULT_OK, 154 null); 155 156 assertThat(activityResultHandled).isFalse(); 157 verify(mContext, never()) 158 .getSharedPreferences(DevelopmentSettings.PREF_FILE, Context.MODE_PRIVATE); 159 } 160 161 @Test onActivityResult_confirmPasswordRequestFailed_doNotEnableDevPref()162 public void onActivityResult_confirmPasswordRequestFailed_doNotEnableDevPref() { 163 final boolean activityResultHandled = mController.onActivityResult( 164 BuildNumberPreferenceController.REQUEST_CONFIRM_PASSWORD_FOR_DEV_PREF, 165 Activity.RESULT_CANCELED, 166 null); 167 168 assertThat(activityResultHandled).isTrue(); 169 verify(mContext, never()) 170 .getSharedPreferences(DevelopmentSettings.PREF_FILE, Context.MODE_PRIVATE); 171 } 172 173 @Test onActivityResult_confirmPasswordRequestCompleted_enableDevPref()174 public void onActivityResult_confirmPasswordRequestCompleted_enableDevPref() { 175 final Context context = RuntimeEnvironment.application; 176 177 when(mFactory.searchFeatureProvider.getIndexingManager(any(Context.class))) 178 .thenReturn(mock(DatabaseIndexingManager.class)); 179 180 mController = new BuildNumberPreferenceController( 181 context, mActivity, mFragment, mLifecycle); 182 183 final boolean activityResultHandled = mController.onActivityResult( 184 BuildNumberPreferenceController.REQUEST_CONFIRM_PASSWORD_FOR_DEV_PREF, 185 Activity.RESULT_OK, 186 null); 187 188 assertThat(activityResultHandled).isTrue(); 189 assertThat(context.getSharedPreferences(DevelopmentSettings.PREF_FILE, 190 Context.MODE_PRIVATE).getBoolean(DevelopmentSettings.PREF_SHOW, false)) 191 .isTrue(); 192 } 193 194 } 195