Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2009 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 
     17 package com.android.camera;
     18 
     19 import com.android.camera.gallery.IImage;
     20 import com.android.camera.gallery.IImageList;
     21 
     22 import android.content.Context;
     23 import android.graphics.Bitmap;
     24 import android.test.AndroidTestCase;
     25 
     26 /**
     27  * BitmapManager's unit tests.
     28  */
     29 public class BitmapManagerUnitTests extends AndroidTestCase {
     30     IImageList mImageList;
     31     IImage mImage;
     32     BitmapManager mBitmapManager;
     33     Context mContext;
     34 
     35     private class DecodeThread extends Thread {
     36         Bitmap bitmap;
     37 
     38         public DecodeThread() {
     39         }
     40 
     41         @Override
     42         public void run() {
     43             bitmap = mImage.thumbBitmap(IImage.ROTATE_AS_NEEDED);
     44         }
     45 
     46         public Bitmap getBitmap() {
     47             return bitmap;
     48         }
     49     }
     50 
     51     @Override
     52     public void setUp() {
     53         mContext = getContext();
     54         mBitmapManager = BitmapManager.instance();
     55         mImageList = ImageManager.makeImageList(
     56                 mContext.getContentResolver(),
     57                 ImageManager.DataLocation.ALL,
     58                 ImageManager.INCLUDE_IMAGES,
     59                 ImageManager.SORT_DESCENDING,
     60                 null);
     61         mImage = mImageList.getImageAt(0);
     62     }
     63 
     64     public void testSingleton() {
     65         BitmapManager manager = BitmapManager.instance();
     66         assertNotNull(manager);
     67         assertNotNull(mBitmapManager);
     68         assertSame(manager, mBitmapManager);
     69     }
     70 
     71     public void testCanThreadDecoding() {
     72         Thread t = new DecodeThread();
     73 
     74         // By default all threads can decode.
     75         assertTrue(mBitmapManager.canThreadDecoding(t));
     76 
     77         // Disallow thread t to decode.
     78         mBitmapManager.cancelThreadDecoding(t, mContext.getContentResolver());
     79         assertFalse(mBitmapManager.canThreadDecoding(t));
     80 
     81         // Allow thread t to decode again.
     82         mBitmapManager.allowThreadDecoding(t);
     83         assertTrue(mBitmapManager.canThreadDecoding(t));
     84     }
     85 
     86     public void testDefaultAllowDecoding() {
     87         DecodeThread t = new DecodeThread();
     88         try {
     89             t.start();
     90             t.join();
     91         } catch (InterruptedException ex) {
     92         } finally {
     93             assertNotNull(t.getBitmap());
     94         }
     95     }
     96 
     97     public void testCancelDecoding() {
     98         DecodeThread t = new DecodeThread();
     99         mBitmapManager.cancelThreadDecoding(t, mContext.getContentResolver());
    100         try {
    101             t.start();
    102             t.join();
    103         } catch (InterruptedException ex) {
    104         } finally {
    105             assertNull(t.getBitmap());
    106         }
    107     }
    108 
    109     public void testAllowDecoding() {
    110         DecodeThread t = new DecodeThread();
    111         mBitmapManager.cancelThreadDecoding(t, mContext.getContentResolver());
    112         mBitmapManager.allowThreadDecoding(t);
    113         try {
    114             t.start();
    115             t.join();
    116         } catch (InterruptedException ex) {
    117         } finally {
    118             assertNotNull(t.getBitmap());
    119         }
    120     }
    121 
    122     public void testThreadDecoding() {
    123         DecodeThread t1 = new DecodeThread();
    124         DecodeThread t2 = new DecodeThread();
    125         mBitmapManager.allowThreadDecoding(t1);
    126         mBitmapManager.cancelThreadDecoding(t2, mContext.getContentResolver());
    127         t1.start();
    128         t2.start();
    129 
    130         try {
    131             t1.join();
    132             t2.join();
    133         } catch (InterruptedException ex) {
    134         } finally {
    135             assertTrue(mBitmapManager.canThreadDecoding(t1));
    136             assertNotNull(t1.getBitmap());
    137             assertFalse(mBitmapManager.canThreadDecoding(t2));
    138             assertNull(t2.getBitmap());
    139         }
    140     }
    141 
    142     @Override
    143     public String toString() {
    144         return "BitmapManagerUnitTest";
    145     }
    146 }
    147