1 package org.robolectric.shadows;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.mockito.Mockito.verify;
5 import static org.mockito.Mockito.verifyZeroInteractions;
6 import static org.robolectric.Shadows.shadowOf;
7 
8 import android.content.Context;
9 import android.view.accessibility.CaptioningManager;
10 import android.view.accessibility.CaptioningManager.CaptioningChangeListener;
11 import androidx.test.core.app.ApplicationProvider;
12 import androidx.test.ext.junit.runners.AndroidJUnit4;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations;
18 import org.robolectric.annotation.Config;
19 
20 /** Tests for the ShadowCaptioningManager. */
21 @RunWith(AndroidJUnit4.class)
22 @Config(minSdk = 19)
23 public final class ShadowCaptioningManagerTest {
24 
25   @Mock private CaptioningChangeListener captioningChangeListener;
26 
27   private CaptioningManager captioningManager;
28 
29   @Before
setUp()30   public void setUp() {
31     MockitoAnnotations.initMocks(this);
32     captioningManager =
33         (CaptioningManager)
34             ApplicationProvider.getApplicationContext()
35                 .getSystemService(Context.CAPTIONING_SERVICE);
36   }
37 
38   @Test
setEnabled_true()39   public void setEnabled_true() {
40     assertThat(captioningManager.isEnabled()).isFalse();
41 
42     shadowOf(captioningManager).setEnabled(true);
43 
44     assertThat(captioningManager.isEnabled()).isTrue();
45   }
46 
47   @Test
setEnabled_false()48   public void setEnabled_false() {
49     shadowOf(captioningManager).setEnabled(false);
50 
51     assertThat(captioningManager.isEnabled()).isFalse();
52   }
53 
54   @Test
setFontScale_changesValueOfGetFontScale()55   public void setFontScale_changesValueOfGetFontScale() {
56     float fontScale = 1.5f;
57     shadowOf(captioningManager).setFontScale(fontScale);
58 
59     assertThat(captioningManager.getFontScale()).isWithin(0.001f).of(fontScale);
60   }
61 
62   @Test
setFontScale_notifiesObservers()63   public void setFontScale_notifiesObservers() {
64     float fontScale = 1.5f;
65     captioningManager.addCaptioningChangeListener(captioningChangeListener);
66 
67     shadowOf(captioningManager).setFontScale(fontScale);
68 
69     verify(captioningChangeListener).onFontScaleChanged(fontScale);
70   }
71 
72   @Test
addCaptioningChangeListener_doesNotRegisterSameListenerTwice()73   public void addCaptioningChangeListener_doesNotRegisterSameListenerTwice() {
74     float fontScale = 1.5f;
75     captioningManager.addCaptioningChangeListener(captioningChangeListener);
76 
77     captioningManager.addCaptioningChangeListener(captioningChangeListener);
78 
79     shadowOf(captioningManager).setFontScale(fontScale);
80     verify(captioningChangeListener).onFontScaleChanged(fontScale);
81   }
82 
83   @Test
removeCaptioningChangeListener_unregistersFontScaleListener()84   public void removeCaptioningChangeListener_unregistersFontScaleListener() {
85     captioningManager.addCaptioningChangeListener(captioningChangeListener);
86 
87     captioningManager.removeCaptioningChangeListener(captioningChangeListener);
88 
89     shadowOf(captioningManager).setFontScale(1.5f);
90     verifyZeroInteractions(captioningChangeListener);
91   }
92 }
93