1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.exoplayer2.database;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.database.sqlite.SQLiteDatabase;
21 import androidx.test.ext.junit.runners.AndroidJUnit4;
22 import com.google.android.exoplayer2.testutil.TestUtil;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 
27 /** Unit tests for {@link VersionTable}. */
28 @RunWith(AndroidJUnit4.class)
29 public class VersionTableTest {
30 
31   private static final int FEATURE_1 = 1;
32   private static final int FEATURE_2 = 2;
33   private static final String INSTANCE_1 = "1";
34   private static final String INSTANCE_2 = "2";
35 
36   private DatabaseProvider databaseProvider;
37   private SQLiteDatabase database;
38 
39   @Before
setUp()40   public void setUp() {
41     databaseProvider = TestUtil.getInMemoryDatabaseProvider();
42     database = databaseProvider.getWritableDatabase();
43   }
44 
45   @Test
getVersion_unsetFeature_returnsVersionUnset()46   public void getVersion_unsetFeature_returnsVersionUnset() throws DatabaseIOException {
47     int version = VersionTable.getVersion(database, FEATURE_1, INSTANCE_1);
48     assertThat(version).isEqualTo(VersionTable.VERSION_UNSET);
49   }
50 
51   @Test
getVersion_unsetVersion_returnsVersionUnset()52   public void getVersion_unsetVersion_returnsVersionUnset() throws DatabaseIOException {
53     VersionTable.setVersion(database, FEATURE_1, INSTANCE_1, 1);
54     int version = VersionTable.getVersion(database, FEATURE_1, INSTANCE_2);
55     assertThat(version).isEqualTo(VersionTable.VERSION_UNSET);
56   }
57 
58   @Test
getVersion_returnsSetVersion()59   public void getVersion_returnsSetVersion() throws DatabaseIOException {
60     VersionTable.setVersion(database, FEATURE_1, INSTANCE_1, 1);
61     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_1)).isEqualTo(1);
62 
63     VersionTable.setVersion(database, FEATURE_1, INSTANCE_1, 2);
64     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_1)).isEqualTo(2);
65 
66     VersionTable.setVersion(database, FEATURE_2, INSTANCE_1, 3);
67     assertThat(VersionTable.getVersion(database, FEATURE_2, INSTANCE_1)).isEqualTo(3);
68     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_1)).isEqualTo(2);
69 
70     VersionTable.setVersion(database, FEATURE_2, INSTANCE_2, 4);
71     assertThat(VersionTable.getVersion(database, FEATURE_2, INSTANCE_2)).isEqualTo(4);
72     assertThat(VersionTable.getVersion(database, FEATURE_2, INSTANCE_1)).isEqualTo(3);
73     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_1)).isEqualTo(2);
74   }
75 
76   @Test
removeVersion_removesSetVersion()77   public void removeVersion_removesSetVersion() throws DatabaseIOException {
78     VersionTable.setVersion(database, FEATURE_1, INSTANCE_1, 1);
79     VersionTable.setVersion(database, FEATURE_1, INSTANCE_2, 2);
80     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_1)).isEqualTo(1);
81     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_2)).isEqualTo(2);
82 
83     VersionTable.removeVersion(database, FEATURE_1, INSTANCE_1);
84     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_1))
85         .isEqualTo(VersionTable.VERSION_UNSET);
86     assertThat(VersionTable.getVersion(database, FEATURE_1, INSTANCE_2)).isEqualTo(2);
87   }
88 
89   @Test
doesTableExist_nonExistingTable_returnsFalse()90   public void doesTableExist_nonExistingTable_returnsFalse() {
91     assertThat(VersionTable.tableExists(database, "NonExistingTable")).isFalse();
92   }
93 
94   @Test
doesTableExist_existingTable_returnsTrue()95   public void doesTableExist_existingTable_returnsTrue() {
96     String table = "TestTable";
97     databaseProvider.getWritableDatabase().execSQL("CREATE TABLE " + table + " (dummy INTEGER)");
98     assertThat(VersionTable.tableExists(database, table)).isTrue();
99   }
100 }
101