Commit 3191afe8 by christosts Committed by Oliver Woodman

Common tests do not depend on testutils

This commit duplicates some code from the testutils module
in common test in order to break the dependency from testutils.

PiperOrigin-RevId: 322366013
parent 6ace2c94
......@@ -18,12 +18,15 @@ android.buildTypes.debug.testCoverageEnabled true
dependencies {
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
implementation 'com.google.guava:guava:' + guavaVersion
implementation 'org.checkerframework:checker-qual:' + checkerframeworkVersion
compileOnly 'com.google.code.findbugs:jsr305:' + jsr305Version
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion
compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkCompatVersion
compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion
testImplementation project(modulePrefix + 'library-core')
testImplementation project(modulePrefix + 'testutils')
testImplementation 'org.mockito:mockito-core:' + mockitoVersion
testImplementation 'androidx.test:core:' + androidxTestCoreVersion
testImplementation 'androidx.test.ext:junit:' + androidxTestJUnitVersion
testImplementation 'junit:junit:' + junitVersion
testImplementation 'com.google.truth:truth:' + truthVersion
testImplementation 'org.robolectric:robolectric:' + robolectricVersion
}
......
......@@ -27,11 +27,11 @@ import com.google.android.exoplayer2.drm.ExoMediaCrypto;
import com.google.android.exoplayer2.drm.UnsupportedMediaCrypto;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame;
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.video.ColorInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -71,11 +71,9 @@ public final class FormatTest {
initializationData.add(initData2);
DrmInitData.SchemeData drmData1 =
new DrmInitData.SchemeData(
WIDEVINE_UUID, VIDEO_MP4, TestUtil.buildTestData(128, 1 /* data seed */));
new DrmInitData.SchemeData(WIDEVINE_UUID, VIDEO_MP4, buildTestData(128, 1 /* data seed */));
DrmInitData.SchemeData drmData2 =
new DrmInitData.SchemeData(
C.UUID_NIL, VIDEO_WEBM, TestUtil.buildTestData(128, 1 /* data seed */));
new DrmInitData.SchemeData(C.UUID_NIL, VIDEO_WEBM, buildTestData(128, 1 /* data seed */));
DrmInitData drmInitData = new DrmInitData(drmData1, drmData2);
byte[] projectionData = new byte[] {1, 2, 3};
......@@ -124,4 +122,12 @@ public final class FormatTest {
/* accessibilityChannel= */ 2,
/* exoMediaCryptoType= */ ExoMediaCrypto.class);
}
/** Generates an array of random bytes with the specified length. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] buildTestData(int length, int seed) {
byte[] source = new byte[length];
new Random(seed).nextBytes(source);
return source;
}
}
......@@ -25,9 +25,9 @@ import android.os.Parcel;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.drm.DrmInitData.SchemeData;
import com.google.android.exoplayer2.testutil.TestUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -35,16 +35,16 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class DrmInitDataTest {
private static final SchemeData DATA_1 = new SchemeData(WIDEVINE_UUID, VIDEO_MP4,
TestUtil.buildTestData(128, 1 /* data seed */));
private static final SchemeData DATA_2 = new SchemeData(PLAYREADY_UUID, VIDEO_MP4,
TestUtil.buildTestData(128, 2 /* data seed */));
private static final SchemeData DATA_1B = new SchemeData(WIDEVINE_UUID, VIDEO_MP4,
TestUtil.buildTestData(128, 1 /* data seed */));
private static final SchemeData DATA_2B = new SchemeData(PLAYREADY_UUID, VIDEO_MP4,
TestUtil.buildTestData(128, 2 /* data seed */));
private static final SchemeData DATA_UNIVERSAL = new SchemeData(C.UUID_NIL, VIDEO_MP4,
TestUtil.buildTestData(128, 3 /* data seed */));
private static final SchemeData DATA_1 =
new SchemeData(WIDEVINE_UUID, VIDEO_MP4, buildTestData(128, 1 /* data seed */));
private static final SchemeData DATA_2 =
new SchemeData(PLAYREADY_UUID, VIDEO_MP4, buildTestData(128, 2 /* data seed */));
private static final SchemeData DATA_1B =
new SchemeData(WIDEVINE_UUID, VIDEO_MP4, buildTestData(128, 1 /* data seed */));
private static final SchemeData DATA_2B =
new SchemeData(PLAYREADY_UUID, VIDEO_MP4, buildTestData(128, 2 /* data seed */));
private static final SchemeData DATA_UNIVERSAL =
new SchemeData(C.UUID_NIL, VIDEO_MP4, buildTestData(128, 3 /* data seed */));
@Test
public void parcelable() {
......@@ -162,4 +162,11 @@ public class DrmInitDataTest {
return schemeDatas;
}
/** Generates an array of random bytes with the specified length. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] buildTestData(int length, int seed) {
byte[] source = new byte[length];
new Random(seed).nextBytes(source);
return source;
}
}
......@@ -15,15 +15,15 @@
*/
package com.google.android.exoplayer2.metadata.emsg;
import static com.google.android.exoplayer2.testutil.TestUtil.createByteArray;
import static com.google.android.exoplayer2.testutil.TestUtil.createMetadataInputBuffer;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
import com.google.android.exoplayer2.util.Assertions;
import com.google.common.primitives.Bytes;
import java.nio.ByteBuffer;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -80,4 +80,27 @@ public final class EventMessageDecoderTest {
assertThrows(IllegalArgumentException.class, () -> decoder.decode(buffer));
}
/** Converts an array of integers in the range [0, 255] into an equivalent byte array. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] createByteArray(int... bytes) {
byte[] byteArray = new byte[bytes.length];
for (int i = 0; i < byteArray.length; i++) {
Assertions.checkState(0x00 <= bytes[i] && bytes[i] <= 0xFF);
byteArray[i] = (byte) bytes[i];
}
return byteArray;
}
/**
* Create a new {@link MetadataInputBuffer} and copy {@code data} into the backing {@link
* ByteBuffer}.
*/
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static MetadataInputBuffer createMetadataInputBuffer(byte[] data) {
MetadataInputBuffer buffer = new MetadataInputBuffer();
buffer.data = ByteBuffer.allocate(data.length).put(data);
buffer.data.flip();
return buffer;
}
}
......@@ -15,15 +15,15 @@
*/
package com.google.android.exoplayer2.metadata.emsg;
import static com.google.android.exoplayer2.testutil.TestUtil.createByteArray;
import static com.google.android.exoplayer2.testutil.TestUtil.createMetadataInputBuffer;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
import com.google.android.exoplayer2.util.Assertions;
import com.google.common.primitives.Bytes;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -78,4 +78,26 @@ public final class EventMessageEncoderTest {
assertThat(encodedByteArray1).isEqualTo(expectedEmsgBody1);
}
/** Converts an array of integers in the range [0, 255] into an equivalent byte array. */
// TODO(internal b/161776534): Move to a single file.
private static byte[] createByteArray(int... bytes) {
byte[] byteArray = new byte[bytes.length];
for (int i = 0; i < byteArray.length; i++) {
Assertions.checkState(0x00 <= bytes[i] && bytes[i] <= 0xFF);
byteArray[i] = (byte) bytes[i];
}
return byteArray;
}
/**
* Create a new {@link MetadataInputBuffer} and copy {@code data} into the backing {@link
* ByteBuffer}.
*/
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static MetadataInputBuffer createMetadataInputBuffer(byte[] data) {
MetadataInputBuffer buffer = new MetadataInputBuffer();
buffer.data = ByteBuffer.allocate(data.length).put(data);
buffer.data.flip();
return buffer;
}
}
......@@ -15,8 +15,6 @@
*/
package com.google.android.exoplayer2.metadata.id3;
import static com.google.android.exoplayer2.testutil.TestUtil.createByteArray;
import static com.google.android.exoplayer2.testutil.TestUtil.createMetadataInputBuffer;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
......@@ -25,6 +23,7 @@ import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.metadata.MetadataInputBuffer;
import com.google.android.exoplayer2.util.Assertions;
import com.google.common.base.Charsets;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -317,4 +316,27 @@ public final class Id3DecoderTest {
this.frameData = frameData;
}
}
/** Converts an array of integers in the range [0, 255] into an equivalent byte array. */
// TODO(internal b/161776534): Move to a single file.
private static byte[] createByteArray(int... bytes) {
byte[] byteArray = new byte[bytes.length];
for (int i = 0; i < byteArray.length; i++) {
Assertions.checkState(0x00 <= bytes[i] && bytes[i] <= 0xFF);
byteArray[i] = (byte) bytes[i];
}
return byteArray;
}
/**
* Create a new {@link MetadataInputBuffer} and copy {@code data} into the backing {@link
* ByteBuffer}.
*/
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static MetadataInputBuffer createMetadataInputBuffer(byte[] data) {
MetadataInputBuffer buffer = new MetadataInputBuffer();
buffer.data = ByteBuffer.allocate(data.length).put(data);
buffer.data.flip();
return buffer;
}
}
......@@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.util;
import static com.google.android.exoplayer2.testutil.TestUtil.createByteArray;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
......@@ -210,4 +209,14 @@ public final class NalUnitUtilTest {
assertThat(Arrays.copyOf(buffer.array(), buffer.position())).isEqualTo(expectedOutputBitstream);
}
/** Converts an array of integers in the range [0, 255] into an equivalent byte array. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] createByteArray(int... bytes) {
byte[] byteArray = new byte[bytes.length];
for (int i = 0; i < byteArray.length; i++) {
Assertions.checkState(0x00 <= bytes[i] && bytes[i] <= 0xFF);
byteArray[i] = (byte) bytes[i];
}
return byteArray;
}
}
......@@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.common.base.Charsets;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -30,7 +29,7 @@ public final class ParsableBitArrayTest {
@Test
public void readAllBytes() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F);
ParsableBitArray testArray = new ParsableBitArray(testData);
byte[] bytesRead = new byte[testData.length];
......@@ -43,7 +42,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitInSameByte() {
byte[] testData = TestUtil.createByteArray(0, 0b00110000);
byte[] testData = createByteArray(0, 0b00110000);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.setPosition(10);
......@@ -55,7 +54,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitInMultipleBytes() {
byte[] testData = TestUtil.createByteArray(1, 1 << 7);
byte[] testData = createByteArray(1, 1 << 7);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.setPosition(6);
......@@ -67,7 +66,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBits0Bits() {
byte[] testData = TestUtil.createByteArray(0x3C);
byte[] testData = createByteArray(0x3C);
ParsableBitArray testArray = new ParsableBitArray(testData);
int result = testArray.readBits(0);
......@@ -77,7 +76,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.readBits(8);
......@@ -89,7 +88,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsNonByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.readBits(3);
......@@ -101,7 +100,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsNegativeValue() {
byte[] testData = TestUtil.createByteArray(0xF0, 0, 0, 0);
byte[] testData = createByteArray(0xF0, 0, 0, 0);
ParsableBitArray testArray = new ParsableBitArray(testData);
int result = testArray.readBits(32);
......@@ -111,7 +110,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsToLong0Bits() {
byte[] testData = TestUtil.createByteArray(0x3C);
byte[] testData = createByteArray(0x3C);
ParsableBitArray testArray = new ParsableBitArray(testData);
long result = testArray.readBitsToLong(0);
......@@ -121,7 +120,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsToLongByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01, 0xFF, 0x14, 0x60);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01, 0xFF, 0x14, 0x60);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.readBits(8);
......@@ -133,7 +132,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsToLongNonByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01, 0xFF, 0x14, 0x60);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01, 0xFF, 0x14, 0x60);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.readBits(3);
......@@ -145,7 +144,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsToLongNegativeValue() {
byte[] testData = TestUtil.createByteArray(0xF0, 0, 0, 0, 0, 0, 0, 0);
byte[] testData = createByteArray(0xF0, 0, 0, 0, 0, 0, 0, 0);
ParsableBitArray testArray = new ParsableBitArray(testData);
long result = testArray.readBitsToLong(64);
......@@ -155,7 +154,7 @@ public final class ParsableBitArrayTest {
@Test
public void readBitsToByteArray() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01, 0xFF, 0x14, 0x60, 0x99);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01, 0xFF, 0x14, 0x60, 0x99);
ParsableBitArray testArray = new ParsableBitArray(testData);
int numBytes = testData.length;
......@@ -204,7 +203,7 @@ public final class ParsableBitArrayTest {
@Test
public void skipBytes() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.skipBytes(2);
......@@ -214,7 +213,7 @@ public final class ParsableBitArrayTest {
@Test
public void skipBitsByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.skipBits(16);
......@@ -224,7 +223,7 @@ public final class ParsableBitArrayTest {
@Test
public void skipBitsNonByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.skipBits(5);
......@@ -234,7 +233,7 @@ public final class ParsableBitArrayTest {
@Test
public void setPositionByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.setPosition(16);
......@@ -244,7 +243,7 @@ public final class ParsableBitArrayTest {
@Test
public void setPositionNonByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.setPosition(5);
......@@ -254,7 +253,7 @@ public final class ParsableBitArrayTest {
@Test
public void byteAlignFromNonByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.setPosition(11);
......@@ -267,7 +266,7 @@ public final class ParsableBitArrayTest {
@Test
public void byteAlignFromByteAligned() {
byte[] testData = TestUtil.createByteArray(0x3C, 0xD2, 0x5F, 0x01);
byte[] testData = createByteArray(0x3C, 0xD2, 0x5F, 0x01);
ParsableBitArray testArray = new ParsableBitArray(testData);
testArray.setPosition(16);
......@@ -363,8 +362,7 @@ public final class ParsableBitArrayTest {
@Test
public void noOverwriting() {
ParsableBitArray output =
new ParsableBitArray(TestUtil.createByteArray(0xFF, 0xFF, 0xFF, 0xFF, 0xFF));
ParsableBitArray output = new ParsableBitArray(createByteArray(0xFF, 0xFF, 0xFF, 0xFF, 0xFF));
output.setPosition(1);
output.putInt(0, 30);
......@@ -373,4 +371,14 @@ public final class ParsableBitArrayTest {
assertThat(output.readBits(32)).isEqualTo(0x80000001);
}
/** Converts an array of integers in the range [0, 255] into an equivalent byte array. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] createByteArray(int... bytes) {
byte[] byteArray = new byte[bytes.length];
for (int i = 0; i < byteArray.length; i++) {
Assertions.checkState(0x00 <= bytes[i] && bytes[i] <= 0xFF);
byteArray[i] = (byte) bytes[i];
}
return byteArray;
}
}
......@@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.util;
import static com.google.android.exoplayer2.testutil.TestUtil.createByteArray;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
......@@ -121,4 +120,14 @@ public final class ParsableNalUnitBitArrayTest {
assertThat(array.canReadBits(25)).isFalse();
}
/** Converts an array of integers in the range [0, 255] into an equivalent byte array. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] createByteArray(int... bytes) {
byte[] byteArray = new byte[bytes.length];
for (int i = 0; i < byteArray.length; i++) {
Assertions.checkState(0x00 <= bytes[i] && bytes[i] <= 0xFF);
byteArray[i] = (byte) bytes[i];
}
return byteArray;
}
}
......@@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.util;
import static com.google.android.exoplayer2.testutil.TestUtil.getInMemoryDatabaseProvider;
import static com.google.android.exoplayer2.util.Util.binarySearchCeil;
import static com.google.android.exoplayer2.util.Util.binarySearchFloor;
import static com.google.android.exoplayer2.util.Util.escapeFileName;
......@@ -26,13 +25,13 @@ import static com.google.android.exoplayer2.util.Util.unescapeFileName;
import static com.google.common.truth.Truth.assertThat;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.text.style.UnderlineSpan;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.TestUtil;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
......@@ -760,7 +759,7 @@ public class UtilTest {
@Test
public void toHexString_returnsHexString() {
byte[] bytes = TestUtil.createByteArray(0x12, 0xFC, 0x06);
byte[] bytes = createByteArray(0x12, 0xFC, 0x06);
assertThat(Util.toHexString(bytes)).isEqualTo("12fc06");
}
......@@ -807,7 +806,7 @@ public class UtilTest {
Random random = new Random(0);
for (int i = 0; i < 1000; i++) {
String string = TestUtil.buildTestString(1000, random);
String string = buildTestString(1000, random);
assertEscapeUnescapeFileName(string);
}
}
......@@ -862,7 +861,7 @@ public class UtilTest {
@Test
public void inflate_withDeflatedData_success() {
byte[] testData = TestUtil.buildTestData(/*arbitrary test data size*/ 256 * 1024);
byte[] testData = buildTestData(/*arbitrary test data size*/ 256 * 1024);
byte[] compressedData = new byte[testData.length * 2];
Deflater compresser = new Deflater(9);
compresser.setInput(testData);
......@@ -1007,7 +1006,7 @@ public class UtilTest {
@Test
public void tableExists_withExistingTable() {
SQLiteDatabase database = getInMemoryDatabaseProvider().getWritableDatabase();
SQLiteDatabase database = getInMemorySQLiteOpenHelper().getWritableDatabase();
database.execSQL("CREATE TABLE TestTable (ID INTEGER NOT NULL)");
assertThat(Util.tableExists(database, "TestTable")).isTrue();
......@@ -1015,7 +1014,7 @@ public class UtilTest {
@Test
public void tableExists_withNonExistingTable() {
SQLiteDatabase database = getInMemoryDatabaseProvider().getReadableDatabase();
SQLiteDatabase database = getInMemorySQLiteOpenHelper().getReadableDatabase();
assertThat(Util.tableExists(database, "table")).isFalse();
}
......@@ -1037,4 +1036,51 @@ public class UtilTest {
}
return longArray;
}
/** Returns a {@link SQLiteOpenHelper} that provides an in-memory database. */
private static SQLiteOpenHelper getInMemorySQLiteOpenHelper() {
return new SQLiteOpenHelper(
/* context= */ null, /* name= */ null, /* factory= */ null, /* version= */ 1) {
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
};
}
/** Generates an array of random bytes with the specified length. */
private static byte[] buildTestData(int length, int seed) {
byte[] source = new byte[length];
new Random(seed).nextBytes(source);
return source;
}
/** Equivalent to {@code buildTestData(length, length)}. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] buildTestData(int length) {
return buildTestData(length, length);
}
/** Generates a random string with the specified maximum length. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static String buildTestString(int maximumLength, Random random) {
int length = random.nextInt(maximumLength);
StringBuilder builder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
builder.append((char) random.nextInt());
}
return builder.toString();
}
/** Converts an array of integers in the range [0, 255] into an equivalent byte array. */
// TODO(internal b/161776534): Use TestUtils when it's available in a dependency we can use here.
private static byte[] createByteArray(int... bytes) {
byte[] byteArray = new byte[bytes.length];
for (int i = 0; i < byteArray.length; i++) {
Assertions.checkState(0x00 <= bytes[i] && bytes[i] <= 0xFF);
byteArray[i] = (byte) bytes[i];
}
return byteArray;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment