HandlerThread#getLooper returns null under Robolectric

1,114 views
Skip to first unread message

Jeremy Wadsack

unread,
Apr 29, 2011, 7:36:55 PM4/29/11
to robol...@googlegroups.com
I have the following, common code, in my service:

        HandlerThread thread = new HandlerThread(ClientGatewayService.class.getSimpleName());
        thread.start();
        serviceLooper = thread.getLooper();

In production this works fine, but under Robolectric, the serviceLooper object above is null. 

According to the Android docs, getLooper can return null if the thread has not started or Thread#isAlive() returns false for any reason. In any other case the call to getLooper is supposed to block until the looper is prepared. 

Given that I'm calling Thread#start() immediately preceding this, it would imply that the HandlerThread, under Robolectric, it would appear the either the HandlerThread has prepared the looper, run the looper and already killed it. I don't see an instance of HandlerThread in Robolectric shadows, so I assume this is using the Android source.

Anyone know what's going on here?

--
Jeremy Wadsack

Jeremy Wadsack

unread,
Apr 29, 2011, 7:47:00 PM4/29/11
to robol...@googlegroups.com
In fact, under a little more testing I was able to verify that the thread actually is not alive. Here's a simple test case that fails under robolectric:

@RunWith(InjectionTestRunner.class)
public class SandboxTest {

    @Test
    public void testHandlerThread() {
        HandlerThread thread = new HandlerThread("TEST");
        thread.start();
        assertTrue(thread.isAlive());
    }

}


--
Jeremy Wadsack

Tyler Schultz

unread,
Apr 29, 2011, 10:07:25 PM4/29/11
to robol...@googlegroups.com, jeremy....@gmail.com
There is no shadow class for HandlerThread (at least in the pivotal fork).  If you want that class's methods to return anything but false, null, 0, etc., you'll need to implement a shadow for it.
Robolectric is returning null because it can't find a shadow class/method to invoke. If you plan on test driving your application to its fullest, you'll need to learn how to write shadows and make your own changes to Robolectric.  Phil wrote a great blog post that covers what you'll need to understand: 

Jeremy Wadsack

unread,
Apr 29, 2011, 10:26:32 PM4/29/11
to Tyler Schultz, robol...@googlegroups.com
Thanks Tyler.

Having seen numerous posts to that effect on this list, I should have recognized it. Particularly as I said, "I don't see an instance of HandlerThread in Robolectric shadows." 

That said, I guess I was under the assumption that parts of the Android SDK were bundled/included in Robolectric or and Android stub library. What's not clear is what's shadowed and what's provided and what doesn't fall under either of those.

Also, given that HandlerThread is an extremely common pattern in services that I've seen, I guess I'm surprised that it hasn't been shadowed already. I'll take care of that.

Thanks,

--
Jeremy Wadsack
Reply all
Reply to author
Forward
0 new messages