1 /*
2  * Copyright (C) 2022 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.accessibility.AccessibilityUtil.State.OFF;
20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.provider.Settings;
28 import android.util.AttributeSet;
29 import android.view.accessibility.CaptioningManager;
30 
31 import androidx.preference.PreferenceScreen;
32 import androidx.test.core.app.ApplicationProvider;
33 
34 import com.android.settings.core.BasePreferenceController;
35 
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnit;
42 import org.mockito.junit.MockitoRule;
43 import org.robolectric.Robolectric;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.shadow.api.Shadow;
46 import org.robolectric.shadows.ShadowCaptioningManager;
47 
48 /** Tests for {@link CaptioningWindowOpacityController}. */
49 @RunWith(RobolectricTestRunner.class)
50 public class CaptioningWindowOpacityControllerTest {
51 
52     @Rule
53     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
54     @Mock
55     private PreferenceScreen mScreen;
56     private final Context mContext = ApplicationProvider.getApplicationContext();
57     private CaptioningWindowOpacityController mController;
58     private ColorPreference mPreference;
59     private ShadowCaptioningManager mShadowCaptioningManager;
60 
61     @Before
setUp()62     public void setUp() {
63         mController = new CaptioningWindowOpacityController(mContext, "captioning_window_opacity");
64         final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
65         mPreference = new ColorPreference(mContext, attributeSet);
66         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
67         CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
68         mShadowCaptioningManager = Shadow.extract(captioningManager);
69     }
70 
71     @Test
getAvailabilityStatus_shouldReturnAvailable()72     public void getAvailabilityStatus_shouldReturnAvailable() {
73         assertThat(mController.getAvailabilityStatus())
74                 .isEqualTo(BasePreferenceController.AVAILABLE);
75     }
76 
77     @Test
getSummary_defaultValue_shouldReturnNonTransparent()78     public void getSummary_defaultValue_shouldReturnNonTransparent() {
79         mController.displayPreference(mScreen);
80 
81         assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
82     }
83 
84     @Test
getSummary_halfTransparentValue_shouldReturnHalfTransparent()85     public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
86         Settings.Secure.putInt(mContext.getContentResolver(),
87                 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0x80FFFFFF);
88 
89         mController.displayPreference(mScreen);
90 
91         assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
92     }
93 
94     @Test
setHalfTransparentValue_shouldReturnHalfTransparent()95     public void setHalfTransparentValue_shouldReturnHalfTransparent() {
96         mController.displayPreference(mScreen);
97 
98         mPreference.setValue(0x80FFFFFF);
99 
100         assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
101     }
102 
103     @Test
onValueChanged_shouldSetCaptionEnabled()104     public void onValueChanged_shouldSetCaptionEnabled() {
105         Settings.Secure.putInt(
106             mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF);
107         mController.displayPreference(mScreen);
108 
109         mController.onValueChanged(mPreference, 0x80FFFFFF);
110 
111         final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
112                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
113         assertThat(isCaptionEnabled).isTrue();
114     }
115 }
116