1 /* 2 * Copyright (C) 2017 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 android.cts.backup; 18 19 import static org.junit.Assert.assertNull; 20 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.targetprep.TargetSetupError; 24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 25 26 import org.junit.After; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 /** 31 * Test checking that restoreAnyVersion manifest flag is respected by backup manager. 32 * 33 * Invokes device side tests provided by 34 * android.cts.backup.restoreanyversionapp.RestoreAnyVersionTest. 35 */ 36 @RunWith(DeviceJUnit4ClassRunner.class) 37 public class RestoreAnyVersionHostSideTest extends BaseBackupHostSideTest { 38 39 /** The name of the package of the app under test */ 40 private static final String RESTORE_ANY_VERSION_APP_PACKAGE = 41 "android.cts.backup.restoreanyversionapp"; 42 43 /** The name of the device side test class */ 44 private static final String RESTORE_ANY_VERSION_DEVICE_TEST_NAME = 45 RESTORE_ANY_VERSION_APP_PACKAGE + ".RestoreAnyVersionTest"; 46 47 /** The name of the APK of the app that has restoreAnyVersion=true in the manifest */ 48 private static final String RESTORE_ANY_VERSION_APP_APK = "CtsBackupRestoreAnyVersionApp.apk"; 49 50 /** The name of the APK of the app that has a higher version code */ 51 private static final String RESTORE_ANY_VERSION_UPDATE_APK = 52 "CtsBackupRestoreAnyVersionAppUpdate.apk"; 53 54 /** The name of the APK of the app that has restoreAnyVersion=false in the manifest */ 55 private static final String NO_RESTORE_ANY_VERSION_APK = 56 "CtsBackupRestoreAnyVersionNoRestoreApp.apk"; 57 58 @After 59 @Override 60 public void tearDown() throws Exception { 61 super.tearDown(); 62 63 if (!mIsBackupSupported) { 64 return; 65 } 66 67 // Clear backup data and uninstall the package (in that order!) 68 clearBackupDataInLocalTransport(RESTORE_ANY_VERSION_APP_PACKAGE); 69 assertNull(uninstallPackage(RESTORE_ANY_VERSION_APP_PACKAGE)); 70 } 71 72 /** 73 * Tests that the app that has restoreAnyVersion=false will not get the restored data from a 74 * newer version of that app at install time 75 */ 76 @Test 77 public void testRestoreAnyVersion_False() throws Exception { 78 if (!mIsBackupSupported) { 79 CLog.i("android.software.backup feature is not supported on this device"); 80 return; 81 } 82 83 installNewVersionApp(); 84 85 saveSharedPreferenceValue(); 86 checkRestoreAnyVersionDeviceTest("checkSharedPrefIsNew"); 87 88 backupNowAndAssertSuccess(RESTORE_ANY_VERSION_APP_PACKAGE); 89 90 assertNull(uninstallPackage(RESTORE_ANY_VERSION_APP_PACKAGE)); 91 92 installNoRestoreAnyVersionApp(); 93 94 // Shared preference shouldn't be restored 95 checkRestoreAnyVersionDeviceTest("checkSharedPrefIsEmpty"); 96 } 97 98 /** 99 * Tests that the app that has restoreAnyVersion=true will get the restored data from a 100 * newer version of that app at install time 101 */ 102 @Test 103 public void testRestoreAnyVersion_True() throws Exception { 104 if (!mIsBackupSupported) { 105 CLog.i("android.software.backup feature is not supported on this device"); 106 return; 107 } 108 109 installNewVersionApp(); 110 111 saveSharedPreferenceValue(); 112 checkRestoreAnyVersionDeviceTest("checkSharedPrefIsNew"); 113 114 backupNowAndAssertSuccess(RESTORE_ANY_VERSION_APP_PACKAGE); 115 116 assertNull(uninstallPackage(RESTORE_ANY_VERSION_APP_PACKAGE)); 117 118 installRestoreAnyVersionApp(); 119 120 // Shared preference should be restored 121 checkRestoreAnyVersionDeviceTest("checkSharedPrefIsNew"); 122 } 123 124 /** 125 * Tests that the app that has restoreAnyVersion=false will still get the restored data from an 126 * older version of that app at install time 127 */ 128 @Test 129 public void testRestoreAnyVersion_OldBackupToNewApp() throws Exception { 130 if (!mIsBackupSupported) { 131 CLog.i("android.software.backup feature is not supported on this device"); 132 return; 133 } 134 135 installNoRestoreAnyVersionApp(); 136 137 saveSharedPreferenceValue(); 138 checkRestoreAnyVersionDeviceTest("checkSharedPrefIsOld"); 139 140 backupNowAndAssertSuccess(RESTORE_ANY_VERSION_APP_PACKAGE); 141 142 assertNull(uninstallPackage(RESTORE_ANY_VERSION_APP_PACKAGE)); 143 144 installNewVersionApp(); 145 146 checkRestoreAnyVersionDeviceTest("checkSharedPrefIsOld"); 147 } 148 149 private void saveSharedPreferenceValue () throws DeviceNotAvailableException { 150 checkRestoreAnyVersionDeviceTest("checkSharedPrefIsEmpty"); 151 checkRestoreAnyVersionDeviceTest("saveSharedPrefValue"); 152 } 153 154 private void installRestoreAnyVersionApp() 155 throws DeviceNotAvailableException, TargetSetupError { 156 installPackage(RESTORE_ANY_VERSION_APP_APK, "-d", "-r"); 157 158 checkRestoreAnyVersionDeviceTest("checkAppVersionIsOld"); 159 } 160 161 private void installNoRestoreAnyVersionApp() 162 throws DeviceNotAvailableException, TargetSetupError { 163 installPackage(NO_RESTORE_ANY_VERSION_APK, "-d", "-r"); 164 165 checkRestoreAnyVersionDeviceTest("checkAppVersionIsOld"); 166 } 167 168 private void installNewVersionApp() 169 throws DeviceNotAvailableException, TargetSetupError { 170 installPackage(RESTORE_ANY_VERSION_UPDATE_APK, "-d", "-r"); 171 172 checkRestoreAnyVersionDeviceTest("checkAppVersionIsNew"); 173 } 174 175 private void checkRestoreAnyVersionDeviceTest(String methodName) 176 throws DeviceNotAvailableException { 177 checkDeviceTest(RESTORE_ANY_VERSION_APP_PACKAGE, RESTORE_ANY_VERSION_DEVICE_TEST_NAME, 178 methodName); 179 } 180 } 181