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