Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.car.settings.system; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.any; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.anyInt; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.eq; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.car.userlib.CarUserManagerHelper; |
| 27 | import android.content.Context; |
| 28 | import android.content.pm.UserInfo; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 29 | import android.os.UserHandle; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 30 | import android.os.UserManager; |
| 31 | import android.provider.Settings; |
| 32 | |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 33 | import androidx.lifecycle.Lifecycle; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 34 | import androidx.preference.Preference; |
| 35 | |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 36 | import com.android.car.settings.R; |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 37 | import com.android.car.settings.common.PreferenceControllerTestHelper; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 38 | import com.android.car.settings.development.DevelopmentSettingsUtil; |
| 39 | import com.android.car.settings.testutils.ShadowCarUserManagerHelper; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 40 | import com.android.car.settings.testutils.ShadowUserManager; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 41 | |
| 42 | import org.junit.After; |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | import org.mockito.Mock; |
| 47 | import org.mockito.MockitoAnnotations; |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 48 | import org.robolectric.RobolectricTestRunner; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 49 | import org.robolectric.RuntimeEnvironment; |
| 50 | import org.robolectric.annotation.Config; |
| 51 | import org.robolectric.shadows.ShadowToast; |
| 52 | |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 53 | @RunWith(RobolectricTestRunner.class) |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 54 | @Config(shadows = {ShadowCarUserManagerHelper.class, ShadowUserManager.class}) |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 55 | public class BuildNumberPreferenceControllerTest { |
| 56 | |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 57 | private Context mContext; |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 58 | private PreferenceControllerTestHelper<BuildNumberPreferenceController> |
| 59 | mPreferenceControllerHelper; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 60 | private BuildNumberPreferenceController mController; |
| 61 | private Preference mPreference; |
| 62 | @Mock |
| 63 | private CarUserManagerHelper mCarUserManagerHelper; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 64 | @Mock |
| 65 | private UserManager mUserManager; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 66 | |
| 67 | @Before |
| 68 | public void setUp() { |
| 69 | MockitoAnnotations.initMocks(this); |
| 70 | ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper); |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 71 | ShadowUserManager.setInstance(mUserManager); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 72 | mContext = RuntimeEnvironment.application; |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 73 | mPreference = new Preference(mContext); |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 74 | mPreferenceControllerHelper = new PreferenceControllerTestHelper<>(mContext, |
| 75 | BuildNumberPreferenceController.class, mPreference); |
| 76 | mController = mPreferenceControllerHelper.getController(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 77 | |
| 78 | // By default, user is an admin user. |
| 79 | when(mCarUserManagerHelper.isCurrentProcessAdminUser()).thenReturn(true); |
| 80 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false); |
| 81 | |
| 82 | // By default, no restrictions on debugging features. |
| 83 | when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(new UserInfo()); |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 84 | when(mUserManager.hasUserRestriction(eq(UserManager.DISALLOW_DEBUGGING_FEATURES), |
| 85 | any(UserHandle.class))).thenReturn(false); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 86 | |
| 87 | // By default device is provisioned. |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 88 | Settings.Global.putInt(mContext.getContentResolver(), |
| 89 | Settings.Global.DEVICE_PROVISIONED, 1); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 90 | |
| 91 | // By default development settings is disabled. |
| 92 | Settings.Global.putInt(mContext.getContentResolver(), |
| 93 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0); |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 94 | mPreferenceControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_RESUME); |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 95 | |
| 96 | when(mUserManager.getUserInfo(anyInt())).thenReturn(new UserInfo(10, null, 0)); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @After |
| 100 | public void tearDown() { |
| 101 | ShadowCarUserManagerHelper.reset(); |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 102 | ShadowUserManager.reset(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 106 | public void testHandlePreferenceClicked_notProvisioned_returnFalse() { |
| 107 | Settings.Global.putInt(mContext.getContentResolver(), |
| 108 | Settings.Global.DEVICE_PROVISIONED, 0); |
| 109 | assertThat(mController.handlePreferenceClicked(mPreference)).isFalse(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 113 | public void testHandlePreferenceClicked_nonAdmin_returnFalse() { |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 114 | when(mCarUserManagerHelper.isCurrentProcessAdminUser()).thenReturn(false); |
| 115 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false); |
| 116 | |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 117 | assertThat(mController.handlePreferenceClicked(mPreference)).isFalse(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 121 | public void testHandlePreferenceClicked_demoUser_returnsTrue() { |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 122 | when(mCarUserManagerHelper.isCurrentProcessAdminUser()).thenReturn(false); |
| 123 | when(mCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(true); |
| 124 | |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 125 | assertThat(mController.handlePreferenceClicked(mPreference)).isTrue(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 129 | public void testHandlePreferenceClicked_adminUser_returnsTrue() { |
| 130 | assertThat(mController.handlePreferenceClicked(mPreference)).isTrue(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 134 | public void testHandlePreferenceClicked_devSettingsDisabled_firstClick_noToast() { |
| 135 | mPreference.performClick(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 136 | assertThat(ShadowToast.shownToastCount()).isEqualTo(0); |
| 137 | } |
| 138 | |
| 139 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 140 | public void testHandlePreferenceClicked_devSettingsDisabled_someClicks_showToast() { |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 141 | for (int i = 0; i < getTapsToShowToast(); i++) { |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 142 | mPreference.performClick(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int remainingClicks = getTapsToBecomeDeveloper() - getTapsToShowToast(); |
| 146 | assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo( |
| 147 | mContext.getResources().getQuantityString(R.plurals.show_dev_countdown, |
| 148 | remainingClicks, remainingClicks)); |
| 149 | } |
| 150 | |
| 151 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 152 | public void testHandlePreferenceClicked_devSettingsDisabled_allClicks_showDevEnabledToast() { |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 153 | for (int i = 0; i < getTapsToBecomeDeveloper(); i++) { |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 154 | mPreference.performClick(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 155 | } |
| 156 | assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo( |
| 157 | mContext.getString(R.string.show_dev_on)); |
| 158 | } |
| 159 | |
| 160 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 161 | public void testHandlePreferenceClicked_devSettingsDisabled_allClicks_devSettingsEnabled() { |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 162 | for (int i = 0; i < getTapsToBecomeDeveloper(); i++) { |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 163 | mPreference.performClick(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 164 | } |
| 165 | assertThat(DevelopmentSettingsUtil.isDevelopmentSettingsEnabled(mContext, |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 166 | mCarUserManagerHelper, mUserManager)).isTrue(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 170 | public void testHandlePreferenceClicked_devSettingsDisabled_extraClicks_noAlreadyDevToast() { |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 171 | int extraClicks = 100; |
| 172 | for (int i = 0; i < getTapsToBecomeDeveloper() + extraClicks; i++) { |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 173 | mPreference.performClick(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 174 | } |
| 175 | assertThat( |
| 176 | ShadowToast.showedToast(mContext.getString(R.string.show_dev_already))).isFalse(); |
| 177 | } |
| 178 | |
| 179 | @Test |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 180 | public void testHandlePreferenceClicked_devSettingsEnabled_click_showAlreadyDevToast() { |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 181 | Settings.Global.putInt(mContext.getContentResolver(), |
| 182 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); |
Heemin Seog | f1ba578 | 2018-12-18 08:45:50 -0800 | [diff] [blame] | 183 | mPreferenceControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_PAUSE); |
| 184 | mPreferenceControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_RESUME); |
| 185 | mPreference.performClick(); |
Heemin Seog | 34925a0 | 2018-12-04 16:45:28 -0800 | [diff] [blame] | 186 | assertThat(ShadowToast.showedToast(mContext.getString(R.string.show_dev_already))).isTrue(); |
| 187 | } |
| 188 | |
| 189 | private int getTapsToBecomeDeveloper() { |
| 190 | return mContext.getResources().getInteger(R.integer.enable_developer_settings_click_count); |
| 191 | } |
| 192 | |
| 193 | private int getTapsToShowToast() { |
| 194 | return mContext.getResources().getInteger( |
| 195 | R.integer.enable_developer_settings_clicks_to_show_toast_count); |
| 196 | } |
| 197 | } |