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.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 com.android.settings.core.BasePreferenceController;
30 import com.android.settings.testutils.ResolveInfoBuilder;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.shadows.ShadowPackageManager;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class RTTSettingPreferenceControllerTest {
41 
42     private Context mContext;
43     private ShadowPackageManager mShadowPackageManager;
44     private RTTSettingPreferenceController mController;
45 
46     @Before
setUp()47     public void setUp() {
48         mContext = RuntimeEnvironment.application;
49         mShadowPackageManager = shadowOf(mContext.getPackageManager());
50         mController = spy(new RTTSettingPreferenceController(mContext, "rtt_setting"));
51         mController.mRTTIntent = new Intent("com.android.test.action.example");
52     }
53 
54     @Test
getAvailabilityStatus_defaultDialerIsExpected_intentCanBeHandled_returnAVAILABLE()55     public void getAvailabilityStatus_defaultDialerIsExpected_intentCanBeHandled_returnAVAILABLE() {
56         // Default dialer is expected.
57         doReturn(true).when(mController).isDialerSupportRTTSetting();
58         // Intent can be handled.
59         final ResolveInfo info = new ResolveInfoBuilder("pkg")
60                 .setActivity("pkg", "class").build();
61         final Intent intent = new Intent("com.android.test.action.example");
62         mShadowPackageManager.addResolveInfoForIntent(intent, info);
63 
64         assertThat(mController.getAvailabilityStatus())
65                 .isEqualTo(BasePreferenceController.AVAILABLE);
66     }
67 
68     @Test
getAvailabilityStatus_intentCanNotBeHandled_shouldReturnUNSUPPORTED_ON_DEVICE()69     public void getAvailabilityStatus_intentCanNotBeHandled_shouldReturnUNSUPPORTED_ON_DEVICE() {
70         // Default dialer is expected.
71         doReturn(true).when(mController).isDialerSupportRTTSetting();
72         // Intent can not be handled.
73 
74         assertThat(mController.getAvailabilityStatus())
75                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
76     }
77 
78     @Test
getAvailabilityStatus_defaultDialerIsNotExpected_returnUNSUPPORTED_ON_DEVICE()79     public void getAvailabilityStatus_defaultDialerIsNotExpected_returnUNSUPPORTED_ON_DEVICE() {
80         // Default dialer is not expected.
81         doReturn(false).when(mController).isDialerSupportRTTSetting();
82         // Intent can be handled.
83         final ResolveInfo info = new ResolveInfoBuilder("pkg")
84                 .setActivity("pkg", "class").build();
85         final Intent intent = new Intent("com.android.test.action.example");
86         mShadowPackageManager.addResolveInfoForIntent(intent, info);
87 
88         assertThat(mController.getAvailabilityStatus())
89                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
90     }
91 }
92