summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGeorge Mount <mount@google.com>2013-02-28 10:52:59 -0800
committerGeorge Mount <mount@google.com>2013-03-04 13:41:13 -0800
commitc0a5c3b8cd7d9724b0f9b692008283abc750f0d0 (patch)
tree29eae27a82f5dcf0abfbba4c9bce54f2716cfe02 /tests
parent56c35f8f9c173e618df34f94df6b4a0269bd7da5 (diff)
downloadandroid_packages_apps_Snap-c0a5c3b8cd7d9724b0f9b692008283abc750f0d0.zip
android_packages_apps_Snap-c0a5c3b8cd7d9724b0f9b692008283abc750f0d0.tar.gz
android_packages_apps_Snap-c0a5c3b8cd7d9724b0f9b692008283abc750f0d0.tar.bz2
Update PhotoProvider and tests to allow tests to run against GalleryGoogle.apk
Change-Id: I6630e7a5ba0883b887915f63094885a2dc9f025c
Diffstat (limited to 'tests')
-rw-r--r--tests/src/com/android/photos/data/PhotoDatabaseTest.java19
-rw-r--r--tests/src/com/android/photos/data/PhotoDatabaseUtils.java6
-rw-r--r--tests/src/com/android/photos/data/PhotoProviderTest.java273
-rw-r--r--tests/src/com/android/photos/data/TestHelper.java53
4 files changed, 123 insertions, 228 deletions
diff --git a/tests/src/com/android/photos/data/PhotoDatabaseTest.java b/tests/src/com/android/photos/data/PhotoDatabaseTest.java
index 48e79d4..d8c5e42 100644
--- a/tests/src/com/android/photos/data/PhotoDatabaseTest.java
+++ b/tests/src/com/android/photos/data/PhotoDatabaseTest.java
@@ -30,29 +30,32 @@ import java.io.IOException;
public class PhotoDatabaseTest extends InstrumentationTestCase {
private PhotoDatabase mDBHelper;
+ private static final String DB_NAME = "dummy.db";
@Override
- protected void setUp() {
+ protected void setUp() throws Exception {
+ super.setUp();
Context context = getInstrumentation().getTargetContext();
- mDBHelper = new PhotoDatabase(context);
+ context.deleteDatabase(DB_NAME);
+ mDBHelper = new PhotoDatabase(context, DB_NAME);
}
@Override
- protected void tearDown() {
+ protected void tearDown() throws Exception {
mDBHelper.close();
+ mDBHelper = null;
+ Context context = getInstrumentation().getTargetContext();
+ context.deleteDatabase(DB_NAME);
+ super.tearDown();
}
public void testCreateDatabase() throws IOException {
Context context = getInstrumentation().getTargetContext();
- File dbFile = context.getDatabasePath(PhotoDatabase.DB_NAME);
- if (dbFile.exists()) {
- dbFile.delete();
- }
+ File dbFile = context.getDatabasePath(DB_NAME);
SQLiteDatabase db = getReadableDB();
db.beginTransaction();
db.endTransaction();
assertTrue(dbFile.exists());
- dbFile.delete();
}
public void testTables() {
diff --git a/tests/src/com/android/photos/data/PhotoDatabaseUtils.java b/tests/src/com/android/photos/data/PhotoDatabaseUtils.java
index 6fd73e1..73a6c78 100644
--- a/tests/src/com/android/photos/data/PhotoDatabaseUtils.java
+++ b/tests/src/com/android/photos/data/PhotoDatabaseUtils.java
@@ -108,10 +108,4 @@ public class PhotoDatabaseUtils {
values.put(Metadata.VALUE, value);
return db.insert(Metadata.TABLE, null, values) != -1;
}
-
- public static void deleteAllContent(SQLiteDatabase db) {
- db.delete(Metadata.TABLE, null, null);
- db.delete(Photos.TABLE, null, null);
- db.delete(Albums.TABLE, null, null);
- }
}
diff --git a/tests/src/com/android/photos/data/PhotoProviderTest.java b/tests/src/com/android/photos/data/PhotoProviderTest.java
index ad913b0..525abec 100644
--- a/tests/src/com/android/photos/data/PhotoProviderTest.java
+++ b/tests/src/com/android/photos/data/PhotoProviderTest.java
@@ -18,21 +18,18 @@ package com.android.photos.data;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
-import android.content.Context;
-import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
-import android.os.Handler;
-import android.os.Looper;
import android.provider.BaseColumns;
-import android.test.InstrumentationTestCase;
+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 InstrumentationTestCase {
+public class PhotoProviderTest extends ProviderTestCase2<PhotoProvider> {
@SuppressWarnings("unused")
private static final String TAG = PhotoProviderTest.class.getSimpleName();
@@ -51,87 +48,25 @@ public class PhotoProviderTest extends InstrumentationTestCase {
private static final String WHERE_METADATA = Metadata.PHOTO_ID + " = ? AND " + Metadata.KEY
+ " = ?";
- private static final long WAIT_FOR_CHANGE_MILLIS = 200;
-
private long mAlbumId;
private long mPhotoId;
private long mMetadataId;
- private PhotoDatabase mDBHelper;
+ private SQLiteOpenHelper mDBHelper;
private ContentResolver mResolver;
+ private NotificationWatcher mNotifications = new NotificationWatcher();
- private static class WatchContentObserverThread extends Thread {
- private WatchContentObserver mObserver;
- private Looper mLooper;
-
- @Override
- public void run() {
- Looper.prepare();
- mLooper = Looper.myLooper();
- WatchContentObserver observer = new WatchContentObserver();
- synchronized (this) {
- mObserver = observer;
- this.notifyAll();
- }
- Looper.loop();
- }
-
- public void waitForObserver() throws InterruptedException {
- synchronized (this) {
- while (mObserver == null) {
- this.wait();
- }
- }
- }
-
- public WatchContentObserver getObserver() {
- return mObserver;
- }
-
- public void stopLooper() {
- mLooper.quit();
- }
- };
-
- private static class WatchContentObserver extends ContentObserver {
- private boolean mOnChangeReceived = false;
- private Uri mUri = null;
-
- public WatchContentObserver() {
- super(new Handler());
- }
-
- @Override
- public synchronized void onChange(boolean selfChange, Uri uri) {
- mOnChangeReceived = true;
- mUri = uri;
- notifyAll();
- }
-
- @Override
- public synchronized void onChange(boolean selfChange) {
- mOnChangeReceived = true;
- notifyAll();
- }
-
- public boolean waitForNotification() {
- synchronized (this) {
- if (!mOnChangeReceived) {
- try {
- wait(WAIT_FOR_CHANGE_MILLIS);
- } catch (InterruptedException e) {
- }
- }
- }
- return mOnChangeReceived;
- }
- };
+ public PhotoProviderTest() {
+ super(PhotoProvider.class, PhotoProvider.AUTHORITY);
+ }
@Override
- protected void setUp() {
- Context context = getInstrumentation().getTargetContext();
- mDBHelper = new PhotoDatabase(context);
- mResolver = context.getContentResolver();
+ 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 {
@@ -150,23 +85,18 @@ public class PhotoProviderTest extends InstrumentationTestCase {
mMetadataId = cursor.getLong(0);
cursor.close();
db.setTransactionSuccessful();
+ mNotifications.reset();
} finally {
db.endTransaction();
}
}
@Override
- protected void tearDown() {
- SQLiteDatabase db = mDBHelper.getWritableDatabase();
- db.beginTransaction();
- try {
- PhotoDatabaseUtils.deleteAllContent(db);
- db.setTransactionSuccessful();
- } finally {
- db.endTransaction();
- }
+ protected void tearDown() throws Exception {
mDBHelper.close();
mDBHelper = null;
+ super.tearDown();
+ getMockContext().deleteDatabase(PhotoProvider.DB_NAME);
}
public void testDelete() {
@@ -204,85 +134,46 @@ public class PhotoProviderTest extends InstrumentationTestCase {
assertEquals(0, cursor.getCount());
cursor.close();
}
+
// Delete the album and ensure that the photos referring to the album are
// deleted.
public void testDeleteAlbumCascade() {
- WatchContentObserverThread observerThread = createObserverThread();
- WatchContentObserver observer = observerThread.getObserver();
- mResolver.registerContentObserver(Photos.CONTENT_URI, true, observer);
- try {
- Uri albumUri = ContentUris.withAppendedId(Albums.CONTENT_URI, mAlbumId);
- mResolver.delete(albumUri, null, null);
- assertTrue(observer.waitForNotification());
- assertEquals(observer.mUri, Photos.CONTENT_URI);
- Cursor cursor = mResolver.query(Photos.CONTENT_URI,
- PhotoDatabaseUtils.PROJECTION_PHOTOS, null, null, null);
- assertEquals(0, cursor.getCount());
- cursor.close();
- } finally {
- mResolver.unregisterContentObserver(observer);
- observerThread.stopLooper();
- }
- }
-
- // Delete the album and ensure that the metadata referring to photos in that
- // album are deleted.
- public void testDeleteAlbumCascade2() {
- WatchContentObserverThread observerThread = createObserverThread();
- WatchContentObserver observer = observerThread.getObserver();
- mResolver.registerContentObserver(Metadata.CONTENT_URI, true, observer);
- try {
- Uri albumUri = ContentUris.withAppendedId(Albums.CONTENT_URI, mAlbumId);
- mResolver.delete(albumUri, null, null);
- assertTrue(observer.waitForNotification());
- assertEquals(observer.mUri, Metadata.CONTENT_URI);
- Cursor cursor = mResolver.query(Metadata.CONTENT_URI,
- PhotoDatabaseUtils.PROJECTION_METADATA, null, null, null);
- assertEquals(0, cursor.getCount());
- cursor.close();
- } finally {
- mResolver.unregisterContentObserver(observer);
- observerThread.stopLooper();
- }
+ 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 testDeleteAlbumCascade3() {
- WatchContentObserverThread observerThread = createObserverThread();
- WatchContentObserver observer = observerThread.getObserver();
- mResolver.registerContentObserver(Photos.CONTENT_URI, true, observer);
- try {
- mResolver.delete(Albums.CONTENT_URI, null, null);
- assertTrue(observer.waitForNotification());
- assertEquals(observer.mUri, Photos.CONTENT_URI);
- Cursor cursor = mResolver.query(Photos.CONTENT_URI,
- PhotoDatabaseUtils.PROJECTION_PHOTOS, null, null, null);
- assertEquals(0, cursor.getCount());
- cursor.close();
- } finally {
- mResolver.unregisterContentObserver(observer);
- observerThread.stopLooper();
- }
+ 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() {
- WatchContentObserverThread observerThread = createObserverThread();
- WatchContentObserver observer = observerThread.getObserver();
- mResolver.registerContentObserver(Metadata.CONTENT_URI, true, observer);
- try {
- Uri albumUri = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId);
- mResolver.delete(albumUri, null, null);
- assertTrue(observer.waitForNotification());
- assertEquals(observer.mUri, Metadata.CONTENT_URI);
- Cursor cursor = mResolver.query(Metadata.CONTENT_URI,
- PhotoDatabaseUtils.PROJECTION_METADATA, null, null, null);
- assertEquals(0, cursor.getCount());
- cursor.close();
- } finally {
- mResolver.unregisterContentObserver(observer);
- observerThread.stopLooper();
- }
+ 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() {
@@ -399,65 +290,19 @@ public class PhotoProviderTest extends InstrumentationTestCase {
}
public void testUpdatePhotoNotification() {
- WatchContentObserverThread observerThread = createObserverThread();
- WatchContentObserver observer = observerThread.getObserver();
- mResolver.registerContentObserver(Photos.CONTENT_URI, true, observer);
- try {
- 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(observer.waitForNotification());
- assertEquals(observer.mUri, photoUri);
- } finally {
- mResolver.unregisterContentObserver(observer);
- observerThread.stopLooper();
- }
+ 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() {
- WatchContentObserverThread observerThread = createObserverThread();
- WatchContentObserver observer = observerThread.getObserver();
- mResolver.registerContentObserver(Metadata.CONTENT_URI, true, observer);
- try {
- 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(observer.waitForNotification());
- assertEquals(observer.mUri, Metadata.CONTENT_URI);
- } finally {
- mResolver.unregisterContentObserver(observer);
- observerThread.stopLooper();
- }
- }
-
- public void testDeletePhotoNotification() {
- WatchContentObserverThread observerThread = createObserverThread();
- WatchContentObserver observer = observerThread.getObserver();
- mResolver.registerContentObserver(Photos.CONTENT_URI, true, observer);
- try {
- Uri photoUri = ContentUris.withAppendedId(Photos.CONTENT_URI, mPhotoId);
- mResolver.delete(photoUri, null, null);
- assertTrue(observer.waitForNotification());
- assertEquals(observer.mUri, photoUri);
- } finally {
- mResolver.unregisterContentObserver(observer);
- observerThread.stopLooper();
- }
- }
-
- private WatchContentObserverThread createObserverThread() {
- WatchContentObserverThread thread = new WatchContentObserverThread();
- thread.start();
- try {
- thread.waitForObserver();
- return thread;
- } catch (InterruptedException e) {
- thread.stopLooper();
- fail("Interruption while waiting for observer being created.");
- return null;
- }
+ 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));
}
}
diff --git a/tests/src/com/android/photos/data/TestHelper.java b/tests/src/com/android/photos/data/TestHelper.java
new file mode 100644
index 0000000..338e160
--- /dev/null
+++ b/tests/src/com/android/photos/data/TestHelper.java
@@ -0,0 +1,53 @@
+/*
+ * 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.util.Log;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import java.lang.reflect.Method;
+
+public class TestHelper {
+ private static String TAG = TestHelper.class.getSimpleName();
+
+ public interface TestInitialization {
+ void initialize(TestCase testCase);
+ }
+
+ public static void addTests(Class<? extends TestCase> testClass, TestSuite suite,
+ TestInitialization initialization) {
+ for (Method method : testClass.getDeclaredMethods()) {
+ if (method.getName().startsWith("test") && method.getParameterTypes().length == 0) {
+ TestCase test;
+ try {
+ test = testClass.newInstance();
+ test.setName(method.getName());
+ initialization.initialize(test);
+ suite.addTest(test);
+ } catch (IllegalArgumentException e) {
+ Log.e(TAG, "Failed to create test case", e);
+ } catch (InstantiationException e) {
+ Log.e(TAG, "Failed to create test case", e);
+ } catch (IllegalAccessException e) {
+ Log.e(TAG, "Failed to create test case", e);
+ }
+ }
+ }
+ }
+
+}