2015-10-12 18:25:05 -07:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.TextSecureTestCase;
|
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2016-12-12 15:05:06 +01:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
2015-10-12 18:25:05 -07:00
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import static org.mockito.Matchers.any;
|
|
|
|
import static org.mockito.Matchers.anyFloat;
|
|
|
|
import static org.mockito.Matchers.eq;
|
|
|
|
import static org.mockito.Mockito.doNothing;
|
|
|
|
import static org.mockito.Mockito.doReturn;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
import static org.mockito.Mockito.spy;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
public class AttachmentDatabaseTest extends TextSecureTestCase {
|
|
|
|
private static final long ROW_ID = 1L;
|
|
|
|
private static final long UNIQUE_ID = 2L;
|
|
|
|
|
|
|
|
private AttachmentDatabase database;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setUp() {
|
2016-11-03 17:36:45 +01:00
|
|
|
super.setUp();
|
2015-10-12 18:25:05 -07:00
|
|
|
database = spy(DatabaseFactory.getAttachmentDatabase(getInstrumentation().getTargetContext()));
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:05:06 +01:00
|
|
|
public void testThumbnailGenerationTaskNotRunWhenThumbnailExists() throws Exception {
|
2015-10-12 18:25:05 -07:00
|
|
|
final AttachmentId attachmentId = new AttachmentId(ROW_ID, UNIQUE_ID);
|
|
|
|
|
2016-12-12 15:05:06 +01:00
|
|
|
DatabaseAttachment mockAttachment = getMockAttachment("x/x");
|
|
|
|
when(database.getAttachment(attachmentId)).thenReturn(mockAttachment);
|
2015-10-12 18:25:05 -07:00
|
|
|
|
2016-12-12 15:05:06 +01:00
|
|
|
InputStream mockInputStream = mock(InputStream.class);
|
|
|
|
doReturn(mockInputStream).when(database).getDataStream(any(MasterSecret.class), any(AttachmentId.class), eq("thumbnail"));
|
2015-10-12 18:25:05 -07:00
|
|
|
database.getThumbnailStream(mock(MasterSecret.class), attachmentId);
|
|
|
|
|
2016-12-12 15:05:06 +01:00
|
|
|
// Works as the Future#get() call in AttachmentDatabase#getThumbnailStream() makes updating synchronous
|
2015-10-12 18:25:05 -07:00
|
|
|
verify(database, never()).updateAttachmentThumbnail(any(MasterSecret.class), any(AttachmentId.class), any(InputStream.class), anyFloat());
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:05:06 +01:00
|
|
|
public void testThumbnailGenerationTaskRunWhenThumbnailMissing() throws Exception {
|
2015-10-12 18:25:05 -07:00
|
|
|
final AttachmentId attachmentId = new AttachmentId(ROW_ID, UNIQUE_ID);
|
|
|
|
|
2016-12-12 15:05:06 +01:00
|
|
|
DatabaseAttachment mockAttachment = getMockAttachment("image/png");
|
|
|
|
when(database.getAttachment(attachmentId)).thenReturn(mockAttachment);
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
doReturn(null).when(database).getDataStream(any(MasterSecret.class), any(AttachmentId.class), eq("thumbnail"));
|
|
|
|
doNothing().when(database).updateAttachmentThumbnail(any(MasterSecret.class), any(AttachmentId.class), any(InputStream.class), anyFloat());
|
|
|
|
|
|
|
|
try {
|
|
|
|
database.new ThumbnailFetchCallable(mock(MasterSecret.class), attachmentId).call();
|
2016-12-12 15:05:06 +01:00
|
|
|
throw new AssertionError("Didn't try to generate thumbnail");
|
|
|
|
} catch (BitmapDecodingException bde) {
|
|
|
|
if (!(bde.getCause() instanceof FileNotFoundException)) {
|
|
|
|
throw new AssertionError("Thumbnail generation failed for another reason than a FileNotFoundException: " + bde.getMessage());
|
|
|
|
} // else success
|
2015-10-12 18:25:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DatabaseAttachment getMockAttachment(String contentType) {
|
|
|
|
DatabaseAttachment attachment = mock(DatabaseAttachment.class);
|
|
|
|
when(attachment.getContentType()).thenReturn(contentType);
|
|
|
|
when(attachment.getDataUri()).thenReturn(Uri.EMPTY);
|
2016-12-12 15:05:06 +01:00
|
|
|
when(attachment.hasData()).thenReturn(true);
|
2015-10-12 18:25:05 -07:00
|
|
|
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
}
|