summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/photos/data/PhotoDatabaseTest.java
blob: 48e79d4e1ae7e3a0b782546a729639d02f782703 (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) 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.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.InstrumentationTestCase;

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

import java.io.File;
import java.io.IOException;

public class PhotoDatabaseTest extends InstrumentationTestCase {

    private PhotoDatabase mDBHelper;

    @Override
    protected void setUp() {
        Context context = getInstrumentation().getTargetContext();
        mDBHelper = new PhotoDatabase(context);
    }

    @Override
    protected void tearDown() {
        mDBHelper.close();
    }

    public void testCreateDatabase() throws IOException {
        Context context = getInstrumentation().getTargetContext();
        File dbFile = context.getDatabasePath(PhotoDatabase.DB_NAME);
        if (dbFile.exists()) {
            dbFile.delete();
        }
        SQLiteDatabase db = getReadableDB();
        db.beginTransaction();
        db.endTransaction();
        assertTrue(dbFile.exists());
        dbFile.delete();
    }

    public void testTables() {
        validateTable(Metadata.TABLE, PhotoDatabaseUtils.PROJECTION_METADATA);
        validateTable(Albums.TABLE, PhotoDatabaseUtils.PROJECTION_ALBUMS);
        validateTable(Photos.TABLE, PhotoDatabaseUtils.PROJECTION_PHOTOS);
    }

    public void testAlbumsConstraints() {
        SQLiteDatabase db = getWriteableDB();
        db.beginTransaction();
        try {
            // Test NOT NULL constraint on name
            assertFalse(PhotoDatabaseUtils
                    .insertAlbum(db, null, null, Albums.VISIBILITY_PRIVATE, null));

            // test NOT NULL constraint on privacy
            assertFalse(PhotoDatabaseUtils.insertAlbum(db, null, "hello", null, null));

            // Normal insert
            assertTrue(PhotoDatabaseUtils.insertAlbum(db, null, "hello", Albums.VISIBILITY_PRIVATE,
                    100L));

            // Test server id uniqueness
            assertFalse(PhotoDatabaseUtils.insertAlbum(db, null, "world", Albums.VISIBILITY_PRIVATE,
                    100L));

            // Different server id allowed
            assertTrue(PhotoDatabaseUtils.insertAlbum(db, null, "world", Albums.VISIBILITY_PRIVATE,
                    101L));

            // Allow null server id
            assertTrue(PhotoDatabaseUtils.insertAlbum(db, null, "hello world",
                    Albums.VISIBILITY_PRIVATE, null));

            long albumId = PhotoDatabaseUtils.queryAlbumIdFromServerId(db, 100);

            // Assign a valid child
            assertTrue(PhotoDatabaseUtils.insertAlbum(db, albumId, "hello", Albums.VISIBILITY_PRIVATE,
                    null));

            long otherAlbumId = PhotoDatabaseUtils.queryAlbumIdFromServerId(db, 101);
            assertNotSame(albumId, otherAlbumId);

            // This is a valid child of another album.
            assertTrue(PhotoDatabaseUtils.insertAlbum(db, otherAlbumId, "hello",
                    Albums.VISIBILITY_PRIVATE, null));

            // This isn't allowed due to uniqueness constraint (parent_id/name)
            assertFalse(PhotoDatabaseUtils.insertAlbum(db, otherAlbumId, "hello",
                    Albums.VISIBILITY_PRIVATE, null));
        } finally {
            db.endTransaction();
        }
    }

    public void testPhotosConstraints() {
        SQLiteDatabase db = getWriteableDB();
        db.beginTransaction();
        try {
            int width = 100;
            int height = 100;
            long dateTaken = System.currentTimeMillis();
            String mimeType = "test/test";

            // Test NOT NULL mime-type
            assertFalse(PhotoDatabaseUtils.insertPhoto(db, null, width, height, dateTaken, null,
                    null));

            // Test NOT NULL width
            assertFalse(PhotoDatabaseUtils.insertPhoto(db, null, null, height, dateTaken, null,
                    mimeType));

            // Test NOT NULL height
            assertFalse(PhotoDatabaseUtils.insertPhoto(db, null, width, null, dateTaken, null,
                    mimeType));

            // Test NOT NULL dateTaken
            assertFalse(PhotoDatabaseUtils.insertPhoto(db, null, width, height, null, null,
                    mimeType));

            // Test normal insert
            assertTrue(PhotoDatabaseUtils.insertPhoto(db, null, width, height, dateTaken, null,
                    mimeType));
        } finally {
            db.endTransaction();
        }
    }

    public void testMetadataConstraints() {
        SQLiteDatabase db = getWriteableDB();
        db.beginTransaction();
        try {
            final String mimeType = "test/test";
            long photoServerId = 100;
            PhotoDatabaseUtils.insertPhoto(db, photoServerId, 100, 100, 100L, null, mimeType);
            long photoId = PhotoDatabaseUtils.queryPhotoIdFromServerId(db, photoServerId);

            // Test NOT NULL PHOTO_ID constraint.
            assertFalse(PhotoDatabaseUtils.insertMetadata(db, null, "foo", "bar"));

            // Normal insert.
            assertTrue(PhotoDatabaseUtils.insertMetadata(db, photoId, "foo", "bar"));

            // Test uniqueness constraint.
            assertFalse(PhotoDatabaseUtils.insertMetadata(db, photoId, "foo", "baz"));
        } finally {
            db.endTransaction();
        }
    }

    private SQLiteDatabase getReadableDB() {
        return mDBHelper.getReadableDatabase();
    }

    private SQLiteDatabase getWriteableDB() {
        return mDBHelper.getWritableDatabase();
    }

    private void validateTable(String table, String[] projection) {
        SQLiteDatabase db = getReadableDB();
        Cursor cursor = db.query(table, projection, null, null, null, null, null);
        assertNotNull(cursor);
        assertEquals(cursor.getCount(), 0);
        assertEquals(cursor.getColumnCount(), projection.length);
        for (int i = 0; i < projection.length; i++) {
            assertEquals(cursor.getColumnName(i), projection[i]);
        }
    }


}