1 /* 2 * Copyright (C) 2024 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 com.android.systemui.Flags.FLAG_GLANCEABLE_HUB_V2; 20 21 import static org.junit.Assert.assertFalse; 22 import static org.junit.Assert.assertTrue; 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.Mockito.doReturn; 26 import static org.mockito.Mockito.spy; 27 28 import android.content.Context; 29 import android.os.UserHandle; 30 import android.platform.test.annotations.DisableFlags; 31 import android.platform.test.annotations.EnableFlags; 32 import android.platform.test.flag.junit.SetFlagsRule; 33 34 import com.android.settings.R; 35 import com.android.settings.testutils.shadow.SettingsShadowResources; 36 import com.android.settings.testutils.shadow.ShadowUserManager; 37 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 import org.robolectric.annotation.Config; 45 46 @RunWith(RobolectricTestRunner.class) 47 @Config(shadows = {SettingsShadowResources.class, ShadowUserManager.class}) 48 public class CommunalPreferenceControllerTest { 49 @Rule 50 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 51 52 private ShadowUserManager mShadowUserManager; 53 private CommunalPreferenceController mController; 54 55 private static final String PREF_KEY = "test_key"; 56 57 @Before setup()58 public void setup() { 59 final Context context = spy(RuntimeEnvironment.application); 60 mShadowUserManager = ShadowUserManager.getShadow(); 61 doReturn(context).when(context).createContextAsUser(any(UserHandle.class), anyInt()); 62 mController = new CommunalPreferenceController(context, PREF_KEY); 63 } 64 65 @Test 66 @DisableFlags(FLAG_GLANCEABLE_HUB_V2) isAvailable_communalEnabled_shouldBeTrueForPrimaryUser()67 public void isAvailable_communalEnabled_shouldBeTrueForPrimaryUser() { 68 setCommunalEnabled(true); 69 mShadowUserManager.setUserForeground(true); 70 assertTrue(mController.isAvailable()); 71 } 72 73 @Test 74 @DisableFlags(FLAG_GLANCEABLE_HUB_V2) isAvailable_communalEnabled_shouldBeFalseForSecondaryUser()75 public void isAvailable_communalEnabled_shouldBeFalseForSecondaryUser() { 76 setCommunalEnabled(true); 77 mShadowUserManager.setUserForeground(false); 78 assertFalse(mController.isAvailable()); 79 } 80 81 @Test 82 @DisableFlags(FLAG_GLANCEABLE_HUB_V2) isAvailable_communalDisabled_shouldBeFalseForPrimaryUser()83 public void isAvailable_communalDisabled_shouldBeFalseForPrimaryUser() { 84 setCommunalEnabled(false); 85 mShadowUserManager.setUserForeground(true); 86 assertFalse(mController.isAvailable()); 87 } 88 89 @Test 90 @EnableFlags(FLAG_GLANCEABLE_HUB_V2) isAvailable_glanceableHubV2Enabled_shouldBeFalseForPrimaryUser()91 public void isAvailable_glanceableHubV2Enabled_shouldBeFalseForPrimaryUser() { 92 setCommunalEnabled(true); 93 mShadowUserManager.setUserForeground(true); 94 assertFalse(mController.isAvailable()); 95 } 96 setCommunalEnabled(boolean enabled)97 private void setCommunalEnabled(boolean enabled) { 98 SettingsShadowResources.overrideResource(R.bool.config_show_communal_settings, enabled); 99 } 100 } 101