blob: 008580ad6944ee2372f0c5c730138ba1c388f70c [file] [log] [blame]
Chris Wren930ecca2014-11-12 17:43:41 -05001/*
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
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040018import android.content.Context;
19import android.support.test.InstrumentationRegistry;
Jason Monk86bc3312016-08-16 13:17:56 -040020import android.os.Handler;
21import android.os.Looper;
22import android.os.MessageQueue;
Jason Monke9789282016-11-09 08:59:56 -050023
24import com.android.systemui.utils.TestableContext;
Jason Monk9abca5e2016-11-11 16:18:14 -050025import com.android.systemui.utils.leaks.Tracker;
Jason Monke9789282016-11-09 08:59:56 -050026
27import org.junit.After;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040028import org.junit.Before;
Chris Wren930ecca2014-11-12 17:43:41 -050029
30/**
31 * Base class that does System UI specific setup.
32 */
Jason Monk9abca5e2016-11-11 16:18:14 -050033public abstract class SysuiTestCase {
Jason Monk86bc3312016-08-16 13:17:56 -040034
35 private Handler mHandler;
Jason Monke9789282016-11-09 08:59:56 -050036 protected TestableContext mContext;
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040037
38 @Before
39 public void SysuiSetup() throws Exception {
Jason Monk9abca5e2016-11-11 16:18:14 -050040 mContext = new TestableContext(InstrumentationRegistry.getTargetContext(), this);
Jason Monke9789282016-11-09 08:59:56 -050041 }
42
43 @After
44 public void cleanup() throws Exception {
45 mContext.getSettingsProvider().clearOverrides(this);
Geoffrey Pitsch2c403db2016-08-26 09:09:39 -040046 }
47
48 protected Context getContext() {
49 return mContext;
Chris Wren930ecca2014-11-12 17:43:41 -050050 }
Jason Monk86bc3312016-08-16 13:17:56 -040051
52 protected void waitForIdleSync() {
53 if (mHandler == null) {
54 mHandler = new Handler(Looper.getMainLooper());
55 }
56 waitForIdleSync(mHandler);
57 }
58
59 protected void waitForIdleSync(Handler h) {
60 validateThread(h.getLooper());
61 Idler idler = new Idler(null);
62 h.getLooper().getQueue().addIdleHandler(idler);
63 // Ensure we are non-idle, so the idle handler can run.
64 h.post(new EmptyRunnable());
65 idler.waitForIdle();
66 }
67
68 private static final void validateThread(Looper l) {
69 if (Looper.myLooper() == l) {
70 throw new RuntimeException(
71 "This method can not be called from the looper being synced");
72 }
73 }
74
Jason Monk9abca5e2016-11-11 16:18:14 -050075 // Used for leak tracking, returns null to indicate no leak tracking by default.
76 public Tracker getTracker(String tag) {
77 return null;
78 }
79
Jason Monk86bc3312016-08-16 13:17:56 -040080 public static final class EmptyRunnable implements Runnable {
81 public void run() {
82 }
83 }
84
85 public static final class Idler implements MessageQueue.IdleHandler {
86 private final Runnable mCallback;
87 private boolean mIdle;
88
89 public Idler(Runnable callback) {
90 mCallback = callback;
91 mIdle = false;
92 }
93
94 @Override
95 public boolean queueIdle() {
96 if (mCallback != null) {
97 mCallback.run();
98 }
99 synchronized (this) {
100 mIdle = true;
101 notifyAll();
102 }
103 return false;
104 }
105
106 public void waitForIdle() {
107 synchronized (this) {
108 while (!mIdle) {
109 try {
110 wait();
111 } catch (InterruptedException e) {
112 }
113 }
114 }
115 }
116 }
Chris Wren930ecca2014-11-12 17:43:41 -0500117}