Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 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 | package com.android.systemui; |
| 17 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 18 | import android.content.Context; |
| 19 | import android.support.test.InstrumentationRegistry; |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.Looper; |
| 22 | import android.os.MessageQueue; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 23 | |
| 24 | import com.android.systemui.utils.TestableContext; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame^] | 25 | import com.android.systemui.utils.leaks.Tracker; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 26 | |
| 27 | import org.junit.After; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 28 | import org.junit.Before; |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Base class that does System UI specific setup. |
| 32 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame^] | 33 | public abstract class SysuiTestCase { |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 34 | |
| 35 | private Handler mHandler; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 36 | protected TestableContext mContext; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 37 | |
| 38 | @Before |
| 39 | public void SysuiSetup() throws Exception { |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame^] | 40 | mContext = new TestableContext(InstrumentationRegistry.getTargetContext(), this); |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @After |
| 44 | public void cleanup() throws Exception { |
| 45 | mContext.getSettingsProvider().clearOverrides(this); |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | protected Context getContext() { |
| 49 | return mContext; |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 50 | } |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 51 | |
| 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 Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame^] | 75 | // 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 Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 80 | 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 Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 117 | } |