• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4 import static android.os.Build.VERSION_CODES.LOLLIPOP;
5 import static com.google.common.truth.Truth.assertThat;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
8 import static org.robolectric.Shadows.shadowOf;
9 
10 import android.media.AudioManager;
11 import android.media.SoundPool;
12 import androidx.test.core.app.ApplicationProvider;
13 import androidx.test.ext.junit.runners.AndroidJUnit4;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.robolectric.R;
17 import org.robolectric.RuntimeEnvironment;
18 import org.robolectric.annotation.Config;
19 import org.robolectric.shadows.ShadowSoundPool.Playback;
20 
21 @RunWith(AndroidJUnit4.class)
22 public class ShadowSoundPoolTest {
23 
24   @Test
25   @Config(minSdk = LOLLIPOP)
shouldCreateSoundPool_Lollipop()26   public void shouldCreateSoundPool_Lollipop() {
27     SoundPool soundPool = new SoundPool.Builder().build();
28     assertThat(soundPool).isNotNull();
29 
30     SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
31     soundPool.setOnLoadCompleteListener(listener);
32   }
33 
34   @Test
35   @Config(maxSdk = JELLY_BEAN_MR2)
shouldCreateSoundPool_JellyBean()36   public void shouldCreateSoundPool_JellyBean() {
37     SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
38     assertThat(soundPool).isNotNull();
39   }
40 
41   @Test
playedSoundsFromResourcesAreRecorded()42   public void playedSoundsFromResourcesAreRecorded() {
43     SoundPool soundPool = createSoundPool();
44 
45     int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
46     soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1);
47 
48     assertThat(shadowOf(soundPool).wasResourcePlayed(R.raw.sound)).isTrue();
49   }
50 
51   @Test
playedSoundsFromResourcesAreCollected()52   public void playedSoundsFromResourcesAreCollected() {
53     SoundPool soundPool = createSoundPool();
54 
55     int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
56     soundPool.play(soundId, 1.0f, 0f, 0, 0, 0.5f);
57     soundPool.play(soundId, 0f, 1.0f, 1, 0, 2.0f);
58 
59     assertThat(shadowOf(soundPool).getResourcePlaybacks(R.raw.sound))
60         .containsExactly(
61             new Playback(soundId, 1.0f, 0f, 0, 0, 0.5f),
62             new Playback(soundId, 0f, 1.0f, 1, 0, 2.0f))
63         .inOrder();
64   }
65 
66   @Test
playedSoundsFromPathAreRecorded()67   public void playedSoundsFromPathAreRecorded() {
68     SoundPool soundPool = createSoundPool();
69 
70     int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
71     soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1);
72 
73     assertThat(shadowOf(soundPool).wasPathPlayed("/mnt/sdcard/sound.wav")).isTrue();
74   }
75 
76   @Test
playedSoundsFromPathAreCollected()77   public void playedSoundsFromPathAreCollected() {
78     SoundPool soundPool = createSoundPool();
79 
80     int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
81     soundPool.play(soundId, 0f, 1.0f, 1, 0, 2.0f);
82     soundPool.play(soundId, 1.0f, 0f, 0, 0, 0.5f);
83 
84     assertThat(shadowOf(soundPool).getPathPlaybacks("/mnt/sdcard/sound.wav"))
85         .containsExactly(
86             new Playback(soundId, 0f, 1.0f, 1, 0, 2.0f),
87             new Playback(soundId, 1.0f, 0f, 0, 0, 0.5f))
88         .inOrder();
89   }
90 
91   @Test
notifyPathLoaded_notifiesListener()92   public void notifyPathLoaded_notifiesListener() {
93     SoundPool soundPool = createSoundPool();
94     SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
95     soundPool.setOnLoadCompleteListener(listener);
96 
97     int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
98     shadowOf(soundPool).notifyPathLoaded("/mnt/sdcard/sound.wav", true);
99 
100     verify(listener).onLoadComplete(soundPool, soundId, 0);
101   }
102 
103   @Test
notifyResourceLoaded_notifiesListener()104   public void notifyResourceLoaded_notifiesListener() {
105     SoundPool soundPool = createSoundPool();
106     SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
107     soundPool.setOnLoadCompleteListener(listener);
108 
109     int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
110     shadowOf(soundPool).notifyResourceLoaded(R.raw.sound, true);
111 
112     verify(listener).onLoadComplete(soundPool, soundId, 0);
113   }
114 
115   @Test
notifyPathLoaded_notifiesFailure()116   public void notifyPathLoaded_notifiesFailure() {
117     SoundPool soundPool = createSoundPool();
118     SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
119     soundPool.setOnLoadCompleteListener(listener);
120 
121     int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
122     shadowOf(soundPool).notifyPathLoaded("/mnt/sdcard/sound.wav", false);
123 
124     verify(listener).onLoadComplete(soundPool, soundId, 1);
125   }
126 
127   @Test
notifyResourceLoaded_doNotFailWithoutListener()128   public void notifyResourceLoaded_doNotFailWithoutListener() {
129     SoundPool soundPool = createSoundPool();
130 
131     soundPool.load("/mnt/sdcard/sound.wav", 1);
132     shadowOf(soundPool).notifyPathLoaded("/mnt/sdcard/sound.wav", false);
133   }
134 
135   @Test(expected = IllegalArgumentException.class)
notifyPathLoaded_failIfLoadWasntCalled()136   public void notifyPathLoaded_failIfLoadWasntCalled() {
137     SoundPool soundPool = createSoundPool();
138 
139     shadowOf(soundPool).notifyPathLoaded("no.mp3", true);
140   }
141 
142   @Test(expected = IllegalArgumentException.class)
notifyResourceLoaded_failIfLoadWasntCalled()143   public void notifyResourceLoaded_failIfLoadWasntCalled() {
144     SoundPool soundPool = createSoundPool();
145 
146     shadowOf(soundPool).notifyResourceLoaded(123, true);
147   }
148 
149   @Test
playedSoundsAreCleared()150   public void playedSoundsAreCleared() {
151     SoundPool soundPool = createSoundPool();
152 
153     int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
154     soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1);
155 
156     assertThat(shadowOf(soundPool).wasResourcePlayed(R.raw.sound)).isTrue();
157     shadowOf(soundPool).clearPlayed();
158     assertThat(shadowOf(soundPool).wasResourcePlayed(R.raw.sound)).isFalse();
159   }
160 
createSoundPool()161   private SoundPool createSoundPool() {
162     return RuntimeEnvironment.getApiLevel() >= LOLLIPOP
163         ? new SoundPool.Builder().build()
164         : new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
165   }
166 }
167