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