<lambda>null1 package com.android.launcher3.model
2 
3 import android.database.sqlite.SQLiteDatabase
4 import android.os.UserHandle
5 import androidx.test.ext.junit.runners.AndroidJUnit4
6 import androidx.test.filters.SmallTest
7 import androidx.test.platform.app.InstrumentationRegistry
8 import com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME
9 import com.android.launcher3.LauncherSettings.Favorites.TMP_TABLE
10 import com.android.launcher3.LauncherSettings.Favorites.addTableToDb
11 import com.android.launcher3.pm.UserCache
12 import com.android.launcher3.provider.LauncherDbUtils
13 import java.util.function.ToLongFunction
14 import org.junit.Assert.assertEquals
15 import org.junit.Assert.assertFalse
16 import org.junit.Test
17 import org.junit.runner.RunWith
18 
19 private const val INSERTION_SQL = "databases/v30_workspace_items.sql"
20 
21 private const val ICON_PACKAGE = "iconPackage"
22 private const val ICON_RESOURCE = "iconResource"
23 
24 @SmallTest
25 @RunWith(AndroidJUnit4::class)
26 class DatabaseHelperTest {
27 
28     /**
29      * b/304687723 occurred when a return was accidentally added to a case statement in
30      * DatabaseHelper.onUpgrade, which stopped the final data migration from successfully occurring.
31      * This test loads an in-memory db from a text file containing SQL statements, and then performs
32      * the migration on the db, and verifies that the correct columns have been deleted.
33      */
34     @Test
35     fun onUpgrade_to_version_32_from_30() {
36         val context = InstrumentationRegistry.getInstrumentation().targetContext
37         val userSerialProvider =
38             ToLongFunction<UserHandle> {
39                 UserCache.INSTANCE.get(context).getSerialNumberForUser(it)
40             }
41         val dbHelper = DatabaseHelper(context, null, userSerialProvider) {}
42         val db = FactitiousDbController(context, INSERTION_SQL).inMemoryDb
43 
44         dbHelper.onUpgrade(db, 30, 32)
45 
46         assertFalse(hasFavoritesColumn(db, ICON_PACKAGE))
47         assertFalse(hasFavoritesColumn(db, ICON_RESOURCE))
48     }
49 
50     /**
51      * b/304687723 causes a crash due to copying a table with 21 columns to a table with 19 columns.
52      * This test loads an in-memory db from a text file containing SQL statements, and then copies
53      * data from the created table into a temporary one, and verifies that no exception is thrown.
54      */
55     @Test
56     fun after_migrating_from_db_v30_to_v32_copy_table() {
57         val context = InstrumentationRegistry.getInstrumentation().targetContext
58         val db = FactitiousDbController(context, INSERTION_SQL).inMemoryDb // v30 - 21 columns
59 
60         addTableToDb(db, 1, true, TMP_TABLE)
61         LauncherDbUtils.copyTable(db, TABLE_NAME, db, TMP_TABLE, context)
62 
63         val c1 = db.query(TABLE_NAME, null, null, null, null, null, null)
64         val c2 = db.query(TMP_TABLE, null, null, null, null, null, null)
65 
66         assertEquals(21, c1.columnCount)
67         assertEquals(19, c2.columnCount)
68         assertEquals(c1.count, c2.count)
69 
70         c1.close()
71         c2.close()
72     }
73 
74     private fun hasFavoritesColumn(db: SQLiteDatabase, columnName: String): Boolean {
75         db.query(TABLE_NAME, null, null, null, null, null, null).use { c ->
76             return c.getColumnIndex(columnName) >= 0
77         }
78     }
79 }
80