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 static android.content.Context.CLIPBOARD_SERVICE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.ArgumentMatchers.eq; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.verify; 27 28 import android.app.Activity; 29 import android.content.ClipboardManager; 30 import android.content.Context; 31 import android.content.pm.UserInfo; 32 import android.os.Process; 33 import android.os.UserHandle; 34 import android.os.UserManager; 35 import android.provider.Settings; 36 37 import androidx.lifecycle.LifecycleOwner; 38 import androidx.preference.Preference; 39 40 import com.android.internal.logging.nano.MetricsProto; 41 import com.android.settings.core.InstrumentedPreferenceFragment; 42 import com.android.settings.testutils.FakeFeatureFactory; 43 import com.android.settings.testutils.shadow.ShadowUtils; 44 import com.android.settingslib.core.lifecycle.Lifecycle; 45 import com.android.settingslib.development.DevelopmentSettingsEnabler; 46 47 import org.junit.After; 48 import org.junit.Before; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 import org.mockito.Answers; 52 import org.mockito.Mock; 53 import org.mockito.MockitoAnnotations; 54 import org.robolectric.RobolectricTestRunner; 55 import org.robolectric.RuntimeEnvironment; 56 import org.robolectric.Shadows; 57 import org.robolectric.annotation.Config; 58 import org.robolectric.shadows.ShadowUserManager; 59 60 @RunWith(RobolectricTestRunner.class) 61 @Config(shadows = ShadowUtils.class) 62 public class BuildNumberPreferenceControllerTest { 63 64 private static final String KEY_BUILD_NUMBER = "build_number"; 65 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 66 private InstrumentedPreferenceFragment mFragment; 67 68 private ShadowUserManager mShadowUserManager; 69 70 private Context mContext; 71 private LifecycleOwner mLifecycleOwner; 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 mContext = RuntimeEnvironment.application; 81 mShadowUserManager = Shadows.shadowOf( 82 RuntimeEnvironment.application.getSystemService(UserManager.class)); 83 mFactory = FakeFeatureFactory.setupForTest(); 84 mLifecycleOwner = () -> mLifecycle; 85 mLifecycle = new Lifecycle(mLifecycleOwner); 86 mController = new BuildNumberPreferenceController(mContext, KEY_BUILD_NUMBER); 87 mController.setHost(mFragment); 88 89 mPreference = new Preference(mContext); 90 mPreference.setKey(mController.getPreferenceKey()); 91 DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(mContext, false); 92 Settings.Global.putInt(mContext.getContentResolver(), 93 Settings.Global.DEVICE_PROVISIONED, 1); 94 } 95 96 @After tearDown()97 public void tearDown() { 98 ShadowUtils.reset(); 99 } 100 101 @Test handlePrefTreeClick_onlyHandleBuildNumberPref()102 public void handlePrefTreeClick_onlyHandleBuildNumberPref() { 103 assertThat(mController.handlePreferenceTreeClick(mock(Preference.class))).isFalse(); 104 } 105 106 @Test handlePrefTreeClick_notAdminUser_notDemoUser_doNothing()107 public void handlePrefTreeClick_notAdminUser_notDemoUser_doNothing() { 108 mShadowUserManager.setIsAdminUser(false); 109 mShadowUserManager.setIsDemoUser(false); 110 111 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 112 } 113 114 @Test handlePrefTreeClick_isAdminUser_notDemoUser_handleBuildNumberPref()115 public void handlePrefTreeClick_isAdminUser_notDemoUser_handleBuildNumberPref() { 116 mShadowUserManager.setIsAdminUser(true); 117 mShadowUserManager.setIsDemoUser(false); 118 119 assertThat(mController.handlePreferenceTreeClick(mPreference)).isTrue(); 120 } 121 122 @Test handlePrefTreeClick_notAdminUser_isDemoUser_handleBuildNumberPref()123 public void handlePrefTreeClick_notAdminUser_isDemoUser_handleBuildNumberPref() { 124 mShadowUserManager.setIsAdminUser(false); 125 mShadowUserManager.addUser(UserHandle.myUserId(), "test", UserInfo.FLAG_DEMO); 126 127 assertThat(mController.handlePreferenceTreeClick(mPreference)).isTrue(); 128 } 129 130 @Test handlePrefTreeClick_deviceNotProvisioned_doNothing()131 public void handlePrefTreeClick_deviceNotProvisioned_doNothing() { 132 mShadowUserManager.setIsAdminUser(true); 133 mShadowUserManager.setIsDemoUser(false); 134 135 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 136 0); 137 138 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 139 verify(mFactory.metricsFeatureProvider).action( 140 any(Context.class), 141 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_BUILD_NUMBER_PREF)); 142 } 143 144 @Test handlePrefTreeClick_isMonkeyRun_doNothing()145 public void handlePrefTreeClick_isMonkeyRun_doNothing() { 146 ShadowUtils.setIsUserAMonkey(true); 147 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 148 } 149 150 @Test handlePrefTreeClick_userHasRestriction_doNothing()151 public void handlePrefTreeClick_userHasRestriction_doNothing() { 152 mShadowUserManager.setIsAdminUser(true); 153 mShadowUserManager.setIsDemoUser(false); 154 155 mShadowUserManager.setUserRestriction(Process.myUserHandle(), 156 UserManager.DISALLOW_DEBUGGING_FEATURES, true); 157 158 assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); 159 verify(mFactory.metricsFeatureProvider).action( 160 any(Context.class), 161 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_BUILD_NUMBER_PREF)); 162 } 163 164 @Test onActivityResult_notConfirmPasswordRequest_doNothing()165 public void onActivityResult_notConfirmPasswordRequest_doNothing() { 166 final boolean activityResultHandled = mController.onActivityResult( 167 BuildNumberPreferenceController.REQUEST_CONFIRM_PASSWORD_FOR_DEV_PREF + 1, 168 Activity.RESULT_OK, 169 null); 170 171 assertThat(activityResultHandled).isFalse(); 172 assertThat(DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)).isFalse(); 173 } 174 175 @Test onActivityResult_confirmPasswordRequestFailed_doNotEnableDevPref()176 public void onActivityResult_confirmPasswordRequestFailed_doNotEnableDevPref() { 177 final boolean activityResultHandled = mController.onActivityResult( 178 BuildNumberPreferenceController.REQUEST_CONFIRM_PASSWORD_FOR_DEV_PREF, 179 Activity.RESULT_CANCELED, 180 null); 181 182 assertThat(activityResultHandled).isTrue(); 183 assertThat(DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)).isFalse(); 184 } 185 186 @Test onActivityResult_confirmPasswordRequestCompleted_enableDevPref()187 public void onActivityResult_confirmPasswordRequestCompleted_enableDevPref() { 188 mShadowUserManager.setIsAdminUser(true); 189 190 final boolean activityResultHandled = mController.onActivityResult( 191 BuildNumberPreferenceController.REQUEST_CONFIRM_PASSWORD_FOR_DEV_PREF, 192 Activity.RESULT_OK, 193 null); 194 195 assertThat(activityResultHandled).isTrue(); 196 assertThat(DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)).isTrue(); 197 } 198 199 @Test copy_shouldCopyBuildNumberToClipboard()200 public void copy_shouldCopyBuildNumberToClipboard() { 201 mController.copy(); 202 203 final ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService( 204 CLIPBOARD_SERVICE); 205 final CharSequence data = clipboard.getPrimaryClip().getItemAt(0).getText(); 206 assertThat(data.toString()).isEqualTo(mController.getSummary()); 207 } 208 } 209