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.restoreanyversionapp; 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.content.BroadcastReceiver; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.IntentFilter; 29 import android.content.SharedPreferences; 30 import android.content.pm.PackageInfo; 31 import android.content.pm.PackageManager.NameNotFoundException; 32 import android.support.test.runner.AndroidJUnit4; 33 import android.util.Log; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 import java.util.concurrent.CountDownLatch; 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * Device side routines to be invoked by the host side RestoreAnyVersionHostSideTest. These 44 * are not designed to be called in any other way, as they rely on state set up by the host side 45 * test. 46 */ 47 @RunWith(AndroidJUnit4.class) 48 public class RestoreAnyVersionTest { 49 private static final String TAG = "BackupTestRestoreAnyVer"; 50 51 static final String TEST_PREFS_1 = "test-prefs-1"; 52 static final String INT_PREF = "int-pref"; 53 static final int DEFAULT_INT_VALUE = 0; 54 55 private static final int OLD_VERSION = 100; 56 private static final int NEW_VERSION = 200; 57 58 private Context mContext; 59 60 @Before setUp()61 public void setUp() { 62 Log.i(TAG, "set up"); 63 mContext = getTargetContext(); 64 } 65 66 @Test saveSharedPrefValue()67 public void saveSharedPrefValue() throws Exception { 68 saveAppVersionCodeToSharedPreference(); 69 } 70 71 @Test checkAppVersionIsNew()72 public void checkAppVersionIsNew() throws Exception { 73 PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo( 74 mContext.getPackageName(), 0); 75 assertEquals(NEW_VERSION, packageInfo.versionCode); 76 } 77 78 @Test checkSharedPrefIsEmpty()79 public void checkSharedPrefIsEmpty() throws Exception { 80 int intValue = getAppSharedPreference(); 81 assertEquals(DEFAULT_INT_VALUE, intValue); 82 } 83 84 @Test checkSharedPrefIsNew()85 public void checkSharedPrefIsNew() throws Exception { 86 int intValue = getAppSharedPreference(); 87 assertEquals(NEW_VERSION, intValue); 88 } 89 90 @Test checkAppVersionIsOld()91 public void checkAppVersionIsOld() throws Exception { 92 PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo( 93 mContext.getPackageName(), 0); 94 assertEquals(OLD_VERSION, packageInfo.versionCode); 95 } 96 97 @Test checkSharedPrefIsOld()98 public void checkSharedPrefIsOld() throws Exception { 99 int intValue = getAppSharedPreference(); 100 assertEquals(OLD_VERSION, intValue); 101 } 102 saveAppVersionCodeToSharedPreference()103 private void saveAppVersionCodeToSharedPreference() throws NameNotFoundException { 104 SharedPreferences prefs = mContext.getSharedPreferences(TEST_PREFS_1, MODE_PRIVATE); 105 SharedPreferences.Editor editor = prefs.edit(); 106 editor.putInt(INT_PREF, 107 mContext.getPackageManager() 108 .getPackageInfo(mContext.getPackageName(), 0).versionCode); 109 assertTrue("Error committing shared preference value", editor.commit()); 110 } 111 getAppSharedPreference()112 private int getAppSharedPreference() throws InterruptedException { 113 SharedPreferences prefs = mContext.getSharedPreferences(TEST_PREFS_1, MODE_PRIVATE); 114 int intValue = prefs.getInt(INT_PREF, DEFAULT_INT_VALUE); 115 116 Log.i(TAG, "Read the shared preference value: " + intValue); 117 118 return intValue; 119 } 120 } 121