aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowCaptioningManagerTest.java
blob: bb9e408fda67cf9c1b8ddd9e0560bed024369b8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.KITKAT;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.os.Looper;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;
import android.view.accessibility.CaptioningManager.CaptioningChangeListener;
import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.Locale;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

/** Tests for the ShadowCaptioningManager. */
@RunWith(AndroidJUnit4.class)
@Config(minSdk = KITKAT)
public final class ShadowCaptioningManagerTest {

  private TestCaptioningChangeListener captioningChangeListener =
      new TestCaptioningChangeListener();

  private static final int ENABLED = 1;
  private static final int DISABLED = 0;

  private CaptioningManager captioningManager;
  private Context context;

  public class TestCaptioningChangeListener extends CaptioningChangeListener {
    public boolean isEnabled = false;
    @Nullable public CaptionStyle captionStyle = null;
    @Nullable public Locale locale = null;
    public float fontScale = 1.0f;
    public boolean systemAudioCaptioningEnabled = false;
    public boolean systemAudioCaptioningUiEnabled = false;

    @Override
    public void onEnabledChanged(boolean enabled) {
      isEnabled = enabled;
    }

    @Override
    public void onUserStyleChanged(@NonNull CaptionStyle userStyle) {
      captionStyle = userStyle;
    }

    @Override
    public void onLocaleChanged(@Nullable Locale locale) {
      this.locale = locale;
    }

    @Override
    public void onFontScaleChanged(float fontScale) {
      this.fontScale = fontScale;
    }

    @Override
    public void onSystemAudioCaptioningChanged(boolean enabled) {
      this.systemAudioCaptioningEnabled = enabled;
    }

    @Override
    public void onSystemAudioCaptioningUiChanged(boolean enabled) {
      this.systemAudioCaptioningUiEnabled = enabled;
    }
  }

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    captioningManager =
        (CaptioningManager)
            ApplicationProvider.getApplicationContext()
                .getSystemService(Context.CAPTIONING_SERVICE);
    context = RuntimeEnvironment.getApplication();
  }

  @Test
  public void setEnabled_true() {
    Settings.Secure.putInt(
        context.getContentResolver(), Secure.ACCESSIBILITY_CAPTIONING_ENABLED, ENABLED);

    assertThat(captioningManager.isEnabled()).isTrue();
  }

  @Test
  public void setEnabled_false() {
    Settings.Secure.putInt(
        context.getContentResolver(), Secure.ACCESSIBILITY_CAPTIONING_ENABLED, DISABLED);

    assertThat(captioningManager.isEnabled()).isFalse();
  }

  @Test
  public void setEnabled_callsCallback() {
    captioningManager.addCaptioningChangeListener(captioningChangeListener);
    Settings.Secure.putInt(
        context.getContentResolver(), Secure.ACCESSIBILITY_CAPTIONING_ENABLED, ENABLED);

    shadowOf(Looper.getMainLooper()).idle();
    assertThat(captioningChangeListener.isEnabled).isTrue();
  }

  @Test
  public void setFontScale_updatesValue() {
    Settings.Secure.putFloat(
        context.getContentResolver(), Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 2.0f);

    assertThat(captioningManager.getFontScale()).isEqualTo(2.0f);
  }

  @Test
  public void setFontScale_callsCallback() {
    captioningManager.addCaptioningChangeListener(captioningChangeListener);
    Settings.Secure.putFloat(
        context.getContentResolver(), Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, 3.0f);

    shadowOf(Looper.getMainLooper()).idle();
    assertThat(captioningChangeListener.fontScale).isEqualTo(3.0f);
  }

  @Test
  public void setLocale_updatesValue() {
    Settings.Secure.putString(
        context.getContentResolver(),
        Secure.ACCESSIBILITY_CAPTIONING_LOCALE,
        Locale.JAPANESE.toLanguageTag());

    assertThat(captioningManager.getLocale()).isEqualTo(Locale.JAPANESE);
  }

  @Test
  public void setLocale_callsCallback() {
    captioningManager.addCaptioningChangeListener(captioningChangeListener);
    Settings.Secure.putString(
        context.getContentResolver(),
        Secure.ACCESSIBILITY_CAPTIONING_LOCALE,
        Locale.FRENCH.toLanguageTag());

    shadowOf(Looper.getMainLooper()).idle();
    assertThat(captioningChangeListener.locale).isEqualTo(Locale.FRENCH);
  }

  @Test
  @Config(minSdk = TIRAMISU)
  public void setSystemAudioCaptioningEnabled_updatesValue() {
    captioningManager.setSystemAudioCaptioningEnabled(true);

    assertThat(captioningManager.isSystemAudioCaptioningEnabled()).isEqualTo(true);
  }

  @Test
  @Config(minSdk = TIRAMISU)
  public void setSystemAudioCaptioningEnabled_callsCallback() {
    captioningManager.setSystemAudioCaptioningEnabled(false);

    shadowOf(Looper.getMainLooper()).idle();
    assertThat(captioningChangeListener.systemAudioCaptioningEnabled).isEqualTo(false);
  }

  @Test
  @Config(minSdk = TIRAMISU)
  public void setSystemAudioCaptioningUiEnabled_updatesValue() {
    captioningManager.setSystemAudioCaptioningUiEnabled(true);

    assertThat(captioningManager.isSystemAudioCaptioningUiEnabled()).isEqualTo(true);
  }

  @Test
  @Config(minSdk = TIRAMISU)
  public void setSystemAudioCaptioningUiEnabled_callsCallback() {
    captioningManager.setSystemAudioCaptioningUiEnabled(false);

    shadowOf(Looper.getMainLooper()).idle();
    assertThat(captioningChangeListener.systemAudioCaptioningUiEnabled).isEqualTo(false);
  }
}