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
17package test.instant.cookie;
18
19import android.content.pm.PackageManager;
20import android.os.Debug;
21import android.support.test.InstrumentationRegistry;
22import android.support.test.runner.AndroidJUnit4;
23import org.junit.Test;
24import org.junit.runner.RunWith;
25
26import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertTrue;
29
30@RunWith(AndroidJUnit4.class)
31public class CookieTest {
32    @Test
33    public void testCookieUpdateAndRetrieval() throws Exception {
34        Debug.waitForDebugger();
35        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
36
37        // We should be an instant app
38        assertTrue(pm.isInstantApp());
39
40        // The max cookie size is greater than zero
41        assertTrue(pm.getInstantAppCookieMaxSize() > 0);
42
43        // Initially there is no cookie
44        byte[] cookie = pm.getInstantAppCookie();
45        assertTrue(cookie != null && cookie.length == 0);
46
47        // Setting a cookie below max size should work
48        assertTrue(pm.setInstantAppCookie("1".getBytes()));
49
50//        // Setting a cookie above max size should not work
51//        assertFalse(pm.setInstantAppCookie(
52//                new byte[pm.getInstantAppCookieMaxSize() + 1]));
53//
54//        // Ensure cookie not modified
55//        assertEquals("1", new String(pm.getInstantAppCookie()));
56    }
57
58//    @Test
59//    public void testCookiePersistedAcrossInstantInstalls1() throws Exception {
60//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
61//
62//        // Set a cookie to later check when reinstalled as instant app
63//        assertTrue(pm.setInstantAppCookie("2".getBytes()));
64//    }
65//
66//    @Test
67//    public void testCookiePersistedAcrossInstantInstalls2() throws Exception {
68//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
69//
70//        // After the upgrade the cookie should be the same
71//        assertEquals("2", new String(pm.getInstantAppCookie()));
72//    }
73//
74//    @Test
75//    public void testCookiePersistedUpgradeFromInstant1() throws Exception {
76//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
77//
78//        // Make sure we are an instant app
79//        assertTrue(pm.isInstantApp());
80//
81//        // Set a cookie to later check when upgrade to a normal app
82//        assertTrue(pm.setInstantAppCookie("3".getBytes()));
83//    }
84//
85//    @Test
86//    public void testCookiePersistedUpgradeFromInstant2() throws Exception {
87//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
88//
89//        // Make sure we are not an instant app
90//        assertFalse(pm.isInstantApp());
91//
92//        // The cookie survives the upgrade to a normal app
93//        assertEquals("3", new String(pm.getInstantAppCookie()));
94//    }
95//
96//    @Test
97//    public void testCookieResetOnNonInstantReinstall1() throws Exception {
98//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
99//
100//        // Set a cookie to later check when reinstalled as normal app
101//        assertTrue(pm.setInstantAppCookie("4".getBytes()));
102//    }
103//
104//    @Test
105//    public void testCookieResetOnNonInstantReinstall2() throws Exception {
106//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
107//
108//        // The cookie should have been wiped if non-instant app is uninstalled
109//        byte[] cookie = pm.getInstantAppCookie();
110//        assertTrue(cookie != null && cookie.length == 0);
111//    }
112}
113