Beth Thibodeau | 48ce589 | 2020-08-07 11:43:35 -0400 | [diff] [blame] | 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 | import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; |
| 21 | |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | import static org.mockito.ArgumentMatchers.any; |
| 26 | import static org.mockito.ArgumentMatchers.anyInt; |
| 27 | import static org.mockito.Mockito.mock; |
| 28 | import static org.mockito.Mockito.spy; |
| 29 | import static org.mockito.Mockito.times; |
| 30 | import static org.mockito.Mockito.verify; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
| 33 | import android.content.ContentResolver; |
| 34 | import android.content.Context; |
| 35 | import android.content.pm.PackageManager; |
| 36 | import android.content.pm.ResolveInfo; |
| 37 | import android.provider.Settings; |
| 38 | |
| 39 | import androidx.preference.PreferenceGroup; |
| 40 | import androidx.preference.PreferenceScreen; |
| 41 | |
| 42 | import com.android.settings.testutils.ResolveInfoBuilder; |
| 43 | |
| 44 | import org.junit.After; |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.mockito.ArgumentCaptor; |
| 49 | import org.mockito.Mock; |
| 50 | import org.robolectric.RobolectricTestRunner; |
| 51 | import org.robolectric.RuntimeEnvironment; |
| 52 | |
| 53 | import java.util.ArrayList; |
| 54 | import java.util.List; |
| 55 | |
| 56 | @RunWith(RobolectricTestRunner.class) |
| 57 | public class ResumableMediaAppsControllerTest { |
| 58 | |
| 59 | private static final String KEY = "media_controls_resumable_apps"; |
| 60 | |
| 61 | private static final String FAKE_APP = "com.test.fakeapp1"; |
| 62 | |
| 63 | private Context mContext; |
| 64 | private int mOriginalQs; |
| 65 | private int mOriginalResume; |
| 66 | private String mOriginalBlocked; |
| 67 | private ContentResolver mContentResolver; |
| 68 | private ResumableMediaAppsController mController; |
| 69 | @Mock |
| 70 | private PackageManager mPackageManager; |
| 71 | @Mock |
| 72 | private PreferenceScreen mPreferenceScreen; |
| 73 | @Mock |
| 74 | private PreferenceGroup mPreferenceGroup; |
| 75 | |
| 76 | @Before |
| 77 | public void setUp() { |
| 78 | mContext = spy(RuntimeEnvironment.application); |
| 79 | mContentResolver = mContext.getContentResolver(); |
| 80 | mOriginalQs = Settings.Global.getInt(mContentResolver, |
| 81 | Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1); |
| 82 | mOriginalResume = Settings.Secure.getInt(mContentResolver, |
| 83 | Settings.Secure.MEDIA_CONTROLS_RESUME, 1); |
| 84 | mOriginalBlocked = Settings.Secure.getString(mContentResolver, |
| 85 | Settings.Secure.MEDIA_CONTROLS_RESUME_BLOCKED); |
| 86 | |
| 87 | // Start all tests with feature enabled, nothing blocked |
| 88 | Settings.Global.putInt(mContentResolver, Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1); |
| 89 | Settings.Secure.putInt(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME, 1); |
| 90 | Settings.Secure.putString(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME_BLOCKED, |
| 91 | mOriginalBlocked); |
| 92 | |
| 93 | mPreferenceScreen = mock(PreferenceScreen.class); |
| 94 | mPreferenceGroup = mock(PreferenceGroup.class); |
| 95 | mPackageManager = mock(PackageManager.class); |
| 96 | |
| 97 | List<ResolveInfo> fakeInfo = new ArrayList<>(); |
| 98 | fakeInfo.add(createResolveInfo(FAKE_APP)); |
| 99 | when(mPackageManager.queryIntentServices(any(), anyInt())).thenReturn(fakeInfo); |
| 100 | |
| 101 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
| 102 | when(mPreferenceScreen.findPreference(KEY)).thenReturn(mPreferenceGroup); |
| 103 | |
| 104 | mController = new ResumableMediaAppsController(mContext, KEY); |
| 105 | } |
| 106 | |
| 107 | @After |
| 108 | public void tearDown() { |
| 109 | Settings.Global.putInt(mContentResolver, Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, |
| 110 | mOriginalQs); |
| 111 | Settings.Secure.putInt(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME, |
| 112 | mOriginalResume); |
| 113 | Settings.Secure.putString(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME_BLOCKED, |
| 114 | mOriginalBlocked); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void getAvailability_hasEligibleApps_isAvailable() { |
| 119 | // The package manager already has an eligible app from setUp() |
| 120 | assertEquals(AVAILABLE, mController.getAvailabilityStatus()); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void getAvailability_noEligibleApps_isConditionallyUnavailable() { |
| 125 | Context context = mock(Context.class); |
| 126 | PackageManager packageManager = mock(PackageManager.class); |
| 127 | List<ResolveInfo> fakeInfo = new ArrayList<>(); |
| 128 | when(packageManager.queryIntentServices(any(), anyInt())).thenReturn(fakeInfo); |
| 129 | when(context.getPackageManager()).thenReturn(packageManager); |
| 130 | ResumableMediaAppsController controller = new ResumableMediaAppsController(context, KEY); |
| 131 | |
| 132 | assertEquals(CONDITIONALLY_UNAVAILABLE, controller.getAvailabilityStatus()); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void displayPreference_addsApps() { |
| 137 | mController.displayPreference(mPreferenceScreen); |
| 138 | verify(mPreferenceGroup, times(1)).addPreference(any()); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void unblockedApp_isChecked() { |
| 143 | ArgumentCaptor<ResumableMediaAppsController.MediaSwitchPreference> argument = |
| 144 | ArgumentCaptor.forClass(ResumableMediaAppsController.MediaSwitchPreference.class); |
| 145 | mController.displayPreference(mPreferenceScreen); |
| 146 | verify(mPreferenceGroup).addPreference(argument.capture()); |
| 147 | assertTrue(argument.getValue().isChecked()); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void blockedApp_isNotChecked() { |
| 152 | Settings.Secure.putString(mContentResolver, Settings.Secure.MEDIA_CONTROLS_RESUME_BLOCKED, |
| 153 | FAKE_APP); |
| 154 | |
| 155 | ArgumentCaptor<ResumableMediaAppsController.MediaSwitchPreference> argument = |
| 156 | ArgumentCaptor.forClass(ResumableMediaAppsController.MediaSwitchPreference.class); |
| 157 | mController.displayPreference(mPreferenceScreen); |
| 158 | verify(mPreferenceGroup).addPreference(argument.capture()); |
| 159 | |
| 160 | assertFalse(argument.getValue().isChecked()); |
| 161 | } |
| 162 | |
| 163 | private ResolveInfo createResolveInfo(String name) { |
| 164 | ResolveInfoBuilder builder = new ResolveInfoBuilder(name); |
| 165 | builder.setActivity(name, name); |
| 166 | return builder.build(); |
| 167 | } |
| 168 | } |