Home | History | Annotate | Download | only in cts
      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 android.os.cts;
     18 
     19 import android.os.Handler;
     20 import android.os.Looper;
     21 import android.os.Message;
     22 import android.os.MessageQueue;
     23 import android.os.SystemClock;
     24 import android.test.AndroidTestCase;
     25 import android.util.Printer;
     26 import android.util.StringBuilderPrinter;
     27 
     28 import com.android.compatibility.common.util.TestThread;
     29 
     30 public class LooperTest extends AndroidTestCase {
     31 
     32     public static final long WAIT_TIME = 1000;
     33 
     34     private boolean mHasRun;
     35 
     36     private Looper mLooper = null;
     37     private boolean mHasQuit;
     38     private Handler mLoopHandler;
     39 
     40     public void testDump() {
     41         StringBuilderPrinter printer = new StringBuilderPrinter(new StringBuilder());
     42         final String prefix = "LooperTest";
     43         Looper.getMainLooper().dump(printer, prefix);
     44     }
     45 
     46     public void testGetMainLooper() {
     47         Looper looper = Looper.getMainLooper();
     48         assertNotNull(looper);
     49     }
     50 
     51     public void testLoop() throws Throwable {
     52         Thread t = new Thread(new Runnable() {
     53             public void run() {
     54                 Looper.prepare();
     55 
     56                 MockRunnable run = new MockRunnable();
     57 
     58                 Handler handler = new Handler();
     59                 Message msg = Message.obtain(handler, run);
     60                 handler.sendMessageAtTime(msg, 0);
     61                 assertFalse(run.runCalled);
     62                 Looper.loop();
     63                 assertTrue(run.runCalled);
     64             }
     65         });
     66         t.start();
     67         t.join();
     68     }
     69 
     70     public void testMyLooper() throws Throwable {
     71         TestThread t = new TestThread(new Runnable() {
     72             public void run() {
     73                 assertNull(Looper.myLooper());
     74                 Looper.prepare();
     75                 assertNotNull(Looper.myLooper());
     76             }
     77         });
     78 
     79         t.runTest(WAIT_TIME);
     80     }
     81 
     82     public void testIsCurrentThread() throws Throwable {
     83         final Looper[] looper = new Looper[1];
     84         TestThread t = new TestThread(new Runnable() {
     85             @Override
     86             public void run() {
     87                 Looper.prepare();
     88                 assertTrue(Looper.myLooper().isCurrentThread());
     89                 looper[0] = Looper.myLooper();
     90             }
     91         });
     92 
     93         t.runTest(WAIT_TIME);
     94         assertFalse(looper[0].isCurrentThread());
     95     }
     96 
     97     public void testMyQueue() throws Throwable {
     98         TestThread t = new TestThread(new Runnable() {
     99             public void run() {
    100                 boolean didThrow = false;
    101                 try {
    102                     assertNull(Looper.myQueue());
    103                 } catch (Throwable e) {
    104                     // expected
    105                     didThrow = true;
    106                 }
    107                 if (!didThrow) {
    108                     fail("should throw exception");
    109                 }
    110                 Looper.prepare();
    111                 MessageQueue mq = Looper.myQueue();
    112                 assertNotNull(mq);
    113             }
    114         });
    115 
    116         t.runTest(WAIT_TIME);
    117     }
    118 
    119     public void testGetQueue() throws Throwable {
    120         TestThread t = new TestThread(new Runnable() {
    121             public void run() {
    122                 Looper.prepare();
    123                 assertNotNull(Looper.myLooper().getQueue());
    124                 assertEquals(Looper.myLooper().getQueue(), Looper.myQueue());
    125             }
    126         });
    127 
    128         t.runTest(WAIT_TIME);
    129     }
    130 
    131     public void testPrepare() throws Throwable {
    132         TestThread t = new TestThread(new Runnable() {
    133             public void run() {
    134                 Looper.prepare();
    135 
    136                 try {
    137                     Looper.prepare();
    138                     fail("should throw exception");
    139                 } catch (Exception e) {
    140                     //expected
    141                 }
    142             }
    143         });
    144 
    145         t.runTest(WAIT_TIME);
    146     }
    147 
    148     public void testPrepareMainLooper() throws Throwable {
    149         TestThread t = new TestThread(new Runnable() {
    150             public void run() {
    151                 try {
    152                     Looper.prepareMainLooper();
    153                     fail("should throw exception because the main thread was already prepared");
    154                 } catch (Exception e) {
    155                     //expected
    156                 }
    157             }
    158         });
    159 
    160         t.runTest(WAIT_TIME);
    161     }
    162 
    163     public void testQuit() throws Throwable {
    164         TestThread t = new TestThread(new Runnable() {
    165             public void run() {
    166                 mHasQuit = false;
    167                 Looper.prepare();
    168                 mLoopHandler = new Handler();
    169                 mLooper = Looper.myLooper();
    170                 Looper.loop();
    171                 mHasQuit = true;
    172             }
    173         });
    174 
    175         // Here doesn't call runTest() because we don't want to wait the runTest finish.
    176         // Just need to handle Looper#quit();
    177         t.start();
    178         Thread.sleep(WAIT_TIME);
    179         assertSame(t, mLooper.getThread());
    180         int time = 100;
    181         // Send message before Looper has quit.
    182         assertTrue(mLoopHandler.sendEmptyMessageAtTime(0, SystemClock.uptimeMillis() + time));
    183         Thread.sleep(WAIT_TIME);
    184 
    185         mLooper.quit();
    186         Thread.sleep(WAIT_TIME);
    187         // Send message after Looper has quit.
    188         assertFalse(mLoopHandler.sendEmptyMessageAtTime(1, SystemClock.uptimeMillis() + time));
    189         assertTrue(mHasQuit);
    190 
    191         t.joinAndCheck(WAIT_TIME);
    192     }
    193 
    194     public void testSetMessageLogging() throws Throwable {
    195         mHasRun = false;
    196 
    197         TestThread t = new TestThread(new Runnable() {
    198             public void run() {
    199                 Looper.prepare();
    200                 MockPrinter mp = new MockPrinter();
    201                 Looper.myLooper().setMessageLogging(mp);
    202                 MockRunnable run = new MockRunnable();
    203 
    204                 Handler handler = new Handler();
    205                 Message msg = Message.obtain(handler, run);
    206                 handler.sendMessageAtTime(msg, 0);
    207 
    208                 Looper.loop();
    209                 assertNotNull(mp.str);
    210                 mHasRun = true;
    211             }
    212         });
    213 
    214         t.runTest(WAIT_TIME);
    215 
    216         assertTrue(mHasRun);
    217     }
    218 
    219     public void testToString() {
    220         assertNotNull(Looper.getMainLooper().toString());
    221     }
    222 
    223     class MockPrinter implements Printer {
    224         public String str;
    225 
    226         public void println(String x) {
    227             str = x;
    228         }
    229     }
    230 
    231     private class MockRunnable implements Runnable {
    232         public boolean runCalled = false;
    233 
    234         public void run() {
    235             runCalled = true;
    236             Looper.myLooper().quit();
    237         }
    238 
    239         public void stop() {
    240             Looper.myLooper().quit();
    241         }
    242     }
    243 }
    244