aboutsummaryrefslogtreecommitdiff
path: root/InstantCookieApp/src/test/instant/cookie/CookieTest.java
blob: 4cc6f86a9dcf16d5cbec5e9a9572177d29245218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test.instant.cookie;

import android.content.pm.PackageManager;
import android.os.Debug;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
public class CookieTest {
    @Test
    public void testCookieUpdateAndRetrieval() throws Exception {
        Debug.waitForDebugger();
        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();

        // We should be an instant app
        assertTrue(pm.isInstantApp());

        // The max cookie size is greater than zero
        assertTrue(pm.getInstantAppCookieMaxSize() > 0);

        // Initially there is no cookie
        byte[] cookie = pm.getInstantAppCookie();
        assertTrue(cookie != null && cookie.length == 0);

        // Setting a cookie below max size should work
        assertTrue(pm.setInstantAppCookie("1".getBytes()));

//        // Setting a cookie above max size should not work
//        assertFalse(pm.setInstantAppCookie(
//                new byte[pm.getInstantAppCookieMaxSize() + 1]));
//
//        // Ensure cookie not modified
//        assertEquals("1", new String(pm.getInstantAppCookie()));
    }

//    @Test
//    public void testCookiePersistedAcrossInstantInstalls1() throws Exception {
//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
//
//        // Set a cookie to later check when reinstalled as instant app
//        assertTrue(pm.setInstantAppCookie("2".getBytes()));
//    }
//
//    @Test
//    public void testCookiePersistedAcrossInstantInstalls2() throws Exception {
//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
//
//        // After the upgrade the cookie should be the same
//        assertEquals("2", new String(pm.getInstantAppCookie()));
//    }
//
//    @Test
//    public void testCookiePersistedUpgradeFromInstant1() throws Exception {
//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
//
//        // Make sure we are an instant app
//        assertTrue(pm.isInstantApp());
//
//        // Set a cookie to later check when upgrade to a normal app
//        assertTrue(pm.setInstantAppCookie("3".getBytes()));
//    }
//
//    @Test
//    public void testCookiePersistedUpgradeFromInstant2() throws Exception {
//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
//
//        // Make sure we are not an instant app
//        assertFalse(pm.isInstantApp());
//
//        // The cookie survives the upgrade to a normal app
//        assertEquals("3", new String(pm.getInstantAppCookie()));
//    }
//
//    @Test
//    public void testCookieResetOnNonInstantReinstall1() throws Exception {
//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
//
//        // Set a cookie to later check when reinstalled as normal app
//        assertTrue(pm.setInstantAppCookie("4".getBytes()));
//    }
//
//    @Test
//    public void testCookieResetOnNonInstantReinstall2() throws Exception {
//        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
//
//        // The cookie should have been wiped if non-instant app is uninstalled
//        byte[] cookie = pm.getInstantAppCookie();
//        assertTrue(cookie != null && cookie.length == 0);
//    }
}