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 | |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 18 | import static org.mockito.Mockito.spy; |
| 19 | import static org.mockito.Mockito.when; |
| 20 | |
| 21 | import android.app.Instrumentation; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 22 | import android.content.Context; |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
| 25 | import android.os.MessageQueue; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 26 | import android.support.test.InstrumentationRegistry; |
Jason Monk | 25a52b65 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 27 | import android.support.test.filters.SmallTest; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 28 | import android.testing.LeakCheck; |
Jorim Jaggi | e549a8d | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 29 | import android.util.Log; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 30 | |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 31 | import org.junit.After; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 32 | import org.junit.Before; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 33 | import org.junit.Rule; |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 34 | import org.mockito.invocation.InvocationOnMock; |
| 35 | import org.mockito.stubbing.Answer; |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 36 | |
Jorim Jaggi | e549a8d | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 37 | import java.util.concurrent.ExecutionException; |
| 38 | import java.util.concurrent.Future; |
| 39 | |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 40 | /** |
| 41 | * Base class that does System UI specific setup. |
| 42 | */ |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 43 | public abstract class SysuiTestCase { |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 44 | |
Jorim Jaggi | e549a8d | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 45 | private static final String TAG = "SysuiTestCase"; |
| 46 | |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 47 | private Handler mHandler; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 48 | @Rule |
| 49 | public SysuiTestableContext mContext = new SysuiTestableContext( |
| 50 | InstrumentationRegistry.getContext(), getLeakCheck()); |
| 51 | public TestableDependency mDependency = new TestableDependency(mContext); |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 52 | private Instrumentation mRealInstrumentation; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 53 | |
| 54 | @Before |
| 55 | public void SysuiSetup() throws Exception { |
Jason Monk | 556cfb6 | 2016-12-16 14:01:25 -0500 | [diff] [blame] | 56 | System.setProperty("dexmaker.share_classloader", "true"); |
Jason Monk | 685db72 | 2017-01-23 17:36:50 -0500 | [diff] [blame] | 57 | SystemUIFactory.createFromConfig(mContext); |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 58 | |
| 59 | mRealInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 60 | Instrumentation inst = spy(mRealInstrumentation); |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 61 | when(inst.getContext()).thenAnswer(invocation -> { |
| 62 | throw new RuntimeException( |
| 63 | "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"); |
| 64 | }); |
| 65 | when(inst.getTargetContext()).thenAnswer(invocation -> { |
| 66 | throw new RuntimeException( |
| 67 | "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"); |
| 68 | }); |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 69 | InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments()); |
| 70 | } |
| 71 | |
| 72 | @After |
| 73 | public void SysuiTeardown() { |
| 74 | InstrumentationRegistry.registerInstance(mRealInstrumentation, |
| 75 | InstrumentationRegistry.getArguments()); |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 78 | protected LeakCheck getLeakCheck() { |
| 79 | return null; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 82 | public Context getContext() { |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 83 | return mContext; |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 84 | } |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 85 | |
| 86 | protected void waitForIdleSync() { |
| 87 | if (mHandler == null) { |
| 88 | mHandler = new Handler(Looper.getMainLooper()); |
| 89 | } |
| 90 | waitForIdleSync(mHandler); |
| 91 | } |
| 92 | |
Jorim Jaggi | e549a8d | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 93 | protected void waitForUiOffloadThread() { |
| 94 | Future<?> future = Dependency.get(UiOffloadThread.class).submit(() -> {}); |
| 95 | try { |
| 96 | future.get(); |
| 97 | } catch (InterruptedException | ExecutionException e) { |
| 98 | Log.e(TAG, "Failed to wait for ui offload thread.", e); |
| 99 | } |
| 100 | } |
| 101 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 102 | public static void waitForIdleSync(Handler h) { |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 103 | validateThread(h.getLooper()); |
| 104 | Idler idler = new Idler(null); |
| 105 | h.getLooper().getQueue().addIdleHandler(idler); |
| 106 | // Ensure we are non-idle, so the idle handler can run. |
| 107 | h.post(new EmptyRunnable()); |
| 108 | idler.waitForIdle(); |
| 109 | } |
| 110 | |
| 111 | private static final void validateThread(Looper l) { |
| 112 | if (Looper.myLooper() == l) { |
| 113 | throw new RuntimeException( |
| 114 | "This method can not be called from the looper being synced"); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public static final class EmptyRunnable implements Runnable { |
| 119 | public void run() { |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public static final class Idler implements MessageQueue.IdleHandler { |
| 124 | private final Runnable mCallback; |
| 125 | private boolean mIdle; |
| 126 | |
| 127 | public Idler(Runnable callback) { |
| 128 | mCallback = callback; |
| 129 | mIdle = false; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean queueIdle() { |
| 134 | if (mCallback != null) { |
| 135 | mCallback.run(); |
| 136 | } |
| 137 | synchronized (this) { |
| 138 | mIdle = true; |
| 139 | notifyAll(); |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | public void waitForIdle() { |
| 145 | synchronized (this) { |
| 146 | while (!mIdle) { |
| 147 | try { |
| 148 | wait(); |
| 149 | } catch (InterruptedException e) { |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 155 | } |