<lambda>null1 package com.android.launcher3.util
2
3 import android.content.ContentValues
4 import android.database.sqlite.SQLiteDatabase
5 import android.os.Process
6 import androidx.test.platform.app.InstrumentationRegistry
7 import com.android.launcher3.Flags
8 import com.android.launcher3.LauncherModel
9 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_ID
10 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_PROVIDER
11 import com.android.launcher3.LauncherSettings.Favorites.APPWIDGET_SOURCE
12 import com.android.launcher3.LauncherSettings.Favorites.CELLX
13 import com.android.launcher3.LauncherSettings.Favorites.CELLY
14 import com.android.launcher3.LauncherSettings.Favorites.CONTAINER
15 import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP
16 import com.android.launcher3.LauncherSettings.Favorites.INTENT
17 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE
18 import com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
19 import com.android.launcher3.LauncherSettings.Favorites.PROFILE_ID
20 import com.android.launcher3.LauncherSettings.Favorites.RESTORED
21 import com.android.launcher3.LauncherSettings.Favorites.SCREEN
22 import com.android.launcher3.LauncherSettings.Favorites.SPANX
23 import com.android.launcher3.LauncherSettings.Favorites.SPANY
24 import com.android.launcher3.LauncherSettings.Favorites.TITLE
25 import com.android.launcher3.LauncherSettings.Favorites._ID
26 import com.android.launcher3.model.BgDataModel
27 import com.android.launcher3.model.ModelDbController
28 import java.io.BufferedReader
29 import java.io.InputStreamReader
30
31 object ModelTestExtensions {
32 /** Clears and reloads Launcher db to cleanup the workspace */
33 fun LauncherModel.clearModelDb() {
34 // Load the model once so that there is no pending migration:
35 loadModelSync()
36 TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {
37 modelDbController.run {
38 if (Flags.gridMigrationRefactor())
39 attemptMigrateDb(null /* restoreEventLogger */, modelDelegate)
40 else tryMigrateDB(null /* restoreEventLogger */, modelDelegate)
41 createEmptyDB()
42 clearEmptyDbFlag()
43 }
44 }
45 // Reload model
46 TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { forceReload() }
47 loadModelSync()
48 }
49
50 /** Loads the model in memory synchronously */
51 fun LauncherModel.loadModelSync() {
52 val mockCb: BgDataModel.Callbacks = object : BgDataModel.Callbacks {}
53 TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { addCallbacksAndLoad(mockCb) }
54 TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {}
55 TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) {}
56 TestUtil.runOnExecutorSync(Executors.MAIN_EXECUTOR) { removeCallbacks(mockCb) }
57 }
58
59 /** Adds and commits a new item to Launcher.db */
60 fun LauncherModel.addItem(
61 title: String = "LauncherTestApp",
62 intent: String =
63 "#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",
64 type: Int = ITEM_TYPE_APPLICATION,
65 restoreFlags: Int = 0,
66 screen: Int = 0,
67 container: Int = CONTAINER_DESKTOP,
68 x: Int,
69 y: Int,
70 spanX: Int = 1,
71 spanY: Int = 1,
72 id: Int = 0,
73 profileId: Int = Process.myUserHandle().identifier,
74 appWidgetId: Int = -1,
75 appWidgetSource: Int = -1,
76 appWidgetProvider: String? = null,
77 ) {
78 loadModelSync()
79 TestUtil.runOnExecutorSync(Executors.MODEL_EXECUTOR) {
80 val controller: ModelDbController = modelDbController
81 controller.attemptMigrateDb(null /* restoreEventLogger */, modelDelegate)
82 modelDbController.newTransaction().use { transaction ->
83 val values =
84 ContentValues().apply {
85 values[_ID] = id
86 values[TITLE] = title
87 values[PROFILE_ID] = profileId
88 values[CONTAINER] = container
89 values[SCREEN] = screen
90 values[CELLX] = x
91 values[CELLY] = y
92 values[SPANX] = spanX
93 values[SPANY] = spanY
94 values[ITEM_TYPE] = type
95 values[RESTORED] = restoreFlags
96 values[INTENT] = intent
97 values[APPWIDGET_ID] = appWidgetId
98 values[APPWIDGET_SOURCE] = appWidgetSource
99 values[APPWIDGET_PROVIDER] = appWidgetProvider
100 }
101 // Migrate any previous data so that the DB state is correct
102 controller.insert(values)
103 transaction.commit()
104 }
105 }
106 }
107
108 /** Creates an in-memory sqlite DB and initializes with the data in [insertFile] */
109 fun createInMemoryDb(insertFile: String): SQLiteDatabase =
110 SQLiteDatabase.createInMemory(SQLiteDatabase.OpenParams.Builder().build()).also { db ->
111 BufferedReader(
112 InputStreamReader(
113 InstrumentationRegistry.getInstrumentation().context.assets.open(insertFile)
114 )
115 )
116 .lines()
117 .forEach { sqlStatement -> db.execSQL(sqlStatement) }
118 }
119 }
120