1 /*
2  * Copyright (C) 2019 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.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 
27 import android.content.Context;
28 import android.content.pm.PackageManager;
29 import android.content.pm.ResolveInfo;
30 import android.provider.Settings;
31 
32 import androidx.preference.Preference;
33 import androidx.test.core.app.ApplicationProvider;
34 
35 import com.android.settings.R;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.Shadows;
42 import org.robolectric.shadows.ShadowPackageManager;
43 
44 import java.util.Collections;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class LiveCaptionPreferenceControllerTest {
48 
49     private Context mContext;
50     private LiveCaptionPreferenceController mController;
51     private Preference mLiveCaptionPreference;
52 
53     @Before
setUp()54     public void setUp() {
55         mContext = spy(ApplicationProvider.getApplicationContext());
56         PackageManager pm = spy(mContext.getPackageManager());
57         doReturn(pm).when(mContext).getPackageManager();
58         doReturn("com.caption").when(pm).getSystemCaptionsServicePackageName();
59         mController = new LiveCaptionPreferenceController(mContext, "test_key");
60         mLiveCaptionPreference = new Preference(mContext);
61         mLiveCaptionPreference.setSummary(R.string.live_caption_summary);
62     }
63 
64     @Test
getAvailabilityStatus_canResolveIntent_shouldReturnAvailable()65     public void getAvailabilityStatus_canResolveIntent_shouldReturnAvailable() {
66         final ShadowPackageManager pm = Shadows.shadowOf(mContext.getPackageManager());
67         pm.addResolveInfoForIntent(LiveCaptionPreferenceController.LIVE_CAPTION_INTENT,
68                 new ResolveInfo());
69 
70         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
71     }
72 
73     @Test
getAvailabilityStatus_noResolveIntent_shouldReturnUnavailable()74     public void getAvailabilityStatus_noResolveIntent_shouldReturnUnavailable() {
75         final ShadowPackageManager pm = Shadows.shadowOf(mContext.getPackageManager());
76         pm.setResolveInfosForIntent(LiveCaptionPreferenceController.LIVE_CAPTION_INTENT,
77                 Collections.emptyList());
78 
79         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
80     }
81 
82     @Test
updateState_liveCaptionEnabled_subtextShowsOnSummary()83     public void updateState_liveCaptionEnabled_subtextShowsOnSummary() {
84         setLiveCaptionEnabled(true);
85 
86         mController.updateState(mLiveCaptionPreference);
87 
88         assertThat(mLiveCaptionPreference.getSummary().toString()).isEqualTo(
89                 mContext.getString(R.string.live_caption_summary)
90         );
91     }
92 
93     @Test
updateState_liveCaptionDisabled_subtextShowsOffSummary()94     public void updateState_liveCaptionDisabled_subtextShowsOffSummary() {
95         setLiveCaptionEnabled(false);
96 
97         mController.updateState(mLiveCaptionPreference);
98 
99         assertThat(mLiveCaptionPreference.getSummary()).isEqualTo(
100                 mContext.getString(R.string.live_caption_summary)
101         );
102     }
103 
setLiveCaptionEnabled(boolean enabled)104     private void setLiveCaptionEnabled(boolean enabled) {
105         Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ODI_CAPTIONS_ENABLED,
106                 enabled ? AccessibilityUtil.State.ON: AccessibilityUtil.State.OFF);
107     }
108 }