summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/camera/BitmapManagerUnitTests.java
blob: 1e6300e1013e1b8c7edb51ec1e610ee54af9de22 (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
168
169
170
/*
 * Copyright (C) 2009 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 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 BitmapManagerUnitTests extends AndroidTestCase {
    IImageList mImageList;
    IImage mImage;
    BitmapManager mBitmapManager;
    Context mContext;

    private class DecodeThread extends Thread {
        Bitmap bitmap;

        public DecodeThread() {
        }

        @Override
        public void run() {
            bitmap = mImage.thumbBitmap();
        }

        public Bitmap getBitmap() {
            return bitmap;
        }
    }

    @Override
    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 testCanThreadDecoding() {
        Thread t = new DecodeThread();

        // 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 testDefaultAllowDecoding() {
        DecodeThread t = new DecodeThread();
        try {
            t.start();
            t.join();
        } catch (InterruptedException ex) {
        } finally {
            assertNotNull(t.getBitmap());
        }
    }

    public void testCancelDecoding() {
        DecodeThread t = new DecodeThread();
        mBitmapManager.cancelThreadDecoding(t);
        try {
            t.start();
            t.join();
        } catch (InterruptedException ex) {
        } finally {
            assertNull(t.getBitmap());
        }
    }

    public void testAllowDecoding() {
        DecodeThread t = new DecodeThread();
        mBitmapManager.cancelThreadDecoding(t);
        mBitmapManager.allowThreadDecoding(t);
        try {
            t.start();
            t.join();
        } catch (InterruptedException ex) {
        } finally {
            assertNotNull(t.getBitmap());
        }
    }

    public void testThreadDecoding() {
        DecodeThread t1 = new DecodeThread();
        DecodeThread t2 = new DecodeThread();
        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());
        }
    }

    public void testThreadSetDecoding() {
        DecodeThread t1 = new DecodeThread();
        DecodeThread t2 = new DecodeThread();
        DecodeThread t3 = new DecodeThread();
        BitmapManager.ThreadSet set = new BitmapManager.ThreadSet();
        set.add(t1);
        set.add(t2);
        mBitmapManager.cancelThreadDecoding(set);
        t1.start();
        t2.start();
        t3.start();
        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException ex) {
        } finally {
            assertFalse(mBitmapManager.canThreadDecoding(t1));
            assertNull(t1.getBitmap());
            assertFalse(mBitmapManager.canThreadDecoding(t2));
            assertNull(t2.getBitmap());
            assertTrue(mBitmapManager.canThreadDecoding(t3));
            assertNotNull(t3.getBitmap());
        }
    }

    @Override
    public String toString() {
        return "BitmapManagerUnitTest";
    }
}