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 org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.robolectric.RobolectricTestRunner;
34 import org.robolectric.RuntimeEnvironment;
35 
36 @RunWith(RobolectricTestRunner.class)
37 public class MediaControlsPreferenceControllerTest {
38 
39     private static final String KEY = "media_controls_resume_switch";
40 
41     private Context mContext;
42     private int mOriginalQs;
43     private int mOriginalResume;
44     private ContentResolver mContentResolver;
45     private MediaControlsPreferenceController mController;
46 
47     @Before
setUp()48     public void setUp() {
49         mContext = spy(RuntimeEnvironment.application);
50         mContentResolver = mContext.getContentResolver();
51         mOriginalQs = Settings.Global.getInt(mContentResolver,
52                 Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);
53         mOriginalResume = Settings.Secure.getInt(mContentResolver,
54                 Settings.Secure.MEDIA_CONTROLS_RESUME, 1);
55         mController = new MediaControlsPreferenceController(mContext, KEY);
56     }
57 
58     @After
tearDown()59     public void tearDown() {
60         Settings.Global.putInt(mContentResolver, Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS,
61                 mOriginalQs);
62         Settings.Secure.putInt(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME,
63                 mOriginalResume);
64     }
65 
66     @Test
getAvailability_returnAvailable()67     public void getAvailability_returnAvailable() {
68         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
69     }
70 
71     @Test
setChecked_enable_shouldTurnOff()72     public void setChecked_enable_shouldTurnOff() {
73         Settings.Global.putInt(mContentResolver, Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);
74         Settings.Secure.putInt(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME, 1);
75 
76         assertThat(mController.isChecked()).isFalse();
77 
78         mController.setChecked(true);
79 
80         assertThat(Settings.Secure.getInt(mContentResolver,
81                 Settings.Secure.MEDIA_CONTROLS_RESUME, -1)).isEqualTo(0);
82     }
83 
84     @Test
setChecked_disable_shouldTurnOn()85     public void setChecked_disable_shouldTurnOn() {
86         Settings.Global.putInt(mContentResolver, Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);
87         Settings.Secure.putInt(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME, 0);
88 
89         assertThat(mController.isChecked()).isTrue();
90 
91         mController.setChecked(false);
92 
93         assertThat(Settings.Secure.getInt(mContentResolver,
94                 Settings.Secure.MEDIA_CONTROLS_RESUME, -1)).isEqualTo(1);
95     }
96 }
97