1 /*
2  * Copyright (C) 2016 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.content.pm.UserInfo;
22 import android.os.UserHandle;
23 import android.provider.Settings;
24 
25 import com.android.settings.R;
26 import com.android.settings.testutils.shadow.ShadowUserManager;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 import org.robolectric.RuntimeEnvironment;
33 import org.robolectric.annotation.Config;
34 
35 @RunWith(RobolectricTestRunner.class)
36 @Config(shadows = ShadowUserManager.class)
37 public class ResetPreferenceControllerTest {
38 
39     private static final String KEY_RESET_DASHBOARD = "reset_dashboard";
40     private ShadowUserManager mShadowUserManager;
41 
42     private Context mContext;
43     private ResetPreferenceController mController;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48         mController = new ResetPreferenceController(mContext, KEY_RESET_DASHBOARD);
49         mShadowUserManager = ShadowUserManager.getShadow();
50     }
51 
52     @Test
isAvailable_byDefault_true()53     public void isAvailable_byDefault_true() {
54         assertThat(mController.isAvailable()).isTrue();
55     }
56 
57     @Test
58     @Config(qualifiers = "mcc999")
isAvailable_ifNotVisible_false()59     public void isAvailable_ifNotVisible_false() {
60         assertThat(mController.isAvailable()).isFalse();
61     }
62 
63     @Test
getSummary_systemUser_shouldReturnFullSummary()64     public void getSummary_systemUser_shouldReturnFullSummary() {
65         mShadowUserManager.setIsAdminUser(true);
66 
67         assertThat(mController.getSummary()).isEqualTo(
68                 mContext.getString(R.string.reset_dashboard_summary));
69     }
70 
71     @Test
getSummary_nonSystemUser_shouldReturnAppsSummary()72     public void getSummary_nonSystemUser_shouldReturnAppsSummary() {
73         mShadowUserManager.setIsAdminUser(false);
74         mShadowUserManager.setIsDemoUser(false);
75 
76         assertThat(mController.getSummary()).isEqualTo(
77                 mContext.getString(R.string.reset_dashboard_summary_onlyApps));
78     }
79 
80     @Test
getSummary_demoUser_shouldReturnFullSummary()81     public void getSummary_demoUser_shouldReturnFullSummary() {
82         mShadowUserManager.setIsAdminUser(false);
83 
84         // Place the device in demo mode.
85         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_DEMO_MODE, 1);
86 
87         // Indicate the user is a demo user.
88         mShadowUserManager.addUser(UserHandle.myUserId(), "test", UserInfo.FLAG_DEMO);
89 
90         assertThat(mController.getSummary()).isEqualTo(
91                 mContext.getString(R.string.reset_dashboard_summary));
92     }
93 }