13e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/*
23e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Copyright (C) 2009 The Android Open Source Project
33e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
43e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
53e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * you may not use this file except in compliance with the License.
63e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * You may obtain a copy of the License at
73e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
83e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
93e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
103e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
113e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
123e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * See the License for the specific language governing permissions and
143e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * limitations under the License.
153e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
163e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
173e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpackage com.android.quicksearchbox;
183e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
19e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwoodimport com.android.quicksearchbox.tests.CrashingIconProvider;
20516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwoodimport com.android.quicksearchbox.util.NamedTaskExecutor;
21516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwoodimport com.android.quicksearchbox.util.PriorityThreadFactory;
22516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwoodimport com.android.quicksearchbox.util.SingleThreadNamedTaskExecutor;
23e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
24e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwoodimport android.os.Handler;
25e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwoodimport android.os.Looper;
26516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwoodimport android.os.Process;
273e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertimport android.test.suitebuilder.annotation.MediumTest;
283e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
293e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert/**
303e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert * Tests for {@link PackageIconLoader}.
313e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert *
323e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert */
333e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert@MediumTest
343e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringertpublic class PackageIconLoaderTest extends IconLoaderTest {
353e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
36e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    private ConsumerThread mThread;
37e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
38e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    @Override
39e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    public void setUp() throws Exception {
40e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        mThread = new ConsumerThread();
41e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        mThread.start();
42e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        // we do this afterwards, as we need to have the thread set up (it calls create()).
43e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        super.setUp();
44e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    }
45e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
46e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    @Override
47e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    public void tearDown() throws Exception {
48e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        mThread.exit();
49e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        super.tearDown();
50e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    }
51e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
52fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    @Override
53e1fc4581ebab65220a82bcdb3272b4eb85453837Bjorn Bringert    protected IconLoader create() throws Exception {
54516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwood        NamedTaskExecutor executor = new SingleThreadNamedTaskExecutor(
55516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwood                new PriorityThreadFactory(Process.THREAD_PRIORITY_DEFAULT));
56516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwood        return new PackageIconLoader(mContext, mContext.getPackageName(), mThread.getHandler(),
57516781305d7427e79928c95c6ea2b7689a4bf6ceMathew Inwood                executor);
58e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    }
59e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
60e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    public void testGetIconCrashingProvider() {
61e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        String uri = "content://" + CrashingIconProvider.AUTHORITY + "/icon";
62e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        assertNull(mLoader.getIcon(uri));
63e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    }
64e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood
65e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood    private class ConsumerThread extends Thread {
66e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        private Handler mHandler;
67e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        private final Object mSync = new Object();
68e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public ConsumerThread() {
69e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
70e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        @Override
71e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public void run() {
72e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            Looper.prepare();
73e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            synchronized (mSync) {
74e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                mHandler = new Handler();
75e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                mSync.notifyAll();
76e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            }
77e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            Looper.loop();
78e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
79e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public Handler getHandler() {
80e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            synchronized (mSync) {
81e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                if (mHandler == null) {
82e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                    try {
83e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                        mSync.wait();
84e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                    } catch (InterruptedException e) {
85e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                    }
86e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                }
87e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                return mHandler;
88e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            }
89e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
90e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        public void exit() {
91e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood            getHandler().post(new Runnable(){
92e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                public void run() {
93e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                    Looper.myLooper().quit();
94e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood                }});
95e29d52aa72c96c3147fa91d83aeb8dafc6d1f578Mathew Inwood        }
963e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert    }
973e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert
983e44ff1f2a204db3f479698cf0b3eab3d451dec2Bjorn Bringert}
99