6

waitForCondition() on the Solo class in Robotium uses a Sleeper object to sleep a thread in between checking for a condition. The Sleeper class has a PAUSE defined as 500 milliseconds. I want to lower that, ideally without downloading the Robotium source code, changing it, and recompiling Robotium.

I tried extending the Solo class and constructing my own Waiter class that would use a custom Sleeper object with lower sleep intervals, but Waiter has package-level access so this route is not available.

The final keyword aside, 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.

Does anyone have any solutions? Thanks!

Update: @vRallev's answer below gets the job done with reflection. I made a pull request that was merged into Robotium today. In the next release, you'll be able to configure sleep times with the Config class.

Mark
  • 7,306
  • 8
  • 45
  • 87
  • if you only want to overwrite the sleep time of "waitForCondition" instead of all wait functoin, why don't you just create your own wait function? – Kit Fung Mar 11 '16 at 06:45
  • Because then I'd effectively be doing what I said I didn't want to do: taking the Robotium source code, changing it, and recompiling. – Mark Mar 11 '16 at 08:04
  • I mean you can create a new function in a new custom class instead of editing the source code. The logic behind "waitForCondition" is pretty clear and easy to implement. It will not require you to recompile it. – Kit Fung Mar 11 '16 at 11:23

1 Answers1

5

Even if the Waiter or Sleeper class were public, you couldn't change the values. The reason is that the waiter field in the Solo class is final and the constructor where the value is assigned is private.

The only way to hack this is with reflection. I tried the solution below and it works. Notice the package of both 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();
  }
}

And

package com.robotium.solo;

public class SleeperHack extends Sleeper {

  @Override
  public void sleep() {
    sleep(50);
  }
}
vRallev
  • 4,982
  • 3
  • 31
  • 34