1 /*
2  * Copyright (C) 2008 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 android.os.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertSame;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.os.HandlerThread;
25 import android.os.Looper;
26 import android.os.Process;
27 import android.platform.test.annotations.AppModeSdkSandbox;
28 import android.platform.test.ravenwood.RavenwoodRule;
29 
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
37 @RunWith(AndroidJUnit4.class)
38 public class HandlerThreadTest {
39     @Rule
40     public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder()
41             .setProvideMainThread(true).build();
42 
43     private static final int SLEEPTIME = 100;
44 
45     @Test
testConstructor()46     public void testConstructor() {
47         // new the HandlerThread instance
48         new HandlerThread("test");
49         // new the HandlerThread instance
50         new HandlerThread("test", Thread.MAX_PRIORITY);
51     }
52 
53     @Test
testGetThreadId()54     public void testGetThreadId() {
55         MockHandlerThread ht = new MockHandlerThread("test");
56         assertEquals(-1, ht.getThreadId());
57         ht.start();
58         sleep(SLEEPTIME);
59 
60         assertEquals(ht.getMyTid(), ht.getThreadId());
61 
62         assertTrue(ht.isRunCalled());
63 
64         assertTrue(ht.isOnLooperPreparedCalled());
65 
66         assertNotNull(ht.getLooper());
67         Looper looper = ht.getLooper();
68         assertNotNull(looper);
69         assertSame(ht.getMyLooper(), looper);
70     }
71 
sleep(long sleepTime)72     private void sleep(long sleepTime) {
73         try {
74             Thread.sleep(sleepTime);
75         } catch (InterruptedException e) {
76         }
77     }
78 
79     static class MockHandlerThread extends HandlerThread {
80 
81         private boolean mIsOnLooperPreparedCalled;
82         private int mMyTid;
83         private Looper mLooper;
84         private boolean mIsRunCalled;
85 
MockHandlerThread(String name)86         public MockHandlerThread(String name) {
87             super(name);
88         }
89 
isRunCalled()90         public boolean isRunCalled() {
91             return mIsRunCalled;
92         }
93 
94         @Override
onLooperPrepared()95         public void onLooperPrepared() {
96             mIsOnLooperPreparedCalled = true;
97             mMyTid = Process.myTid();
98             mLooper = getLooper();
99             super.onLooperPrepared();
100         }
101 
102         @Override
run()103         public void run() {
104             mIsRunCalled = true;
105             super.run();
106         }
107 
isOnLooperPreparedCalled()108         public boolean isOnLooperPreparedCalled() {
109             return mIsOnLooperPreparedCalled;
110         }
111 
getMyTid()112         public int getMyTid() {
113             return mMyTid;
114         }
115 
getMyLooper()116         public Looper getMyLooper() {
117             return mLooper;
118         }
119     }
120 
121 }
122