1 /* 2 * Copyright (C) 2023 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.settings.communal; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.os.UserManager; 27 28 import androidx.test.core.app.ApplicationProvider; 29 import androidx.test.ext.junit.runners.AndroidJUnit4; 30 31 import com.android.settings.Utils; 32 import com.android.settings.testutils.ResourcesUtils; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 40 @RunWith(AndroidJUnit4.class) 41 public class CommunalPreferenceControllerTest { 42 @Mock 43 private UserManager mUserManager; 44 45 private Context mContext; 46 private Resources mResources; 47 private CommunalPreferenceController mController; 48 49 private static final String PREF_KEY = "test_key"; 50 51 @Before setup()52 public void setup() { 53 MockitoAnnotations.initMocks(this); 54 mContext = spy(ApplicationProvider.getApplicationContext()); 55 mResources = spy(mContext.getResources()); 56 57 mController = new CommunalPreferenceController(mContext, PREF_KEY); 58 59 when(mContext.getResources()).thenReturn(mResources); 60 when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); 61 } 62 63 @Test isAvailable_communalEnabled_shouldBeTrueForDockUser()64 public void isAvailable_communalEnabled_shouldBeTrueForDockUser() { 65 setCommunalEnabled(true); 66 when(Utils.canCurrentUserDream(mContext)).thenReturn(true); 67 assertTrue(mController.isAvailable()); 68 } 69 70 @Test isAvailable_communalEnabled_shouldBeFalseForNonDockUser()71 public void isAvailable_communalEnabled_shouldBeFalseForNonDockUser() { 72 setCommunalEnabled(true); 73 when(Utils.canCurrentUserDream(mContext)).thenReturn(false); 74 assertFalse(mController.isAvailable()); 75 } 76 77 @Test isAvailable_communalDisabled_shouldBeFalseForDockUser()78 public void isAvailable_communalDisabled_shouldBeFalseForDockUser() { 79 setCommunalEnabled(false); 80 when(Utils.canCurrentUserDream(mContext)).thenReturn(true); 81 assertFalse(mController.isAvailable()); 82 } 83 setCommunalEnabled(boolean enabled)84 private void setCommunalEnabled(boolean enabled) { 85 final int boolId = ResourcesUtils.getResourcesId( 86 ApplicationProvider.getApplicationContext(), "bool", 87 "config_show_communal_settings"); 88 when(mResources.getBoolean(boolId)).thenReturn(enabled); 89 } 90 } 91