diff options
author | Ray Chen <raychen@google.com> | 2009-05-11 13:40:16 +0800 |
---|---|---|
committer | Ray Chen <raychen@google.com> | 2009-05-12 10:07:15 +0800 |
commit | 60550bd901342c7367d18ef19d36c575b6d4dc35 (patch) | |
tree | 139caf6ca8b66daf176b02236349102f179ad9a1 /tests | |
parent | 383e1628910068229ba3df34100d99574236603e (diff) | |
download | packages_apps_LegacyCamera-60550bd901342c7367d18ef19d36c575b6d4dc35.zip packages_apps_LegacyCamera-60550bd901342c7367d18ef19d36c575b6d4dc35.tar.gz packages_apps_LegacyCamera-60550bd901342c7367d18ef19d36c575b6d4dc35.tar.bz2 |
new file: ../CameraAppUnitTestRunner.java
new file: BitmapManagerUnitTest.java
modified: ../../../../../../src/com/android/camera/BitmapManager.java
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/com/android/camera/CameraAppUnitTestRunner.java | 44 | ||||
-rw-r--r-- | tests/src/com/android/camera/unit/BitmapManagerUnitTest.java | 177 |
2 files changed, 221 insertions, 0 deletions
diff --git a/tests/src/com/android/camera/CameraAppUnitTestRunner.java b/tests/src/com/android/camera/CameraAppUnitTestRunner.java new file mode 100644 index 0000000..dbbce37 --- /dev/null +++ b/tests/src/com/android/camera/CameraAppUnitTestRunner.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2008 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.camera; + +import android.test.InstrumentationTestRunner; +import android.test.InstrumentationTestSuite; +import com.android.camera.unit.*; + +import junit.framework.TestSuite; + +/** + * Instrumentation Test Runner for all camera application unit tests. + * Usage: adb shell am instrument -w -e package com.android.camera.tests \ + * com.android.camera.tests/android.test.InstrumentationTest + */ + +public class CameraAppUnitTestRunner extends InstrumentationTestRunner { + + @Override + public TestSuite getAllTests() { + TestSuite suite = new InstrumentationTestSuite(this); + suite.addTestSuite(CameraAppUnitTest.class); + return suite; + } + + @Override + public ClassLoader getLoader() { + return CameraAppUnitTestRunner.class.getClassLoader(); + } +} diff --git a/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java b/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java new file mode 100644 index 0000000..4968718 --- /dev/null +++ b/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java @@ -0,0 +1,177 @@ + +package com.android.camera.tests.unit; + +import com.android.camera.BitmapManager; +import com.android.camera.ImageManager; +import com.android.camera.gallery.IImage; +import com.android.camera.gallery.IImageList; + +import android.graphics.Bitmap; +import android.test.AndroidTestCase; + +/** + * BitmapManager's unit tests. + */ +public class BitmapManagerUnitTest extends AndroidTestCase { + IImageList mImageList; + IImage mImage; + BitmapManager mBitmapManager; + + private class DecodeThread extends Thread { + Bitmap bitmap; + boolean needsLock; + + public DecodeThread(boolean needsLock) { + this.needsLock = needsLock; + } + + public void run() { + if (needsLock) { + BitmapManager.instance().acquireResourceLock(); + } + bitmap = mImage.thumbBitmap(); + if (needsLock) { + BitmapManager.instance().releaseResourceLock(); + } + } + + public Bitmap getBitmap() { + return bitmap; + } + } + + public void setUp() { + mBitmapManager = BitmapManager.instance(); + mImageList = ImageManager.allImages(getContext().getContentResolver(), + ImageManager.DataLocation.ALL, + ImageManager.INCLUDE_IMAGES, + ImageManager.SORT_DESCENDING); + mImage = mImageList.getImageAt(0); + } + + public void testSingleton() { + BitmapManager manager = BitmapManager.instance(); + assertNotNull(manager); + assertNotNull(mBitmapManager); + assertSame(manager, mBitmapManager); + } + + public void testCanDecoding() { + assertFalse(mBitmapManager.canDecode()); + mBitmapManager.allowAllDecoding(); + assertTrue(mBitmapManager.canDecode()); + mBitmapManager.cancelAllDecoding(); + assertFalse(mBitmapManager.canDecode()); + } + + public void testCheckResourceLockWithoutAcquiringLock() { + mBitmapManager.allowAllDecoding(); + DecodeThread t = new DecodeThread(false); + assertTrue(mBitmapManager.canThreadDecoding(t)); + mBitmapManager.setCheckResourceLock(true); + try { + assertFalse(mBitmapManager.canThreadDecoding(t)); + t.start(); + t.join(); + } catch (InterruptedException ex) { + } finally { + mBitmapManager.setCheckResourceLock(false); + assertNull(t.getBitmap()); + } + } + + public void testCheckResourceLockWithAcquiringLock() { + mBitmapManager.allowAllDecoding(); + DecodeThread t1 = new DecodeThread(true); + DecodeThread t2 = new DecodeThread(true); + assertTrue(mBitmapManager.canThreadDecoding(t1)); + assertTrue(mBitmapManager.canThreadDecoding(t2)); + + // If checking resource lock is necessary, then we can't + // proceed without acquiring the lock first. + mBitmapManager.setCheckResourceLock(true); + assertFalse(mBitmapManager.canThreadDecoding(t1)); + assertFalse(mBitmapManager.canThreadDecoding(t2)); + + try { + // Start two threads at the same time. + t1.start(); + t2.start(); + + Thread.currentThread().sleep(100); + boolean b1 = mBitmapManager.canThreadDecoding(t1); + boolean b2 = mBitmapManager.canThreadDecoding(t2); + + // Only one of them can get the lock. + assertTrue(b1 ^ b2); + + t1.join(); + t2.join(); + } catch (InterruptedException ex) { + } finally { + mBitmapManager.setCheckResourceLock(false); + + // Both threads can decode the bitmap eventually. + assertNotNull(t1.getBitmap()); + assertNotNull(t2.getBitmap()); + } + } + + public void testDecoding() { + assertNotNull(mImage); + mBitmapManager.allowAllDecoding(); + mBitmapManager.setCheckResourceLock(false); + Bitmap bitmap = mImage.thumbBitmap(); + assertNotNull(bitmap); + + // Disable all decoding. + mBitmapManager.cancelAllDecoding(); + bitmap = mImage.thumbBitmap(); + assertNull(bitmap); + } + + public void testCanThreadDecoding() { + mBitmapManager.allowAllDecoding(); + Thread t = new DecodeThread(false); + + // By default all threads can decode. + assertTrue(mBitmapManager.canThreadDecoding(t)); + + // Disallow thread t to decode. + mBitmapManager.cancelThreadDecoding(t); + assertFalse(mBitmapManager.canThreadDecoding(t)); + + // Allow thread t to decode again. + mBitmapManager.allowThreadDecoding(t); + assertTrue(mBitmapManager.canThreadDecoding(t)); + } + + public void testThreadDecoding() { + DecodeThread t1 = new DecodeThread(false); + DecodeThread t2 = new DecodeThread(false); + mBitmapManager.allowAllDecoding(); + mBitmapManager.setCheckResourceLock(false); + mBitmapManager.allowThreadDecoding(t1); + mBitmapManager.cancelThreadDecoding(t2); + t1.start(); + t2.start(); + + try { + t1.join(); + t2.join(); + } catch (InterruptedException ex) { + } finally { + assertTrue(mBitmapManager.canThreadDecoding(t1)); + assertNotNull(t1.getBitmap()); + assertFalse(mBitmapManager.canThreadDecoding(t2)); + assertNull(t2.getBitmap()); + } + + mBitmapManager.cancelAllDecoding(); + } + + @Override + public String toString() { + return "BitmapManagerUnitTest"; + } +} |