/** * Tests normal mailer behaviour with non-default locale. * * @throws Exception when fails */ public void testSendLocalized() throws Exception { InvocationsCollector inits = new InvocationsCollector(); InvocationsCollector sends = new InvocationsCollector(); InvocationsCollector errors = new InvocationsCollector(); doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt()); doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString()); doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString()); when(preferences.getString(eq(KEY_PREF_EMAIL_LOCALE), anyString())).thenReturn("ru_RU"); 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), true, null)); assertTrue(errors.isEmpty()); assertArrayEquals( new Object[] {"*****@*****.**", "decrypted password", "smtp.mail.com", "111"}, inits.get(0)); assertEquals("[SMailer] Исходящий звонок на +12345678901", sends.get(0)[0]); }
/** * Tests normal mailer behaviour. * * @throws Exception when fails */ public void testSend() throws Exception { InvocationsCollector inits = new InvocationsCollector(); InvocationsCollector sends = new InvocationsCollector(); InvocationsCollector errors = new InvocationsCollector(); doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt()); doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString()); doAnswer(sends).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), true, null)); assertTrue(errors.isEmpty()); assertArrayEquals( new Object[] {"*****@*****.**", "decrypted password", "smtp.mail.com", "111"}, inits.get(0)); assertEquals("[SMailer] Outgoing call to +12345678901", sends.get(0)[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()); }
/** * Check that mailer produces notification without internet connection. * * @throws Exception when fails */ public void testErrorNotConnected() throws Exception { InvocationsCollector inits = new InvocationsCollector(); InvocationsCollector sends = new InvocationsCollector(); InvocationsCollector errors = new InvocationsCollector(); doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt()); doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString()); doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString()); when(networkInfo.isConnected()).thenReturn(false); Mailer mailer = new Mailer(context, transport, cryptor, notifications, database); mailer.send( new MailMessage("+12345678901", false, null, null, false, false, null, null, true, null)); assertTrue(inits.isEmpty()); assertTrue(sends.isEmpty()); assertEquals(R.string.notification_error_no_connection, errors.get(0)[0]); }
/** * Check that mailer produces notification when port parameter is empty. * * @throws Exception when fails */ public void testErrorEmptyPort() throws Exception { InvocationsCollector inits = new InvocationsCollector(); InvocationsCollector sends = new InvocationsCollector(); InvocationsCollector errors = new InvocationsCollector(); doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt()); doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString()); doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString()); when(preferences.getString(eq(KEY_PREF_EMAIL_PORT), anyString())).thenReturn(null); Mailer mailer = new Mailer(context, transport, cryptor, notifications, database); mailer.send( new MailMessage("+12345678901", false, null, null, false, false, null, null, true, null)); assertTrue(inits.isEmpty()); assertTrue(sends.isEmpty()); assertEquals(R.string.notification_error_no_port, errors.get(0)[0]); }
/** * Check that mailer produces notification on other transport exceptions. * * @throws Exception when fails */ public void testErrorOtherExceptions() throws Exception { InvocationsCollector inits = new InvocationsCollector(); InvocationsCollector sends = new InvocationsCollector(); InvocationsCollector errors = new InvocationsCollector(); doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt()); doAnswer(inits).when(transport).init(anyString(), anyString(), anyString(), anyString()); doAnswer(sends).when(transport).send(anyString(), anyString(), anyString(), anyString()); 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, null, true, null)); assertFalse(inits.isEmpty()); assertTrue(sends.isEmpty()); assertEquals(R.string.notification_error_mail_general, errors.get(0)[0]); }
/** * When {@link Settings#KEY_PREF_NOTIFY_SEND_SUCCESS} set to true success notification should be * shown. * * @throws Exception when fails */ public void testSuccessNotification() throws Exception { InvocationsCollector errors = new InvocationsCollector(); InvocationsCollector successes = new InvocationsCollector(); doAnswer(errors).when(notifications).showMailError(anyInt(), anyLong(), anyInt()); doAnswer(successes).when(notifications).showMailSuccess(anyLong()); Mailer mailer = new Mailer(context, transport, cryptor, notifications, database); /* settings is off */ when(preferences.getBoolean(eq(KEY_PREF_NOTIFY_SEND_SUCCESS), anyBoolean())).thenReturn(false); mailer.send(new MailMessage("1", false, null, null, false, false, null, null, true, null)); assertTrue(errors.isEmpty()); assertTrue(successes.isEmpty()); /* settings is on */ when(preferences.getBoolean(eq(KEY_PREF_NOTIFY_SEND_SUCCESS), anyBoolean())).thenReturn(true); mailer.send(new MailMessage("1", false, null, null, false, false, null, null, true, null)); assertTrue(errors.isEmpty()); assertFalse(successes.isEmpty()); }
/** * 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()); }