runOnUiThread() does not seem to work when being executed within a thread. Anybody knows of a workaround?
Note: I filed a ticket here - http://ift.tt/25x4uPk
import android.app.Activity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class RunOnUiThreadTest {
/**
* Works
* @throws Exception
*/
@Test
public void inside_the_main_thread() throws Exception {
final Activity activity = Robolectric.setupActivity(Activity.class);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean didRun = new AtomicBoolean(false);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
didRun.set(true);
latch.countDown();
}
});
latch.await(20, TimeUnit.SECONDS);
assertTrue(didRun.get());
}
/**
* Fails
* @throws Exception
*/
@Test
public void inside_a_new_thread() throws Exception {
final Activity activity = Robolectric.setupActivity(Activity.class);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean didRun = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
didRun.set(true);
latch.countDown();
}
});
}
});
thread.start();
latch.await(20, TimeUnit.SECONDS);
assertTrue(didRun.get());
}
}
Aucun commentaire:
Enregistrer un commentaire