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