Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 007cc880 authored by Prince's avatar Prince
Browse files

Adding spanY of a widget in the database

Fixes: 368053826
Test: atest CommunalBackupHelperTest CommunalWidgetRepositoryImplTest CommunalDatabaseMigrationsTest
Flag: NONE database migration
Change-Id: I884ca27950ff85f1f6cdac60b50c62e2c0601322
parent a0811c0e
Loading
Loading
Loading
Loading
+72 −4
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.communal.nano.CommunalHubState
import com.android.systemui.communal.shared.model.CommunalContentSize
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.lifecycle.InstantTaskExecutorRule
import com.google.common.truth.Truth.assertThat
@@ -102,7 +103,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                    widgetId = widgetId,
                    provider = provider,
                    rank = rank,
                    userSerialNumber = userSerialNumber
                    userSerialNumber = userSerialNumber,
                )
            }
            assertThat(widgets())
@@ -110,7 +111,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                    communalItemRankEntry1,
                    communalWidgetItemEntry1,
                    communalItemRankEntry2,
                    communalWidgetItemEntry2
                    communalWidgetItemEntry2,
                )
        }

@@ -129,7 +130,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                communalWidgetDao.addWidget(
                    widgetId = widgetId,
                    provider = provider,
                    userSerialNumber = userSerialNumber
                    userSerialNumber = userSerialNumber,
                )
            }

@@ -165,7 +166,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                    communalItemRankEntry1,
                    communalWidgetItemEntry1,
                    communalItemRankEntry2,
                    communalWidgetItemEntry2
                    communalWidgetItemEntry2,
                )

            communalWidgetDao.deleteWidgetById(communalWidgetItemEntry1.widgetId)
@@ -251,6 +252,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                    componentName = "pk_name/cls_name_4",
                    itemId = 4L,
                    userSerialNumber = 0,
                    spanY = 3,
                )
            assertThat(widgets())
                .containsExactly(
@@ -266,6 +268,68 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                .inOrder()
        }

    @Test
    fun addWidget_withDifferentSpanY_readsCorrectValuesInDb() =
        testScope.runTest {
            val widgets = collectLastValue(communalWidgetDao.getWidgets())

            // Add widgets with different spanY values
            communalWidgetDao.addWidget(
                widgetId = 1,
                provider = ComponentName("pkg_name", "cls_name_1"),
                rank = 0,
                userSerialNumber = 0,
                spanY = CommunalContentSize.FULL.span,
            )
            communalWidgetDao.addWidget(
                widgetId = 2,
                provider = ComponentName("pkg_name", "cls_name_2"),
                rank = 1,
                userSerialNumber = 0,
                spanY = CommunalContentSize.HALF.span,
            )
            communalWidgetDao.addWidget(
                widgetId = 3,
                provider = ComponentName("pkg_name", "cls_name_3"),
                rank = 2,
                userSerialNumber = 0,
                spanY = CommunalContentSize.THIRD.span,
            )

            // Verify that the widgets have the correct spanY values
            assertThat(widgets())
                .containsExactly(
                    CommunalItemRank(uid = 1L, rank = 0),
                    CommunalWidgetItem(
                        uid = 1L,
                        widgetId = 1,
                        componentName = "pkg_name/cls_name_1",
                        itemId = 1L,
                        userSerialNumber = 0,
                        spanY = CommunalContentSize.FULL.span,
                    ),
                    CommunalItemRank(uid = 2L, rank = 1),
                    CommunalWidgetItem(
                        uid = 2L,
                        widgetId = 2,
                        componentName = "pkg_name/cls_name_2",
                        itemId = 2L,
                        userSerialNumber = 0,
                        spanY = CommunalContentSize.HALF.span,
                    ),
                    CommunalItemRank(uid = 3L, rank = 2),
                    CommunalWidgetItem(
                        uid = 3L,
                        widgetId = 3,
                        componentName = "pkg_name/cls_name_3",
                        itemId = 3L,
                        userSerialNumber = 0,
                        spanY = CommunalContentSize.THIRD.span,
                    ),
                )
                .inOrder()
        }

    @Test
    fun restoreCommunalHubState() =
        testScope.runTest {
@@ -288,6 +352,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                        componentName = fakeWidget.componentName,
                        itemId = rank.uid,
                        userSerialNumber = fakeWidget.userSerialNumber,
                        spanY = 3,
                    )
                expected[rank] = widget
            }
@@ -343,6 +408,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                componentName = widgetInfo1.provider.flattenToString(),
                itemId = communalItemRankEntry1.uid,
                userSerialNumber = widgetInfo1.userSerialNumber,
                spanY = 3,
            )
        val communalWidgetItemEntry2 =
            CommunalWidgetItem(
@@ -351,6 +417,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                componentName = widgetInfo2.provider.flattenToString(),
                itemId = communalItemRankEntry2.uid,
                userSerialNumber = widgetInfo2.userSerialNumber,
                spanY = 3,
            )
        val communalWidgetItemEntry3 =
            CommunalWidgetItem(
@@ -359,6 +426,7 @@ class CommunalWidgetDaoTest : SysuiTestCase() {
                componentName = widgetInfo3.provider.flattenToString(),
                itemId = communalItemRankEntry3.uid,
                userSerialNumber = widgetInfo3.userSerialNumber,
                spanY = 3,
            )
        val fakeState =
            CommunalHubState().apply {
+4 −0
Original line number Diff line number Diff line
@@ -117,6 +117,7 @@ class DefaultWidgetPopulationTest : SysuiTestCase() {
                    componentName = defaultWidgets[0],
                    rank = 0,
                    userSerialNumber = 0,
                    spanY = 3,
                )
            verify(communalWidgetDao)
                .addWidget(
@@ -124,6 +125,7 @@ class DefaultWidgetPopulationTest : SysuiTestCase() {
                    componentName = defaultWidgets[1],
                    rank = 1,
                    userSerialNumber = 0,
                    spanY = 3,
                )
            verify(communalWidgetDao)
                .addWidget(
@@ -131,6 +133,7 @@ class DefaultWidgetPopulationTest : SysuiTestCase() {
                    componentName = defaultWidgets[2],
                    rank = 2,
                    userSerialNumber = 0,
                    spanY = 3,
                )
        }

@@ -152,6 +155,7 @@ class DefaultWidgetPopulationTest : SysuiTestCase() {
                    componentName = any(),
                    rank = anyInt(),
                    userSerialNumber = anyInt(),
                    spanY = anyInt(),
                )
        }
}
+38 −34
Original line number Diff line number Diff line
@@ -143,7 +143,8 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
    fun communalWidgets_queryWidgetsFromDb() =
        testScope.runTest {
            val communalItemRankEntry = CommunalItemRank(uid = 1L, rank = 1)
            val communalWidgetItemEntry = CommunalWidgetItem(uid = 1L, 1, "pk_name/cls_name", 1L, 0)
            val communalWidgetItemEntry =
                CommunalWidgetItem(uid = 1L, 1, "pk_name/cls_name", 1L, 0, 3)
            fakeWidgets.value = mapOf(communalItemRankEntry to communalWidgetItemEntry)
            fakeProviders.value = mapOf(1 to providerInfoA)

@@ -169,19 +170,15 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            fakeWidgets.value =
                mapOf(
                    CommunalItemRank(uid = 1L, rank = 1) to
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0),
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0, 3),
                    CommunalItemRank(uid = 2L, rank = 2) to
                        CommunalWidgetItem(uid = 2L, 2, "pk_2/cls_2", 2L, 0),
                        CommunalWidgetItem(uid = 2L, 2, "pk_2/cls_2", 2L, 0, 3),
                    CommunalItemRank(uid = 3L, rank = 3) to
                        CommunalWidgetItem(uid = 3L, 3, "pk_3/cls_3", 3L, 0),
                        CommunalWidgetItem(uid = 3L, 3, "pk_3/cls_3", 3L, 0, 3),
                    CommunalItemRank(uid = 4L, rank = 4) to
                        CommunalWidgetItem(uid = 4L, 4, "pk_4/cls_4", 4L, 0),
                )
            fakeProviders.value =
                mapOf(
                    1 to providerInfoA,
                    2 to providerInfoB,
                        CommunalWidgetItem(uid = 4L, 4, "pk_4/cls_4", 4L, 0, 3),
                )
            fakeProviders.value = mapOf(1 to providerInfoA, 2 to providerInfoB)

            // Expect to see only widget 1 and 2
            val communalWidgets by collectLastValue(underTest.communalWidgets)
@@ -207,15 +204,11 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            fakeWidgets.value =
                mapOf(
                    CommunalItemRank(uid = 1L, rank = 1) to
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0),
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0, 3),
                    CommunalItemRank(uid = 2L, rank = 2) to
                        CommunalWidgetItem(uid = 2L, 2, "pk_2/cls_2", 2L, 0),
                )
            fakeProviders.value =
                mapOf(
                    1 to providerInfoA,
                    2 to providerInfoB,
                        CommunalWidgetItem(uid = 2L, 2, "pk_2/cls_2", 2L, 0, 3),
                )
            fakeProviders.value = mapOf(1 to providerInfoA, 2 to providerInfoB)

            // Expect two widgets
            val communalWidgets by collectLastValue(underTest.communalWidgets)
@@ -235,11 +228,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                )

            // Provider info updated for widget 1
            fakeProviders.value =
                mapOf(
                    1 to providerInfoC,
                    2 to providerInfoB,
                )
            fakeProviders.value = mapOf(1 to providerInfoC, 2 to providerInfoB)
            runCurrent()

            assertThat(communalWidgets)
@@ -269,7 +258,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            whenever(
                    communalWidgetHost.allocateIdAndBindWidget(
                        any<ComponentName>(),
                        any<UserHandle>()
                        any<UserHandle>(),
                    )
                )
                .thenReturn(id)
@@ -294,7 +283,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            whenever(
                    communalWidgetHost.allocateIdAndBindWidget(
                        any<ComponentName>(),
                        any<UserHandle>()
                        any<UserHandle>(),
                    )
                )
                .thenReturn(id)
@@ -303,7 +292,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {

            verify(communalWidgetHost).allocateIdAndBindWidget(provider, mainUser)
            verify(communalWidgetDao, never())
                .addWidget(anyInt(), any<ComponentName>(), anyInt(), anyInt())
                .addWidget(anyInt(), any<ComponentName>(), anyInt(), anyInt(), anyInt())
            verify(appWidgetHost).deleteAppWidgetId(id)

            // Verify backup not requested
@@ -321,7 +310,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            whenever(
                    communalWidgetHost.allocateIdAndBindWidget(
                        any<ComponentName>(),
                        any<UserHandle>()
                        any<UserHandle>(),
                    )
                )
                .thenReturn(id)
@@ -332,7 +321,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {

            verify(communalWidgetHost).allocateIdAndBindWidget(provider, mainUser)
            verify(communalWidgetDao, never())
                .addWidget(anyInt(), any<ComponentName>(), anyInt(), anyInt())
                .addWidget(anyInt(), any<ComponentName>(), anyInt(), anyInt(), anyInt())
            verify(appWidgetHost).deleteAppWidgetId(id)

            // Verify backup not requested
@@ -350,7 +339,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            whenever(
                    communalWidgetHost.allocateIdAndBindWidget(
                        any<ComponentName>(),
                        any<UserHandle>()
                        any<UserHandle>(),
                    )
                )
                .thenReturn(id)
@@ -650,8 +639,10 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                    eq(newWidgetId),
                    componentNameCaptor.capture(),
                    eq(2),
                    eq(testUserSerialNumber(workProfile))
                    eq(testUserSerialNumber(workProfile)),
                    anyInt(),
                )

            assertThat(componentNameCaptor.firstValue)
                .isEqualTo(ComponentName("pk_name", "fake_widget_2"))
        }
@@ -662,9 +653,9 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            fakeWidgets.value =
                mapOf(
                    CommunalItemRank(uid = 1L, rank = 1) to
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0),
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0, 3),
                    CommunalItemRank(uid = 2L, rank = 2) to
                        CommunalWidgetItem(uid = 2L, 2, "pk_2/cls_2", 2L, 0),
                        CommunalWidgetItem(uid = 2L, 2, "pk_2/cls_2", 2L, 0, 3),
                )

            // Widget 1 is installed
@@ -707,7 +698,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
            fakeWidgets.value =
                mapOf(
                    CommunalItemRank(uid = 1L, rank = 1) to
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0),
                        CommunalWidgetItem(uid = 1L, 1, "pk_1/cls_1", 1L, 0, 3)
                )

            // Widget 1 is pending install
@@ -732,7 +723,7 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                        componentName = ComponentName("pk_1", "cls_1"),
                        icon = fakeIcon,
                        user = mainUser,
                    ),
                    )
                )

            // Package for widget 1 finished installing
@@ -749,8 +740,21 @@ class CommunalWidgetRepositoryImplTest : SysuiTestCase() {
                        appWidgetId = 1,
                        providerInfo = providerInfoA,
                        rank = 1,
                    ),
                    )
                )
        }

    @Test
    fun updateWidgetSpanY_updatesWidgetInDaoAndRequestsBackup() =
        testScope.runTest {
            val widgetId = 1
            val newSpanY = 6

            underTest.updateWidgetSpanY(widgetId, newSpanY)
            runCurrent()

            verify(communalWidgetDao).updateWidgetSpanY(widgetId, newSpanY)
            verify(backupManager).dataChanged()
        }

    private fun setAppWidgetIds(ids: List<Int>) {
+88 −0
Original line number Diff line number Diff line
{
  "formatVersion": 1,
  "database": {
    "version": 4,
    "identityHash": "a49f2f7d25cf12d1baf9a3a3e6243b64",
    "entities": [
      {
        "tableName": "communal_widget_table",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `widget_id` INTEGER NOT NULL, `component_name` TEXT NOT NULL, `item_id` INTEGER NOT NULL, `user_serial_number` INTEGER NOT NULL DEFAULT -1, `span_y` INTEGER NOT NULL DEFAULT 3)",
        "fields": [
          {
            "fieldPath": "uid",
            "columnName": "uid",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "widgetId",
            "columnName": "widget_id",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "componentName",
            "columnName": "component_name",
            "affinity": "TEXT",
            "notNull": true
          },
          {
            "fieldPath": "itemId",
            "columnName": "item_id",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "userSerialNumber",
            "columnName": "user_serial_number",
            "affinity": "INTEGER",
            "notNull": true,
            "defaultValue": "-1"
          },
          {
            "fieldPath": "spanY",
            "columnName": "span_y",
            "affinity": "INTEGER",
            "notNull": true,
            "defaultValue": "3"
          }
        ],
        "primaryKey": {
          "autoGenerate": true,
          "columnNames": [
            "uid"
          ]
        }
      },
      {
        "tableName": "communal_item_rank_table",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `rank` INTEGER NOT NULL DEFAULT 0)",
        "fields": [
          {
            "fieldPath": "uid",
            "columnName": "uid",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "rank",
            "columnName": "rank",
            "affinity": "INTEGER",
            "notNull": true,
            "defaultValue": "0"
          }
        ],
        "primaryKey": {
          "autoGenerate": true,
          "columnNames": [
            "uid"
          ]
        }
      }
    ],
    "setupQueries": [
      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'a49f2f7d25cf12d1baf9a3a3e6243b64')"
    ]
  }
}
 No newline at end of file
+20 −7
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.android.systemui.res.R

@Database(entities = [CommunalWidgetItem::class, CommunalItemRank::class], version = 3)
@Database(entities = [CommunalWidgetItem::class, CommunalItemRank::class], version = 4)
abstract class CommunalDatabase : RoomDatabase() {
    abstract fun communalWidgetDao(): CommunalWidgetDao

@@ -43,19 +43,16 @@ abstract class CommunalDatabase : RoomDatabase() {
         * @param callback An optional callback registered to the database. Only effective when a
         *   new instance is created.
         */
        fun getInstance(
            context: Context,
            callback: Callback? = null,
        ): CommunalDatabase {
        fun getInstance(context: Context, callback: Callback? = null): CommunalDatabase {
            if (instance == null) {
                instance =
                    Room.databaseBuilder(
                            context,
                            CommunalDatabase::class.java,
                            context.resources.getString(R.string.config_communalDatabase)
                            context.resources.getString(R.string.config_communalDatabase),
                        )
                        .also { builder ->
                            builder.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
                            builder.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
                            builder.fallbackToDestructiveMigration(dropAllTables = true)
                            callback?.let { callback -> builder.addCallback(callback) }
                        }
@@ -103,5 +100,21 @@ abstract class CommunalDatabase : RoomDatabase() {
                    )
                }
            }

        /**
         * This migration adds a span_y column to the communal_widget_table and sets its default
         * value to 3.
         */
        @VisibleForTesting
        val MIGRATION_3_4 =
            object : Migration(3, 4) {
                override fun migrate(db: SupportSQLiteDatabase) {
                    Log.i(TAG, "Migrating from version 3 to 4")
                    db.execSQL(
                        "ALTER TABLE communal_widget_table " +
                            "ADD COLUMN span_y INTEGER NOT NULL DEFAULT 3"
                    )
                }
            }
    }
}
Loading