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.sound; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.content.ContentResolver; 24 import android.content.Context; 25 import android.provider.Settings; 26 27 import androidx.test.core.app.ApplicationProvider; 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 30 import org.junit.After; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 @RunWith(AndroidJUnit4.class) 36 public class MediaControlsLockScreenPreferenceControllerTest { 37 38 private static final String KEY = "media_controls_lockscreen"; 39 private final Context mContext = ApplicationProvider.getApplicationContext(); 40 41 private int mOriginalPreference; 42 private ContentResolver mContentResolver; 43 private MediaControlsLockScreenPreferenceController mController; 44 45 @Before setUp()46 public void setUp() { 47 mContentResolver = mContext.getContentResolver(); 48 mOriginalPreference = Settings.Secure.getInt(mContentResolver, 49 Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, 1); 50 mController = new MediaControlsLockScreenPreferenceController(mContext, KEY); 51 } 52 53 @After tearDown()54 public void tearDown() { 55 Settings.Secure.putInt(mContentResolver, Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, 56 mOriginalPreference); 57 } 58 59 @Test getAvailability_returnAvailable()60 public void getAvailability_returnAvailable() { 61 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 62 } 63 64 @Test setChecked_disable_shouldTurnOff()65 public void setChecked_disable_shouldTurnOff() { 66 Settings.Secure.putInt(mContentResolver, 67 Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, 1); 68 69 assertThat(mController.isChecked()).isTrue(); 70 71 mController.setChecked(false); 72 73 assertThat(Settings.Secure.getInt(mContentResolver, 74 Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, -1)).isEqualTo(0); 75 } 76 77 @Test setChecked_enable_shouldTurnOn()78 public void setChecked_enable_shouldTurnOn() { 79 Settings.Secure.putInt(mContentResolver, 80 Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, 0); 81 82 assertThat(mController.isChecked()).isFalse(); 83 84 mController.setChecked(true); 85 86 assertThat(Settings.Secure.getInt(mContentResolver, 87 Settings.Secure.MEDIA_CONTROLS_LOCK_SCREEN, -1)).isEqualTo(1); 88 } 89 } 90