1 /*
2  * Copyright (C) 2024 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 
17 package com.android.launcher3.backuprestore
18 
19 import android.platform.test.annotations.DisableFlags
20 import android.platform.test.annotations.EnableFlags
21 import android.platform.test.flag.junit.SetFlagsRule
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.MediumTest
24 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
25 import com.android.launcher3.Flags
26 import com.android.launcher3.LauncherPrefs
27 import com.android.launcher3.model.ModelDbController
28 import com.android.launcher3.provider.RestoreDbTask
29 import com.android.launcher3.util.Executors.MODEL_EXECUTOR
30 import com.android.launcher3.util.TestUtil
31 import com.android.launcher3.util.rule.BackAndRestoreRule
32 import com.android.launcher3.util.rule.setFlags
33 import org.junit.Before
34 import org.junit.Rule
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 
38 /**
39  * Makes sure to test {@code RestoreDbTask#removeOldDBs}, we need to remove all the dbs that are not
40  * the last one used when we restore the device.
41  */
42 @RunWith(AndroidJUnit4::class)
43 @MediumTest
44 class BackupAndRestoreDBSelectionTest {
45 
46     @JvmField @Rule var backAndRestoreRule = BackAndRestoreRule()
47 
48     @JvmField
49     @Rule
50     val setFlagsRule = SetFlagsRule(SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT)
51 
52     @Before
setUpnull53     fun setUp() {
54         setFlagsRule.setFlags(true, Flags.FLAG_ENABLE_NARROW_GRID_RESTORE)
55     }
56 
57     @EnableFlags(Flags.FLAG_GRID_MIGRATION_REFACTOR)
oldDatabasesNotPresentAfterRestoreRefactorFlagEnablednull58     fun oldDatabasesNotPresentAfterRestoreRefactorFlagEnabled() {
59         oldDatabasesNotPresentAfterRestore()
60     }
61 
62     @DisableFlags(Flags.FLAG_GRID_MIGRATION_REFACTOR)
oldDatabasesNotPresentAfterRestoreRefactorFlagDisablednull63     fun oldDatabasesNotPresentAfterRestoreRefactorFlagDisabled() {
64         oldDatabasesNotPresentAfterRestore()
65     }
66 
67     @Test
oldDatabasesNotPresentAfterRestorenull68     fun oldDatabasesNotPresentAfterRestore() {
69         val dbController = ModelDbController(getInstrumentation().targetContext)
70         if (Flags.gridMigrationRefactor()) {
71             dbController.attemptMigrateDb(null)
72         } else {
73             dbController.tryMigrateDB(null)
74         }
75         TestUtil.runOnExecutorSync(MODEL_EXECUTOR) {
76             assert(backAndRestoreRule.getDatabaseFiles().size == 1) {
77                 "There should only be one database after restoring, the last one used. Actual databases ${backAndRestoreRule.getDatabaseFiles()}"
78             }
79             assert(
80                 !LauncherPrefs.get(getInstrumentation().targetContext)
81                     .has(LauncherPrefs.RESTORE_DEVICE)
82             ) {
83                 "RESTORE_DEVICE shouldn't be present after a backup and restore."
84             }
85         }
86     }
87 
88     @Test
testExistingDbsAndRemovingDbsnull89     fun testExistingDbsAndRemovingDbs() {
90         var existingDbs = RestoreDbTask.existingDbs(getInstrumentation().targetContext)
91         assert(existingDbs.size == 4)
92         RestoreDbTask.removeOldDBs(getInstrumentation().targetContext, "launcher_4_by_4.db")
93         existingDbs = RestoreDbTask.existingDbs(getInstrumentation().targetContext)
94         assert(existingDbs.size == 1)
95     }
96 }
97