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.keyvaluerestoreapp; 18 19 import static android.content.Context.MODE_PRIVATE; 20 import static android.support.test.InstrumentationRegistry.getTargetContext; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertTrue; 24 25 import android.app.backup.BackupManager; 26 import android.app.backup.FileBackupHelper; 27 import android.app.backup.SharedPreferencesBackupHelper; 28 import android.content.BroadcastReceiver; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.IntentFilter; 32 import android.content.SharedPreferences; 33 import android.support.test.runner.AndroidJUnit4; 34 import android.util.Log; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 import java.io.File; 42 import java.io.FileNotFoundException; 43 import java.io.PrintWriter; 44 import java.util.Scanner; 45 import java.util.concurrent.CountDownLatch; 46 import java.util.concurrent.TimeUnit; 47 48 /** 49 * Device side routines to be invoked by the host side KeyValueBackupRestoreHostSideTest. These 50 * are not designed to be called in any other way, as they rely on state set up by the host side 51 * test. 52 * 53 * Some tests invoked by KeyValueBackupRestoreHostSideTest#testSharedPreferencesRestore() are 54 * interacting with SharedPrefsRestoreTestActivity from another package. 55 * 56 */ 57 @RunWith(AndroidJUnit4.class) 58 public class KeyValueBackupRestoreTest { 59 private static final String TAG = "KeyValueBackupRestore"; 60 61 // Names and values of the test files for 62 // KeyValueBackupRestoreHostSideTest#testKeyValueBackupAndRestore() 63 private static final String TEST_FILE_1 = "test-file-1"; 64 private static final String TEST_FILE_1_DATA = "test-file-1-data"; 65 66 private static final String TEST_FILE_2 = "test-file-2"; 67 private static final String TEST_FILE_2_DATA = "test-file-2-data"; 68 69 private static final String TEST_PREFS_1 = "test-prefs-1"; 70 private static final String INT_PREF = "int-pref"; 71 private static final int INT_PREF_VALUE = 111; 72 private static final String BOOL_PREF = "bool-pref"; 73 private static final boolean BOOL_PREF_VALUE = true; 74 75 private static final String TEST_PREFS_2 = "test-prefs-2"; 76 private static final String FLOAT_PREF = "float-pref"; 77 private static final float FLOAT_PREF_VALUE = 0.12345f; 78 private static final String LONG_PREF = "long-pref"; 79 private static final long LONG_PREF_VALUE = 12345L; 80 private static final String STRING_PREF = "string-pref"; 81 private static final String STRING_PREF_VALUE = "string-pref-value"; 82 83 private static final int DEFAULT_INT_VALUE = 0; 84 private static final boolean DEFAULT_BOOL_VALUE = false; 85 private static final float DEFAULT_FLOAT_VALUE = 0.0f; 86 private static final long DEFAULT_LONG_VALUE = 0L; 87 private static final String DEFAULT_STRING_VALUE = null; 88 private static final String DEFAULT_FILE_CONTENT = ""; 89 90 91 /** Package name of the test app for 92 * KeyValueBackupRestoreHostSideTest#testSharedPreferencesRestore() */ 93 private static final String SHARED_PREFERENCES_RESTORE_PACKAGE_NAME = 94 "android.cts.backup.sharedprefrestoreapp"; 95 96 /** Test activity for KeyValueBackupRestoreHostSideTest#testSharedPreferencesRestore() */ 97 private static final String SHARED_PREFERENCES_RESTORE_ACTIVITY_NAME = 98 SHARED_PREFERENCES_RESTORE_PACKAGE_NAME + ".SharedPrefsRestoreTestActivity"; 99 100 // Shared prefs test activity actions 101 private static final String INIT_ACTION = "android.backup.cts.backuprestore.INIT"; 102 private static final String UPDATE_ACTION = "android.backup.cts.backuprestore.UPDATE"; 103 private static final String TEST_ACTION = "android.backup.cts.backuprestore.TEST"; 104 105 // Action for returning the result of shared preference activity's operations 106 private static final String RESULT_ACTION = "android.backup.cts.backuprestore.RESULT"; 107 private static final String EXTRA_SUCCESS = "EXTRA_SUCCESS"; 108 109 private static final int ACTIVITY_TEST_TIMEOUT_MS = 5000; 110 111 private Context mContext; 112 113 private int mIntValue; 114 private boolean mBoolValue; 115 private float mFloatValue; 116 private long mLongValue; 117 private String mStringValue; 118 private String mFileContent1; 119 private String mFileContent2; 120 121 private boolean mSharedPrefTestSuccess; 122 private CountDownLatch mLatch; 123 private Intent mSharedPrefActivityIntent; 124 125 private final BroadcastReceiver mSharedPrefencesReceiver = new BroadcastReceiver() { 126 @Override 127 public void onReceive(Context context, Intent intent) { 128 Log.i(TAG, "Received shared preference activity result broadcast"); 129 mSharedPrefTestSuccess = intent.getBooleanExtra(EXTRA_SUCCESS, false); 130 mLatch.countDown(); 131 } 132 }; 133 134 @Before 135 public void setUp() { 136 Log.i(TAG, "set up"); 137 138 mContext = getTargetContext(); 139 mLatch = new CountDownLatch(1); 140 mSharedPrefTestSuccess = false; 141 mSharedPrefActivityIntent = new Intent() 142 .setClassName(SHARED_PREFERENCES_RESTORE_PACKAGE_NAME, 143 SHARED_PREFERENCES_RESTORE_ACTIVITY_NAME) 144 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 145 mContext.registerReceiver(mSharedPrefencesReceiver, new IntentFilter(RESULT_ACTION)); 146 } 147 148 @After 149 public void tearDown() { 150 mContext.unregisterReceiver(mSharedPrefencesReceiver); 151 } 152 153 @Test 154 public void saveSharedPreferencesAndNotifyBackupManager() throws Exception { 155 saveSharedPreferencesValues(); 156 157 //Let BackupManager know that the data has changed 158 BackupManager backupManager = new BackupManager(mContext); 159 backupManager.dataChanged(); 160 } 161 162 @Test 163 public void checkSharedPrefIsEmpty() throws Exception { 164 readSharedPrefValues(); 165 assertEquals(DEFAULT_INT_VALUE, mIntValue); 166 assertEquals(DEFAULT_BOOL_VALUE, mBoolValue); 167 assertEquals(DEFAULT_FLOAT_VALUE, mFloatValue, 0.001f); 168 assertEquals(DEFAULT_LONG_VALUE, mLongValue); 169 assertEquals(DEFAULT_STRING_VALUE, mStringValue); 170 assertEquals(DEFAULT_FILE_CONTENT, mFileContent1); 171 assertEquals(DEFAULT_FILE_CONTENT, mFileContent2); 172 } 173 174 @Test 175 public void checkSharedPreferencesAreRestored() throws Exception { 176 readSharedPrefValues(); 177 assertEquals(INT_PREF_VALUE, mIntValue); 178 assertEquals(BOOL_PREF_VALUE, mBoolValue); 179 assertEquals(FLOAT_PREF_VALUE, mFloatValue, 0.001f); 180 assertEquals(LONG_PREF_VALUE, mLongValue); 181 assertEquals(STRING_PREF_VALUE, mStringValue); 182 assertEquals(TEST_FILE_1_DATA, mFileContent1); 183 assertEquals(TEST_FILE_2_DATA, mFileContent2); 184 } 185 186 @Test 187 public void launchSharedPrefActivity() throws Exception { 188 mContext.startActivity(mSharedPrefActivityIntent.setAction(INIT_ACTION)); 189 190 assertTrue("Activity init timed out", 191 mLatch.await(ACTIVITY_TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)); 192 assertTrue("Saving shared preferences didn't succeed", mSharedPrefTestSuccess); 193 } 194 195 @Test 196 public void updateSharedPrefActivity() throws Exception { 197 mContext.startActivity(mSharedPrefActivityIntent.setAction(UPDATE_ACTION)); 198 199 assertTrue("Activity launch timed out", 200 mLatch.await(ACTIVITY_TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)); 201 assertTrue("Saving shared preferences didn't succeed", mSharedPrefTestSuccess); 202 } 203 204 @Test 205 public void checkSharedPrefActivity() throws Exception { 206 mContext.startActivity(mSharedPrefActivityIntent.setAction(TEST_ACTION)); 207 208 assertTrue("Activity test timed out", 209 mLatch.await(ACTIVITY_TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)); 210 assertTrue("Shared preference value wasn't updated from the restore set", 211 mSharedPrefTestSuccess); 212 } 213 214 /** Saves predefined constant values to shared preferences and files. */ 215 private void saveSharedPreferencesValues() throws FileNotFoundException { 216 SharedPreferences prefs = mContext.getSharedPreferences(TEST_PREFS_1, MODE_PRIVATE); 217 SharedPreferences.Editor editor = prefs.edit(); 218 editor.putInt(INT_PREF, INT_PREF_VALUE); 219 editor.putBoolean(BOOL_PREF, BOOL_PREF_VALUE); 220 assertTrue("Error committing shared preference 1 value", editor.commit()); 221 222 prefs = mContext.getSharedPreferences(TEST_PREFS_2, MODE_PRIVATE); 223 editor = prefs.edit(); 224 editor.putFloat(FLOAT_PREF, FLOAT_PREF_VALUE); 225 editor.putLong(LONG_PREF, LONG_PREF_VALUE); 226 editor.putString(STRING_PREF, STRING_PREF_VALUE); 227 assertTrue("Error committing shared preference 2 value", editor.commit()); 228 229 File file = new File(mContext.getFilesDir(), TEST_FILE_1); 230 PrintWriter writer = new PrintWriter(file); 231 writer.write(TEST_FILE_1_DATA); 232 writer.close(); 233 assertTrue("Error writing file 1 data", file.exists()); 234 235 file = new File(mContext.getFilesDir(), TEST_FILE_2); 236 writer = new PrintWriter(file); 237 writer.write(TEST_FILE_2_DATA); 238 writer.close(); 239 assertTrue("Error writing file 2 data", file.exists()); 240 } 241 242 private void readSharedPrefValues() throws InterruptedException { 243 SharedPreferences prefs = mContext.getSharedPreferences(TEST_PREFS_1, MODE_PRIVATE); 244 mIntValue = prefs.getInt(INT_PREF, DEFAULT_INT_VALUE); 245 mBoolValue = prefs.getBoolean(BOOL_PREF, DEFAULT_BOOL_VALUE); 246 247 prefs = mContext.getSharedPreferences(TEST_PREFS_2, MODE_PRIVATE); 248 mFloatValue = prefs.getFloat(FLOAT_PREF, DEFAULT_FLOAT_VALUE); 249 mLongValue = prefs.getLong(LONG_PREF, DEFAULT_LONG_VALUE); 250 mStringValue = prefs.getString(STRING_PREF, DEFAULT_STRING_VALUE); 251 252 mFileContent1 = readFileContent(TEST_FILE_1); 253 mFileContent2 = readFileContent(TEST_FILE_2); 254 255 Log.i(TAG, INT_PREF + ":" + mIntValue + "\n" 256 + BOOL_PREF + ":" + mBoolValue + "\n" 257 + FLOAT_PREF + ":" + mFloatValue + "\n" 258 + LONG_PREF + ":" + mLongValue + "\n" 259 + STRING_PREF + ":" + mStringValue + "\n" 260 + TEST_FILE_1 + ":" + mFileContent1 + "\n" 261 + TEST_FILE_2 + ":" + mFileContent2 + "\n"); 262 } 263 264 private String readFileContent(String fileName) { 265 StringBuilder contents = new StringBuilder(); 266 Scanner scanner = null; 267 try { 268 scanner = new Scanner(new File(mContext.getFilesDir(), fileName)); 269 while (scanner.hasNext()) { 270 contents.append(scanner.nextLine()); 271 } 272 scanner.close(); 273 } catch (FileNotFoundException e) { 274 Log.i(TAG, "Couldn't find test file but this may be fine..."); 275 } finally { 276 if (scanner != null) { 277 scanner.close(); 278 } 279 } 280 return contents.toString(); 281 } 282 283 public static SharedPreferencesBackupHelper getSharedPreferencesBackupHelper(Context context) { 284 return new SharedPreferencesBackupHelper(context, TEST_PREFS_1, TEST_PREFS_2); 285 } 286 287 public static FileBackupHelper getFileBackupHelper(Context context) { 288 return new FileBackupHelper(context, TEST_FILE_1, TEST_FILE_2); 289 } 290 } 291