How to mock user interaction (“fromUser=true”) with the SeekBar in a Robotium test?

谁都会走 提交于 2019-12-06 06:16:01

You cannot achieve that with method setProgressBar, because it uses setProgress from ProgressBar.setProgress so there is fromUser set to false by default.

  1. solution is to use click on screen - but you actually don't know exactly in which point (% of progress) you hit, if you hit on progress bar at all.

  2. solution is to use reflection - use setProgress method with extra parameter (fromUser), so it will use the protected method. I can help in implementation of that in case you have problems with it.

  3. solution is to ask robotium team to implement method from 2nd point.

Solution (Reflections)

SeekBar is an indirect subclass of the ProgressBar that has a public setProgress(int) that has only one line that calls package method setProgress(int, boolean) and passes fromUser == false to it. It is possible to use Reflections and to call setProgress(int, boolean) directly and pass desired fromUser parameter:

    private void setSeekBarProgress(int newProgress, boolean fromUser) {

        Method privateSetProgressMethod = null;

        try {
            privateSetProgressMethod = ProgressBar.class.getDeclaredMethod("setProgress", Integer.TYPE, Boolean.TYPE);
            privateSetProgressMethod.setAccessible(true);
            privateSetProgressMethod.invoke(mSeekBar, newProgress, fromUser);
        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
            fail("Error while invoking private method.");
        }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!