2014-12-30 09:36:51 +00:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
2015-01-12 21:34:36 +00:00
|
|
|
import org.thoughtcrime.securesms.TextSecureTestCase;
|
2014-12-30 09:36:51 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-05-31 23:08:40 +00:00
|
|
|
import org.thoughtcrime.securesms.database.PartDatabase.PartId;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-01-12 21:34:36 +00:00
|
|
|
public class PartDatabaseTest extends TextSecureTestCase {
|
2015-05-31 23:08:40 +00:00
|
|
|
private static final long ROW_ID = 1L;
|
|
|
|
private static final long UNIQUE_ID = 2L;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
private PartDatabase database;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setUp() {
|
|
|
|
database = spy(DatabaseFactory.getPartDatabase(getInstrumentation().getTargetContext()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testTaskNotRunWhenThumbnailExists() throws Exception {
|
2015-05-31 23:08:40 +00:00
|
|
|
when(database.getPart(new PartId(ROW_ID, UNIQUE_ID))).thenReturn(getPduPartSkeleton("x/x"));
|
|
|
|
doReturn(mock(InputStream.class)).when(database).getDataStream(any(MasterSecret.class), any(PartId.class), eq("thumbnail"));
|
2014-12-30 09:36:51 +00:00
|
|
|
|
2015-05-31 23:08:40 +00:00
|
|
|
database.getThumbnailStream(null, new PartId(ROW_ID, UNIQUE_ID));
|
2014-12-30 09:36:51 +00:00
|
|
|
|
2015-05-31 23:08:40 +00:00
|
|
|
verify(database, never()).updatePartThumbnail(any(MasterSecret.class), any(PartId.class), any(PduPart.class), any(InputStream.class), anyFloat());
|
2014-12-30 09:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testTaskRunWhenThumbnailMissing() throws Exception {
|
2015-05-31 23:08:40 +00:00
|
|
|
when(database.getPart(new PartId(ROW_ID, UNIQUE_ID))).thenReturn(getPduPartSkeleton("image/png"));
|
|
|
|
doReturn(null).when(database).getDataStream(any(MasterSecret.class), any(PartId.class), eq("thumbnail"));
|
|
|
|
doNothing().when(database).updatePartThumbnail(any(MasterSecret.class), any(PartId.class), any(PduPart.class), any(InputStream.class), anyFloat());
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
try {
|
2015-05-31 23:08:40 +00:00
|
|
|
database.new ThumbnailFetchCallable(mock(MasterSecret.class), new PartId(ROW_ID, UNIQUE_ID)).call();
|
2014-12-30 09:36:51 +00:00
|
|
|
throw new AssertionError("didn't try to generate thumbnail");
|
|
|
|
} catch (FileNotFoundException fnfe) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private PduPart getPduPartSkeleton(String contentType) {
|
|
|
|
PduPart part = new PduPart();
|
|
|
|
part.setContentType(contentType.getBytes());
|
|
|
|
part.setDataUri(Uri.EMPTY);
|
|
|
|
return part;
|
|
|
|
}
|
|
|
|
}
|