private static void assertEquals(byte[] expected, byte[] actual) { assertThat(actual).isEqualTo(expected); } }
private static void assertSegment( Segment segment, byte[] data, int length, long byteOffset, Runnable runnable, IOException exception) { if (data != null) { assertThat(segment.data).isEqualTo(data); assertThat(data).hasLength(length); } else { assertThat(segment.data).isNull(); } assertThat(segment.length).isEqualTo(length); assertThat(segment.byteOffset).isEqualTo(byteOffset); assertThat(segment.action).isEqualTo(runnable); assertThat(segment.isActionSegment()).isEqualTo(runnable != null); assertThat(segment.exception).isEqualTo(exception); assertThat(segment.isErrorSegment()).isEqualTo(exception != null); } }
@Test public void testDecodeApicFrame() { byte[] rawId3 = buildSingleFrameTag("APIC", new byte[] {3, 105, 109, 97, 103, 101, 47, 106, 112, 101, 103, 0, 16, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); Id3Decoder decoder = new Id3Decoder(); Metadata metadata = decoder.decode(rawId3, rawId3.length); assertThat(metadata.length()).isEqualTo(1); ApicFrame apicFrame = (ApicFrame) metadata.get(0); assertThat(apicFrame.mimeType).isEqualTo("image/jpeg"); assertThat(apicFrame.pictureType).isEqualTo(16); assertThat(apicFrame.description).isEqualTo("Hello World"); assertThat(apicFrame.pictureData).hasLength(10); assertThat(apicFrame.pictureData).isEqualTo(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); }
@Test public void loadBitmapDrawable_asBytes_providesBytesOfBitmap() { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(new BitmapDrawable(context.getResources(), bitmap)) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void testInsertEmptyBlobArgument() throws Exception { ContentValues emptyBlobValues = new ContentValues(); emptyBlobValues.put("id", 1); emptyBlobValues.put("blob_col", new byte[]{}); ContentValues nullBlobValues = new ContentValues(); nullBlobValues.put("id", 2); nullBlobValues.put("blob_col", (byte[])null); long key = database.insertWithOnConflict("blob_table", null, emptyBlobValues, SQLiteDatabase.CONFLICT_FAIL); assertThat(key).isNotEqualTo(0L); key = database.insertWithOnConflict("blob_table", null, nullBlobValues, SQLiteDatabase.CONFLICT_FAIL); assertThat(key).isNotEqualTo(0L); Cursor cursor = database.query("blob_table", new String[]{"blob_col"}, "id=1", null, null, null, null); try { assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.getBlob(cursor.getColumnIndexOrThrow("blob_col"))).isNotNull(); } finally { cursor.close(); } cursor = database.query("blob_table", new String[]{"blob_col"}, "id=2", null, null, null, null); try { assertThat(cursor.moveToFirst()).isTrue(); assertThat(cursor.getBlob(cursor.getColumnIndexOrThrow("blob_col"))).isNull(); } finally { cursor.close(); } }
@Test public void testDownloadLicenseFailsIfNoKeySetIdIsReturned() throws Exception { setStubLicenseAndPlaybackDurationValues(1000, 200); byte[] offlineLicenseKeySetId = offlineLicenseHelper.downloadLicense(newDrmInitData()); assertThat(offlineLicenseKeySetId).isNull(); }
@Test public void testReadSetupHeadersWithIOExceptions() throws IOException, InterruptedException { byte[] data = OggTestData.getVorbisHeaderPages(); ExtractorInput input = new FakeExtractorInput.Builder().setData(data).setSimulateIOErrors(true) .setSimulateUnknownLength(true).setSimulatePartialReads(true).build(); VorbisReader reader = new VorbisReader(); VorbisReader.VorbisSetup vorbisSetup = readSetupHeaders(reader, input); assertThat(vorbisSetup.idHeader).isNotNull(); assertThat(vorbisSetup.commentHeader).isNotNull(); assertThat(vorbisSetup.setupHeaderData).isNotNull(); assertThat(vorbisSetup.modes).isNotNull(); assertThat(vorbisSetup.commentHeader.length).isEqualTo(45); assertThat(vorbisSetup.idHeader.data).hasLength(30); assertThat(vorbisSetup.setupHeaderData).hasLength(3597); assertThat(vorbisSetup.idHeader.bitrateMax).isEqualTo(-1); assertThat(vorbisSetup.idHeader.bitrateMin).isEqualTo(-1); assertThat(vorbisSetup.idHeader.bitrateNominal).isEqualTo(66666); assertThat(vorbisSetup.idHeader.blockSize0).isEqualTo(512); assertThat(vorbisSetup.idHeader.blockSize1).isEqualTo(1024); assertThat(vorbisSetup.idHeader.channels).isEqualTo(2); assertThat(vorbisSetup.idHeader.framingFlag).isTrue(); assertThat(vorbisSetup.idHeader.sampleRate).isEqualTo(22050); assertThat(vorbisSetup.idHeader.version).isEqualTo(0); assertThat(vorbisSetup.commentHeader.vendor).isEqualTo("Xiph.Org libVorbis I 20030909"); assertThat(vorbisSetup.iLogModes).isEqualTo(1); assertThat(vorbisSetup.setupHeaderData[vorbisSetup.setupHeaderData.length - 1]) .isEqualTo(data[data.length - 1]); assertThat(vorbisSetup.modes[0].blockFlag).isFalse(); assertThat(vorbisSetup.modes[1].blockFlag).isTrue(); }
@Test public void write_withZeroLengthBuffer_doesNothing() throws IOException { os.write(new byte[0]); assertThat(inner.toByteArray()).hasLength(0); }
@Test public void write_withEmptyBuffer_andDataSmallerThanBuffer_doesNotWriteToStream() throws IOException { os.write(next(bufferSize - 1)); assertThat(inner.toByteArray()).isEmpty(); }
@Test public void testPeekId3Data_returnId3Tag_ifId3TagPresent() throws IOException, InterruptedException { Id3Peeker id3Peeker = new Id3Peeker(); byte[] rawId3 = Id3DecoderTest.buildSingleFrameTag( "APIC", new byte[] { 3, 105, 109, 97, 103, 101, 47, 106, 112, 101, 103, 0, 16, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }); FakeExtractorInput input = new FakeExtractorInput.Builder().setData(rawId3).build(); Metadata metadata = id3Peeker.peekId3Data(input, /* id3FramePredicate= */ null); assertThat(metadata.length()).isEqualTo(1); ApicFrame apicFrame = (ApicFrame) metadata.get(0); assertThat(apicFrame.mimeType).isEqualTo("image/jpeg"); assertThat(apicFrame.pictureType).isEqualTo(16); assertThat(apicFrame.description).isEqualTo("Hello World"); assertThat(apicFrame.pictureData).hasLength(10); assertThat(apicFrame.pictureData).isEqualTo(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); }
@Test public void loadBitmap_asBytes_providesBytesOfBitmap() { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(bitmap) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }
@Test public void testClearPreviewCallback() throws Exception { TestPreviewCallback callback = new TestPreviewCallback(); assertThat(callback.camera).isNull(); assertThat(callback.data).isNull(); camera.setPreviewCallback(callback); camera.setPreviewCallback(null); shadowCamera.invokePreviewCallback("foobar".getBytes(UTF_8)); assertThat(callback.camera).isNull(); assertThat(callback.data).isNull(); camera.setOneShotPreviewCallback(callback); camera.setOneShotPreviewCallback(null); shadowCamera.invokePreviewCallback("foobar".getBytes(UTF_8)); assertThat(callback.camera).isNull(); assertThat(callback.data).isNull(); camera.setPreviewCallbackWithBuffer(callback); camera.setPreviewCallbackWithBuffer(null); shadowCamera.invokePreviewCallback("foobar".getBytes(UTF_8)); assertThat(callback.camera).isNull(); assertThat(callback.data).isNull(); }
@Test public void write_withZeroLengthBufferAndZeroOffsetAndLength_doesNothing() throws IOException { os.write(new byte[0], 0, 0); assertThat(inner.toByteArray()).hasLength(0); }
@Test public void write_withEmptyBuffer_andDataWithPaddingSizeSmallerThanBuffer_doesNotWriteToStream() throws IOException { int padding = 1; int length = bufferSize - padding; byte[] data = nextWithPadding(length, padding); os.write(data, 0, length); assertThat(inner.toByteArray()).isEmpty(); }
private static void testDecodes(BaseEncoding encoding, String encoded, String decoded) { assertTrue(encoding.canDecode(encoded)); assertThat(encoding.decode(encoded)).isEqualTo(decoded.getBytes(UTF_8)); }
@Test public void testSetPreviewCallbacks() throws Exception { TestPreviewCallback callback = new TestPreviewCallback(); assertThat(callback.camera).isNull(); assertThat(callback.data).isNull(); camera.setPreviewCallback(callback); shadowCamera.invokePreviewCallback("foobar".getBytes(UTF_8)); assertThat(callback.camera).isSameAs(camera); assertThat(callback.data).isEqualTo("foobar".getBytes(UTF_8)); }
@Test public void testDecodeMultiFrames() { byte[] rawId3 = buildMultiFramesTag( new FrameSpec( "COMM", new byte[] { 3, 101, 110, 103, 100, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 0, 116, 101, 120, 116, 0 }), new FrameSpec( "APIC", new byte[] { 3, 105, 109, 97, 103, 101, 47, 106, 112, 101, 103, 0, 16, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })); Id3Decoder decoder = new Id3Decoder(); Metadata metadata = decoder.decode(rawId3, rawId3.length); assertThat(metadata.length()).isEqualTo(2); CommentFrame commentFrame = (CommentFrame) metadata.get(0); ApicFrame apicFrame = (ApicFrame) metadata.get(1); assertThat(commentFrame.language).isEqualTo("eng"); assertThat(commentFrame.description).isEqualTo("description"); assertThat(commentFrame.text).isEqualTo("text"); assertThat(apicFrame.mimeType).isEqualTo("image/jpeg"); assertThat(apicFrame.pictureType).isEqualTo(16); assertThat(apicFrame.description).isEqualTo("Hello World"); assertThat(apicFrame.pictureData).hasLength(10); assertThat(apicFrame.pictureData).isEqualTo(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); }
@Test public void loadImageResourceId_asBytes_providesBytesOfBitmap() { byte[] data = concurrency.get( Glide.with(context) .as(byte[].class) .load(ResourceIds.raw.canonical) .submit()); assertThat(data).isNotNull(); assertThat(BitmapFactory.decodeByteArray(data, 0, data.length)).isNotNull(); }