Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -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 | |
Heemin Seog | 31cd79e | 2019-01-07 16:25:13 -0800 | [diff] [blame] | 19 | import static com.android.car.settings.common.PreferenceController.AVAILABLE; |
| 20 | import static com.android.car.settings.common.PreferenceController.CONDITIONALLY_UNAVAILABLE; |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 21 | |
| 22 | import static com.google.common.truth.Truth.assertThat; |
| 23 | |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -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; |
| 29 | import android.os.UserManager; |
| 30 | import android.provider.Settings; |
| 31 | |
| 32 | import androidx.preference.Preference; |
| 33 | |
davidln | 8cd3736 | 2018-12-12 10:51:39 -0800 | [diff] [blame] | 34 | import com.android.car.settings.common.PreferenceControllerTestHelper; |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 35 | import com.android.car.settings.testutils.ShadowCarUserManagerHelper; |
| 36 | |
| 37 | import org.junit.After; |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.MockitoAnnotations; |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 43 | import org.robolectric.RobolectricTestRunner; |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 44 | import org.robolectric.RuntimeEnvironment; |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 45 | import org.robolectric.Shadows; |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 46 | import org.robolectric.annotation.Config; |
| 47 | |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 48 | @RunWith(RobolectricTestRunner.class) |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 49 | @Config(shadows = {ShadowCarUserManagerHelper.class}) |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 50 | public class DeveloperOptionsEntryPreferenceControllerTest { |
| 51 | |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 52 | private Context mContext; |
| 53 | private DeveloperOptionsEntryPreferenceController mController; |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 54 | private UserInfo mUserInfo; |
| 55 | @Mock |
| 56 | private CarUserManagerHelper mShadowCarUserManagerHelper; |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | MockitoAnnotations.initMocks(this); |
| 61 | ShadowCarUserManagerHelper.setMockInstance(mShadowCarUserManagerHelper); |
| 62 | mContext = RuntimeEnvironment.application; |
davidln | 8cd3736 | 2018-12-12 10:51:39 -0800 | [diff] [blame] | 63 | mController = new PreferenceControllerTestHelper<>(mContext, |
| 64 | DeveloperOptionsEntryPreferenceController.class, |
| 65 | new Preference(mContext)).getController(); |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 66 | |
| 67 | // Setup admin user who is able to enable developer settings. |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 68 | mUserInfo = new UserInfo(10, null, 0); |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 69 | when(mShadowCarUserManagerHelper.isCurrentProcessAdminUser()).thenReturn(true); |
| 70 | when(mShadowCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false); |
| 71 | when(mShadowCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(mUserInfo); |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @After |
| 75 | public void tearDown() { |
| 76 | ShadowCarUserManagerHelper.reset(); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testGetAvailabilityStatus_devOptionsEnabled_isAvailable() { |
| 81 | Settings.Global.putInt(mContext.getContentResolver(), |
| 82 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); |
| 83 | assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testGetAvailabilityStatus_devOptionsDisabled_isUnavailable() { |
| 88 | Settings.Global.putInt(mContext.getContentResolver(), |
| 89 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0); |
| 90 | assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testGetAvailabilityStatus_devOptionsEnabled_hasUserRestriction_isUnavailable() { |
| 95 | Settings.Global.putInt(mContext.getContentResolver(), |
| 96 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 97 | Shadows.shadowOf(UserManager.get(mContext)).setUserRestriction( |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 98 | mUserInfo.getUserHandle(), UserManager.DISALLOW_DEBUGGING_FEATURES, true); |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 99 | assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); |
| 100 | } |
Heemin Seog | f401cf2 | 2018-12-06 16:34:00 -0800 | [diff] [blame] | 101 | } |