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.ondevicepersonalization.services; 18 19 import static com.android.ondevicepersonalization.services.Flags.GLOBAL_KILL_SWITCH; 20 import static com.android.ondevicepersonalization.services.PhFlags.KEY_GLOBAL_KILL_SWITCH; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import android.provider.DeviceConfig; 25 26 import androidx.test.InstrumentationRegistry; 27 import androidx.test.runner.AndroidJUnit4; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 /** Unit tests for {@link com.android.ondevicepersonalization.service.PhFlags} */ 34 @RunWith(AndroidJUnit4.class) 35 public class PhFlagsTest { 36 private static final String WRITE_DEVICE_CONFIG_PERMISSION = 37 "android.permission.WRITE_DEVICE_CONFIG"; 38 39 private static final String READ_DEVICE_CONFIG_PERMISSION = 40 "android.permission.READ_DEVICE_CONFIG"; 41 42 private static final String MONITOR_DEVICE_CONFIG_ACCESS = 43 "android.permission.MONITOR_DEVICE_CONFIG_ACCESS"; 44 45 /** 46 * Get necessary permissions to access Setting.Config API and set up context 47 */ 48 @Before setUpContext()49 public void setUpContext() throws Exception { 50 InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( 51 WRITE_DEVICE_CONFIG_PERMISSION, READ_DEVICE_CONFIG_PERMISSION, 52 MONITOR_DEVICE_CONFIG_ACCESS); 53 // sContext = InstrumentationRegistry.getContext(); 54 // sContentResolver = sContext.getContentResolver(); 55 } 56 57 @Test testGetGlobalKillSwitch()58 public void testGetGlobalKillSwitch() { 59 // Without any overriding, the value is the hard coded constant. 60 assertThat(FlagsFactory.getFlags().getGlobalKillSwitch()).isEqualTo(GLOBAL_KILL_SWITCH); 61 62 // Now overriding with the value from PH. 63 final boolean phOverridingValue = true; 64 DeviceConfig.setProperty( 65 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION, 66 KEY_GLOBAL_KILL_SWITCH, 67 Boolean.toString(phOverridingValue), 68 /* makeDefault */ false); 69 70 Flags phFlags = FlagsFactory.getFlags(); 71 assertThat(phFlags.getGlobalKillSwitch()).isEqualTo(phOverridingValue); 72 } 73 disableGlobalKillSwitch()74 private void disableGlobalKillSwitch() { 75 // Override the global_kill_switch to test other flag values. 76 DeviceConfig.setProperty( 77 DeviceConfig.NAMESPACE_ON_DEVICE_PERSONALIZATION, 78 KEY_GLOBAL_KILL_SWITCH, 79 Boolean.toString(false), 80 /* makeDefault */ false); 81 } 82 } 83