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

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.robolectric.Shadows.shadowOf;

import android.media.AudioManager;
import android.media.SoundPool;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.R;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowSoundPool.Playback;

@RunWith(AndroidJUnit4.class)
public class ShadowSoundPoolTest {

  @Test
  @Config(minSdk = LOLLIPOP)
  public void shouldCreateSoundPool_Lollipop() {
    SoundPool soundPool = new SoundPool.Builder().build();
    assertThat(soundPool).isNotNull();

    SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
    soundPool.setOnLoadCompleteListener(listener);
  }

  @Test
  public void playedSoundsFromResourcesAreRecorded() {
    SoundPool soundPool = createSoundPool();

    int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
    soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1);

    assertThat(shadowOf(soundPool).wasResourcePlayed(R.raw.sound)).isTrue();
  }

  @Test
  public void playedSoundsFromResourcesAreCollected() {
    SoundPool soundPool = createSoundPool();

    int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
    soundPool.play(soundId, 1.0f, 0f, 0, 0, 0.5f);
    soundPool.play(soundId, 0f, 1.0f, 1, 0, 2.0f);

    assertThat(shadowOf(soundPool).getResourcePlaybacks(R.raw.sound))
        .containsExactly(
            new Playback(soundId, 1.0f, 0f, 0, 0, 0.5f),
            new Playback(soundId, 0f, 1.0f, 1, 0, 2.0f))
        .inOrder();
  }

  @Test
  public void playedSoundsFromPathAreRecorded() {
    SoundPool soundPool = createSoundPool();

    int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
    soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1);

    assertThat(shadowOf(soundPool).wasPathPlayed("/mnt/sdcard/sound.wav")).isTrue();
  }

  @Test
  public void playedSoundsFromPathAreCollected() {
    SoundPool soundPool = createSoundPool();

    int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
    soundPool.play(soundId, 0f, 1.0f, 1, 0, 2.0f);
    soundPool.play(soundId, 1.0f, 0f, 0, 0, 0.5f);

    assertThat(shadowOf(soundPool).getPathPlaybacks("/mnt/sdcard/sound.wav"))
        .containsExactly(
            new Playback(soundId, 0f, 1.0f, 1, 0, 2.0f),
            new Playback(soundId, 1.0f, 0f, 0, 0, 0.5f))
        .inOrder();
  }

  @Test
  public void notifyPathLoaded_notifiesListener() {
    SoundPool soundPool = createSoundPool();
    SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
    soundPool.setOnLoadCompleteListener(listener);

    int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
    shadowOf(soundPool).notifyPathLoaded("/mnt/sdcard/sound.wav", true);

    verify(listener).onLoadComplete(soundPool, soundId, 0);
  }

  @Test
  public void notifyResourceLoaded_notifiesListener() {
    SoundPool soundPool = createSoundPool();
    SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
    soundPool.setOnLoadCompleteListener(listener);

    int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
    shadowOf(soundPool).notifyResourceLoaded(R.raw.sound, true);

    verify(listener).onLoadComplete(soundPool, soundId, 0);
  }

  @Test
  public void notifyPathLoaded_notifiesFailure() {
    SoundPool soundPool = createSoundPool();
    SoundPool.OnLoadCompleteListener listener = mock(SoundPool.OnLoadCompleteListener.class);
    soundPool.setOnLoadCompleteListener(listener);

    int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);
    shadowOf(soundPool).notifyPathLoaded("/mnt/sdcard/sound.wav", false);

    verify(listener).onLoadComplete(soundPool, soundId, 1);
  }

  @Test
  public void notifyResourceLoaded_doNotFailWithoutListener() {
    SoundPool soundPool = createSoundPool();

    soundPool.load("/mnt/sdcard/sound.wav", 1);
    shadowOf(soundPool).notifyPathLoaded("/mnt/sdcard/sound.wav", false);
  }

  @Test(expected = IllegalArgumentException.class)
  public void notifyPathLoaded_failIfLoadWasntCalled() {
    SoundPool soundPool = createSoundPool();

    shadowOf(soundPool).notifyPathLoaded("no.mp3", true);
  }

  @Test(expected = IllegalArgumentException.class)
  public void notifyResourceLoaded_failIfLoadWasntCalled() {
    SoundPool soundPool = createSoundPool();

    shadowOf(soundPool).notifyResourceLoaded(123, true);
  }

  @Test
  public void playedSoundsAreCleared() {
    SoundPool soundPool = createSoundPool();

    int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);
    soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1);

    assertThat(shadowOf(soundPool).wasResourcePlayed(R.raw.sound)).isTrue();
    shadowOf(soundPool).clearPlayed();
    assertThat(shadowOf(soundPool).wasResourcePlayed(R.raw.sound)).isFalse();
  }

  @Test
  public void loadSoundWithResId_positiveId () {
    SoundPool soundPool = createSoundPool();

    int soundId = soundPool.load(ApplicationProvider.getApplicationContext(), R.raw.sound, 1);

    assertThat(soundId).isGreaterThan(0);
  }

  @Test
  public void loadSoundWithPath_positiveId () {
    SoundPool soundPool = createSoundPool();

    int soundId = soundPool.load("/mnt/sdcard/sound.wav", 1);

    assertThat(soundId).isGreaterThan(0);
  }

  private SoundPool createSoundPool() {
    return RuntimeEnvironment.getApiLevel() >= LOLLIPOP
        ? new SoundPool.Builder().build()
        : new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
  }
}