summaryrefslogtreecommitdiffstats
path: root/tests/src/com/cyngn/audiofx/service/AudioFxServiceTests.java
blob: 1bf531cc9576658ae207bb8e39d9e740a5ec8799 (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
package com.cyngn.audiofx.service;

import android.content.Intent;
import android.media.audiofx.AudioEffect;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import android.util.Log;
import com.cyngn.audiofx.util.BaseAudioFxServiceInstrumentationTest;
import com.cyngn.audiofx.util.TestMediaPlayer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.cyngn.audiofx.tests.R;

import static org.junit.Assert.*;

/**
 * Created by roman on 3/1/16.
 */
@RunWith(AndroidJUnit4.class)
public class AudioFxServiceTests extends BaseAudioFxServiceInstrumentationTest {

    private static final String TAG = "AudioFxServiceTests";

    private static final int SANE_MAX_LOOP_TIME = 1000 * 20; // 20 seconds !?
    private static final int LOOP_SLEEP_TIME = 25;

    TestMediaPlayer mPlayer;

    @Before
    public void setUp() throws Exception {
        mPlayer = new TestMediaPlayer(getContext(), R.raw.testmp3);
        assertNotNull(mPlayer);
    }

    @After
    public void tearDown() throws Exception {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Test
    public void testServiceCreatesEffectsDirect() throws Exception {
        setupService(); // this might be reused

        Intent intent = new Intent(getTargetContext(), AudioFxService.class);
        intent.setAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
        intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mPlayer.getAudioSessionId());
        intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getTargetContext().getPackageName());

        mServiceRule.startService(intent);
        Thread.sleep(100);
        assertNotNull(mService.getEffect(mPlayer.getAudioSessionId()));
    }

    @Test
    public void testServiceDestroysEffectsDirect() throws Exception {
        testServiceCreatesEffectsDirect();

        Intent intent = new Intent(getTargetContext(), AudioFxService.class);
        intent.setAction(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
        intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mPlayer.getAudioSessionId());
        intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getTargetContext().getPackageName());

        mServiceRule.startService(intent);

        // sleep for 10s to let effects die
        Thread.sleep(10100);
        assertNull(mService.getEffect(mPlayer.getAudioSessionId()));
    }

    @Test
    public void testServiceCreatesEffects() throws Exception {
        setupService();

        openFxSession(true);
        assertNotNull(mService.getEffect(mPlayer.getAudioSessionId()));
    }

    @Test
    @LargeTest
    public void testServiceDestroysEffects() throws Exception {
        testServiceCreatesEffects();

        closeFxSession(true);
        // sleep for 10s to let effects die
        assertNull(mService.getEffect(mPlayer.getAudioSessionId()));
    }

    @Test
    public void testServiceDoesNotDestroyDeferredEffect() throws Exception {
        setupService();
        assertNull(mService.getEffect(mPlayer.getAudioSessionId()));

        openFxSession(false);
        assertNotNull(mService.getEffect(mPlayer.getAudioSessionId()));

        closeFxSession(false);
        // shouldn't go away immediately after we close it
        assertNotNull(mService.getEffect(mPlayer.getAudioSessionId()));

        Thread.sleep(8000);

        // it should *still* be there not destroyed
        assertNotNull(mService.getEffect(mPlayer.getAudioSessionId()));

        // reopen the session
        openFxSession(false);

        // session should still be there
        assertNotNull(mService.getEffect(mPlayer.getAudioSessionId()));

        Thread.sleep(10000);

        // it's been more than 10s (deferred destroy time) and we re-opened it, so it should be
        // alive still
        assertNotNull(mService.getEffect(mPlayer.getAudioSessionId()));
    }

    private void openFxSession(boolean block) throws InterruptedException {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
                intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mPlayer.getAudioSessionId());
                intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getTargetContext().getPackageName());
                getContext().sendBroadcast(intent);
            }
        });
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();

        if (block) {
            int cnt = 0;
            while (mService.getEffect(mPlayer.getAudioSessionId()) == null
                    && (cnt * LOOP_SLEEP_TIME < SANE_MAX_LOOP_TIME)) {
                cnt++;
                Thread.sleep(LOOP_SLEEP_TIME);
            }
            Log.d(TAG, "took " + (cnt * LOOP_SLEEP_TIME) + "ms to open effect");
        } else {
            // TODO have a timeout here for failure based on time limits?
            Thread.sleep(300);
        }
    }

    private void closeFxSession(boolean block) throws InterruptedException {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
                intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mPlayer.getAudioSessionId());
                intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getTargetContext().getPackageName());
                getContext().sendBroadcast(intent);
            }
        });
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();

        if (block) {
            int cnt = 0;
            while (mService.getEffect(mPlayer.getAudioSessionId()) != null
                    && (cnt * LOOP_SLEEP_TIME < SANE_MAX_LOOP_TIME)) {
                cnt++;
                Thread.sleep(LOOP_SLEEP_TIME);
            }
            Log.d(TAG, "took " + (cnt * LOOP_SLEEP_TIME) + "ms to close effect");
        } else {
            // TODO have a timeout here for failure based on time limits?
            Thread.sleep(300);
        }
    }

}