Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.model; |
| 18 | |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.INSTALL_REASON_DEVICE_RESTORE; |
| 20 | |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 21 | import static com.android.launcher3.LauncherSettings.Favorites.BACKUP_TABLE_NAME; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 22 | import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME; |
| 23 | import static com.android.launcher3.LauncherSettings.Favorites.addTableToDb; |
| 24 | import static com.android.launcher3.provider.LauncherDbUtils.dropTable; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 25 | import static com.android.launcher3.provider.LauncherDbUtils.tableExists; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 26 | import static com.android.launcher3.util.LauncherModelHelper.APP_ICON; |
| 27 | import static com.android.launcher3.util.LauncherModelHelper.NO__ICON; |
| 28 | import static com.android.launcher3.util.LauncherModelHelper.SHORTCUT; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 29 | |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 30 | import static org.junit.Assert.assertEquals; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 31 | import static org.junit.Assert.assertTrue; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 32 | import static org.robolectric.util.ReflectionHelpers.setField; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 33 | |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 34 | import android.app.backup.BackupManager; |
| 35 | import android.content.pm.PackageInstaller; |
| 36 | import android.database.Cursor; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 37 | import android.database.sqlite.SQLiteDatabase; |
Pinyao Ting | a39f45b | 2021-04-07 19:40:02 -0700 | [diff] [blame] | 38 | import android.os.Process; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 39 | import android.os.UserHandle; |
| 40 | import android.os.UserManager; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 41 | |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 42 | import com.android.launcher3.InvariantDeviceProfile; |
Sunny Goyal | c0e9df6 | 2020-01-16 14:54:36 -0800 | [diff] [blame] | 43 | import com.android.launcher3.provider.RestoreDbTask; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 44 | import com.android.launcher3.shadows.LShadowBackupManager; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 45 | import com.android.launcher3.util.LauncherModelHelper; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 46 | |
| 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
| 49 | import org.junit.runner.RunWith; |
Sunny Goyal | c57a797 | 2020-04-08 16:27:28 -0700 | [diff] [blame] | 50 | import org.robolectric.RobolectricTestRunner; |
Sunny Goyal | c0e9df6 | 2020-01-16 14:54:36 -0800 | [diff] [blame] | 51 | import org.robolectric.RuntimeEnvironment; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 52 | import org.robolectric.annotation.LooperMode; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 53 | import org.robolectric.shadow.api.Shadow; |
Sunny Goyal | 3751be0 | 2020-11-13 13:48:51 -0800 | [diff] [blame] | 54 | import org.robolectric.shadows.ShadowUserManager; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Tests to verify backup and restore flow. |
| 58 | */ |
Sunny Goyal | c57a797 | 2020-04-08 16:27:28 -0700 | [diff] [blame] | 59 | @RunWith(RobolectricTestRunner.class) |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 60 | @LooperMode(LooperMode.Mode.PAUSED) |
| 61 | public class BackupRestoreTest { |
| 62 | |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 63 | private static final long MY_OLD_PROFILE_ID = 1; |
| 64 | private static final long MY_PROFILE_ID = 0; |
| 65 | private static final long OLD_WORK_PROFILE_ID = 11; |
| 66 | private static final int WORK_PROFILE_ID = 10; |
| 67 | |
Sunny Goyal | 3751be0 | 2020-11-13 13:48:51 -0800 | [diff] [blame] | 68 | private ShadowUserManager mUserManager; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 69 | private BackupManager mBackupManager; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 70 | private LauncherModelHelper mModelHelper; |
| 71 | private SQLiteDatabase mDb; |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 72 | private InvariantDeviceProfile mIdp; |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 73 | |
| 74 | @Before |
| 75 | public void setUp() { |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 76 | setupUserManager(); |
| 77 | setupBackupManager(); |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 78 | mModelHelper = new LauncherModelHelper(); |
Sunny Goyal | c0e9df6 | 2020-01-16 14:54:36 -0800 | [diff] [blame] | 79 | RestoreDbTask.setPending(RuntimeEnvironment.application, true); |
| 80 | mDb = mModelHelper.provider.getDb(); |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 81 | mIdp = InvariantDeviceProfile.INSTANCE.get(RuntimeEnvironment.application); |
| 82 | } |
| 83 | |
| 84 | private void setupUserManager() { |
| 85 | final UserManager userManager = RuntimeEnvironment.application.getSystemService( |
| 86 | UserManager.class); |
| 87 | mUserManager = Shadow.extract(userManager); |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 88 | // sign in to work profile |
Pinyao Ting | a39f45b | 2021-04-07 19:40:02 -0700 | [diff] [blame] | 89 | mUserManager.addUser(WORK_PROFILE_ID, "work", ShadowUserManager.FLAG_MANAGED_PROFILE); |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private void setupBackupManager() { |
| 93 | mBackupManager = new BackupManager(RuntimeEnvironment.application); |
| 94 | final LShadowBackupManager bm = Shadow.extract(mBackupManager); |
Pinyao Ting | a39f45b | 2021-04-07 19:40:02 -0700 | [diff] [blame] | 95 | bm.addProfile(MY_OLD_PROFILE_ID, Process.myUserHandle()); |
| 96 | bm.addProfile(OLD_WORK_PROFILE_ID, UserHandle.of(WORK_PROFILE_ID)); |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testOnCreateDbIfNotExists_CreatesBackup() { |
| 101 | assertTrue(tableExists(mDb, BACKUP_TABLE_NAME)); |
| 102 | } |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 103 | |
| 104 | @Test |
| 105 | public void testOnRestoreSessionWithValidCondition_PerformsRestore() throws Exception { |
| 106 | setupBackup(); |
| 107 | verifyTableIsFilled(BACKUP_TABLE_NAME, false); |
| 108 | verifyTableIsEmpty(TABLE_NAME); |
| 109 | createRestoreSession(); |
| 110 | verifyTableIsFilled(TABLE_NAME, true); |
| 111 | } |
| 112 | |
| 113 | private void setupBackup() { |
| 114 | createTableUsingOldProfileId(); |
| 115 | // setup grid for main user on first screen |
| 116 | mModelHelper.createGrid(new int[][][]{{ |
| 117 | { APP_ICON, APP_ICON, SHORTCUT, SHORTCUT}, |
| 118 | { SHORTCUT, SHORTCUT, NO__ICON, NO__ICON}, |
| 119 | { NO__ICON, NO__ICON, SHORTCUT, SHORTCUT}, |
| 120 | { APP_ICON, SHORTCUT, SHORTCUT, APP_ICON}, |
| 121 | }}, 1, MY_OLD_PROFILE_ID); |
| 122 | // setup grid for work profile on second screen |
| 123 | mModelHelper.createGrid(new int[][][]{{ |
| 124 | { NO__ICON, APP_ICON, SHORTCUT, SHORTCUT}, |
| 125 | { SHORTCUT, SHORTCUT, NO__ICON, NO__ICON}, |
| 126 | { NO__ICON, NO__ICON, SHORTCUT, SHORTCUT}, |
| 127 | { APP_ICON, SHORTCUT, SHORTCUT, NO__ICON}, |
| 128 | }}, 2, OLD_WORK_PROFILE_ID); |
| 129 | // simulates the creation of backup upon restore |
Tony Wickham | b87f3cd | 2021-04-07 15:02:37 -0700 | [diff] [blame] | 130 | new GridBackupTable(RuntimeEnvironment.application, mDb, mIdp.numDatabaseHotseatIcons, |
Pinyao Ting | 5f8a1ab | 2020-01-24 16:39:08 -0800 | [diff] [blame] | 131 | mIdp.numColumns, mIdp.numRows).doBackup( |
| 132 | MY_OLD_PROFILE_ID, GridBackupTable.OPTION_REQUIRES_SANITIZATION); |
| 133 | // reset favorites table |
| 134 | createTableUsingOldProfileId(); |
| 135 | } |
| 136 | |
| 137 | private void verifyTableIsEmpty(String tableName) { |
| 138 | assertEquals(0, getCount(mDb, "SELECT * FROM " + tableName)); |
| 139 | } |
| 140 | |
| 141 | private void verifyTableIsFilled(String tableName, boolean sanitized) { |
| 142 | assertEquals(sanitized ? 12 : 13, getCount(mDb, |
| 143 | "SELECT * FROM " + tableName + " WHERE profileId = " |
| 144 | + (sanitized ? MY_PROFILE_ID : MY_OLD_PROFILE_ID))); |
| 145 | assertEquals(10, getCount(mDb, "SELECT * FROM " + tableName + " WHERE profileId = " |
| 146 | + (sanitized ? WORK_PROFILE_ID : OLD_WORK_PROFILE_ID))); |
| 147 | } |
| 148 | |
| 149 | private void createTableUsingOldProfileId() { |
| 150 | // simulates the creation of favorites table on old device |
| 151 | dropTable(mDb, TABLE_NAME); |
| 152 | addTableToDb(mDb, MY_OLD_PROFILE_ID, false); |
| 153 | } |
| 154 | |
| 155 | private void createRestoreSession() throws Exception { |
| 156 | final PackageInstaller.SessionParams params = new PackageInstaller.SessionParams( |
| 157 | PackageInstaller.SessionParams.MODE_FULL_INSTALL); |
| 158 | final PackageInstaller installer = RuntimeEnvironment.application.getPackageManager() |
| 159 | .getPackageInstaller(); |
| 160 | final int sessionId = installer.createSession(params); |
| 161 | final PackageInstaller.SessionInfo info = installer.getSessionInfo(sessionId); |
| 162 | setField(info, "installReason", INSTALL_REASON_DEVICE_RESTORE); |
| 163 | // TODO: (b/148410677) we should verify the following call instead |
| 164 | // InstallSessionHelper.INSTANCE.get(getContext()).restoreDbIfApplicable(info); |
| 165 | RestoreDbTask.restoreIfPossible(RuntimeEnvironment.application, |
| 166 | mModelHelper.provider.getHelper(), mBackupManager); |
| 167 | } |
| 168 | |
| 169 | private static int getCount(SQLiteDatabase db, String sql) { |
| 170 | try (Cursor c = db.rawQuery(sql, null)) { |
| 171 | return c.getCount(); |
| 172 | } |
| 173 | } |
Pinyao Ting | 9be1cfd | 2020-01-16 12:29:04 -0800 | [diff] [blame] | 174 | } |