summaryrefslogtreecommitdiffstats
path: root/tests/src/com/cyngn/audiofx/receiver/ServiceDispatcherTests.java
blob: 64d583d14822558eab792b2a5cbb1ada5e7e1e09 (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
package com.cyngn.audiofx.receiver;

import android.app.ActivityManager;
import android.content.Intent;
import android.media.AudioManager;
import android.media.audiofx.AudioEffect;
import android.test.InstrumentationTestCase;
import android.test.UiThreadTest;

import com.cyngn.audiofx.service.AudioFxService;
import com.cyngn.audiofx.tests.TestMediaPlayer;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * Created by roman on 3/4/16.
 */
public class ServiceDispatcherTests extends InstrumentationTestCase {

    private ServiceDispatcher mServiceReceiver;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mServiceReceiver = new ServiceDispatcher();
    }

    @Override
    protected void tearDown() throws Exception {
        getInstrumentation().getTargetContext().stopService(
                new Intent(getInstrumentation().getTargetContext(), AudioFxService.class));
        mServiceReceiver = null;

        Thread.sleep(100);
        assertFalse(isAudioFxServiceRunning());
        super.tearDown();
    }


    public void testOpenSessionStartsService() throws Throwable {
        assertFalse(isAudioFxServiceRunning());
        TestMediaPlayer mediaPlayer = new TestMediaPlayer(getInstrumentation().getContext());

        Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
        intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mediaPlayer.getSessionId());
        intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getInstrumentation().getContext().getPackageName());

        mServiceReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
        Thread.sleep(100);
        assertTrue(isAudioFxServiceRunning());

        mediaPlayer.release();
    }

    public void testCloseSessionStartsService() throws Exception {
        assertFalse(isAudioFxServiceRunning());
        TestMediaPlayer mediaPlayer = new TestMediaPlayer(getInstrumentation().getContext());

        Intent intent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
        intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mediaPlayer.getSessionId());
        intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getInstrumentation().getContext().getPackageName());

        mServiceReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
        Thread.sleep(100);
        assertTrue(isAudioFxServiceRunning());

        mediaPlayer.release();
    }

    public void testAudioBecomingNoisyStartsService() throws InterruptedException {
        assertFalse(isAudioFxServiceRunning());

        Intent intent = new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
        mServiceReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
        Thread.sleep(100);
        assertTrue(isAudioFxServiceRunning());
    }

    private boolean isAudioFxServiceRunning() {
        ActivityManager manager = (ActivityManager) getInstrumentation().getContext().getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (AudioFxService.class.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}