1 /* 2 * Copyright (C) 2022 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.development; 18 19 import static junit.framework.Assert.assertFalse; 20 import static junit.framework.Assert.assertTrue; 21 22 import android.content.Context; 23 import android.os.SystemProperties; 24 25 import androidx.fragment.app.Fragment; 26 import androidx.preference.Preference; 27 import androidx.test.core.app.ApplicationProvider; 28 29 import com.android.settingslib.development.DevelopmentSettingsEnabler; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.annotation.Config; 38 import org.robolectric.shadows.ShadowSystemProperties; 39 40 @RunWith(RobolectricTestRunner.class) 41 @Config( 42 shadows = { 43 ShadowSystemProperties.class, 44 }) 45 public class RebootWithMtePreferenceControllerTest { 46 private Context mContext; 47 private RebootWithMtePreferenceController mController; 48 @Mock private Fragment mFragment; 49 50 @Before setup()51 public void setup() throws Exception { 52 MockitoAnnotations.initMocks(this); 53 54 mContext = ApplicationProvider.getApplicationContext(); 55 mController = new RebootWithMtePreferenceController(mContext); 56 mController.setFragment(mFragment); 57 DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(mContext, true); 58 } 59 60 @Test onAvailable_falseByDefault()61 public void onAvailable_falseByDefault() { 62 assertFalse(mController.isAvailable()); 63 } 64 65 @Test onAvailable_sysPropEnabled()66 public void onAvailable_sysPropEnabled() { 67 SystemProperties.set("ro.arm64.memtag.bootctl_supported", "1"); 68 assertTrue(mController.isAvailable()); 69 } 70 71 @Test updateState_enabledByDefault()72 public void updateState_enabledByDefault() { 73 Preference preference = new Preference(mContext); 74 mController.updateState(preference); 75 assertTrue(preference.isEnabled()); 76 } 77 78 @Test updateState_disabledIfAlreadyOn()79 public void updateState_disabledIfAlreadyOn() { 80 SystemProperties.set("arm64.memtag.bootctl", "memtag"); 81 Preference preference = new Preference(mContext); 82 mController.updateState(preference); 83 assertFalse(preference.isEnabled()); 84 } 85 } 86