1 /* 2 * Copyright (C) 2013 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 package com.android.photos.data; 17 18 import android.content.Context; 19 import android.database.Cursor; 20 import android.database.DatabaseUtils; 21 import android.database.sqlite.SQLiteDatabase; 22 import android.test.InstrumentationTestCase; 23 24 import com.android.photos.data.PhotoProvider.Accounts; 25 import com.android.photos.data.PhotoProvider.Albums; 26 import com.android.photos.data.PhotoProvider.Metadata; 27 import com.android.photos.data.PhotoProvider.Photos; 28 29 import java.io.File; 30 import java.io.IOException; 31 32 public class PhotoDatabaseTest extends InstrumentationTestCase { 33 34 private PhotoDatabase mDBHelper; 35 private static final String DB_NAME = "dummy.db"; 36 private static final long PARENT_ID1 = 100; 37 private static final long PARENT_ID2 = 101; 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 Context context = getInstrumentation().getTargetContext(); 43 context.deleteDatabase(DB_NAME); 44 mDBHelper = new PhotoDatabase(context, DB_NAME); 45 } 46 47 @Override 48 protected void tearDown() throws Exception { 49 mDBHelper.close(); 50 mDBHelper = null; 51 Context context = getInstrumentation().getTargetContext(); 52 context.deleteDatabase(DB_NAME); 53 super.tearDown(); 54 } 55 56 public void testCreateDatabase() throws IOException { 57 Context context = getInstrumentation().getTargetContext(); 58 File dbFile = context.getDatabasePath(DB_NAME); 59 SQLiteDatabase db = getReadableDB(); 60 db.beginTransaction(); 61 db.endTransaction(); 62 assertTrue(dbFile.exists()); 63 } 64 65 public void testTables() { 66 validateTable(Metadata.TABLE, PhotoDatabaseUtils.PROJECTION_METADATA); 67 validateTable(Albums.TABLE, PhotoDatabaseUtils.PROJECTION_ALBUMS); 68 validateTable(Photos.TABLE, PhotoDatabaseUtils.PROJECTION_PHOTOS); 69 } 70 71 public void testAlbumsConstraints() { 72 SQLiteDatabase db = getWritableDB(); 73 db.beginTransaction(); 74 try { 75 long accountId = 100; 76 // Test NOT NULL constraint on name 77 assertFalse(PhotoDatabaseUtils.insertAlbum(db, null, null, Albums.VISIBILITY_PRIVATE, 78 accountId)); 79 80 // test NOT NULL constraint on privacy 81 assertFalse(PhotoDatabaseUtils.insertAlbum(db, null, "hello", null, accountId)); 82 83 // test NOT NULL constraint on account_id 84 assertFalse(PhotoDatabaseUtils.insertAlbum(db, null, "hello", 85 Albums.VISIBILITY_PRIVATE, null)); 86 87 // Normal insert 88 assertTrue(PhotoDatabaseUtils.insertAlbum(db, PARENT_ID1, "hello", 89 Albums.VISIBILITY_PRIVATE, accountId)); 90 91 long albumId = PhotoDatabaseUtils.queryAlbumIdFromParentId(db, PARENT_ID1); 92 93 // Assign a valid child 94 assertTrue(PhotoDatabaseUtils.insertAlbum(db, PARENT_ID2, "hello", 95 Albums.VISIBILITY_PRIVATE, accountId)); 96 97 long otherAlbumId = PhotoDatabaseUtils.queryAlbumIdFromParentId(db, PARENT_ID2); 98 assertNotSame(albumId, otherAlbumId); 99 100 // This is a valid child of another album. 101 assertTrue(PhotoDatabaseUtils.insertAlbum(db, otherAlbumId, "hello", 102 Albums.VISIBILITY_PRIVATE, accountId)); 103 104 // This isn't allowed due to uniqueness constraint (parent_id/name) 105 assertFalse(PhotoDatabaseUtils.insertAlbum(db, otherAlbumId, "hello", 106 Albums.VISIBILITY_PRIVATE, accountId)); 107 } finally { 108 db.endTransaction(); 109 } 110 } 111 112 public void testPhotosConstraints() { 113 SQLiteDatabase db = getWritableDB(); 114 db.beginTransaction(); 115 try { 116 int width = 100; 117 int height = 100; 118 long dateTaken = System.currentTimeMillis(); 119 String mimeType = "test/test"; 120 long accountId = 100; 121 122 // Test NOT NULL mime-type 123 assertFalse(PhotoDatabaseUtils.insertPhoto(db, width, height, dateTaken, null, null, 124 accountId)); 125 126 // Test NOT NULL width 127 assertFalse(PhotoDatabaseUtils.insertPhoto(db, null, height, dateTaken, null, mimeType, 128 accountId)); 129 130 // Test NOT NULL height 131 assertFalse(PhotoDatabaseUtils.insertPhoto(db, width, null, dateTaken, null, mimeType, 132 accountId)); 133 134 // Test NOT NULL dateTaken 135 assertFalse(PhotoDatabaseUtils.insertPhoto(db, width, height, null, null, mimeType, 136 accountId)); 137 138 // Test NOT NULL accountId 139 assertFalse(PhotoDatabaseUtils.insertPhoto(db, width, height, dateTaken, null, 140 mimeType, null)); 141 142 // Test normal insert 143 assertTrue(PhotoDatabaseUtils.insertPhoto(db, width, height, dateTaken, null, mimeType, 144 accountId)); 145 } finally { 146 db.endTransaction(); 147 } 148 } 149 150 public void testMetadataConstraints() { 151 SQLiteDatabase db = getWritableDB(); 152 db.beginTransaction(); 153 try { 154 final String mimeType = "test/test"; 155 PhotoDatabaseUtils.insertPhoto(db, 100, 100, 100L, PARENT_ID1, mimeType, 100L); 156 long photoId = PhotoDatabaseUtils.queryPhotoIdFromAlbumId(db, PARENT_ID1); 157 158 // Test NOT NULL PHOTO_ID constraint. 159 assertFalse(PhotoDatabaseUtils.insertMetadata(db, null, "foo", "bar")); 160 161 // Normal insert. 162 assertTrue(PhotoDatabaseUtils.insertMetadata(db, photoId, "foo", "bar")); 163 164 // Test uniqueness constraint. 165 assertFalse(PhotoDatabaseUtils.insertMetadata(db, photoId, "foo", "baz")); 166 } finally { 167 db.endTransaction(); 168 } 169 } 170 171 public void testAccountsConstraints() { 172 SQLiteDatabase db = getWritableDB(); 173 db.beginTransaction(); 174 try { 175 assertFalse(PhotoDatabaseUtils.insertAccount(db, null)); 176 assertTrue(PhotoDatabaseUtils.insertAccount(db, "hello")); 177 assertTrue(PhotoDatabaseUtils.insertAccount(db, "hello")); 178 } finally { 179 db.endTransaction(); 180 } 181 } 182 183 public void testUpgrade() { 184 SQLiteDatabase db = getWritableDB(); 185 db.beginTransaction(); 186 try { 187 assertTrue(PhotoDatabaseUtils.insertAccount(db, "Hello")); 188 assertTrue(PhotoDatabaseUtils.insertAlbum(db, PARENT_ID1, "hello", 189 Albums.VISIBILITY_PRIVATE, 100L)); 190 final String mimeType = "test/test"; 191 assertTrue(PhotoDatabaseUtils.insertPhoto(db, 100, 100, 100L, PARENT_ID1, mimeType, 192 100L)); 193 // Normal insert. 194 assertTrue(PhotoDatabaseUtils.insertMetadata(db, 100L, "foo", "bar")); 195 db.setTransactionSuccessful(); 196 } finally { 197 db.endTransaction(); 198 } 199 mDBHelper.close(); 200 Context context = getInstrumentation().getTargetContext(); 201 mDBHelper = new PhotoDatabase(context, DB_NAME, PhotoDatabase.DB_VERSION + 1); 202 db = getReadableDB(); 203 assertEquals(0, DatabaseUtils.queryNumEntries(db, Accounts.TABLE)); 204 assertEquals(0, DatabaseUtils.queryNumEntries(db, Photos.TABLE)); 205 assertEquals(0, DatabaseUtils.queryNumEntries(db, Albums.TABLE)); 206 assertEquals(0, DatabaseUtils.queryNumEntries(db, Metadata.TABLE)); 207 } 208 209 private SQLiteDatabase getReadableDB() { 210 return mDBHelper.getReadableDatabase(); 211 } 212 213 private SQLiteDatabase getWritableDB() { 214 return mDBHelper.getWritableDatabase(); 215 } 216 217 private void validateTable(String table, String[] projection) { 218 SQLiteDatabase db = getReadableDB(); 219 Cursor cursor = db.query(table, projection, null, null, null, null, null); 220 assertNotNull(cursor); 221 assertEquals(cursor.getCount(), 0); 222 assertEquals(cursor.getColumnCount(), projection.length); 223 for (int i = 0; i < projection.length; i++) { 224 assertEquals(cursor.getColumnName(i), projection[i]); 225 } 226 } 227 228 229 } 230