blob: 90313abe1d58dc7d7b15dd404f2c3d88bc4c0f74 [file] [log] [blame]
Pinyao Ting9be1cfd2020-01-16 12:29:04 -08001/*
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
17package com.android.launcher3.model;
18
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080019import static android.content.pm.PackageManager.INSTALL_REASON_DEVICE_RESTORE;
20
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080021import static com.android.launcher3.LauncherSettings.Favorites.BACKUP_TABLE_NAME;
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080022import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
23import static com.android.launcher3.LauncherSettings.Favorites.addTableToDb;
24import static com.android.launcher3.provider.LauncherDbUtils.dropTable;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080025import static com.android.launcher3.provider.LauncherDbUtils.tableExists;
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080026import static com.android.launcher3.util.LauncherModelHelper.APP_ICON;
27import static com.android.launcher3.util.LauncherModelHelper.NO__ICON;
28import static com.android.launcher3.util.LauncherModelHelper.SHORTCUT;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080029
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080030import static org.junit.Assert.assertEquals;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080031import static org.junit.Assert.assertTrue;
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080032import static org.robolectric.util.ReflectionHelpers.setField;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080033
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080034import android.app.backup.BackupManager;
35import android.content.pm.PackageInstaller;
36import android.database.Cursor;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080037import android.database.sqlite.SQLiteDatabase;
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080038import android.os.UserHandle;
39import android.os.UserManager;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080040
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080041import com.android.launcher3.InvariantDeviceProfile;
Sunny Goyalc0e9df62020-01-16 14:54:36 -080042import com.android.launcher3.provider.RestoreDbTask;
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080043import com.android.launcher3.shadows.LShadowBackupManager;
44import com.android.launcher3.shadows.LShadowUserManager;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080045import com.android.launcher3.util.LauncherModelHelper;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080046
47import org.junit.Before;
48import org.junit.Test;
49import org.junit.runner.RunWith;
Sunny Goyalc57a7972020-04-08 16:27:28 -070050import org.robolectric.RobolectricTestRunner;
Sunny Goyalc0e9df62020-01-16 14:54:36 -080051import org.robolectric.RuntimeEnvironment;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080052import org.robolectric.annotation.LooperMode;
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080053import org.robolectric.shadow.api.Shadow;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080054
55/**
56 * Tests to verify backup and restore flow.
57 */
Sunny Goyalc57a7972020-04-08 16:27:28 -070058@RunWith(RobolectricTestRunner.class)
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080059@LooperMode(LooperMode.Mode.PAUSED)
60public class BackupRestoreTest {
61
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080062 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 Ting9be1cfd2020-01-16 12:29:04 -080073 private LauncherModelHelper mModelHelper;
74 private SQLiteDatabase mDb;
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080075 private InvariantDeviceProfile mIdp;
76 private UserHandle mMainProfileUser;
77 private UserHandle mWorkProfileUser;
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080078
79 @Before
80 public void setUp() {
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080081 setupUserManager();
82 setupBackupManager();
Pinyao Ting9be1cfd2020-01-16 12:29:04 -080083 mModelHelper = new LauncherModelHelper();
Sunny Goyalc0e9df62020-01-16 14:54:36 -080084 RestoreDbTask.setPending(RuntimeEnvironment.application, true);
85 mDb = mModelHelper.provider.getDb();
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -080086 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 Ting9be1cfd2020-01-16 12:29:04 -0800104 }
105
106 @Test
107 public void testOnCreateDbIfNotExists_CreatesBackup() {
108 assertTrue(tableExists(mDb, BACKUP_TABLE_NAME));
109 }
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -0800110
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 Zhou02cb0602020-02-12 17:22:09 -0800137 new GridBackupTable(RuntimeEnvironment.application, mDb, mIdp.numHotseatIcons,
Pinyao Ting5f8a1ab2020-01-24 16:39:08 -0800138 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 Ting9be1cfd2020-01-16 12:29:04 -0800181}