summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/photos/data/PhotoProviderTest.java
blob: 525abece442260c1ac45aa8eb4b6109bae5252e0 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2013 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.photos.data;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;
import android.test.ProviderTestCase2;

import com.android.photos.data.PhotoProvider.Albums;
import com.android.photos.data.PhotoProvider.Metadata;
import com.android.photos.data.PhotoProvider.Photos;

public class PhotoProviderTest extends ProviderTestCase2<PhotoProvider> {
    @SuppressWarnings("unused")
    private static final String TAG = PhotoProviderTest.class.getSimpleName();

    private static final String MIME_TYPE = "test/test";
    private static final String ALBUM_NAME = "My Album";
    private static final long ALBUM_SERVER_ID = 100;
    private static final long PHOTO_SERVER_ID = 50;
    private static final String META_KEY = "mykey";
    private static final String META_VALUE = "myvalue";

    private static final Uri NO_TABLE_URI = PhotoProvider.BASE_CONTENT_URI;
    private static final Uri BAD_TABLE_URI = Uri.withAppendedPath(PhotoProvider.BASE_CONTENT_URI,
            "bad_table");

    private static final String WHERE_METADATA_PHOTOS_ID = Metadata.PHOTO_ID + " = ?";
    private static final String WHERE_METADATA = Metadata.PHOTO_ID + " = ? AND " + Metadata.KEY
            + " = ?";

    private long mAlbumId;
    private long mPhotoId;
    private long mMetadataId;

    private SQLiteOpenHelper mDBHelper;
    private ContentResolver mResolver;
    private NotificationWatcher mNotifications = new NotificationWatcher();

    public PhotoProviderTest() {
        super(PhotoProvider.class, PhotoProvider.AUTHORITY);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mResolver = getMockContentResolver();
        PhotoProvider provider = (PhotoProvider) getProvider();
        provider.setMockNotification(mNotifications);
        mDBHelper = provider.getDatabaseHelper();
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        db.beginTransaction();
        try {
            PhotoDatabaseUtils.insertAlbum(db, null, ALBUM_NAME, Albums.VISIBILITY_PRIVATE,
                    ALBUM_SERVER_ID);
            mAlbumId = PhotoDatabaseUtils.queryAlbumIdFromServerId(db, ALBUM_SERVER_ID);
            PhotoDatabaseUtils.insertPhoto(db, PHOTO_SERVER_ID, 100, 100,
                    System.currentTimeMillis(), mAlbumId, MIME_TYPE);
            mPhotoId = PhotoDatabaseUtils.queryPhotoIdFromServerId(db, PHOTO_SERVER_ID);
            PhotoDatabaseUtils.insertMetadata(db, mPhotoId, META_KEY, META_VALUE);
            String[] projection = {
                    BaseColumns._ID,
            };
            Cursor cursor = db.query(Metadata.TABLE, projection, null, null, null, null, null);
            cursor.moveToNext();
            mMetadataId = cursor.getLong(0);
            cursor.close();
            db.setTransactionSuccessful();
            mNotifications.reset();
        } finally {
            db.endTransaction();
        }
    }

    @Override
    protected void tearDown() throws Exception {
        mDBHelper.close();
        mDBHelper = null;
        super.tearDown();
        getMockContext().deleteDatabase(PhotoProvider.DB_NAME);
    }

    public void testDelete() {
        try {
            mResolver.delete(NO_TABLE_URI, null, null);
            fail("Exeption should be thrown when no table given");
        } catch (Exception e) {
            // expected exception
        }
        try {
            mResolver.delete(BAD_TABLE_URI, null, null);
            fail("Exeption should be thrown when deleting from a table that doesn't exist");
        } catch (Exception e) {
            // expected exception
        }

        String[] selectionArgs = {
            String.valueOf(mPhotoId)
        };
        // Delete some metadata
        assertEquals(1,
                mResolver.delete(Metadata.CONTENT_URI, WHERE_METADATA_PHOTOS_ID, selectionArgs));
        Uri photoUri = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId);
        assertEquals(1, mResolver.delete(photoUri, null, null));
        Uri albumUri = ContentUris.withAppendedId(Albums.CONTENT_URI, mAlbumId);
        assertEquals(1, mResolver.delete(albumUri, null, null));
        // now delete something that isn't there
        assertEquals(0, mResolver.delete(photoUri, null, null));
    }

    public void testDeleteMetadataId() {
        Uri metadataUri = ContentUris.withAppendedId(Metadata.CONTENT_URI, mMetadataId);
        assertEquals(1, mResolver.delete(metadataUri, null, null));
        Cursor cursor = mResolver.query(Metadata.CONTENT_URI, null, null, null, null);
        assertEquals(0, cursor.getCount());
        cursor.close();
    }

    // Delete the album and ensure that the photos referring to the album are
    // deleted.
    public void testDeleteAlbumCascade() {
        Uri albumUri = ContentUris.withAppendedId(Albums.CONTENT_URI, mAlbumId);
        mResolver.delete(albumUri, null, null);
        assertTrue(mNotifications.isNotified(Photos.CONTENT_URI));
        assertTrue(mNotifications.isNotified(Metadata.CONTENT_URI));
        assertTrue(mNotifications.isNotified(albumUri));
        assertEquals(3, mNotifications.notificationCount());
        Cursor cursor = mResolver.query(Photos.CONTENT_URI, PhotoDatabaseUtils.PROJECTION_PHOTOS,
                null, null, null);
        assertEquals(0, cursor.getCount());
        cursor.close();
    }

    // Delete all albums and ensure that photos in any album are deleted.
    public void testDeleteAlbumCascade2() {
        mResolver.delete(Albums.CONTENT_URI, null, null);
        assertTrue(mNotifications.isNotified(Photos.CONTENT_URI));
        assertTrue(mNotifications.isNotified(Metadata.CONTENT_URI));
        assertTrue(mNotifications.isNotified(Albums.CONTENT_URI));
        assertEquals(3, mNotifications.notificationCount());
        Cursor cursor = mResolver.query(Photos.CONTENT_URI, PhotoDatabaseUtils.PROJECTION_PHOTOS,
                null, null, null);
        assertEquals(0, cursor.getCount());
        cursor.close();
    }

    // Delete a photo and ensure that the metadata for that photo are deleted.
    public void testDeletePhotoCascade() {
        Uri photoUri = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId);
        mResolver.delete(photoUri, null, null);
        assertTrue(mNotifications.isNotified(photoUri));
        assertTrue(mNotifications.isNotified(Metadata.CONTENT_URI));
        assertEquals(2, mNotifications.notificationCount());
        Cursor cursor = mResolver.query(Metadata.CONTENT_URI,
                PhotoDatabaseUtils.PROJECTION_METADATA, null, null, null);
        assertEquals(0, cursor.getCount());
        cursor.close();
    }

    public void testGetType() {
        // We don't return types for albums
        assertNull(mResolver.getType(Albums.CONTENT_URI));

        Uri noImage = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId + 1);
        assertNull(mResolver.getType(noImage));

        Uri image = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId);
        assertEquals(MIME_TYPE, mResolver.getType(image));
    }

    public void testInsert() {
        ContentValues values = new ContentValues();
        values.put(Albums.NAME, "don't add me");
        values.put(Albums.VISIBILITY, Albums.VISIBILITY_PRIVATE);
        assertNull(mResolver.insert(Albums.CONTENT_URI, values));
    }

    public void testUpdate() {
        ContentValues values = new ContentValues();
        // Normal update -- use an album.
        values.put(Albums.NAME, "foo");
        Uri albumUri = ContentUris.withAppendedId(Albums.CONTENT_URI, mAlbumId);
        assertEquals(1, mResolver.update(albumUri, values, null, null));
        String[] projection = {
            Albums.NAME,
        };
        Cursor cursor = mResolver.query(albumUri, projection, null, null, null);
        assertEquals(1, cursor.getCount());
        assertTrue(cursor.moveToNext());
        assertEquals("foo", cursor.getString(0));
        cursor.close();

        // Update a row that doesn't exist.
        Uri noAlbumUri = ContentUris.withAppendedId(Albums.CONTENT_URI, mAlbumId + 1);
        values.put(Albums.NAME, "bar");
        assertEquals(0, mResolver.update(noAlbumUri, values, null, null));

        // Update a metadata value that exists.
        ContentValues metadata = new ContentValues();
        metadata.put(Metadata.PHOTO_ID, mPhotoId);
        metadata.put(Metadata.KEY, META_KEY);
        metadata.put(Metadata.VALUE, "new value");
        assertEquals(1, mResolver.update(Metadata.CONTENT_URI, metadata, null, null));

        projection = new String[] {
            Metadata.VALUE,
        };

        String[] selectionArgs = {
                String.valueOf(mPhotoId), META_KEY,
        };

        cursor = mResolver.query(Metadata.CONTENT_URI, projection, WHERE_METADATA, selectionArgs,
                null);
        assertEquals(1, cursor.getCount());
        assertTrue(cursor.moveToNext());
        assertEquals("new value", cursor.getString(0));
        cursor.close();

        // Update a metadata value that doesn't exist.
        metadata.put(Metadata.KEY, "other stuff");
        assertEquals(1, mResolver.update(Metadata.CONTENT_URI, metadata, null, null));

        selectionArgs[1] = "other stuff";
        cursor = mResolver.query(Metadata.CONTENT_URI, projection, WHERE_METADATA, selectionArgs,
                null);
        assertEquals(1, cursor.getCount());
        assertTrue(cursor.moveToNext());
        assertEquals("new value", cursor.getString(0));
        cursor.close();

        // Remove a metadata value using update.
        metadata.putNull(Metadata.VALUE);
        assertEquals(1, mResolver.update(Metadata.CONTENT_URI, metadata, null, null));
        cursor = mResolver.query(Metadata.CONTENT_URI, projection, WHERE_METADATA, selectionArgs,
                null);
        assertEquals(0, cursor.getCount());
        cursor.close();
    }

    public void testQuery() {
        // Query a photo that exists.
        Cursor cursor = mResolver.query(Photos.CONTENT_URI, PhotoDatabaseUtils.PROJECTION_PHOTOS,
                null, null, null);
        assertNotNull(cursor);
        assertEquals(1, cursor.getCount());
        assertTrue(cursor.moveToNext());
        assertEquals(mPhotoId, cursor.getLong(0));
        cursor.close();

        // Query a photo that doesn't exist.
        Uri noPhotoUri = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId + 1);
        cursor = mResolver.query(noPhotoUri, PhotoDatabaseUtils.PROJECTION_PHOTOS, null, null,
                null);
        assertNotNull(cursor);
        assertEquals(0, cursor.getCount());
        cursor.close();

        // Query a photo that exists using selection arguments.
        String[] selectionArgs = {
            String.valueOf(mPhotoId),
        };

        cursor = mResolver.query(Photos.CONTENT_URI, PhotoDatabaseUtils.PROJECTION_PHOTOS,
                Photos._ID + " = ?", selectionArgs, null);
        assertNotNull(cursor);
        assertEquals(1, cursor.getCount());
        assertTrue(cursor.moveToNext());
        assertEquals(mPhotoId, cursor.getLong(0));
        cursor.close();
    }

    public void testUpdatePhotoNotification() {
        Uri photoUri = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId);
        ContentValues values = new ContentValues();
        values.put(Photos.MIME_TYPE, "not-a/mime-type");
        mResolver.update(photoUri, values, null, null);
        assertTrue(mNotifications.isNotified(photoUri));
    }

    public void testUpdateMetadataNotification() {
        ContentValues values = new ContentValues();
        values.put(Metadata.PHOTO_ID, mPhotoId);
        values.put(Metadata.KEY, META_KEY);
        values.put(Metadata.VALUE, "hello world");
        mResolver.update(Metadata.CONTENT_URI, values, null, null);
        assertTrue(mNotifications.isNotified(Metadata.CONTENT_URI));
    }
}