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.privatespace; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.spy; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.os.Flags; 28 import android.platform.test.annotations.RequiresFlagsEnabled; 29 import android.platform.test.flag.junit.CheckFlagsRule; 30 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 31 import android.provider.Settings; 32 33 import androidx.test.core.app.ApplicationProvider; 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 @RunWith(AndroidJUnit4.class) 43 @RequiresFlagsEnabled({Flags.FLAG_ALLOW_PRIVATE_PROFILE, 44 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES}) 45 public class HidePrivateSpaceSummaryControllerTest { 46 @Rule 47 public final CheckFlagsRule mCheckFlagsRule = 48 DeviceFlagsValueProvider.createCheckFlagsRule(); 49 private Context mContext; 50 private HidePrivateSpaceSummaryController mHidePrivateSpaceSummaryController; 51 private ContentResolver mContentResolver; 52 private int mOriginalHiddenValue; 53 54 /** Required setup before a test. */ 55 @Before setUp()56 public void setUp() { 57 mContext = spy(ApplicationProvider.getApplicationContext()); 58 mContentResolver = mContext.getContentResolver(); 59 final String preferenceKey = "private_space_hidden"; 60 61 mHidePrivateSpaceSummaryController = 62 new HidePrivateSpaceSummaryController(mContext, preferenceKey); 63 mOriginalHiddenValue = Settings.Secure.getInt(mContentResolver, 64 Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT, 0); 65 } 66 67 @After tearDown()68 public void tearDown() { 69 Settings.Secure.putInt(mContentResolver, Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT, 70 mOriginalHiddenValue); 71 } 72 73 /** Tests that the controller is always available. */ 74 @Test getAvailabilityStatus_returnsAvailable()75 public void getAvailabilityStatus_returnsAvailable() { 76 assertThat(mHidePrivateSpaceSummaryController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 77 } 78 79 /** Tests that the preference summary displays On when hide is enabled.*/ 80 @Test setEnabled_shouldDisplayOn()81 public void setEnabled_shouldDisplayOn() { 82 Settings.Secure.putInt(mContentResolver, Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT, 1); 83 84 assertThat(Settings.Secure.getInt(mContentResolver, 85 Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT, -1)).isEqualTo(1); 86 assertThat(mHidePrivateSpaceSummaryController.getSummary().toString()) 87 .isEqualTo("On"); 88 } 89 90 /** Tests that the preference summary displays Off when hide is disabled.*/ 91 @Test setDisabled_shouldDisplayOff()92 public void setDisabled_shouldDisplayOff() { 93 Settings.Secure.putInt(mContentResolver, Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT, 0); 94 95 assertThat(Settings.Secure.getInt(mContentResolver, 96 Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT, -1)).isEqualTo(0); 97 assertThat(mHidePrivateSpaceSummaryController.getSummary().toString()) 98 .isEqualTo("Off"); 99 } 100 } 101