Home | History | Annotate | Download | only in backup
      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.testtype.DeviceJUnit4ClassRunner;
     24 
     25 import org.junit.After;
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 
     30 /**
     31  * Test for checking that key/value backup and restore works correctly.
     32  * It interacts with the app that saves values in different shared preferences and files.
     33  * The app uses BackupAgentHelper to do key/value backup of those values.
     34  *
     35  * NB: The tests use "bmgr backupnow" for backup, which works on N+ devices.
     36  */
     37 @RunWith(DeviceJUnit4ClassRunner.class)
     38 public class KeyValueBackupRestoreHostSideTest extends BaseBackupHostSideTest {
     39 
     40     /** The name of the package of the app under test */
     41     private static final String KEY_VALUE_RESTORE_APP_PACKAGE =
     42             "android.cts.backup.keyvaluerestoreapp";
     43 
     44     /** The name of the package with the activity testing shared preference restore. */
     45     private static final String SHARED_PREFERENCES_RESTORE_APP_PACKAGE =
     46             "android.cts.backup.sharedprefrestoreapp";
     47 
     48     /** The name of the device side test class */
     49     private static final String KEY_VALUE_RESTORE_DEVICE_TEST_NAME =
     50             KEY_VALUE_RESTORE_APP_PACKAGE + ".KeyValueBackupRestoreTest";
     51 
     52     /** The name of the apk of the app under test */
     53     private static final String KEY_VALUE_RESTORE_APP_APK = "CtsKeyValueBackupRestoreApp.apk";
     54 
     55     /** The name of the apk with the activity testing shared preference restore. */
     56     private static final String SHARED_PREFERENCES_RESTORE_APP_APK =
     57             "CtsSharedPreferencesRestoreApp.apk";
     58 
     59 
     60     @Before
     61     @Override
     62     public void setUp() throws Exception {
     63         super.setUp();
     64         installPackage(KEY_VALUE_RESTORE_APP_APK);
     65         clearPackageData(KEY_VALUE_RESTORE_APP_PACKAGE);
     66 
     67         installPackage(SHARED_PREFERENCES_RESTORE_APP_APK);
     68         clearPackageData(SHARED_PREFERENCES_RESTORE_APP_APK);
     69     }
     70 
     71     @After
     72     @Override
     73     public void tearDown() throws Exception {
     74         super.tearDown();
     75 
     76         // Clear backup data and uninstall the package (in that order!)
     77         clearBackupDataInLocalTransport(KEY_VALUE_RESTORE_APP_PACKAGE);
     78         assertNull(uninstallPackage(KEY_VALUE_RESTORE_APP_PACKAGE));
     79 
     80         clearBackupDataInLocalTransport(SHARED_PREFERENCES_RESTORE_APP_PACKAGE);
     81         assertNull(uninstallPackage(SHARED_PREFERENCES_RESTORE_APP_PACKAGE));
     82     }
     83 
     84     /**
     85      * Test that verifies key/value backup and restore.
     86      *
     87      * The flow of the test:
     88      * 1. Check that app has no saved data
     89      * 2. App saves the predefined values to shared preferences and files.
     90      * 3. Backup the app's data
     91      * 4. Uninstall the app
     92      * 5. Install the app back
     93      * 6. Check that all the shared preferences and files were restored.
     94      */
     95     @Test
     96     public void testKeyValueBackupAndRestore() throws Exception {
     97         if (!mIsBackupSupported) {
     98             CLog.i("android.software.backup feature is not supported on this device");
     99             return;
    100         }
    101 
    102         checkDeviceTest("checkSharedPrefIsEmpty");
    103 
    104         checkDeviceTest("saveSharedPreferencesAndNotifyBackupManager");
    105 
    106         backupNowAndAssertSuccess(KEY_VALUE_RESTORE_APP_PACKAGE);
    107 
    108         assertNull(uninstallPackage(KEY_VALUE_RESTORE_APP_PACKAGE));
    109 
    110         installPackage(KEY_VALUE_RESTORE_APP_APK);
    111 
    112         // Shared preference should be restored
    113         checkDeviceTest("checkSharedPreferencesAreRestored");
    114     }
    115 
    116     /**
    117      * Test that verifies SharedPreference restore behavior.
    118      *
    119      * The tests uses device-side test routines and a test activity in *another* package, since
    120      * the app containing the instrumented tests is killed after each test.
    121      *
    122      * Test logic:
    123      *   1. The activity is launched; it creates a new SharedPreferences instance and writes
    124      *       a known value to the INT_PREF element's via that instance.  The instance is
    125      *       kept live.
    126      *   2. The app is backed up, storing this known value in the backup dataset.
    127      *   3. Next, the activity is instructed to write a different value to the INT_PREF
    128      *       shared preferences element.  At this point, the app's current on-disk state
    129      *       and the live shared preferences instance are in agreement, holding a value
    130      *       different from that in the backup.
    131      *   4. The runner triggers a restore for this app.  This will rewrite the shared prefs
    132      *       file itself with the backed-up content (i.e. different from what was just
    133      *       committed from this activity).
    134      *   5. Finally, the runner instructs the activity to compare the value of its existing
    135      *       shared prefs instance's INT_PREF element with what was previously written.
    136      *       The test passes if these differ, i.e. if the live shared prefs instance picked
    137      *       up the newly-restored data.
    138      */
    139     @Test
    140     public void testSharedPreferencesRestore() throws Exception {
    141         if (!mIsBackupSupported) {
    142             CLog.i("android.software.backup feature is not supported on this device");
    143             return;
    144         }
    145 
    146         checkDeviceTest("launchSharedPrefActivity");
    147 
    148         backupNowAndAssertSuccess(SHARED_PREFERENCES_RESTORE_APP_PACKAGE);
    149 
    150         checkDeviceTest("updateSharedPrefActivity");
    151 
    152         restoreAndAssertSuccess(SHARED_PREFERENCES_RESTORE_APP_PACKAGE);
    153 
    154         checkDeviceTest("checkSharedPrefActivity");
    155     }
    156 
    157     private void checkDeviceTest(String methodName)
    158             throws DeviceNotAvailableException {
    159         super.checkDeviceTest(KEY_VALUE_RESTORE_APP_PACKAGE, KEY_VALUE_RESTORE_DEVICE_TEST_NAME,
    160                 methodName);
    161     }
    162 }
    163