Commit 82da627c by olly Committed by Oliver Woodman

Split out shared database components for reuse in caching

PiperOrigin-RevId: 229946997
parent 02dc937c
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.database;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
/**
* Provides {@link SQLiteDatabase} instances to ExoPlayer components, which may read and write
* tables prefixed with {@link #TABLE_PREFIX}.
*/
public interface DatabaseProvider {
/** Prefix for tables that can be read and written by ExoPlayer components. */
String TABLE_PREFIX = "ExoPlayer";
/**
* Creates and/or opens a database that will be used for reading and writing.
*
* <p>Once opened successfully, the database is cached, so you can call this method every time you
* need to write to the database. Errors such as bad permissions or a full disk may cause this
* method to fail, but future attempts may succeed if the problem is fixed.
*
* @throws SQLiteException If the database cannot be opened for writing.
* @return A read/write database object.
*/
SQLiteDatabase getWritableDatabase();
/**
* Creates and/or opens a database. This will be the same object returned by {@link
* #getWritableDatabase()} unless some problem, such as a full disk, requires the database to be
* opened read-only. In that case, a read-only database object will be returned. If the problem is
* fixed, a future call to {@link #getWritableDatabase()} may succeed, in which case the read-only
* database object will be closed and the read/write object will be returned in the future.
*
* <p>Once opened successfully, the database is cached, so you can call this method every time you
* need to read from the database.
*
* @throws SQLiteException If the database cannot be opened.
* @return A database object valid until {@link #getWritableDatabase()} is called.
*/
SQLiteDatabase getReadableDatabase();
}
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.database;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/** A {@link DatabaseProvider} that provides instances obtained from a {@link SQLiteOpenHelper}. */
public final class DefaultDatabaseProvider implements DatabaseProvider {
private final SQLiteOpenHelper sqliteOpenHelper;
/**
* @param sqliteOpenHelper An {@link SQLiteOpenHelper} from which to obtain database instances.
*/
public DefaultDatabaseProvider(SQLiteOpenHelper sqliteOpenHelper) {
this.sqliteOpenHelper = sqliteOpenHelper;
}
@Override
public SQLiteDatabase getWritableDatabase() {
return sqliteOpenHelper.getWritableDatabase();
}
@Override
public SQLiteDatabase getReadableDatabase() {
return sqliteOpenHelper.getReadableDatabase();
}
}
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* An {@link SQLiteOpenHelper} that provides instances of a standalone ExoPlayer database.
*
* <p>Suitable for use by applications that do not already have their own database, or which would
* prefer to keep ExoPlayer tables isolated in their own database. Other applications should prefer
* to use {@link DefaultDatabaseProvider} with their own {@link SQLiteOpenHelper}.
*/
public final class ExoDatabaseProvider extends SQLiteOpenHelper implements DatabaseProvider {
/** The file name used for the standalone ExoPlayer database. */
public static final String DATABASE_NAME = "exoplayer_internal.db";
private static final int VERSION = 1;
public ExoDatabaseProvider(Context context) {
super(context.getApplicationContext(), DATABASE_NAME, /* factory= */ null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Features create their own tables.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Features handle their own upgrades.
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO: Wipe the database.
super.onDowngrade(db, oldVersion, newVersion);
}
}
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.database;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.IntDef;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* A table that holds version information about other ExoPlayer tables. This allows ExoPlayer tables
* to be versioned independently to the version of the containing database.
*/
public final class VersionTable {
/** Returned by {@link #getVersion(int)} if the version is unset. */
public static final int VERSION_UNSET = -1;
/** Version of tables used for offline functionality. */
public static final int FEATURE_OFFLINE = 0;
/** Version of tables used for cache functionality. */
public static final int FEATURE_CACHE = 1;
private static final String TABLE_NAME = DatabaseProvider.TABLE_PREFIX + "Versions";
private static final String COLUMN_FEATURE = "feature";
private static final String COLUMN_VERSION = "version";
private static final String SQL_CREATE_TABLE_IF_NOT_EXISTS =
"CREATE TABLE IF NOT EXISTS "
+ TABLE_NAME
+ " ("
+ COLUMN_FEATURE
+ " INTEGER PRIMARY KEY NOT NULL,"
+ COLUMN_VERSION
+ " INTEGER NOT NULL)";
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef({FEATURE_OFFLINE, FEATURE_CACHE})
private @interface Feature {}
private final DatabaseProvider databaseProvider;
public VersionTable(DatabaseProvider databaseProvider) {
this.databaseProvider = databaseProvider;
// Check whether the table exists to avoid getting a writable database if we don't need one.
if (!doesTableExist(databaseProvider, TABLE_NAME)) {
databaseProvider.getWritableDatabase().execSQL(SQL_CREATE_TABLE_IF_NOT_EXISTS);
}
}
/**
* Sets the version of tables belonging to the specified feature.
*
* @param feature The feature.
* @param version The version.
*/
public void setVersion(@Feature int feature, int version) {
ContentValues values = new ContentValues();
values.put(COLUMN_FEATURE, feature);
values.put(COLUMN_VERSION, version);
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.replace(TABLE_NAME, /* nullColumnHack= */ null, values);
}
/**
* Returns the version of tables belonging to the specified feature, or {@link #VERSION_UNSET} if
* no version information is available.
*/
public int getVersion(@Feature int feature) {
String selection = COLUMN_FEATURE + " = ?";
String[] selectionArgs = {Integer.toString(feature)};
try (Cursor cursor =
databaseProvider
.getReadableDatabase()
.query(
TABLE_NAME,
new String[] {COLUMN_VERSION},
selection,
selectionArgs,
/* groupBy= */ null,
/* having= */ null,
/* orderBy= */ null)) {
if (cursor.getCount() == 0) {
return VERSION_UNSET;
}
cursor.moveToNext();
return cursor.getInt(/* COLUMN_VERSION index */ 0);
}
}
/* package */ static boolean doesTableExist(DatabaseProvider databaseProvider, String tableName) {
SQLiteDatabase readableDatabase = databaseProvider.getReadableDatabase();
long count =
DatabaseUtils.queryNumEntries(
readableDatabase, "sqlite_master", "tbl_name = ?", new String[] {tableName});
return count > 0;
}
}
...@@ -19,8 +19,6 @@ import android.support.annotation.Nullable; ...@@ -19,8 +19,6 @@ import android.support.annotation.Nullable;
/** Persists {@link DownloadState}s. */ /** Persists {@link DownloadState}s. */
interface DownloadIndex { interface DownloadIndex {
/** Releases the used resources. */
void release();
/** /**
* Returns the {@link DownloadState} with the given {@code id}, or null. * Returns the {@link DownloadState} with the given {@code id}, or null.
......
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.database;
import static com.google.android.exoplayer2.database.VersionTable.FEATURE_CACHE;
import static com.google.android.exoplayer2.database.VersionTable.FEATURE_OFFLINE;
import static com.google.common.truth.Truth.assertThat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
/** Unit tests for {@link VersionTable}. */
@RunWith(RobolectricTestRunner.class)
public class VersionTableTest {
private ExoDatabaseProvider databaseProvider;
@Before
public void setUp() {
databaseProvider = new ExoDatabaseProvider(RuntimeEnvironment.application);
}
@After
public void tearDown() {
databaseProvider.close();
}
@Test
public void getVersion_nonExistingTable_returnsVersionUnset() {
VersionTable versionTable = new VersionTable(databaseProvider);
int version = versionTable.getVersion(FEATURE_OFFLINE);
assertThat(version).isEqualTo(VersionTable.VERSION_UNSET);
}
@Test
public void getVersion_returnsSetVersion() {
VersionTable versionTable = new VersionTable(databaseProvider);
versionTable.setVersion(FEATURE_OFFLINE, 1);
assertThat(versionTable.getVersion(FEATURE_OFFLINE)).isEqualTo(1);
versionTable.setVersion(FEATURE_OFFLINE, 10);
assertThat(versionTable.getVersion(FEATURE_OFFLINE)).isEqualTo(10);
versionTable.setVersion(FEATURE_CACHE, 5);
assertThat(versionTable.getVersion(FEATURE_CACHE)).isEqualTo(5);
assertThat(versionTable.getVersion(FEATURE_OFFLINE)).isEqualTo(10);
}
@Test
public void doesTableExist_nonExistingTable_returnsFalse() {
assertThat(VersionTable.doesTableExist(databaseProvider, "NonExistingTable")).isFalse();
}
@Test
public void doesTableExist_existingTable_returnsTrue() {
String table = "TestTable";
databaseProvider.getWritableDatabase().execSQL("CREATE TABLE " + table + " (dummy INTEGER)");
assertThat(VersionTable.doesTableExist(databaseProvider, table)).isTrue();
}
}
...@@ -15,15 +15,13 @@ ...@@ -15,15 +15,13 @@
*/ */
package com.google.android.exoplayer2.offline; package com.google.android.exoplayer2.offline;
import static com.google.android.exoplayer2.offline.DefaultDownloadIndex.VersionTable.FEATURE_CACHE;
import static com.google.android.exoplayer2.offline.DefaultDownloadIndex.VersionTable.FEATURE_OFFLINE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.database.VersionTable;
import java.util.Arrays; import java.util.Arrays;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
...@@ -36,16 +34,18 @@ import org.robolectric.RuntimeEnvironment; ...@@ -36,16 +34,18 @@ import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class DefaultDownloadIndexTest { public class DefaultDownloadIndexTest {
private ExoDatabaseProvider databaseProvider;
private DefaultDownloadIndex downloadIndex; private DefaultDownloadIndex downloadIndex;
@Before @Before
public void setUp() { public void setUp() {
downloadIndex = new DefaultDownloadIndex(RuntimeEnvironment.application); databaseProvider = new ExoDatabaseProvider(RuntimeEnvironment.application);
downloadIndex = new DefaultDownloadIndex(databaseProvider);
} }
@After @After
public void tearDown() { public void tearDown() {
downloadIndex.release(); databaseProvider.close();
} }
@Test @Test
...@@ -99,29 +99,12 @@ public class DefaultDownloadIndexTest { ...@@ -99,29 +99,12 @@ public class DefaultDownloadIndexTest {
public void releaseAndRecreateDownloadIndex_returnsTheSameDownloadState() { public void releaseAndRecreateDownloadIndex_returnsTheSameDownloadState() {
String id = "id"; String id = "id";
DownloadState downloadState = new DownloadStateBuilder(id).build(); DownloadState downloadState = new DownloadStateBuilder(id).build();
downloadIndex.putDownloadState(downloadState); downloadIndex.putDownloadState(downloadState);
downloadIndex.release();
downloadIndex = new DefaultDownloadIndex(RuntimeEnvironment.application);
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
assertThat(readDownloadState).isNotNull();
assertEqual(readDownloadState, downloadState);
}
@Test
public void customDatabaseProvider_getDownloadStateReturnsNull() {
String id = "id";
DownloadState downloadState = new DownloadStateBuilder(id).build();
downloadIndex.putDownloadState(downloadState);
downloadIndex.release();
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl();
downloadIndex = new DefaultDownloadIndex(databaseProvider); downloadIndex = new DefaultDownloadIndex(databaseProvider);
DownloadState readDownloadState = downloadIndex.getDownloadState(id); DownloadState readDownloadState = downloadIndex.getDownloadState(id);
assertThat(readDownloadState).isNotNull();
assertThat(readDownloadState).isNull(); assertEqual(readDownloadState, downloadState);
databaseProvider.close();
} }
@Test @Test
...@@ -134,10 +117,9 @@ public class DefaultDownloadIndexTest { ...@@ -134,10 +117,9 @@ public class DefaultDownloadIndexTest {
String id = "id"; String id = "id";
DownloadState downloadState = new DownloadStateBuilder(id).build(); DownloadState downloadState = new DownloadStateBuilder(id).build();
downloadIndex.putDownloadState(downloadState); downloadIndex.putDownloadState(downloadState);
downloadIndex.removeDownloadState(id); downloadIndex.removeDownloadState(id);
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
DownloadState readDownloadState = downloadIndex.getDownloadState(id);
assertThat(readDownloadState).isNull(); assertThat(readDownloadState).isNull();
} }
...@@ -160,6 +142,7 @@ public class DefaultDownloadIndexTest { ...@@ -160,6 +142,7 @@ public class DefaultDownloadIndexTest {
assertEqual(cursor.getDownloadState(), downloadState2); assertEqual(cursor.getDownloadState(), downloadState2);
cursor.moveToNext(); cursor.moveToNext();
assertEqual(cursor.getDownloadState(), downloadState1); assertEqual(cursor.getDownloadState(), downloadState1);
cursor.close();
} }
@Test @Test
...@@ -191,111 +174,39 @@ public class DefaultDownloadIndexTest { ...@@ -191,111 +174,39 @@ public class DefaultDownloadIndexTest {
assertEqual(cursor.getDownloadState(), downloadState1); assertEqual(cursor.getDownloadState(), downloadState1);
cursor.moveToNext(); cursor.moveToNext();
assertEqual(cursor.getDownloadState(), downloadState3); assertEqual(cursor.getDownloadState(), downloadState3);
cursor.close();
} }
@Test @Test
public void doesTableExist_nonExistingTable_returnsFalse() { public void putDownloadState_setsVersion() {
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl(); VersionTable versionTable = new VersionTable(databaseProvider);
assertThat(versionTable.getVersion(VersionTable.FEATURE_OFFLINE))
assertThat(DefaultDownloadIndex.doesTableExist(databaseProvider, "NonExistingTable")).isFalse(); .isEqualTo(VersionTable.VERSION_UNSET);
databaseProvider.close();
}
@Test
public void doesTableExist_existingTable_returnsTrue() {
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl();
String tableName = "ExistingTable";
databaseProvider.getWritableDatabase().execSQL("CREATE TABLE " + tableName + "(dummy)");
assertThat(DefaultDownloadIndex.doesTableExist(databaseProvider, tableName)).isTrue();
databaseProvider.close();
}
@Test
public void getVersion_nonExistingTable_returnsZero() {
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl();
DefaultDownloadIndex.VersionTable versionTable =
new DefaultDownloadIndex.VersionTable(databaseProvider);
int version = versionTable.getVersion(FEATURE_OFFLINE);
assertThat(version).isEqualTo(0);
databaseProvider.close();
}
@Test
public void getVersion_returnsSetVersion() {
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl();
DefaultDownloadIndex.VersionTable versionTable =
new DefaultDownloadIndex.VersionTable(databaseProvider);
versionTable.setVersion(FEATURE_OFFLINE, 1); downloadIndex.putDownloadState(new DownloadStateBuilder("id1").build());
assertThat(versionTable.getVersion(FEATURE_OFFLINE)).isEqualTo(1);
versionTable.setVersion(FEATURE_OFFLINE, 10); assertThat(versionTable.getVersion(VersionTable.FEATURE_OFFLINE))
assertThat(versionTable.getVersion(FEATURE_OFFLINE)).isEqualTo(10); .isEqualTo(DefaultDownloadIndex.TABLE_VERSION);
versionTable.setVersion(FEATURE_CACHE, 5);
assertThat(versionTable.getVersion(FEATURE_CACHE)).isEqualTo(5);
assertThat(versionTable.getVersion(FEATURE_OFFLINE)).isEqualTo(10);
databaseProvider.close();
} }
@Test @Test
public void downloadStateTableConstructor_noTable_createsTable() { public void downloadIndex_versionDowngradeWipesData() {
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl(); DownloadState downloadState1 = new DownloadStateBuilder("id1").build();
assertThat( downloadIndex.putDownloadState(downloadState1);
DefaultDownloadIndex.doesTableExist( DownloadStateCursor cursor = downloadIndex.getDownloadStates();
databaseProvider, DefaultDownloadIndex.DownloadStateTable.TABLE_NAME)) assertThat(cursor.getCount()).isEqualTo(1);
.isFalse(); cursor.close();
new DefaultDownloadIndex.DownloadStateTable(databaseProvider);
assertThat(
DefaultDownloadIndex.doesTableExist(
databaseProvider, DefaultDownloadIndex.DownloadStateTable.TABLE_NAME))
.isTrue();
databaseProvider.close();
}
@Test
public void downloadStateTableConstructor_versionZero_versionSet() {
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl();
new DefaultDownloadIndex.DownloadStateTable(databaseProvider);
DefaultDownloadIndex.VersionTable versionTable = VersionTable versionTable = new VersionTable(databaseProvider);
new DefaultDownloadIndex.VersionTable(databaseProvider); versionTable.setVersion(VersionTable.FEATURE_OFFLINE, Integer.MAX_VALUE);
assertThat(versionTable.getVersion(FEATURE_OFFLINE))
.isEqualTo(DefaultDownloadIndex.DownloadStateTable.TABLE_VERSION);
databaseProvider.close();
}
@Test downloadIndex = new DefaultDownloadIndex(databaseProvider);
public void downloadStateTableConstructor_greaterVersion_tableRecreated() {
DatabaseProviderImpl databaseProvider = new DatabaseProviderImpl();
databaseProvider
.getWritableDatabase()
.execSQL("CREATE TABLE " + DefaultDownloadIndex.DownloadStateTable.TABLE_NAME + "(dummy)");
DefaultDownloadIndex.VersionTable versionTable =
new DefaultDownloadIndex.VersionTable(databaseProvider);
versionTable.setVersion(FEATURE_OFFLINE, Integer.MAX_VALUE);
DefaultDownloadIndex.DownloadStateTable downloadStateTable =
new DefaultDownloadIndex.DownloadStateTable(databaseProvider);
String id = "id";
DownloadState downloadState = new DownloadStateBuilder(id).build();
downloadStateTable.replace(downloadState);
DownloadState readDownloadState = downloadStateTable.get(id);
assertEqual(readDownloadState, downloadState);
assertThat(versionTable.getVersion(FEATURE_OFFLINE)) cursor = downloadIndex.getDownloadStates();
.isEqualTo(DefaultDownloadIndex.DownloadStateTable.TABLE_VERSION); assertThat(cursor.getCount()).isEqualTo(0);
databaseProvider.close(); cursor.close();
assertThat(versionTable.getVersion(VersionTable.FEATURE_OFFLINE))
.isEqualTo(DefaultDownloadIndex.TABLE_VERSION);
} }
private static void assertEqual(DownloadState downloadState, DownloadState expected) { private static void assertEqual(DownloadState downloadState, DownloadState expected) {
...@@ -468,24 +379,4 @@ public class DefaultDownloadIndexTest { ...@@ -468,24 +379,4 @@ public class DefaultDownloadIndexTest {
customMetadata); customMetadata);
} }
} }
private static final class DatabaseProviderImpl extends SQLiteOpenHelper
implements DefaultDownloadIndex.DatabaseProvider {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "TestExoPlayerDownloadIndex.db";
public DatabaseProviderImpl() {
super(RuntimeEnvironment.application, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Do nothing.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Do nothing.
}
}
} }
...@@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_DASH; ...@@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_DASH;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.net.Uri; import android.net.Uri;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
...@@ -34,18 +35,20 @@ import org.robolectric.RuntimeEnvironment; ...@@ -34,18 +35,20 @@ import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class DownloadIndexUtilTest { public class DownloadIndexUtilTest {
private DefaultDownloadIndex downloadIndex;
private File tempFile; private File tempFile;
private ExoDatabaseProvider databaseProvider;
private DefaultDownloadIndex downloadIndex;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
tempFile = Util.createTempFile(RuntimeEnvironment.application, "ExoPlayerTest"); tempFile = Util.createTempFile(RuntimeEnvironment.application, "ExoPlayerTest");
downloadIndex = new DefaultDownloadIndex(RuntimeEnvironment.application); databaseProvider = new ExoDatabaseProvider(RuntimeEnvironment.application);
downloadIndex = new DefaultDownloadIndex(databaseProvider);
} }
@After @After
public void tearDown() { public void tearDown() {
downloadIndex.release(); databaseProvider.close();
tempFile.delete(); tempFile.delete();
} }
......
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