Skip to content

Commit

Permalink
Move Model conversions into a separate file in Glide's emulator tests.
Browse files Browse the repository at this point in the history
This makes it a bit easier to re-use in different files.

PiperOrigin-RevId: 462167970
  • Loading branch information
sjudd authored and glide-copybara-robot committed Jul 20, 2022
1 parent 4016448 commit 6f681a2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@
import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.bumptech.glide.test.GlideApp;
import com.bumptech.glide.test.ModelGeneratorRule;
import com.bumptech.glide.test.ResourceIds;
import com.bumptech.glide.testutil.ConcurrencyHelper;
import com.bumptech.glide.testutil.TearDownGlide;
import com.google.common.io.ByteStreams;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -31,6 +26,7 @@
@RunWith(AndroidJUnit4.class)
public class AsBytesTest {
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide();
@Rule public final ModelGeneratorRule modelGeneratorRule = new ModelGeneratorRule();
private final ConcurrencyHelper concurrency = new ConcurrencyHelper();

private Context context;
Expand Down Expand Up @@ -149,8 +145,7 @@ public void loadVideoFilePath_asBytes_withFrameTime_providesByteOfFrame() throws
@Test
public void loadVideoFileUri_asBytes_providesByteOfFrame() throws IOException {
byte[] data =
concurrency.get(
Glide.with(context).as(byte[].class).load(Uri.fromFile(writeVideoToFile())).submit());
concurrency.get(Glide.with(context).as(byte[].class).load(writeVideoToFileUri()).submit());

assertThat(data).isNotNull();
assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull();
Expand All @@ -162,7 +157,7 @@ public void loadVideoFileUri_asBytes_withFrameTime_providesByteOfFrame() throws
concurrency.get(
GlideApp.with(context)
.as(byte[].class)
.load(Uri.fromFile(writeVideoToFile()))
.load(writeVideoToFileUri())
.frame(TimeUnit.SECONDS.toMicros(1))
.submit());

Expand All @@ -171,32 +166,10 @@ public void loadVideoFileUri_asBytes_withFrameTime_providesByteOfFrame() throws
}

private File writeVideoToFile() throws IOException {
byte[] videoData = loadVideoBytes();
File parent = context.getCacheDir();
if (!parent.mkdirs() && (!parent.exists() || !parent.isDirectory())) {
throw new IllegalStateException("Failed to mkdirs for: " + parent);
}
File toWrite = new File(parent, "temp.jpeg");
if (toWrite.exists() && !toWrite.delete()) {
throw new IllegalStateException("Failed to delete existing temp file: " + toWrite);
}

OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(toWrite));
os.write(videoData);
os.close();
} finally {
if (os != null) {
os.close();
}
}
return toWrite;
return modelGeneratorRule.asFile(ResourceIds.raw.video);
}

private byte[] loadVideoBytes() throws IOException {
Resources resources = context.getResources();
InputStream is = resources.openRawResource(ResourceIds.raw.video);
return ByteStreams.toByteArray(is);
private Uri writeVideoToFileUri() throws IOException {
return Uri.fromFile(writeVideoToFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.test.GlideApp;
import com.bumptech.glide.test.ModelGeneratorRule;
import com.bumptech.glide.test.ResourceIds;
import com.bumptech.glide.testutil.ConcurrencyHelper;
import com.bumptech.glide.testutil.MockModelLoader;
import com.bumptech.glide.testutil.TearDownGlide;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -25,7 +25,10 @@
@RunWith(AndroidJUnit4.class)
public class AsFileTest {
private static final String URL = "https://imgs.xkcd.com/comics/mc_hammer_age.png";

@Rule public final ModelGeneratorRule modelGeneratorRule = new ModelGeneratorRule();
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide();

private final ConcurrencyHelper concurrency = new ConcurrencyHelper();
private final Context context = ApplicationProvider.getApplicationContext();

Expand Down Expand Up @@ -95,27 +98,10 @@ public void asFile_withUrlAndDiskCacheStrategyAll_fails() {
}

private InputStream getData() {
InputStream is = null;
try {
is = context.getResources().openRawResource(ResourceIds.raw.canonical);
byte[] buffer = new byte[1024 * 1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int read;
while ((read = is.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
byte[] data = outputStream.toByteArray();
return new ByteArrayInputStream(data);
return new ByteArrayInputStream(modelGeneratorRule.asByteArray(ResourceIds.raw.canonical));
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Ignored.
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.bumptech.glide.test;

import android.content.Context;
import android.content.res.Resources;
import androidx.annotation.RawRes;
import androidx.test.core.app.ApplicationProvider;
import com.google.common.io.ByteStreams;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.rules.ExternalResource;

/** Converts raw resources into specific model types (Uris, Files, byte arrays etc). */
public final class ModelGeneratorRule extends ExternalResource {
private static final String TEMP_FOLDER_NAME = "model_generator_rule_cache";

private final Context context = ApplicationProvider.getApplicationContext();
private final AtomicInteger fileNameCounter = new AtomicInteger();

private File getTempDir() {
File tempDir = new File(context.getCacheDir(), TEMP_FOLDER_NAME);
if (!tempDir.mkdirs() && (!tempDir.exists() || !tempDir.isDirectory())) {
throw new IllegalStateException("Failed to mkdirs for: " + tempDir);
}
return tempDir;
}

private File nextTempFile() {
String name = "model_generator" + fileNameCounter.getAndIncrement();
return new File(getTempDir(), name);
}

public File asFile(@RawRes int resourceId) throws IOException {
return writeToFile(resourceId);
}

public byte[] asByteArray(@RawRes int resourceId) throws IOException {
Resources resources = context.getResources();
InputStream is = resources.openRawResource(resourceId);
return ByteStreams.toByteArray(is);
}

private File writeToFile(@RawRes int resourceId) throws IOException {
byte[] data = asByteArray(resourceId);
File result = nextTempFile();
try (OutputStream os = new FileOutputStream(result)) {
os.write(data);
}
return result;
}

@Override
protected void after() {
super.after();
cleanupTempDir();
}

private void cleanupTempDir() {
File tempDir = getTempDir();
File[] children = tempDir.listFiles();
if (children != null) {
for (File child : children) {
if (child.isDirectory()) {
throw new IllegalStateException("Expected a file, but was a directory: " + child);
}
if (!child.delete()) {
throw new IllegalStateException("Failed to delete: " + child);
}
}
}
if (!tempDir.delete()) {
throw new IllegalStateException("Failed to delete temp dir: " + tempDir);
}
}
}

0 comments on commit 6f681a2

Please sign in to comment.