|
| 1 | +package com.bumptech.glide.load; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.junit.Assume.assumeTrue; |
| 5 | + |
| 6 | +import android.content.Context; |
| 7 | +import android.os.ParcelFileDescriptor; |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import androidx.test.core.app.ApplicationProvider; |
| 10 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 11 | +import com.bumptech.glide.load.data.ParcelFileDescriptorRewinder; |
| 12 | +import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; |
| 13 | +import com.bumptech.glide.load.engine.bitmap_recycle.LruArrayPool; |
| 14 | +import com.bumptech.glide.util.ByteBufferUtil; |
| 15 | +import java.io.ByteArrayInputStream; |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileOutputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | + |
| 29 | +@RunWith(AndroidJUnit4.class) |
| 30 | +public class ImageHeaderParserUtilsTest { |
| 31 | + private final List<FakeImageHeaderParser> fakeParsers = |
| 32 | + Arrays.asList(new FakeImageHeaderParser(), new FakeImageHeaderParser()); |
| 33 | + private List<ImageHeaderParser> parsers; |
| 34 | + private final Context context = ApplicationProvider.getApplicationContext(); |
| 35 | + private final byte[] expectedData = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8}; |
| 36 | + private final LruArrayPool lruArrayPool = new LruArrayPool(); |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUp() { |
| 40 | + parsers = new ArrayList<ImageHeaderParser>(); |
| 41 | + for (FakeImageHeaderParser parser : fakeParsers) { |
| 42 | + parsers.add(parser); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void getType_withTwoParsers_andStream_rewindsBeforeEachParser() throws IOException { |
| 48 | + ImageHeaderParserUtils.getType(parsers, new ByteArrayInputStream(expectedData), lruArrayPool); |
| 49 | + |
| 50 | + assertAllParsersReceivedTheSameData(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void getType_withTwoParsers_andByteBuffer_rewindsBeforeEachParser() throws IOException { |
| 55 | + ImageHeaderParserUtils.getType(parsers, ByteBuffer.wrap(expectedData)); |
| 56 | + |
| 57 | + assertAllParsersReceivedTheSameData(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void getType_withTwoParsers_andFileDescriptor_rewindsBeforeEachParser() |
| 62 | + throws IOException { |
| 63 | + // This test can't work if file descriptor rewinding isn't supported. Sadly that means this |
| 64 | + // test doesn't work in Robolectric. |
| 65 | + assumeTrue(ParcelFileDescriptorRewinder.isSupported()); |
| 66 | + |
| 67 | + ParcelFileDescriptor fileDescriptor = null; |
| 68 | + try { |
| 69 | + fileDescriptor = asFileDescriptor(expectedData); |
| 70 | + ParcelFileDescriptorRewinder rewinder = new ParcelFileDescriptorRewinder(fileDescriptor); |
| 71 | + ImageHeaderParserUtils.getType(parsers, rewinder, lruArrayPool); |
| 72 | + } finally { |
| 73 | + if (fileDescriptor != null) { |
| 74 | + fileDescriptor.close(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + assertAllParsersReceivedTheSameData(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void getOrientation_withTwoParsers_andStream_rewindsBeforeEachParser() throws IOException { |
| 83 | + ImageHeaderParserUtils.getOrientation( |
| 84 | + parsers, new ByteArrayInputStream(expectedData), lruArrayPool); |
| 85 | + |
| 86 | + assertAllParsersReceivedTheSameData(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void getOrientation_withTwoParsers_andByteBuffer_rewindsBeforeEachParser() |
| 91 | + throws IOException { |
| 92 | + ImageHeaderParserUtils.getOrientation(parsers, ByteBuffer.wrap(expectedData), lruArrayPool); |
| 93 | + |
| 94 | + assertAllParsersReceivedTheSameData(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void getOrientation_withTwoParsers_andFileDescriptor_rewindsBeforeEachParser() |
| 99 | + throws IOException { |
| 100 | + // This test can't work if file descriptor rewinding isn't supported. Sadly that means this |
| 101 | + // test doesn't work in Robolectric. |
| 102 | + assumeTrue(ParcelFileDescriptorRewinder.isSupported()); |
| 103 | + ParcelFileDescriptor fileDescriptor = null; |
| 104 | + try { |
| 105 | + fileDescriptor = asFileDescriptor(expectedData); |
| 106 | + ParcelFileDescriptorRewinder rewinder = new ParcelFileDescriptorRewinder(fileDescriptor); |
| 107 | + ImageHeaderParserUtils.getOrientation(parsers, rewinder, lruArrayPool); |
| 108 | + } finally { |
| 109 | + if (fileDescriptor != null) { |
| 110 | + fileDescriptor.close(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + assertAllParsersReceivedTheSameData(); |
| 115 | + } |
| 116 | + |
| 117 | + private void assertAllParsersReceivedTheSameData() { |
| 118 | + for (FakeImageHeaderParser parser : fakeParsers) { |
| 119 | + assertThat(parser.data).isNotNull(); |
| 120 | + assertThat(parser.data).asList().containsExactlyElementsIn(asList(expectedData)).inOrder(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static List<Byte> asList(byte[] data) { |
| 125 | + List<Byte> result = new ArrayList<>(); |
| 126 | + for (byte item : data) { |
| 127 | + result.add(item); |
| 128 | + } |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | + private ParcelFileDescriptor asFileDescriptor(byte[] data) throws IOException { |
| 133 | + File file = new File(context.getCacheDir(), "temp"); |
| 134 | + OutputStream os = null; |
| 135 | + try { |
| 136 | + os = new FileOutputStream(file); |
| 137 | + os.write(data); |
| 138 | + os.close(); |
| 139 | + } finally { |
| 140 | + if (os != null) { |
| 141 | + os.close(); |
| 142 | + } |
| 143 | + } |
| 144 | + return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); |
| 145 | + } |
| 146 | + |
| 147 | + private static final class FakeImageHeaderParser implements ImageHeaderParser { |
| 148 | + |
| 149 | + private byte[] data; |
| 150 | + |
| 151 | + private void readData(InputStream is) throws IOException { |
| 152 | + readData(ByteBufferUtil.fromStream(is)); |
| 153 | + } |
| 154 | + |
| 155 | + // This is rather roundabout, but it's a simple way of reading the remaining data in the buffer. |
| 156 | + private void readData(ByteBuffer byteBuffer) { |
| 157 | + |
| 158 | + byte[] data = new byte[byteBuffer.remaining()]; |
| 159 | + // A 0 length means we read no data. If we try to pass this to ByteBuffer it will throw. We'd |
| 160 | + // rather not get that obscure exception and instead have an assertion above trigger because |
| 161 | + // we didn't read enough data. So we work around the exception here if we have no data to |
| 162 | + // read. |
| 163 | + if (data.length != 0) { |
| 164 | + byteBuffer.get(data, byteBuffer.position(), byteBuffer.remaining()); |
| 165 | + } |
| 166 | + this.data = data; |
| 167 | + } |
| 168 | + |
| 169 | + @NonNull |
| 170 | + @Override |
| 171 | + public ImageType getType(@NonNull InputStream is) throws IOException { |
| 172 | + readData(is); |
| 173 | + return ImageType.UNKNOWN; |
| 174 | + } |
| 175 | + |
| 176 | + @NonNull |
| 177 | + @Override |
| 178 | + public ImageType getType(@NonNull ByteBuffer byteBuffer) throws IOException { |
| 179 | + readData(byteBuffer); |
| 180 | + return ImageType.UNKNOWN; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public int getOrientation(@NonNull InputStream is, @NonNull ArrayPool byteArrayPool) |
| 185 | + throws IOException { |
| 186 | + readData(is); |
| 187 | + return ImageHeaderParser.UNKNOWN_ORIENTATION; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public int getOrientation(@NonNull ByteBuffer byteBuffer, @NonNull ArrayPool byteArrayPool) |
| 192 | + throws IOException { |
| 193 | + readData(byteBuffer); |
| 194 | + return ImageHeaderParser.UNKNOWN_ORIENTATION; |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments