• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.profilekeyvalueapp;
18 
19 import static androidx.test.InstrumentationRegistry.getTargetContext;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.content.Context;
24 import android.content.SharedPreferences;
25 import android.platform.test.annotations.AppModeFull;
26 
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 /** Device side test invoked by profile host side tests. */
34 @RunWith(AndroidJUnit4.class)
35 @AppModeFull
36 public class ProfileKeyValueBackupRestoreTest {
37     static final String TEST_PREFS_NAME = "user-test-prefs";
38     private static final String PREFS_USER_KEY = "userId";
39     private static final int PREFS_USER_DEFAULT = -1000;
40 
41     private int mUserId;
42     private SharedPreferences mPreferences;
43 
44     @Before
setUp()45     public void setUp() {
46         Context context = getTargetContext();
47         mUserId = context.getUserId();
48         mPreferences = context.getSharedPreferences(TEST_PREFS_NAME, Context.MODE_PRIVATE);
49     }
50 
51     @Test
assertSharedPrefsIsEmpty()52     public void assertSharedPrefsIsEmpty() {
53         int userIdPref = mPreferences.getInt(PREFS_USER_KEY, PREFS_USER_DEFAULT);
54         assertThat(userIdPref).isEqualTo(PREFS_USER_DEFAULT);
55     }
56 
57     @Test
writeSharedPrefsAndAssertSuccess()58     public void writeSharedPrefsAndAssertSuccess() {
59         SharedPreferences.Editor editor = mPreferences.edit();
60         editor.putInt(PREFS_USER_KEY, mUserId);
61         assertThat(editor.commit()).isTrue();
62     }
63 
64     @Test
assertSharedPrefsRestored()65     public void assertSharedPrefsRestored() {
66         int userIdPref = mPreferences.getInt(PREFS_USER_KEY, PREFS_USER_DEFAULT);
67         assertThat(userIdPref).isEqualTo(mUserId);
68     }
69 }
70