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 com.android.settings.development.MockModemPreferenceController 20 .ALLOW_MOCK_MODEM_PROPERTY; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import android.content.Context; 25 import android.os.Looper; 26 import android.os.SystemProperties; 27 28 import androidx.preference.PreferenceManager; 29 import androidx.preference.PreferenceScreen; 30 import androidx.preference.SwitchPreference; 31 import androidx.test.core.app.ApplicationProvider; 32 import androidx.test.ext.junit.runners.AndroidJUnit4; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 @RunWith(AndroidJUnit4.class) 39 public class MockModemPreferenceControllerTest { 40 41 private Context mContext; 42 private MockModemPreferenceController mController; 43 private SwitchPreference mPreference; 44 45 @Before setUp()46 public void setUp() { 47 mContext = ApplicationProvider.getApplicationContext(); 48 mController = new MockModemPreferenceController(mContext); 49 if (Looper.myLooper() == null) { 50 Looper.prepare(); 51 } 52 53 final PreferenceManager preferenceManager = new PreferenceManager(mContext); 54 final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext); 55 mPreference = new SwitchPreference(mContext); 56 mPreference.setKey(mController.getPreferenceKey()); 57 screen.addPreference(mPreference); 58 mController.displayPreference(screen); 59 } 60 61 @Test onPreferenceChanged_settingDisabled_shouldNotAllowedMockModem()62 public void onPreferenceChanged_settingDisabled_shouldNotAllowedMockModem() { 63 mController.onPreferenceChange(mPreference, false /* new value */); 64 65 final boolean mode = SystemProperties.getBoolean( 66 ALLOW_MOCK_MODEM_PROPERTY, false /* default */); 67 68 assertThat(mode).isFalse(); 69 } 70 71 @Test onPreferenceChanged_settingEnabled_shouldAllowMockModem()72 public void onPreferenceChanged_settingEnabled_shouldAllowMockModem() { 73 mController.onPreferenceChange(mPreference, true /* new value */); 74 75 final boolean mode = SystemProperties.getBoolean( 76 ALLOW_MOCK_MODEM_PROPERTY, false /* default */); 77 78 assertThat(mode).isTrue(); 79 } 80 81 @Test updateState_settingEnabled_preferenceShouldBeChecked()82 public void updateState_settingEnabled_preferenceShouldBeChecked() { 83 SystemProperties.set(ALLOW_MOCK_MODEM_PROPERTY, 84 Boolean.toString(true)); 85 86 mController.updateState(mPreference); 87 assertThat(mPreference.isChecked()).isTrue(); 88 } 89 90 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()91 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 92 SystemProperties.set(ALLOW_MOCK_MODEM_PROPERTY, 93 Boolean.toString(false)); 94 95 mController.updateState(mPreference); 96 assertThat(mPreference.isChecked()).isFalse(); 97 } 98 99 @Test onDeveloperOptionsDisabled_shouldDisablePreference()100 public void onDeveloperOptionsDisabled_shouldDisablePreference() { 101 mController.onDeveloperOptionsSwitchDisabled(); 102 final boolean mode = SystemProperties.getBoolean( 103 ALLOW_MOCK_MODEM_PROPERTY, 104 false /* default */); 105 106 mController.updateState(mPreference); 107 108 assertThat(mode).isFalse(); 109 assertThat(mPreference.isChecked()).isFalse(); 110 } 111 } 112