2014-11-11 19:57:53 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
import org.junit.Test;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.mockito.ArgumentCaptor;
|
|
|
|
import org.mockito.Mockito;
|
2015-11-18 18:09:39 -08:00
|
|
|
import org.thoughtcrime.securesms.BaseUnitTest;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
2015-02-27 16:57:32 -08:00
|
|
|
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
2014-11-12 11:35:54 -08:00
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.NotFoundException;
|
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.PushNetworkException;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import dagger.Module;
|
|
|
|
import dagger.ObjectGraph;
|
|
|
|
import dagger.Provides;
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
import static junit.framework.Assert.assertFalse;
|
|
|
|
import static junit.framework.Assert.assertTrue;
|
2014-11-11 19:57:53 -08:00
|
|
|
import static org.mockito.Matchers.any;
|
|
|
|
import static org.mockito.Matchers.eq;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
public class DeliveryReceiptJobTest extends BaseUnitTest {
|
|
|
|
@Test
|
2014-11-11 19:57:53 -08:00
|
|
|
public void testDelivery() throws IOException {
|
|
|
|
TextSecureMessageSender textSecureMessageSender = mock(TextSecureMessageSender.class);
|
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
DeliveryReceiptJob deliveryReceiptJob = new DeliveryReceiptJob(context,
|
2014-11-11 19:57:53 -08:00
|
|
|
"+14152222222",
|
|
|
|
timestamp, "foo");
|
|
|
|
|
|
|
|
ObjectGraph objectGraph = ObjectGraph.create(new TestModule(textSecureMessageSender));
|
|
|
|
objectGraph.inject(deliveryReceiptJob);
|
|
|
|
|
|
|
|
deliveryReceiptJob.onRun();
|
|
|
|
|
2015-02-27 16:57:32 -08:00
|
|
|
ArgumentCaptor<TextSecureAddress> captor = ArgumentCaptor.forClass(TextSecureAddress.class);
|
2014-11-11 19:57:53 -08:00
|
|
|
verify(textSecureMessageSender).sendDeliveryReceipt(captor.capture(), eq(timestamp));
|
|
|
|
|
2015-03-17 18:10:08 -07:00
|
|
|
assertTrue(captor.getValue().getRelay().get().equals("foo"));
|
2014-11-11 19:57:53 -08:00
|
|
|
assertTrue(captor.getValue().getNumber().equals("+14152222222"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNetworkError() throws IOException {
|
|
|
|
TextSecureMessageSender textSecureMessageSender = mock(TextSecureMessageSender.class);
|
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
|
|
|
|
|
Mockito.doThrow(new PushNetworkException("network error"))
|
|
|
|
.when(textSecureMessageSender)
|
2015-02-27 16:57:32 -08:00
|
|
|
.sendDeliveryReceipt(any(TextSecureAddress.class), eq(timestamp));
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
DeliveryReceiptJob deliveryReceiptJob = new DeliveryReceiptJob(context,
|
2014-11-11 19:57:53 -08:00
|
|
|
"+14152222222",
|
|
|
|
timestamp, "foo");
|
|
|
|
|
|
|
|
ObjectGraph objectGraph = ObjectGraph.create(new TestModule(textSecureMessageSender));
|
|
|
|
objectGraph.inject(deliveryReceiptJob);
|
|
|
|
|
|
|
|
try {
|
|
|
|
deliveryReceiptJob.onRun();
|
|
|
|
throw new AssertionError();
|
|
|
|
} catch (IOException e) {
|
|
|
|
assertTrue(deliveryReceiptJob.onShouldRetry(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
Mockito.doThrow(new NotFoundException("not found"))
|
|
|
|
.when(textSecureMessageSender)
|
2015-02-27 16:57:32 -08:00
|
|
|
.sendDeliveryReceipt(any(TextSecureAddress.class), eq(timestamp));
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
try {
|
|
|
|
deliveryReceiptJob.onRun();
|
|
|
|
throw new AssertionError();
|
|
|
|
} catch (IOException e) {
|
|
|
|
assertFalse(deliveryReceiptJob.onShouldRetry(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Module(injects = DeliveryReceiptJob.class)
|
|
|
|
public static class TestModule {
|
|
|
|
|
|
|
|
private final TextSecureMessageSender textSecureMessageSender;
|
|
|
|
|
|
|
|
public TestModule(TextSecureMessageSender textSecureMessageSender) {
|
|
|
|
this.textSecureMessageSender = textSecureMessageSender;
|
|
|
|
}
|
|
|
|
|
2015-11-18 18:09:39 -08:00
|
|
|
@Provides
|
|
|
|
TextSecureMessageSenderFactory provideTextSecureMessageSenderFactory() {
|
2014-11-11 19:57:53 -08:00
|
|
|
return new TextSecureMessageSenderFactory() {
|
|
|
|
@Override
|
2015-07-30 13:03:44 -07:00
|
|
|
public TextSecureMessageSender create() {
|
2014-11-11 19:57:53 -08:00
|
|
|
return textSecureMessageSender;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|