Robotium - 在 Sleeper 中自定义暂停持续时间 class

Robotium - customize PAUSE duration in Sleeper class

Robotium 中 Solo class 上的 waitForCondition() 使用 Sleeper 对象在检查条件之间休眠线程。 Sleeper class 的 PAUSE 定义为 500 毫秒。我想降低它,最好不要下载 Robotium 源代码、更改它并重新编译 Robotium。

我尝试扩展 Solo class 并构建我自己的 Waiter class,它将使用具有较低睡眠间隔的自定义 Sleeper 对象,但 Waiter 具有包级访问权限,因此此路由不可用.

最后一个关键字放在一边,this commit message seems to indicate that custom configurations should be (or are coming) but I don't see any way to customize those constants in the Solo.Config class

有人有解决办法吗?谢谢!

更新: @vRallev 下面的回答通过反思完成了工作。我做了一个 pull request 今天被合并到 Robotium 中。在下一个版本中,您将能够使用 Config class.

配置睡眠时间

即使 WaiterSleeper class 是 public,您也无法更改这些值。原因是Soloclass中的waiter字段是final的,赋值的构造函数是private的。

解决这个问题的唯一方法是反思。我尝试了下面的解决方案并且它有效。注意两个 classes!

的包
package com.robotium.solo;

import java.lang.reflect.Field;

public class SoloHack {

  private final Solo mSolo;

  public SoloHack(Solo solo) {
    mSolo = solo;
  }

  public void hack() throws NoSuchFieldException, IllegalAccessException {
    Field field = mSolo.waiter.getClass().getDeclaredField("sleeper");
    field.setAccessible(true);

    // Object value = field.get(mSolo.waiter);
    // Class<?> aClass = value.getClass();

    field.set(mSolo.waiter, new SleeperHack());

    // Object newValue = field.get(mSolo.waiter);
    // Class<?> newClass = newValue.getClass();
  }
}

package com.robotium.solo;

public class SleeperHack extends Sleeper {

  @Override
  public void sleep() {
    sleep(50);
  }
}