java - Android Junit 测试卡在 "Launching: Creating source locator..."

标签 java android junit android-testing motionevent

编辑
经过一番调试,我发现整个测试没有运行。但奇怪的是,它运行了几次,当我再次尝试时,它无法运行。在进度选项卡下,始终卡在“正在启动:正在创建源定位器...”,并且测试未执行。

我尝试重新启动 Eclipse,但无法让它工作。

我是 Android 测试的新手,我想生成触摸事件来测试 Android 应用程序中视频的暂停和播放。我使用Android测试框架选择视频并播放它,视频开始正​​常运行,但是触摸事件没有执行,测试也没有结束。我尝试调试,但测试运行时从未调用 touchlistener。我不确定为什么代码执行没有到达 MotionEvents 部分。非常感谢您的帮助!

public class VideoPlaybackTest extends ActivityInstrumentationTestCase2<MainActivity> {

private Activity activity;
private Fragment localvideosfragment;
private VideoPlayer videoplayer;
private ZoomState zoomstate;
private RenderView renderView;

public VideoPlaybackTest() {
    super(MainActivity.class);
}

@Before
public void setUp() throws Exception {
    super.setUp();

    setActivityInitialTouchMode(true);

    activity = getActivity();
    FragmentManager fragmentmanager = activity.getFragmentManager();
    localvideosfragment = fragmentmanager.findFragmentById(R.id.frame_container);
}

@Test
public void testVideoSelection() {

    activity.runOnUiThread(new Runnable() {
        public void run() {
            assertNotNull(localvideosfragment);
            View rootView = (View) localvideosfragment.getView();
            ListView videolist = (ListView) rootView.findViewById(R.id.list);
            assertNotNull(videolist);
            // Select video to play 
            videolist.performItemClick(videolist.getChildAt(0), 0, videolist.getChildAt(0).getId());
        }
    });

    // Wait for VideoPlayer Activity to be started
    ActivityMonitor monitor = getInstrumentation().addMonitor(VideoPlayer.class.getName(), null, false);
    Instrumentation instrumentation = getInstrumentation();
    instrumentation.waitForIdleSync();

    // get the started video player Activity
    videoplayer = (VideoPlayer) instrumentation.waitForMonitor(monitor);
    assertNotNull(videoplayer);

    // Get zoomstate and assert that video is playing 
    renderView = videoplayer.getRenderView();
    assertNotNull(renderView);
    zoomstate = renderView.getZoomState();
    assertNotNull(zoomstate);
    ROIState roistate = renderView.getROIState();
    Boolean isPlaying = roistate.isPlaying;
    assertTrue(zoomstate.isPlaying());
    assertTrue(isPlaying);

    // Obtain MotionEvent object
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    float x = 0.0f;
    float y = 0.0f;
    int metaState = 0;
    MotionEvent motionEventup = MotionEvent.obtain(
            downTime, 
            eventTime, 
            MotionEvent.ACTION_UP, 
            x, 
            y, 
            metaState
            );
    MotionEvent motionEventdown = MotionEvent.obtain(
            downTime, 
            eventTime, 
            MotionEvent.ACTION_DOWN, 
            x, 
            y, 
            metaState
            );

    renderView.dispatchTouchEvent(motionEventdown);
    renderView.dispatchTouchEvent(motionEventup);
    roistate = renderView.getROIState();
    isPlaying = roistate.isPlaying;
    zoomstate = renderView.getZoomState();
    Boolean state = zoomstate.isPlaying();
    assertFalse(state);
    assertFalse(isPlaying);
}

}

最佳答案

好吧,我找到了让它发挥作用的方法。我已从设备中卸载现有应用程序,然后重新开始。此后测试运行良好。

关于java - Android Junit 测试卡在 "Launching: Creating source locator...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27533692/

相关文章:

java - 任务 :app:compileDebugJavaWithJavac in Android Studio 执行失败

java - 将 Baritone API 实现到 Minecraft Forge mod 中

android - 显示进度条直到从服务器下载图像

java - 如何在JUnit(Play Framework)中测试删除?

java - 从作为参数传递的数组创建测试用例名称(使用 JUnitParams)

java - 如何使用 doCatch 在 Apache Camel 中出错后移动文件

java - 使用 org.springframework.data.jpa.repository.Query 使用列表参数有条件删除

android - Recycler View 仅显示 android 中共享首选项中的一项

android - 有没有办法设置 PreferenceFragmentCompat 的样式

java - 混淆 JUnit 方法的执行顺序