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