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