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