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 com.android.sdksandbox.cts.host;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.app.sdksandbox.hosttestutils.DeviceSupportHostUtils;
24 
25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
26 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @RunWith(DeviceJUnit4ClassRunner.class)
34 public final class CtsSdkSandboxStorageHostTest extends BaseHostJUnit4Test {
35 
36     private static final String TEST_APP_PACKAGE_NAME = "com.android.sdksandbox.cts.app";
37     private static final String TEST_APP_APK_NAME = "CtsSdkSandboxHostTestApp.apk";
38 
39     private final DeviceSupportHostUtils mDeviceSupportUtils = new DeviceSupportHostUtils(this);
40 
41     /**
42      * Runs the given phase of a test by calling into the device. Throws an exception if the test
43      * phase fails.
44      *
45      * <p>For example, <code>runPhase("testExample");</code>
46      */
runPhase(String phase)47     private void runPhase(String phase) throws Exception {
48         assertThat(
49                         runDeviceTests(
50                                 TEST_APP_PACKAGE_NAME,
51                                 TEST_APP_PACKAGE_NAME + ".CtsSdkSandboxStorageTestApp",
52                                 phase))
53                 .isTrue();
54     }
55 
56     @Before
setUp()57     public void setUp() throws Exception {
58         assumeTrue("Device supports SdkSandbox", mDeviceSupportUtils.isSdkSandboxSupported());
59         uninstallPackage(TEST_APP_PACKAGE_NAME);
60     }
61 
62     @After
tearDown()63     public void tearDown() throws Exception {
64         uninstallPackage(TEST_APP_PACKAGE_NAME);
65     }
66 
67     @Test
testSharedPreferences_IsSyncedFromAppToSandbox()68     public void testSharedPreferences_IsSyncedFromAppToSandbox() throws Exception {
69         installPackage(TEST_APP_APK_NAME);
70         runPhase("testSharedPreferences_IsSyncedFromAppToSandbox");
71     }
72 
73     @Test
testSharedPreferences_SyncPropagatesUpdates()74     public void testSharedPreferences_SyncPropagatesUpdates() throws Exception {
75         installPackage(TEST_APP_APK_NAME);
76         runPhase("testSharedPreferences_SyncPropagatesUpdates");
77     }
78 
79     @Test
testSharedPreferences_SyncStartedBeforeLoadingSdk()80     public void testSharedPreferences_SyncStartedBeforeLoadingSdk() throws Exception {
81         installPackage(TEST_APP_APK_NAME);
82         runPhase("testSharedPreferences_SyncStartedBeforeLoadingSdk");
83     }
84 
85     @Test
testSharedPreferences_SyncRemoveKeys()86     public void testSharedPreferences_SyncRemoveKeys() throws Exception {
87         installPackage(TEST_APP_APK_NAME);
88         runPhase("testSharedPreferences_SyncRemoveKeys");
89     }
90 
91     @Test
testSharedPreferences_SyncedDataClearedOnSandboxRestart()92     public void testSharedPreferences_SyncedDataClearedOnSandboxRestart() throws Exception {
93         installPackage(TEST_APP_APK_NAME);
94         runPhase("testSharedPreferences_IsSyncedFromAppToSandbox");
95         runPhase("testSharedPreferences_SyncedDataClearedOnSandboxRestart");
96     }
97 }
98