SysuiTestCase.java revision 556cfb6a36d494f4e536b802f31924c56ee6d9c0
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;
23
24import com.android.systemui.utils.TestableContext;
25import com.android.systemui.utils.leaks.Tracker;
26
27import org.junit.After;
28import org.junit.Before;
29
30/**
31 * Base class that does System UI specific setup.
32 */
33public abstract class SysuiTestCase {
34
35    private Handler mHandler;
36    protected TestableContext mContext;
37
38    @Before
39    public void SysuiSetup() throws Exception {
40        System.setProperty("dexmaker.share_classloader", "true");
41        mContext = new TestableContext(InstrumentationRegistry.getTargetContext(), this);
42    }
43
44    @After
45    public void cleanup() throws Exception {
46        mContext.getSettingsProvider().clearOverrides(this);
47    }
48
49    protected 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    protected 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    // Used for leak tracking, returns null to indicate no leak tracking by default.
77    public Tracker getTracker(String tag) {
78        return null;
79    }
80
81    public static final class EmptyRunnable implements Runnable {
82        public void run() {
83        }
84    }
85
86    public static final class Idler implements MessageQueue.IdleHandler {
87        private final Runnable mCallback;
88        private boolean mIdle;
89
90        public Idler(Runnable callback) {
91            mCallback = callback;
92            mIdle = false;
93        }
94
95        @Override
96        public boolean queueIdle() {
97            if (mCallback != null) {
98                mCallback.run();
99            }
100            synchronized (this) {
101                mIdle = true;
102                notifyAll();
103            }
104            return false;
105        }
106
107        public void waitForIdle() {
108            synchronized (this) {
109                while (!mIdle) {
110                    try {
111                        wait();
112                    } catch (InterruptedException e) {
113                    }
114                }
115            }
116        }
117    }
118}
119