summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java
blob: 2a11ae0461ed18284ebd36f9f32368ac490cad77 (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

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.content.Context;
import android.graphics.Bitmap;
import android.test.AndroidTestCase;

/**
 * BitmapManager's unit tests.
 */
public class BitmapManagerUnitTest extends AndroidTestCase {
    IImageList mImageList;
    IImage mImage;
    BitmapManager mBitmapManager;
    Context mContext;

    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() {
        mContext = getContext();
        mBitmapManager = BitmapManager.instance();
        mImageList = ImageManager.allImages(mContext.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 testCheckResourceLockWithoutAcquiringLock() {
        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() {
        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.setCheckResourceLock(false);
        Bitmap bitmap = mImage.thumbBitmap();
        assertNotNull(bitmap);

        // Disable all decoding.
        mBitmapManager.cancelAllDecoding();
        bitmap = mImage.thumbBitmap();
        assertNull(bitmap);
    }

    public void testCanThreadDecoding() {
        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.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";
    }
}