lundi 25 mai 2015

When starting a service in android, does the ui thread need to go to wait in order for it to start?

I have encountered a problem while writing tests for my activity which made me wonder about this issue. I'm using ActivityInstrumentationTestCase2 and my activity sends intents to the service using startService when a button is clicked and it works fine. (My service is a singelton so i can know if it is running)
When in testing I'm doing the following:

  1. assert service isn't running.
  2. send intent to start the service (or press the button tried both)
  3. wait for the service to start and assert it is running

sounds pretty simple but when i try to wait using Thread.sleep the service wont start, and same for when i try doing busy waiting. So i assumed it will need to finish the testMethod to start the service and i made the following design:

Thread helper;

@Override
tearDown() {
    if (helper) join helper
    super.tearDown
}

testMethod() {
    assert stuff
    send intent
    start helper
    finish
}

helperMethod() {
    wait for service to start
    assert stuff
}

this actually works, my guess is that it lets the main thread to finish the testMethod and then the intents are handled and my test finishes. This behaviour seems very odd as i would expect sleeping for enough time will let the service start. So anyone knows what is going on?

Aucun commentaire:

Enregistrer un commentaire