summaryrefslogtreecommitdiffstats
path: root/tests/src/com/cyngn/audiofx/util/BaseAudioFxServiceInstrumentationTest.java
blob: e882c9d1c7b0d959d381265eb0f1622d56f21590 (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
package com.cyngn.audiofx.util;

import android.content.Context;
import android.content.Intent;
import android.media.audiofx.AudioEffect;
import android.os.IBinder;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ServiceTestRule;
import android.support.test.runner.AndroidJUnit4;
import com.cyngn.audiofx.service.AudioFxService;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.TimeoutException;

import static org.junit.Assert.*;

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

    private static final int MAX_ITERATION = 100;

    protected AudioFxService.LocalBinder mService;

    @Rule
    public ServiceTestRule mServiceRule = new ServiceTestRule();

    protected void setupService() throws TimeoutException {
        int it = 0;

        /**
         * lol
         * https://code.google.com/p/android/issues/detail?id=180396
         */
        IBinder binder;
        while((binder = mServiceRule.bindService(
                new Intent(InstrumentationRegistry.getTargetContext(),
                        AudioFxService.class))) == null && it < MAX_ITERATION){
            it++;
        }
        mService = (AudioFxService.LocalBinder) binder;
        assertNotNull(mService);
    }

    protected final Context getContext() {
        return InstrumentationRegistry.getContext();
    }

    protected final Context getTargetContext() {
        return InstrumentationRegistry.getTargetContext();
    }

}