1 /* 2 * Copyright (C) 2025 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.supervision 17 18 import android.content.Context 19 import android.provider.Settings 20 import android.provider.Settings.Secure.BROWSER_CONTENT_FILTERS_ENABLED 21 import android.provider.Settings.SettingNotFoundException 22 import androidx.test.core.app.ApplicationProvider 23 import androidx.test.ext.junit.runners.AndroidJUnit4 24 import com.android.settings.R 25 import com.android.settingslib.preference.createAndBindWidget 26 import com.android.settingslib.widget.SelectorWithWidgetPreference 27 import com.google.common.truth.Truth.assertThat 28 import org.junit.Assert.assertThrows 29 import org.junit.Before 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 33 @RunWith(AndroidJUnit4::class) 34 class SupervisionSafeSitesPreferenceTest { 35 private val context: Context = ApplicationProvider.getApplicationContext() 36 private lateinit var dataStore: SupervisionSafeSitesDataStore 37 private lateinit var allowAllSitesPreference: SupervisionAllowAllSitesPreference 38 private lateinit var blockExplicitSitesPreference: SupervisionBlockExplicitSitesPreference 39 40 @Before setUpnull41 fun setUp() { 42 dataStore = SupervisionSafeSitesDataStore(context) 43 allowAllSitesPreference = SupervisionAllowAllSitesPreference(dataStore) 44 blockExplicitSitesPreference = SupervisionBlockExplicitSitesPreference(dataStore) 45 } 46 47 @Test getTitle_allowAllSitesnull48 fun getTitle_allowAllSites() { 49 assertThat(allowAllSitesPreference.title) 50 .isEqualTo(R.string.supervision_web_content_filters_browser_allow_all_sites_title) 51 } 52 53 @Test getTitle_blockExplicitSitesnull54 fun getTitle_blockExplicitSites() { 55 assertThat(blockExplicitSitesPreference.title) 56 .isEqualTo(R.string.supervision_web_content_filters_browser_block_explicit_sites_title) 57 } 58 59 @Test getSummary_blockExplicitSitesnull60 fun getSummary_blockExplicitSites() { 61 assertThat(blockExplicitSitesPreference.summary) 62 .isEqualTo( 63 R.string.supervision_web_content_filters_browser_block_explicit_sites_summary 64 ) 65 } 66 67 @Test allowAllSitesIsChecked_whenNoValueIsSetnull68 fun allowAllSitesIsChecked_whenNoValueIsSet() { 69 assertThrows(SettingNotFoundException::class.java) { 70 Settings.Secure.getInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED) 71 } 72 assertThat(getBlockExplicitSitesWidget().isChecked).isFalse() 73 assertThat(getAllowAllSitesWidget().isChecked).isTrue() 74 } 75 76 @Test blockExplicitSitesIsChecked_whenPreviouslyEnablednull77 fun blockExplicitSitesIsChecked_whenPreviouslyEnabled() { 78 Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 1) 79 assertThat(getAllowAllSitesWidget().isChecked).isFalse() 80 assertThat(getBlockExplicitSitesWidget().isChecked).isTrue() 81 } 82 83 @Test clickBlockExplicitSites_enablesFilternull84 fun clickBlockExplicitSites_enablesFilter() { 85 Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 0) 86 val blockExplicitSitesWidget = getBlockExplicitSitesWidget() 87 assertThat(blockExplicitSitesWidget.isChecked).isFalse() 88 89 blockExplicitSitesWidget.performClick() 90 91 assertThat( 92 Settings.Secure.getInt( 93 context.getContentResolver(), 94 BROWSER_CONTENT_FILTERS_ENABLED, 95 ) 96 ) 97 .isEqualTo(1) 98 assertThat(blockExplicitSitesWidget.isChecked).isTrue() 99 } 100 101 @Test clickAllowAllSites_disablesFilternull102 fun clickAllowAllSites_disablesFilter() { 103 Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 1) 104 val allowAllSitesWidget = getAllowAllSitesWidget() 105 assertThat(allowAllSitesWidget.isChecked).isFalse() 106 107 allowAllSitesWidget.performClick() 108 109 assertThat( 110 Settings.Secure.getInt( 111 context.getContentResolver(), 112 BROWSER_CONTENT_FILTERS_ENABLED, 113 ) 114 ) 115 .isEqualTo(0) 116 assertThat(allowAllSitesWidget.isChecked).isTrue() 117 } 118 getBlockExplicitSitesWidgetnull119 private fun getBlockExplicitSitesWidget(): SelectorWithWidgetPreference { 120 return blockExplicitSitesPreference.createAndBindWidget(context) 121 } 122 getAllowAllSitesWidgetnull123 private fun getAllowAllSitesWidget(): SelectorWithWidgetPreference { 124 return allowAllSitesPreference.createAndBindWidget(context) 125 } 126 } 127