public static void loadAndAssertStatusForRow(
      long downloadRowId, DownloadStatus status, boolean mockNetworkError) {
    FileDownloader loader = FileDownloader.newForDownloadRow(downloadRowId);
    if (mockNetworkError) {
      loader.connectionMock =
          new ConnectionTwitterGnuSocialMock(new ConnectionException("Mocked IO exception"));
    }
    CommandData commandData = CommandData.newCommand(CommandEnum.FETCH_AVATAR);
    loader.load(commandData);

    DownloadData data = DownloadData.fromId(downloadRowId);
    if (DownloadStatus.LOADED.equals(status)) {
      assertFalse("Loaded " + data.getUri() + "; " + data, commandData.getResult().hasError());
      assertEquals("Loaded " + data.getUri(), status, loader.getStatus());
    } else {
      assertTrue("Error loading " + data.getUri(), commandData.getResult().hasError());
    }

    if (DownloadStatus.LOADED.equals(status)) {
      assertTrue("File exists " + data.getUri(), data.getFile().exists());
    } else {
      assertFalse("File doesn't exist " + data.getUri(), data.getFile().exists());
    }

    assertEquals("Loaded " + data.getUri(), status, loader.getStatus());
  }
  private void testFileProvider(long downloadRowId) throws IOException {
    DownloadData data = DownloadData.fromId(downloadRowId);
    assertTrue(data.getFilename(), data.getFile().exists());

    Uri uri = FileProvider.downloadFilenameToUri(data.getFile().getFilename());
    InputStream in = MyContextHolder.get().context().getContentResolver().openInputStream(uri);
    byte[] buffer = new byte[100];
    int bytesRead = in.read(buffer);
    assertEquals(buffer.length, bytesRead);
    in.close();
  }
  public void testImageAttachmentLoad() throws IOException {
    String body = "A message with an image attachment";
    MessageInserter mi = new MessageInserter(ma);
    MbMessage message = mi.buildMessage(mi.buildUser(), body, null, null, DownloadStatus.LOADED);
    message.attachments.add(
        MbAttachment.fromUrlAndContentType(
            new URL(
                "http://www.publicdomainpictures.net/pictures/60000/nahled/landscape-1376582205Yno.jpg"),
            MyContentType.IMAGE));
    long msgId = mi.addMessage(message);

    DownloadData dd =
        DownloadData.getSingleForMessage(msgId, message.attachments.get(0).contentType, null);
    assertEquals("Image URI stored", message.attachments.get(0).getUri(), dd.getUri());

    loadAndAssertStatusForRow(dd.getDownloadId(), DownloadStatus.ABSENT, true);

    loadAndAssertStatusForRow(dd.getDownloadId(), DownloadStatus.LOADED, false);

    testFileProvider(dd.getDownloadId());
  }