我正在尝试测试创建的实例化一个新SimpleExoPlayer对象的类,这是该类:

public class PlayerFactory {
    public static SimpleExoPlayer getPlayerInstance(Context context, Uri fileUri, String encryptionProtocol, byte[] secretKey, byte[] iv)
            throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
        //Controls track changes
        TrackSelector trackSelector = new DefaultTrackSelector();

        //Controls buffering
        LoadControl loadControl = new DefaultLoadControl();

        //Extracts data from data source
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

        //Factory of EncryptedFileDataSource
        DataSource.Factory encryptedFileDataSourceFactory = new EncryptedFileDataSourceFactory(encryptionProtocol, secretKey, iv, null);

        MediaSource mediaSource = new ExtractorMediaSource(fileUri, encryptedFileDataSourceFactory, extractorsFactory, null, null);

        //Creates a SimpleExoPlayer instance
        SimpleExoPlayer simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);

        //Attache media source to player
        simpleExoPlayer.prepare(mediaSource);

        return simpleExoPlayer;
    }
}


然后在PlayerTest.java类中,我尝试测试从PlayerFactory.getPlayerInstance()调用得到的对象是否等于SimpleExoPlayer类对象。

这是测试:

@RunWith(AndroidJUnit4.class)
public class PlayerTest {
    //Encryption algorithm
    static final String ENC_ALG = "AES";
    //Encryption block mode
    static final String ENC_PROTOCOL = ENC_ALG + "/CTR/NoPadding";
    //File to be played
    static final String ENC_FILE = "song_encrypted_ctr.mp3";
    //Secrect key used in encryption
    public static final String mKey = "0123456789012345";
    //Initialization vector used in encryption
    public static final String mIV = "abcdefrtyqowueyr";

    @Test
    public void testCorrectIqraalyPlayerObject() throws Throwable {
        Context context = InstrumentationRegistry.getContext();
        SimpleExoPlayer simpleExoPlayer = PlayerFactory.getPlayerInstance(context, Uri.parse("testuri"), ENC_PROTOCOL, mKey.getBytes(), mIV.getBytes());
        Assert.assertEquals(SimpleExoPlayer.class.getName(), simpleExoPlayer.getClass().getName());
    }


但是我总是得到一个RuntimeException


  java.lang.RuntimeException:无法在该线程内部创建处理程序
  尚未调用Looper.prepare()


在这条线上:

SimpleExoPlayer simpleExoPlayer = PlayerFactory.getPlayerInstance(context, Uri.parse("testuri"), ENC_PROTOCOL, mKey.getBytes(), mIV.getBytes());


我注意到在ExoPlayerFactory.newSimpleInstance()中的PlayerFactory方法内部,有一个新的Handler创建并传递,我不知道它的用途。如果有人知道为什么会这样,我将不胜感激。谢谢。



更新:
我尝试使用@UiThreadTest而不是@Test,但出现此错误:


  java.lang.Exception:没有可运行的方法




更新2:
我尝试同时使用@UiThreadTest@Test


  给定调用方包com.example.rafael.exoplayerpoc.test不是
  在进程Record中运行{c32179c
  10198:com.example.rafael.exoplayerpoc / u0a58}

最佳答案

使用UiThreadTestRule在UI线程上运行测试。更多详细信息here

07-24 09:48
查看更多