I am stuck and will be glad to get any help!
I am writing tests for an android library. The task is to make some actions in an activity and check that library responds correctly. My problem is that my test finishes just after all actions in activity are done but I get the test result via a callback (and I receive this callback only when the test is over). So, I want to somehow tell testing framework that the test is not over until the callback is received (or until time runs out). Here is what I have now:
@Test
public void testSimpleSetup() {
/* ... */
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
testManager.startTest(MAX_WAIT_TIME); // this object calls onTestResult(boolean) after time t (t <= MAX_WAIT_TIME)
/* working with activity here */
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
}
@Override
public void onTestResult(boolean passed) {
// assertTrue(passed);
Assert.fail();
}
I expect this test to fail but in fact onTestResult
is called after testSimpleSetup
finishes and Assert has no influence on the result of the test.
Thanks in advance.
Thanks to @Gordak. His answer has almost worked. But, unfortunately, it blocks the main thread so the test never ends. I modified it a little bit so now it works.
@Before
public void setUp() throws Exception {
activity = testRule.getActivity();
latch = new CountDownLatch(1);
}
@Test
public void testSimpleSetup() {
/* ... */
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
testManager.startTest(MAX_WAIT_TIME); // this object calls onTestResult(boolean) after time t (t <= MAX_WAIT_TIME)
/* working with activity here */
}
});
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
latch.await(); // here we block test thread and not UI-thread
// presumably, you will want to set the timeout here
}
@Override
public void onTestResult(boolean passed) {
// assertTrue(passed);
Assert.fail();
latch.countDown();
}