Exemplo n.º 1
0
  /**
   * When mailer parameters goes back to normal last notification should be removed.
   *
   * @throws Exception when fails
   */
  public void testClearNotificationExceptions() throws Exception {
    InvocationsCollector errors = new InvocationsCollector();
    InvocationsCollector clears = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doAnswer(clears).when(notifications).hideMailError();
    doAnswer(
            new Answer() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                String subject = invocation.getArgumentAt(0, String.class);
                if (subject.equals("[SMailer] Outgoing call to bad_phone")) {
                  throw new MessagingException("bad_phone");
                }
                return null;
              }
            })
        .when(transport)
        .send(anyString(), anyString(), anyString(), anyString());

    Mailer mailer = new Mailer(context, transport, cryptor, notifications, database);

    /* bad_phone produces notification */

    mailer.send(
        new MailMessage("bad_phone", false, null, null, false, false, null, null, true, null));
    assertEquals(R.string.notification_error_mail_general, errors.get(0)[0]);
    assertTrue(clears.isEmpty());

    /* good_phone removes it */

    errors.clear();
    clears.clear();

    mailer.send(
        new MailMessage("good_phone", false, null, null, false, false, null, null, true, null));

    assertTrue(errors.isEmpty());
    assertFalse(clears.isEmpty());
  }
Exemplo n.º 2
0
  /**
   * Test resending unsent messages.
   *
   * @throws Exception when fails
   */
  public void testSendUnsent() throws Exception {
    InvocationsCollector errors = new InvocationsCollector();

    doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt());
    doThrow(MessagingException.class)
        .when(transport)
        .send(anyString(), anyString(), anyString(), anyString());

    Mailer mailer = new Mailer(context, transport, cryptor, notifications, database);
    mailer.send(
        new MailMessage(
            "+12345678901",
            false,
            null,
            null,
            false,
            false,
            null,
            new GeoCoordinates(30.0, 60.0),
            false,
            null));
    mailer.send(
        new MailMessage(
            "+12345678901",
            false,
            null,
            null,
            false,
            false,
            null,
            new GeoCoordinates(30.0, 60.0),
            false,
            null));
    mailer.send(
        new MailMessage(
            "+12345678901",
            false,
            null,
            null,
            false,
            false,
            null,
            new GeoCoordinates(30.0, 60.0),
            false,
            null));

    assertEquals(3, database.getMessages().getCount());
    assertEquals(3, database.getUnsentMessages().getCount());
    assertEquals(3, errors.size());

    /* try resend with transport still disabled */
    errors.clear();

    mailer.sendAllUnsent();

    assertEquals(3, database.getMessages().getCount());
    assertEquals(3, database.getUnsentMessages().getCount());
    assertTrue(errors.isEmpty()); /* no error notifications should be shown */

    /* enable transport an try again */
    doNothing().when(transport).send(anyString(), anyString(), anyString(), anyString());
    errors.clear();

    mailer.sendAllUnsent();

    assertEquals(3, database.getMessages().getCount());
    assertEquals(0, database.getUnsentMessages().getCount());
    assertTrue(errors.isEmpty());
  }