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