aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowCaptioningManagerTest.java
blob: 470eeff7579fc28d8cc4de44fbe51ae34dfcccd2 (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
package org.robolectric.shadows;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptioningChangeListener;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;

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

  @Mock private CaptioningChangeListener captioningChangeListener;

  private CaptioningManager captioningManager;

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

  @Test
  public void setEnabled_true() {
    assertThat(captioningManager.isEnabled()).isFalse();

    shadowOf(captioningManager).setEnabled(true);

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

  @Test
  public void setEnabled_false() {
    shadowOf(captioningManager).setEnabled(false);

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

  @Test
  public void setFontScale_changesValueOfGetFontScale() {
    float fontScale = 1.5f;
    shadowOf(captioningManager).setFontScale(fontScale);

    assertThat(captioningManager.getFontScale()).isWithin(0.001f).of(fontScale);
  }

  @Test
  public void setFontScale_notifiesObservers() {
    float fontScale = 1.5f;
    captioningManager.addCaptioningChangeListener(captioningChangeListener);

    shadowOf(captioningManager).setFontScale(fontScale);

    verify(captioningChangeListener).onFontScaleChanged(fontScale);
  }

  @Test
  public void addCaptioningChangeListener_doesNotRegisterSameListenerTwice() {
    float fontScale = 1.5f;
    captioningManager.addCaptioningChangeListener(captioningChangeListener);

    captioningManager.addCaptioningChangeListener(captioningChangeListener);

    shadowOf(captioningManager).setFontScale(fontScale);
    verify(captioningChangeListener).onFontScaleChanged(fontScale);
  }

  @Test
  public void removeCaptioningChangeListener_unregistersFontScaleListener() {
    captioningManager.addCaptioningChangeListener(captioningChangeListener);

    captioningManager.removeCaptioningChangeListener(captioningChangeListener);

    shadowOf(captioningManager).setFontScale(1.5f);
    verifyNoMoreInteractions(captioningChangeListener);
  }

  @Test
  public void setLocale_nonNull() {
    Locale locale = Locale.US;
    assertThat(captioningManager.getLocale()).isNull();

    shadowOf(captioningManager).setLocale(locale);

    assertThat(captioningManager.getLocale()).isEqualTo(locale);
  }

  @Test
  public void setLocale_null() {
    shadowOf(captioningManager).setLocale(null);

    assertThat(captioningManager.getLocale()).isNull();
  }

  @Test
  public void setLocale_notifiesObservers() {
    Locale locale = Locale.US;
    captioningManager.addCaptioningChangeListener(captioningChangeListener);

    shadowOf(captioningManager).setLocale(locale);

    verify(captioningChangeListener).onLocaleChanged(locale);
  }
}