1 /*
2  * Copyright (C) 2018 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.cts.instantupgradeapp;
18 
19 import static org.hamcrest.CoreMatchers.is;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.content.Context;
25 import android.content.SharedPreferences;
26 
27 import androidx.test.InstrumentationRegistry;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 
36 @RunWith(AndroidJUnit4.class)
37 public class ClientTest {
38     private static final String TAG = "InstantUpgradeApp";
39 
40     @Test
testInstantApplicationWritePreferences()41     public void testInstantApplicationWritePreferences() throws Exception {
42         // only done in the context of an instant application
43         assertTrue(InstrumentationRegistry.getContext().getPackageManager().isInstantApp());
44         final SharedPreferences prefWriter =
45                 InstrumentationRegistry.getContext().getSharedPreferences("preferences", 0);
46         prefWriter.edit().putString("test", "value").commit();
47     }
48     @Test
testFullApplicationReadPreferences()49     public void testFullApplicationReadPreferences() throws Exception {
50         // only done in the context of an full application
51         assertFalse(InstrumentationRegistry.getContext().getPackageManager().isInstantApp());
52         final SharedPreferences prefReader =
53                 InstrumentationRegistry.getContext().getSharedPreferences("preferences", 0);
54         assertThat(prefReader.getString("test", "default"), is("value"));
55     }
56     @Test
testInstantApplicationWriteFile()57     public void testInstantApplicationWriteFile() throws Exception {
58         // only done in the context of an instant application
59         assertTrue(InstrumentationRegistry.getContext().getPackageManager().isInstantApp());
60         try (FileOutputStream fos = InstrumentationRegistry
61                 .getContext().openFileOutput("test.txt", Context.MODE_PRIVATE)) {
62             fos.write("MyTest".getBytes());
63         }
64     }
65     @Test
testFullApplicationReadFile()66     public void testFullApplicationReadFile() throws Exception {
67         // only done in the context of an full application
68         assertFalse(InstrumentationRegistry.getContext().getPackageManager().isInstantApp());
69         try (FileInputStream fis = InstrumentationRegistry
70                 .getContext().openFileInput("test.txt")) {
71             final StringBuffer buffer = new StringBuffer();
72             final byte[] b = new byte[1024];
73             int count;
74             while ((count = fis.read(b)) != -1) {
75                 buffer.append(new String(b, 0, count));
76             }
77             assertThat(buffer.toString(), is("MyTest"));
78         }
79     }
80 }
81