• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.app.sdksandbox;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.os.Bundle;
22 import android.os.Parcel;
23 
24 import com.android.server.sdksandbox.DeviceSupportedBaseTest;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 import java.util.List;
31 
32 /** Tests {@link SharedPreferencesUpdate} APIs. */
33 @RunWith(JUnit4.class)
34 public class SharedPreferencesUpdateUnitTest extends DeviceSupportedBaseTest {
35 
36     private static final String KEY_LONG = "long";
37     private static final String KEY_STRING = "string";
38     private static final long VALUE_LONG = 1L;
39     private static final String VALUE_STRING = "value";
40 
41     private static final List<SharedPreferencesKey> KEYS_TO_SYNC =
42             List.of(
43                     new SharedPreferencesKey(KEY_LONG, SharedPreferencesKey.KEY_TYPE_LONG),
44                     new SharedPreferencesKey(KEY_STRING, SharedPreferencesKey.KEY_TYPE_STRING));
45 
46     @Test
testSharedPreferencesUpdate_DescribeContents()47     public void testSharedPreferencesUpdate_DescribeContents() throws Exception {
48         final SharedPreferencesUpdate update =
49                 new SharedPreferencesUpdate(KEYS_TO_SYNC, getTestBundle());
50         assertThat(update.describeContents()).isEqualTo(0);
51     }
52 
53     @Test
testSharedPreferencesUpdate_IsParcelable()54     public void testSharedPreferencesUpdate_IsParcelable() throws Exception {
55         final SharedPreferencesUpdate update =
56                 new SharedPreferencesUpdate(KEYS_TO_SYNC, getTestBundle());
57 
58         final Parcel p = Parcel.obtain();
59         update.writeToParcel(p, /*flags=*/ 0);
60 
61         // Create SharedPreferencesUpdate with the same parcel
62         p.setDataPosition(0); // rewind
63         final SharedPreferencesUpdate newUpdate =
64                 SharedPreferencesUpdate.CREATOR.createFromParcel(p);
65 
66         // Verify the two objects have same value
67         assertThat(newUpdate.getKeysInUpdate()).containsExactlyElementsIn(update.getKeysInUpdate());
68         assertThat(newUpdate.getData().getString(KEY_STRING)).isEqualTo(VALUE_STRING);
69         assertThat(newUpdate.getData().getLong(KEY_LONG)).isEqualTo(VALUE_LONG);
70     }
71 
getTestBundle()72     Bundle getTestBundle() {
73         final Bundle data = new Bundle();
74         data.putLong(KEY_LONG, VALUE_LONG);
75         data.putString(KEY_STRING, VALUE_STRING);
76         return data;
77     }
78 }
79