jeudi 27 août 2015

Issue with Android Junit concurrency

I have a Android JUnit test extending ServiceTestCase. I have a number of test that successfully run, however one of my tests is having a problem.

The test begins a series of events that require the Service that I'm testing to save an image file. My JUnit test is then supposed to check the pixel size of the file to make sure the service is outputting the correct size of image.

At first I thought it was just a timing issue, which is something I expected and I wrapped my reading function in a while loop. However, no matter how long end up waiting the file never seems to complete until after the test completes. I can just run the test twice (since then the files are there for interrogation), but that kind of eliminates the usefulness of the Unit Test!

I had even went so far as causing the series of events with the service to start on a different thread to ensure that I wasn't locking up the thread somehow, but no luck

Any help would be appreciated.

Here is the code that I'm currently reading the file in with

    private HeightWidth readOutPutImageSize()
  {

    Bitmap bitmap = null;
    int iterations = 10;
    while ((iterations > 0) && (bitmap == null))
    {
      String fileNameAndPath = "/mnt/extSdCard/asdf/Logs/test.png";
      bitmap = BitmapFactory.decodeFile(fileNameAndPath);
      sleep(500);
      iterations--;
    }

    HeightWidth hw = null;

    if(bitmap != null)
    {
      hw = new HeightWidth(bitmap.getHeight(), bitmap.getWidth());
    }
    return hw;
  }

Aucun commentaire:

Enregistrer un commentaire