2014-11-12 03:57:53 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import org.mockito.ArgumentCaptor;
|
|
|
|
import org.mockito.Mockito;
|
2015-01-12 21:34:36 +00:00
|
|
|
import org.thoughtcrime.securesms.TextSecureTestCase;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
2015-02-28 00:57:32 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
2014-11-12 19:35:54 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.NotFoundException;
|
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.PushNetworkException;
|
2014-11-12 03:57:53 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import dagger.Module;
|
|
|
|
import dagger.ObjectGraph;
|
|
|
|
import dagger.Provides;
|
|
|
|
|
|
|
|
import static org.mockito.Matchers.any;
|
|
|
|
import static org.mockito.Matchers.eq;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
|
|
|
|
2015-01-12 21:34:36 +00:00
|
|
|
public class DeliveryReceiptJobTest extends TextSecureTestCase {
|
2014-11-12 03:57:53 +00:00
|
|
|
|
|
|
|
public void testDelivery() throws IOException {
|
|
|
|
TextSecureMessageSender textSecureMessageSender = mock(TextSecureMessageSender.class);
|
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
|
|
|
|
|
DeliveryReceiptJob deliveryReceiptJob = new DeliveryReceiptJob(getContext(),
|
|
|
|
"+14152222222",
|
|
|
|
timestamp, "foo");
|
|
|
|
|
|
|
|
ObjectGraph objectGraph = ObjectGraph.create(new TestModule(textSecureMessageSender));
|
|
|
|
objectGraph.inject(deliveryReceiptJob);
|
|
|
|
|
|
|
|
deliveryReceiptJob.onRun();
|
|
|
|
|
2015-02-28 00:57:32 +00:00
|
|
|
ArgumentCaptor<TextSecureAddress> captor = ArgumentCaptor.forClass(TextSecureAddress.class);
|
2014-11-12 03:57:53 +00:00
|
|
|
verify(textSecureMessageSender).sendDeliveryReceipt(captor.capture(), eq(timestamp));
|
|
|
|
|
2015-03-18 01:10:08 +00:00
|
|
|
assertTrue(captor.getValue().getRelay().get().equals("foo"));
|
2014-11-12 03:57:53 +00: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-28 00:57:32 +00:00
|
|
|
.sendDeliveryReceipt(any(TextSecureAddress.class), eq(timestamp));
|
2014-11-12 03:57:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
DeliveryReceiptJob deliveryReceiptJob = new DeliveryReceiptJob(getContext(),
|
|
|
|
"+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-28 00:57:32 +00:00
|
|
|
.sendDeliveryReceipt(any(TextSecureAddress.class), eq(timestamp));
|
2014-11-12 03:57:53 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides TextSecureMessageSenderFactory provideTextSecureMessageSenderFactory() {
|
|
|
|
return new TextSecureMessageSenderFactory() {
|
|
|
|
@Override
|
|
|
|
public TextSecureMessageSender create(MasterSecret masterSecret) {
|
|
|
|
return textSecureMessageSender;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|