SysuiTestCase.java revision 340b0e5216b4fcc435e0459b1ca46155a572100d
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.os.Handler;
20import android.os.Looper;
21import android.os.MessageQueue;
22import android.support.test.InstrumentationRegistry;
23import android.testing.LeakCheck;
24
25import org.junit.Before;
26import org.junit.Rule;
27
28/**
29 * Base class that does System UI specific setup.
30 */
31public abstract class SysuiTestCase {
32
33    private Handler mHandler;
34    @Rule
35    public SysuiTestableContext mContext = new SysuiTestableContext(
36            InstrumentationRegistry.getContext(), getLeakCheck());
37    public TestableDependency mDependency = new TestableDependency(mContext);
38
39    @Before
40    public void SysuiSetup() throws Exception {
41        System.setProperty("dexmaker.share_classloader", "true");
42        SystemUIFactory.createFromConfig(mContext);
43    }
44
45    protected LeakCheck getLeakCheck() {
46        return null;
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