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 CaptionBackgroundOpacityController}. */
49 @RunWith(RobolectricTestRunner.class)
50 public class CaptioningBackgroundOpacityControllerTest {
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 CaptioningBackgroundOpacityController mController;
58     private ColorPreference mPreference;
59     private ShadowCaptioningManager mShadowCaptioningManager;
60 
61     @Before
setUp()62     public void setUp() {
63         mController = new CaptioningBackgroundOpacityController(mContext,
64                 "captioning_background_opacity");
65         final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
66         mPreference = new ColorPreference(mContext, attributeSet);
67         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
68         CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
69         mShadowCaptioningManager = Shadow.extract(captioningManager);
70     }
71 
72     @Test
getAvailabilityStatus_shouldReturnAvailable()73     public void getAvailabilityStatus_shouldReturnAvailable() {
74         assertThat(mController.getAvailabilityStatus())
75                 .isEqualTo(BasePreferenceController.AVAILABLE);
76     }
77 
78     @Test
getSummary_defaultValue_shouldReturnNonTransparent()79     public void getSummary_defaultValue_shouldReturnNonTransparent() {
80         mController.displayPreference(mScreen);
81 
82         assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
83     }
84 
85     @Test
getSummary_halfTransparentValue_shouldReturnHalfTransparent()86     public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
87         Settings.Secure.putInt(mContext.getContentResolver(),
88                 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0x80FFFFFF);
89 
90         mController.displayPreference(mScreen);
91 
92         assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
93     }
94 
95     @Test
setHalfTransparentValue_shouldReturnHalfTransparent()96     public void setHalfTransparentValue_shouldReturnHalfTransparent() {
97         mController.displayPreference(mScreen);
98 
99         mPreference.setValue(0x80FFFFFF);
100 
101         assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
102     }
103 
104     @Test
onValueChanged_shouldSetCaptionEnabled()105     public void onValueChanged_shouldSetCaptionEnabled() {
106         Settings.Secure.putInt(
107             mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF);
108         mController.displayPreference(mScreen);
109 
110         mController.onValueChanged(mPreference, 0x80FFFFFF);
111 
112         final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
113                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
114         assertThat(isCaptionEnabled).isTrue();
115     }
116 }
117