summaryrefslogtreecommitdiffstats
path: root/tests/AndroidTests/src/com/android/unit_tests/CheckinProviderTest.java
blob: f9a2ec0f08a6d0c6ff9f27820c0059313987e368 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (C) 2006 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 com.android.unit_tests;

import org.apache.commons.codec.binary.Base64;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Checkin;
import android.server.checkin.CheckinProvider;
import android.server.data.BuildData;
import android.server.data.CrashData;
import android.server.data.ThrowableData;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;

import java.io.DataInputStream;
import java.io.ByteArrayInputStream;

/** Unit test for {@link CheckinProvider}. */
public class CheckinProviderTest extends AndroidTestCase {
    @MediumTest
    public void testEventReport() {
        long start = System.currentTimeMillis();
        ContentResolver r = getContext().getContentResolver();
        Checkin.logEvent(r, Checkin.Events.Tag.TEST, "Test Value");

        Cursor c = r.query(Checkin.Events.CONTENT_URI,
                null,
                Checkin.Events.TAG + "=?",
                new String[] { Checkin.Events.Tag.TEST.toString() },
                null);

        long id = -1;
        while (c.moveToNext()) {
            String tag = c.getString(c.getColumnIndex(Checkin.Events.TAG));
            String value = c.getString(c.getColumnIndex(Checkin.Events.VALUE));
            long date = c.getLong(c.getColumnIndex(Checkin.Events.DATE));
            assertEquals(Checkin.Events.Tag.TEST.toString(), tag);
            if ("Test Value".equals(value) && date >= start) {
                assertTrue(id < 0);
                id = c.getInt(c.getColumnIndex(Checkin.Events._ID));
            }
        }
        assertTrue(id > 0);

        int rows = r.delete(ContentUris.withAppendedId(Checkin.Events.CONTENT_URI, id), null, null);
        assertEquals(1, rows);
        c.requery();
        while (c.moveToNext()) {
            long date = c.getLong(c.getColumnIndex(Checkin.Events.DATE));
            assertTrue(date < start);  // Have deleted the only newer TEST.
        }

        c.close();
    }

    @MediumTest
    public void testStatsUpdate() {
        ContentResolver r = getContext().getContentResolver();

        // First, delete any existing data associated with the TEST tag.
        Uri uri = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, 0, 0);
        assertNotNull(uri);
        assertEquals(1, r.delete(uri, null, null));
        assertFalse(r.query(uri, null, null, null, null).moveToNext());

        // Now, add a known quantity to the TEST tag.
        Uri u2 = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, 1, 0.5);
        assertFalse(uri.equals(u2));

        Cursor c = r.query(u2, null, null, null, null);
        assertTrue(c.moveToNext());
        assertEquals(1, c.getInt(c.getColumnIndex(Checkin.Stats.COUNT)));
        assertEquals(0.5, c.getDouble(c.getColumnIndex(Checkin.Stats.SUM)));
        assertFalse(c.moveToNext());  // Only one.

        // Add another known quantity to TEST (should sum with the first).
        Uri u3 = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, 2, 1.0);
        assertEquals(u2, u3);
        c.requery();
        assertTrue(c.moveToNext());
        assertEquals(3, c.getInt(c.getColumnIndex(Checkin.Stats.COUNT)));
        assertEquals(1.5, c.getDouble(c.getColumnIndex(Checkin.Stats.SUM)));
        assertFalse(c.moveToNext());  // Only one.

        // Now subtract the values; the whole row should disappear.
        Uri u4 = Checkin.updateStats(r, Checkin.Stats.Tag.TEST, -3, -1.5);
        assertNull(u4);
        c.requery();
        assertFalse(c.moveToNext());  // Row has been deleted.
        c.close();
    }

    @MediumTest
    public void testCrashReport() throws Exception {
        long start = System.currentTimeMillis();
        ContentResolver r = getContext().getContentResolver();

        // Log a test (fake) crash report.
        Checkin.reportCrash(r, new CrashData(
                "Test",
                "Test Activity",
                new BuildData("Test Build", "123", start),
                new ThrowableData(new RuntimeException("Test Exception"))));


        // Crashes aren't indexed; go through them all to find the one we added.
        Cursor c = r.query(Checkin.Crashes.CONTENT_URI, null, null, null, null);

        Uri uri = null;
        while (c.moveToNext()) {
            String coded = c.getString(c.getColumnIndex(Checkin.Crashes.DATA));
            byte[] bytes = Base64.decodeBase64(coded.getBytes());
            CrashData crash = new CrashData(
                    new DataInputStream(new ByteArrayInputStream(bytes)));

            // Should be exactly one recently added "Test" crash.
            if (crash.getId().equals("Test") && crash.getTime() > start) {
                assertEquals("Test Activity", crash.getActivity());
                assertEquals("Test Build", crash.getBuildData().getFingerprint());
                assertEquals("Test Exception",
                        crash.getThrowableData().getMessage());

                assertNull(uri);
                uri = ContentUris.withAppendedId(Checkin.Crashes.CONTENT_URI, c.getInt(c.getColumnIndex(Checkin.Crashes._ID)));
            }
        }
        assertNotNull(uri);
        c.close();

        // Update the "logs" column.
        ContentValues values = new ContentValues();
        values.put(Checkin.Crashes.LOGS, "Test Logs");
        assertEquals(1, r.update(uri, values, null, null));

        c = r.query(uri, null, null, null, null);
        assertTrue(c.moveToNext());
        String logs = c.getString(c.getColumnIndex(Checkin.Crashes.LOGS));
        assertEquals("Test Logs", logs);
        c.deleteRow();
        c.close();

        c.requery();
        assertFalse(c.moveToNext());
        c.close();
    }

    @MediumTest
    public void testPropertiesRestricted() throws Exception {
        ContentResolver r = getContext().getContentResolver();

        // The test app doesn't have the permission to access properties,
        // so any attempt to do so should fail.
        try {
            r.insert(Checkin.Properties.CONTENT_URI, new ContentValues());
            fail("SecurityException expected");
        } catch (SecurityException e) {
            // expected
        }

        try {
            r.query(Checkin.Properties.CONTENT_URI, null, null, null, null);
            fail("SecurityException expected");
        } catch (SecurityException e) {
            // expected
        }
    }
}