Home | History | Annotate | Download | only in system
      1 /*
      2  * Copyright (C) 2017 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 package com.android.settings.system;
     17 
     18 import static com.google.common.truth.Truth.assertThat;
     19 
     20 import android.content.Context;
     21 import android.os.UserManager;
     22 import android.provider.Settings;
     23 
     24 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     25 import com.android.settings.testutils.shadow.ShadowUtils;
     26 
     27 import org.junit.After;
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 import org.robolectric.RuntimeEnvironment;
     32 import org.robolectric.Shadows;
     33 import org.robolectric.shadows.ShadowUserManager;
     34 
     35 @RunWith(SettingsRobolectricTestRunner.class)
     36 public class FactoryResetPreferenceControllerTest {
     37 
     38     private static final String FACTORY_RESET_KEY = "factory_reset";
     39 
     40     private ShadowUserManager mShadowUserManager;
     41 
     42     private Context mContext;
     43     private FactoryResetPreferenceController mController;
     44 
     45     @Before
     46     public void setUp() {
     47         mContext = RuntimeEnvironment.application;
     48         UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
     49         mShadowUserManager = Shadows.shadowOf(userManager);
     50 
     51         mController = new FactoryResetPreferenceController(mContext);
     52     }
     53 
     54     @After
     55     public void tearDown() {
     56         ShadowUtils.reset();
     57         mShadowUserManager.setIsAdminUser(false);
     58         mShadowUserManager.setIsDemoUser(false);
     59         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_DEMO_MODE, 0);
     60     }
     61 
     62     @Test
     63     public void isAvailable_systemUser() {
     64         mShadowUserManager.setIsAdminUser(true);
     65 
     66         assertThat(mController.isAvailable()).isTrue();
     67     }
     68 
     69     @Test
     70     public void isAvailable_nonSystemUser() {
     71         mShadowUserManager.setIsAdminUser(false);
     72         mShadowUserManager.setIsDemoUser(false);
     73 
     74         assertThat(mController.isAvailable()).isFalse();
     75     }
     76 
     77     @Test
     78     public void isAvailable_demoUser() {
     79         mShadowUserManager.setIsAdminUser(false);
     80 
     81         // Place the device in demo mode.
     82         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_DEMO_MODE, 1);
     83 
     84         // Indicate the user is a demo user.
     85         mShadowUserManager.setIsDemoUser(true);
     86 
     87         assertThat(mController.isAvailable()).isTrue();
     88     }
     89 
     90     @Test
     91     public void getPreferenceKey() {
     92         assertThat(mController.getPreferenceKey()).isEqualTo(FACTORY_RESET_KEY);
     93     }
     94 }
     95