Home | History | Annotate | Download | only in shared
      1 /*
      2  * Copyright (C) 2014 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.systemui.shared;
     18 
     19 import android.content.Context;
     20 import android.os.Handler;
     21 import android.os.Looper;
     22 import android.os.MessageQueue;
     23 import android.support.test.InstrumentationRegistry;
     24 
     25 import org.junit.After;
     26 import org.junit.Before;
     27 
     28 /**
     29  * Base class that does System UI Shared Lib specific setup.
     30  */
     31 public abstract class SysuiSharedLibTestCase {
     32 
     33     private static final String TAG = "SysuiSharedLibTestCase";
     34 
     35     private Handler mHandler;
     36     private Context mContext = InstrumentationRegistry.getContext();
     37 
     38     @Before
     39     public void SysuiSetup() throws Exception {
     40         // Enable shared class loader to test package-private classes/methods
     41         System.setProperty("dexmaker.share_classloader", "true");
     42     }
     43 
     44     @After
     45     public void SysuiTeardown() {
     46         // Do nothing
     47     }
     48 
     49     public Context getContext() {
     50         return mContext;
     51     }
     52 
     53     protected void waitForIdleSync() {
     54         if (mHandler == null) {
     55             mHandler = new Handler(Looper.getMainLooper());
     56         }
     57         waitForIdleSync(mHandler);
     58     }
     59 
     60     public static void waitForIdleSync(Handler h) {
     61         validateThread(h.getLooper());
     62         Idler idler = new Idler(null);
     63         h.getLooper().getQueue().addIdleHandler(idler);
     64         // Ensure we are non-idle, so the idle handler can run.
     65         h.post(new EmptyRunnable());
     66         idler.waitForIdle();
     67     }
     68 
     69     private static final void validateThread(Looper l) {
     70         if (Looper.myLooper() == l) {
     71             throw new RuntimeException(
     72                 "This method can not be called from the looper being synced");
     73         }
     74     }
     75 
     76     public static final class EmptyRunnable implements Runnable {
     77         public void run() {
     78         }
     79     }
     80 
     81     public static final class Idler implements MessageQueue.IdleHandler {
     82         private final Runnable mCallback;
     83         private boolean mIdle;
     84 
     85         public Idler(Runnable callback) {
     86             mCallback = callback;
     87             mIdle = false;
     88         }
     89 
     90         @Override
     91         public boolean queueIdle() {
     92             if (mCallback != null) {
     93                 mCallback.run();
     94             }
     95             synchronized (this) {
     96                 mIdle = true;
     97                 notifyAll();
     98             }
     99             return false;
    100         }
    101 
    102         public void waitForIdle() {
    103             synchronized (this) {
    104                 while (!mIdle) {
    105                     try {
    106                         wait();
    107                     } catch (InterruptedException e) {
    108                     }
    109                 }
    110             }
    111         }
    112     }
    113 }
    114