Search in sources :

Example 26 with HandlerThread

use of android.os.HandlerThread in project robolectric by robolectric.

the class ShadowLooperTest method differentThreads_getDifferentLoopers.

@Test
public void differentThreads_getDifferentLoopers() {
    HandlerThread ht = getHandlerThread();
    assertThat(ht.getLooper()).isNotSameAs(Looper.getMainLooper());
}
Also used : HandlerThread(android.os.HandlerThread) Test(org.junit.Test)

Example 27 with HandlerThread

use of android.os.HandlerThread in project realm-java by realm.

the class RealmTests method closeRealmInChangeListener.

// Tests if close can be called from Realm change listener when there is no other listeners.
@Test
public void closeRealmInChangeListener() {
    realm.close();
    final CountDownLatch signalTestFinished = new CountDownLatch(1);
    HandlerThread handlerThread = new HandlerThread("background");
    handlerThread.start();
    final Handler handler = new Handler(handlerThread.getLooper());
    handler.post(new Runnable() {

        @Override
        public void run() {
            final Realm realm = Realm.getInstance(realmConfig);
            final RealmChangeListener<Realm> listener = new RealmChangeListener<Realm>() {

                @Override
                public void onChange(Realm object) {
                    if (realm.where(AllTypes.class).count() == 1) {
                        realm.removeChangeListener(this);
                        realm.close();
                        signalTestFinished.countDown();
                    }
                }
            };
            realm.addChangeListener(listener);
            realm.executeTransactionAsync(new Realm.Transaction() {

                @Override
                public void execute(Realm realm) {
                    realm.createObject(AllTypes.class);
                }
            });
        }
    });
    TestHelper.awaitOrFail(signalTestFinished);
}
Also used : HandlerThread(android.os.HandlerThread) Handler(android.os.Handler) AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) SharedRealm(io.realm.internal.SharedRealm) Test(org.junit.Test)

Example 28 with HandlerThread

use of android.os.HandlerThread in project robolectric by robolectric.

the class ShadowHandlerThreadTest method shouldReturnNullIfThreadHasNotBeenStarted.

@Test
public void shouldReturnNullIfThreadHasNotBeenStarted() throws Exception {
    handlerThread = new HandlerThread("test");
    assertNull(handlerThread.getLooper());
}
Also used : HandlerThread(android.os.HandlerThread) Test(org.junit.Test)

Example 29 with HandlerThread

use of android.os.HandlerThread in project robolectric by robolectric.

the class ShadowHandlerThreadTest method shouldQuitLooperAndThread.

@Test
public void shouldQuitLooperAndThread() throws Exception {
    handlerThread = new HandlerThread("test");
    Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
    handlerThread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
    handlerThread.start();
    assertTrue(handlerThread.isAlive());
    assertTrue(handlerThread.quit());
    handlerThread.join();
    assertFalse(handlerThread.isAlive());
    handlerThread = null;
}
Also used : HandlerThread(android.os.HandlerThread) Test(org.junit.Test)

Example 30 with HandlerThread

use of android.os.HandlerThread in project robolectric by robolectric.

the class ShadowHandlerThreadTest method shouldCallOnLooperPrepared.

@Test
public void shouldCallOnLooperPrepared() throws Exception {
    final Boolean[] wasCalled = new Boolean[] { false };
    final CountDownLatch latch = new CountDownLatch(1);
    handlerThread = new HandlerThread("test") {

        @Override
        protected void onLooperPrepared() {
            wasCalled[0] = true;
            latch.countDown();
        }
    };
    handlerThread.start();
    try {
        assertNotNull(handlerThread.getLooper());
        latch.await(1, TimeUnit.SECONDS);
        assertTrue(wasCalled[0]);
    } finally {
        handlerThread.quit();
    }
}
Also used : HandlerThread(android.os.HandlerThread) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

HandlerThread (android.os.HandlerThread)292 Handler (android.os.Handler)128 Message (android.os.Message)26 IntentFilter (android.content.IntentFilter)24 Intent (android.content.Intent)22 Context (android.content.Context)20 Test (org.junit.Test)20 PowerManager (android.os.PowerManager)15 Runnable (java.lang.Runnable)15 ComponentName (android.content.ComponentName)14 View (android.view.View)14 PendingIntent (android.app.PendingIntent)12 MediumTest (android.test.suitebuilder.annotation.MediumTest)12 Looper (android.os.Looper)11 RemoteException (android.os.RemoteException)11 MediaFormat (android.media.MediaFormat)10 Pair (android.util.Pair)10 SmallTest (android.test.suitebuilder.annotation.SmallTest)9 Uri (android.net.Uri)8 IOException (java.io.IOException)8