1 /*
2  * Copyright (C) 2021 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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.robolectric.Shadows.shadowOf;
24 
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.ResolveInfo;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settings.testutils.ResolveInfoBuilder;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.shadows.ShadowPackageManager;
40 
41 /** Tests for {@link RTTSettingPreferenceController}. */
42 @RunWith(RobolectricTestRunner.class)
43 public class RTTSettingPreferenceControllerTest {
44 
45     private ShadowPackageManager mShadowPackageManager;
46     private RTTSettingPreferenceController mController;
47 
48     @Before
setUp()49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51         final Context context = spy(ApplicationProvider.getApplicationContext());
52         mShadowPackageManager = shadowOf(context.getPackageManager());
53         mController = spy(new RTTSettingPreferenceController(context, "rtt_setting"));
54         mController.mRTTIntent = new Intent("com.android.test.action.example");
55     }
56 
57     @Test
getAvailabilityStatus_resolvedIsEmpty_shouldReturnUNSUPPORTED_ON_DEVICE()58     public void getAvailabilityStatus_resolvedIsEmpty_shouldReturnUNSUPPORTED_ON_DEVICE() {
59         doReturn(true).when(mController).isRttSettingSupported();
60 
61         assertThat(mController.getAvailabilityStatus())
62                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
63     }
64 
65     @Test
getAvailabilityStatus_intentIsHandledButRttSettingNotSupported_returnAVAILABLE()66     public void getAvailabilityStatus_intentIsHandledButRttSettingNotSupported_returnAVAILABLE() {
67         setupTestIntent();
68         doReturn(false).when(mController).isRttSettingSupported();
69 
70         assertThat(mController.getAvailabilityStatus())
71                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
72     }
73 
74     @Test
getAvailabilityStatus_intentCanBeHandledAndRttSettingSupported_returnAVAILABLE()75     public void getAvailabilityStatus_intentCanBeHandledAndRttSettingSupported_returnAVAILABLE() {
76         setupTestIntent();
77         doReturn(true).when(mController).isRttSettingSupported();
78 
79         assertThat(mController.getAvailabilityStatus())
80                 .isEqualTo(BasePreferenceController.AVAILABLE);
81     }
82 
setupTestIntent()83     private void setupTestIntent() {
84         final ResolveInfo info = new ResolveInfoBuilder("pkg")
85                 .setActivity("pkg", "class").build();
86         final Intent intent = new Intent("com.android.test.action.example");
87         mShadowPackageManager.addResolveInfoForIntent(intent, info);
88     }
89 }
90