<lambda>null1 package com.android.launcher3.util
2 
3 import android.content.ContentValues
4 import com.android.launcher3.LauncherModel
5 import com.android.launcher3.LauncherSettings.Favorites
6 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_ID
7 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_PROVIDER
8 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_SOURCE
9 import com.android.launcher3.LauncherSettings.Favorites.CELLX
10 import com.android.launcher3.LauncherSettings.Favorites.CELLY
11 import com.android.launcher3.LauncherSettings.Favorites.CONTAINER
12 import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP
13 import com.android.launcher3.LauncherSettings.Favorites.INTENT
14 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE
15 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
16 import com.android.launcher3.LauncherSettings.Favorites.PROFILE_ID
17 import com.android.launcher3.LauncherSettings.Favorites.RESTORED
18 import com.android.launcher3.LauncherSettings.Favorites.SCREEN
19 import com.android.launcher3.LauncherSettings.Favorites.SPANX
20 import com.android.launcher3.LauncherSettings.Favorites.SPANY
21 import com.android.launcher3.LauncherSettings.Favorites.TITLE
22 import com.android.launcher3.LauncherSettings.Favorites._ID
23 import com.android.launcher3.model.BgDataModel
24 import com.android.launcher3.model.ModelDbController
25 
26 object ModelTestExtensions {
27     /** Clears and reloads Launcher db to cleanup the workspace */
28     fun LauncherModel.clearModelDb() {
29         // Load the model once so that there is no pending migration:
30         loadModelSync()
31         TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {
32             modelDbController.run {
33                 tryMigrateDB(null /* restoreEventLogger */)
34                 createEmptyDB()
35                 clearEmptyDbFlag()
36             }
37         }
38         // Reload model
39         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { forceReload() }
40         loadModelSync()
41     }
42 
43     /** Loads the model in memory synchronously */
44     fun LauncherModel.loadModelSync() {
45         val mockCb: BgDataModel.Callbacks = object : BgDataModel.Callbacks {}
46         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { addCallbacksAndLoad(mockCb) }
47         TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {}
48         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) {}
49         TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { removeCallbacks(mockCb) }
50     }
51 
52     /** Adds and commits a new item to Launcher.db */
53     fun LauncherModel.addItem(
54         title: String = "LauncherTestApp",
55         intent: String =
56             "#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;component=com.google.android.apps.nexuslauncher.tests/com.android.launcher3.testcomponent.BaseTestingActivity;launchFlags=0x10200000;end",
57         type: Int = ITEM_TYPE_APPLICATION,
58         restoreFlags: Int = 0,
59         screen: Int = 0,
60         container: Int = CONTAINER_DESKTOP,
61         x: Int,
62         y: Int,
63         spanX: Int = 1,
64         spanY: Int = 1,
65         id: Int = 0,
66         profileId: Int = 0,
67         tableName: String = Favorites.TABLE_NAME,
68         appWidgetId: Int = -1,
69         appWidgetSource: Int = -1,
70         appWidgetProvider: String? = null
71     ) {
72         loadModelSync()
73         TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {
74             val controller: ModelDbController = modelDbController
75             controller.tryMigrateDB(null /* restoreEventLogger */)
76             modelDbController.newTransaction().use { transaction ->
77                 val values =
78                     ContentValues().apply {
79                         values[_ID] = id
80                         values[TITLE] = title
81                         values[PROFILE_ID] = profileId
82                         values[CONTAINER] = container
83                         values[SCREEN] = screen
84                         values[CELLX] = x
85                         values[CELLY] = y
86                         values[SPANX] = spanX
87                         values[SPANY] = spanY
88                         values[ITEM_TYPE] = type
89                         values[RESTORED] = restoreFlags
90                         values[INTENT] = intent
91                         values[APPWIDGET_ID] = appWidgetId
92                         values[APPWIDGET_SOURCE] = appWidgetSource
93                         values[APPWIDGET_PROVIDER] = appWidgetProvider
94                     }
95                 // Migrate any previous data so that the DB state is correct
96                 controller.insert(tableName, values)
97                 transaction.commit()
98             }
99         }
100     }
101 }
102