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; |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Looper; |
| 24 | import android.os.MessageQueue; |
Winson Chung | 16e185e | 2017-11-07 08:30:54 -0800 | [diff] [blame] | 25 | import android.os.ParcelFileDescriptor; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 26 | import android.support.test.InstrumentationRegistry; |
Adrian Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 27 | import android.testing.DexmakerShareClassLoaderRule; |
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; |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 34 | |
Winson Chung | 16e185e | 2017-11-07 08:30:54 -0800 | [diff] [blame] | 35 | import java.io.FileInputStream; |
| 36 | import java.io.IOException; |
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()); |
Adrian Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 51 | @Rule |
| 52 | public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = |
| 53 | new DexmakerShareClassLoaderRule(); |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 54 | public TestableDependency mDependency = new TestableDependency(mContext); |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 55 | private Instrumentation mRealInstrumentation; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 56 | |
| 57 | @Before |
| 58 | public void SysuiSetup() throws Exception { |
Lucas Dupin | 6865b71 | 2017-09-11 12:28:03 -0700 | [diff] [blame] | 59 | mContext.setTheme(R.style.Theme_SystemUI); |
Jason Monk | 685db72 | 2017-01-23 17:36:50 -0500 | [diff] [blame] | 60 | SystemUIFactory.createFromConfig(mContext); |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 61 | |
| 62 | mRealInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 63 | Instrumentation inst = spy(mRealInstrumentation); |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 64 | when(inst.getContext()).thenAnswer(invocation -> { |
| 65 | throw new RuntimeException( |
| 66 | "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"); |
| 67 | }); |
| 68 | when(inst.getTargetContext()).thenAnswer(invocation -> { |
| 69 | throw new RuntimeException( |
| 70 | "SysUI Tests should use SysuiTestCase#getContext or SysuiTestCase#mContext"); |
| 71 | }); |
Jason Monk | 893f0bd | 2017-06-01 11:21:14 -0400 | [diff] [blame] | 72 | InstrumentationRegistry.registerInstance(inst, InstrumentationRegistry.getArguments()); |
| 73 | } |
| 74 | |
| 75 | @After |
| 76 | public void SysuiTeardown() { |
| 77 | InstrumentationRegistry.registerInstance(mRealInstrumentation, |
| 78 | InstrumentationRegistry.getArguments()); |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 81 | protected LeakCheck getLeakCheck() { |
| 82 | return null; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Julia Reynolds | 5daa472 | 2017-12-20 09:53:59 -0500 | [diff] [blame] | 85 | public SysuiTestableContext getContext() { |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 86 | return mContext; |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 87 | } |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 88 | |
Winson Chung | 16e185e | 2017-11-07 08:30:54 -0800 | [diff] [blame] | 89 | protected void runShellCommand(String command) throws IOException { |
| 90 | ParcelFileDescriptor pfd = mRealInstrumentation.getUiAutomation() |
| 91 | .executeShellCommand(command); |
| 92 | |
| 93 | // Read the input stream fully. |
| 94 | FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd); |
| 95 | while (fis.read() != -1); |
| 96 | fis.close(); |
| 97 | } |
| 98 | |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 99 | protected void waitForIdleSync() { |
| 100 | if (mHandler == null) { |
| 101 | mHandler = new Handler(Looper.getMainLooper()); |
| 102 | } |
| 103 | waitForIdleSync(mHandler); |
| 104 | } |
| 105 | |
Jorim Jaggi | e549a8d | 2017-05-15 02:40:05 +0200 | [diff] [blame] | 106 | protected void waitForUiOffloadThread() { |
| 107 | Future<?> future = Dependency.get(UiOffloadThread.class).submit(() -> {}); |
| 108 | try { |
| 109 | future.get(); |
| 110 | } catch (InterruptedException | ExecutionException e) { |
| 111 | Log.e(TAG, "Failed to wait for ui offload thread.", e); |
| 112 | } |
| 113 | } |
| 114 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 115 | public static void waitForIdleSync(Handler h) { |
Jason Monk | 86bc331 | 2016-08-16 13:17:56 -0400 | [diff] [blame] | 116 | validateThread(h.getLooper()); |
| 117 | Idler idler = new Idler(null); |
| 118 | h.getLooper().getQueue().addIdleHandler(idler); |
| 119 | // Ensure we are non-idle, so the idle handler can run. |
| 120 | h.post(new EmptyRunnable()); |
| 121 | idler.waitForIdle(); |
| 122 | } |
| 123 | |
| 124 | private static final void validateThread(Looper l) { |
| 125 | if (Looper.myLooper() == l) { |
| 126 | throw new RuntimeException( |
| 127 | "This method can not be called from the looper being synced"); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | public static final class EmptyRunnable implements Runnable { |
| 132 | public void run() { |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public static final class Idler implements MessageQueue.IdleHandler { |
| 137 | private final Runnable mCallback; |
| 138 | private boolean mIdle; |
| 139 | |
| 140 | public Idler(Runnable callback) { |
| 141 | mCallback = callback; |
| 142 | mIdle = false; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean queueIdle() { |
| 147 | if (mCallback != null) { |
| 148 | mCallback.run(); |
| 149 | } |
| 150 | synchronized (this) { |
| 151 | mIdle = true; |
| 152 | notifyAll(); |
| 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | public void waitForIdle() { |
| 158 | synchronized (this) { |
| 159 | while (!mIdle) { |
| 160 | try { |
| 161 | wait(); |
| 162 | } catch (InterruptedException e) { |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
Chris Wren | 930ecca | 2014-11-12 17:43:41 -0500 | [diff] [blame] | 168 | } |