title: Wait for callback without blocking main thread
body: <p>I am stuck and will be glad to get any help!</p>
  
  <p>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:</p>
  
  <pre><code>@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 &lt;= MAX_WAIT_TIME)
  
              /* working with activity here */
          }
      });
      InstrumentationRegistry.getInstrumentation().waitForIdleSync();
  }
  
  @Override
  public void onTestResult(boolean passed) {
      // assertTrue(passed);
      Assert.fail();
  }
  </code></pre>
  
  <p>I expect this test to fail but in fact <code>onTestResult</code> is called after <code>testSimpleSetup</code> finishes and Assert has no influence on the result of the test.</p>
  
  <p>Thanks in advance.</p>