Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2006 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;
     18 
     19 import junit.framework.TestCase;
     20 import android.os.Handler;
     21 import android.os.HandlerThread;
     22 import android.os.Message;
     23 import android.os.Process;
     24 import android.test.suitebuilder.annotation.MediumTest;
     25 import android.test.suitebuilder.annotation.SmallTest;
     26 
     27 public class HandlerThreadTest extends TestCase {
     28     private static final int TEST_WHAT = 1;
     29 
     30     private boolean mGotMessage = false;
     31     private int mGotMessageWhat = -1;
     32     private volatile boolean mDidSetup = false;
     33     private volatile int mLooperTid = -1;
     34 
     35     @MediumTest
     36     public void testHandlerThread() throws Exception {
     37         HandlerThread th1 =  new HandlerThread("HandlerThreadTest") {
     38             protected void onLooperPrepared() {
     39                 synchronized (HandlerThreadTest.this) {
     40                     mDidSetup = true;
     41                     mLooperTid = Process.myTid();
     42                     HandlerThreadTest.this.notify();
     43                 }
     44             }
     45         };
     46 
     47         assertFalse(th1.isAlive());
     48         assertNull(th1.getLooper());
     49 
     50         th1.start();
     51 
     52         assertTrue(th1.isAlive());
     53         assertNotNull(th1.getLooper());
     54 
     55         // The call to getLooper() internally blocks until the looper is
     56         // available, but will call onLooperPrepared() after that.  So we
     57         // need to block here to wait for our onLooperPrepared() to complete
     58         // and fill in the values we expect.
     59         synchronized (this) {
     60             while (!mDidSetup) {
     61                 try {
     62                     wait();
     63                 } catch (InterruptedException e) {
     64                 }
     65             }
     66         }
     67 
     68         // Make sure that the process was set.
     69         assertNotSame(-1, mLooperTid);
     70         // Make sure that the onLooperPrepared() was called on a different thread.
     71         assertNotSame(Process.myTid(), mLooperTid);
     72 
     73         final Handler h1 = new Handler(th1.getLooper()) {
     74             public void handleMessage(Message msg) {
     75                 assertEquals(TEST_WHAT, msg.what);
     76                 // Ensure that we are running on the same thread in which the looper was setup on.
     77                 assertEquals(mLooperTid, Process.myTid());
     78 
     79                 mGotMessageWhat = msg.what;
     80                 mGotMessage = true;
     81                 synchronized(this) {
     82                     notifyAll();
     83                 }
     84             }
     85         };
     86 
     87         Message msg = h1.obtainMessage(TEST_WHAT);
     88 
     89         synchronized (h1) {
     90             // wait until we have the lock before sending the message.
     91             h1.sendMessage(msg);
     92             try {
     93                 // wait for the message to be handled
     94                 h1.wait();
     95             } catch (InterruptedException e) {
     96             }
     97         }
     98 
     99         assertTrue(mGotMessage);
    100         assertEquals(TEST_WHAT, mGotMessageWhat);
    101     }
    102 }
    103