blob: 06659088aa865d0db4434682ba67c86d95338155 [file] [log] [blame]
Heemin Seogf401cf22018-12-06 16:34:00 -08001/*
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
17package com.android.car.settings.system;
18
Heemin Seog31cd79e2019-01-07 16:25:13 -080019import static com.android.car.settings.common.PreferenceController.AVAILABLE;
20import static com.android.car.settings.common.PreferenceController.CONDITIONALLY_UNAVAILABLE;
Heemin Seogf401cf22018-12-06 16:34:00 -080021
22import static com.google.common.truth.Truth.assertThat;
23
Heemin Seogf401cf22018-12-06 16:34:00 -080024import static org.mockito.Mockito.when;
25
26import android.car.userlib.CarUserManagerHelper;
27import android.content.Context;
28import android.content.pm.UserInfo;
29import android.os.UserManager;
30import android.provider.Settings;
31
32import androidx.preference.Preference;
33
davidln8cd37362018-12-12 10:51:39 -080034import com.android.car.settings.common.PreferenceControllerTestHelper;
Heemin Seogf401cf22018-12-06 16:34:00 -080035import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
36
37import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
davidln15c93512019-06-07 16:58:00 -070043import org.robolectric.RobolectricTestRunner;
Heemin Seogf401cf22018-12-06 16:34:00 -080044import org.robolectric.RuntimeEnvironment;
davidln5a2ca272019-07-11 15:51:15 -070045import org.robolectric.Shadows;
Heemin Seogf401cf22018-12-06 16:34:00 -080046import org.robolectric.annotation.Config;
47
davidln15c93512019-06-07 16:58:00 -070048@RunWith(RobolectricTestRunner.class)
davidln5a2ca272019-07-11 15:51:15 -070049@Config(shadows = {ShadowCarUserManagerHelper.class})
Heemin Seogf401cf22018-12-06 16:34:00 -080050public class DeveloperOptionsEntryPreferenceControllerTest {
51
Heemin Seogf401cf22018-12-06 16:34:00 -080052 private Context mContext;
53 private DeveloperOptionsEntryPreferenceController mController;
Heemin Seogf401cf22018-12-06 16:34:00 -080054 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;
davidln8cd37362018-12-12 10:51:39 -080063 mController = new PreferenceControllerTestHelper<>(mContext,
64 DeveloperOptionsEntryPreferenceController.class,
65 new Preference(mContext)).getController();
Heemin Seogf401cf22018-12-06 16:34:00 -080066
67 // Setup admin user who is able to enable developer settings.
Anthony Hugh492f1842019-07-11 13:50:15 -070068 mUserInfo = new UserInfo(10, null, 0);
Heemin Seogf401cf22018-12-06 16:34:00 -080069 when(mShadowCarUserManagerHelper.isCurrentProcessAdminUser()).thenReturn(true);
70 when(mShadowCarUserManagerHelper.isCurrentProcessDemoUser()).thenReturn(false);
71 when(mShadowCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(mUserInfo);
Heemin Seogf401cf22018-12-06 16:34:00 -080072 }
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);
davidln5a2ca272019-07-11 15:51:15 -070097 Shadows.shadowOf(UserManager.get(mContext)).setUserRestriction(
Anthony Hugh492f1842019-07-11 13:50:15 -070098 mUserInfo.getUserHandle(), UserManager.DISALLOW_DEBUGGING_FEATURES, true);
Heemin Seogf401cf22018-12-06 16:34:00 -080099 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
100 }
Heemin Seogf401cf22018-12-06 16:34:00 -0800101}