PackageIconLoaderTest.java revision e29d52aa72c96c3147fa91d83aeb8dafc6d1f578
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
17package com.android.quicksearchbox;
18
19import com.android.quicksearchbox.tests.CrashingIconProvider;
20
21import android.os.Handler;
22import android.os.Looper;
23import android.test.suitebuilder.annotation.MediumTest;
24
25/**
26 * Tests for {@link PackageIconLoader}.
27 *
28 */
29@MediumTest
30public class PackageIconLoaderTest extends IconLoaderTest {
31
32    private ConsumerThread mThread;
33
34    @Override
35    public void setUp() throws Exception {
36        mThread = new ConsumerThread();
37        mThread.start();
38        // we do this afterwards, as we need to have the thread set up (it calls create()).
39        super.setUp();
40    }
41
42    @Override
43    public void tearDown() throws Exception {
44        mThread.exit();
45        super.tearDown();
46    }
47
48    @Override
49    protected IconLoader create() throws Exception {
50        return new PackageIconLoader(mContext, mContext.getPackageName(), mThread.getHandler());
51    }
52
53    public void testGetIconCrashingProvider() {
54        String uri = "content://" + CrashingIconProvider.AUTHORITY + "/icon";
55        assertNull(mLoader.getIcon(uri));
56    }
57
58    private class ConsumerThread extends Thread {
59        private Handler mHandler;
60        private final Object mSync = new Object();
61        public ConsumerThread() {
62        }
63        @Override
64        public void run() {
65            Looper.prepare();
66            synchronized (mSync) {
67                mHandler = new Handler();
68                mSync.notifyAll();
69            }
70            Looper.loop();
71        }
72        public Handler getHandler() {
73            synchronized (mSync) {
74                if (mHandler == null) {
75                    try {
76                        mSync.wait();
77                    } catch (InterruptedException e) {
78                    }
79                }
80                return mHandler;
81            }
82        }
83        public void exit() {
84            getHandler().post(new Runnable(){
85                public void run() {
86                    Looper.myLooper().quit();
87                }});
88        }
89    }
90
91}
92