In my test case I have to record for 1 hour, in robotium solo.sleep(600000) had done my work, but In espresso I am confused with IdlingResource concept. I have to start recording and wait for some time(depending on the type of test) 15mins, 60mins etc.
Equivalent code in robotium
solo.clickOnView(solo.getView("start_record"));
solo.sleep(duration * 60 * 1000);
solo.clickOnView(solo.getView("stop_record"));
I tried to use it like this in espresso
@RunWith(AndroidJUnit4.class)
@SmallTest
public class AaEspressoTest {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.absd.rec.RecorderActivity";
private static Class<?> launcherActivityClass;
private Solo solo;
private static CoreRecordingTest skyroTestRunner;
private static Class<? extends Activity> activityClass;
static {
try {
activityClass = (Class<? extends Activity>) Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Rule
public final ActivityTestRule<?> activityRule
= new ActivityTestRule<>(activityClass);
private IntentServiceIdlingResource idlingResource;
@Before
public void registerIntentServiceIdlingResource() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
idlingResource = new IntentServiceIdlingResource(instrumentation.getTargetContext());
Espresso.registerIdlingResources(idlingResource);
}
@After
public void unregisterIntentServiceIdlingResource() {
Espresso.unregisterIdlingResources(idlingResource);
}
@Test
public void testHello() throws Exception {
onView(withId(AaEspressoTest.getId("recorderpage_record"))).perform(click());
registerIntentServiceIdlingResource();
onView(withId(AaEspressoTest.getId("recorderpage_stop"))).perform(click());
}
}
Idling resource
public class IntentServiceIdlingResource implements IdlingResource {
private final Context context;
private ResourceCallback resourceCallback;
public static boolean status = false;
public IntentServiceIdlingResource(Context context) {
this.context = context;
}
@Override
public String getName() {
return IntentServiceIdlingResource.class.getName();
}
@Override
public boolean isIdleNow() {
return getTimer();
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
private static boolean getTimer() {
new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// Do Nothing
status = false;
}
@Override
public void onFinish() {
status = true;
}
};
return status;
}
}
Exception:
android.support.test.espresso.IdlingResourceTimeoutException: Wait for [com.adbs.recorder.IntentServiceIdlingResource] to become idle timed out
I test on my project, It works. Just setup before register idling resources. please check:
https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IdlingResourceSample and
https://developer.android.com/reference/android/support/test/espresso/IdlingPolicies
You need an
IdlingResource
with anisIdleNow()
that returnstrue
only if the specific amount of time has passed. To achieve that, save the start time and compare it with current time:Create and register this idling resource in your test:
You need the
setMasterPolicyTimeout
andsetIdlingResourceTimeout
calls to make sure Espresso does not terminate the test due to time out.Full example: https://github.com/chiuki/espresso-samples/tree/master/idling-resource-elapsed-time
The default timeout that Espresso will wait for all registered resources to become idle is one minute.
You can change this using the
IdlingPolicies
class to set an explicit timeout: