Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package android.wm; |
| 18 | |
| 19 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 20 | |
Riddle Hsu | f5622d2 | 2019-09-24 17:56:05 -0600 | [diff] [blame^] | 21 | import android.app.Activity; |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 22 | import android.app.UiAutomation; |
Riddle Hsu | f5622d2 | 2019-09-24 17:56:05 -0600 | [diff] [blame^] | 23 | import android.content.Intent; |
| 24 | import android.perftests.utils.PerfTestActivity; |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 25 | |
Riddle Hsu | f5622d2 | 2019-09-24 17:56:05 -0600 | [diff] [blame^] | 26 | import androidx.test.rule.ActivityTestRule; |
| 27 | import androidx.test.runner.lifecycle.ActivityLifecycleCallback; |
| 28 | import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; |
| 29 | import androidx.test.runner.lifecycle.Stage; |
| 30 | |
| 31 | import org.junit.BeforeClass; |
| 32 | import org.junit.runner.Description; |
| 33 | import org.junit.runners.model.Statement; |
| 34 | |
| 35 | import java.util.concurrent.TimeUnit; |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 36 | |
| 37 | public class WindowManagerPerfTestBase { |
| 38 | static final UiAutomation sUiAutomation = getInstrumentation().getUiAutomation(); |
| 39 | static final long NANOS_PER_S = 1000L * 1000 * 1000; |
Riddle Hsu | 5ef56dd6 | 2019-07-26 21:28:51 -0600 | [diff] [blame] | 40 | static final long TIME_1_S_IN_NS = 1 * NANOS_PER_S; |
| 41 | static final long TIME_5_S_IN_NS = 5 * NANOS_PER_S; |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 42 | |
Riddle Hsu | f5622d2 | 2019-09-24 17:56:05 -0600 | [diff] [blame^] | 43 | @BeforeClass |
| 44 | public static void setUpOnce() { |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 45 | // In order to be closer to the real use case. |
| 46 | sUiAutomation.executeShellCommand("input keyevent KEYCODE_WAKEUP"); |
| 47 | sUiAutomation.executeShellCommand("wm dismiss-keyguard"); |
Riddle Hsu | f5622d2 | 2019-09-24 17:56:05 -0600 | [diff] [blame^] | 48 | getInstrumentation().getContext().startActivity(new Intent(Intent.ACTION_MAIN) |
| 49 | .addCategory(Intent.CATEGORY_HOME).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Provides an activity that keeps screen on and is able to wait for a stable lifecycle stage. |
| 54 | */ |
| 55 | static class PerfTestActivityRule extends ActivityTestRule<PerfTestActivity> { |
| 56 | private final Intent mStartIntent = |
| 57 | new Intent().putExtra(PerfTestActivity.INTENT_EXTRA_KEEP_SCREEN_ON, true); |
| 58 | private final LifecycleListener mLifecycleListener = new LifecycleListener(); |
| 59 | |
| 60 | PerfTestActivityRule() { |
| 61 | this(false /* launchActivity */); |
| 62 | } |
| 63 | |
| 64 | PerfTestActivityRule(boolean launchActivity) { |
| 65 | super(PerfTestActivity.class, false /* initialTouchMode */, launchActivity); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Statement apply(Statement base, Description description) { |
| 70 | final Statement wrappedStatement = new Statement() { |
| 71 | @Override |
| 72 | public void evaluate() throws Throwable { |
| 73 | ActivityLifecycleMonitorRegistry.getInstance() |
| 74 | .addLifecycleCallback(mLifecycleListener); |
| 75 | base.evaluate(); |
| 76 | ActivityLifecycleMonitorRegistry.getInstance() |
| 77 | .removeLifecycleCallback(mLifecycleListener); |
| 78 | } |
| 79 | }; |
| 80 | return super.apply(wrappedStatement, description); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected Intent getActivityIntent() { |
| 85 | return mStartIntent; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public PerfTestActivity launchActivity(Intent intent) { |
| 90 | final PerfTestActivity activity = super.launchActivity(intent); |
| 91 | mLifecycleListener.setTargetActivity(activity); |
| 92 | return activity; |
| 93 | } |
| 94 | |
| 95 | PerfTestActivity launchActivity() { |
| 96 | return launchActivity(mStartIntent); |
| 97 | } |
| 98 | |
| 99 | void waitForIdleSync(Stage state) { |
| 100 | mLifecycleListener.waitForIdleSync(state); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static class LifecycleListener implements ActivityLifecycleCallback { |
| 105 | private Activity mTargetActivity; |
| 106 | private Stage mWaitingStage; |
| 107 | private Stage mReceivedStage; |
| 108 | |
| 109 | void setTargetActivity(Activity activity) { |
| 110 | mTargetActivity = activity; |
| 111 | mReceivedStage = mWaitingStage = null; |
| 112 | } |
| 113 | |
| 114 | void waitForIdleSync(Stage stage) { |
| 115 | synchronized (this) { |
| 116 | if (stage != mReceivedStage) { |
| 117 | mWaitingStage = stage; |
| 118 | try { |
| 119 | wait(TimeUnit.NANOSECONDS.toMillis(TIME_5_S_IN_NS)); |
| 120 | } catch (InterruptedException impossible) { } |
| 121 | } |
| 122 | mWaitingStage = mReceivedStage = null; |
| 123 | } |
| 124 | getInstrumentation().waitForIdleSync(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onActivityLifecycleChanged(Activity activity, Stage stage) { |
| 129 | if (mTargetActivity != activity) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | synchronized (this) { |
| 134 | mReceivedStage = stage; |
| 135 | if (mWaitingStage == mReceivedStage) { |
| 136 | notifyAll(); |
| 137 | } |
| 138 | } |
| 139 | } |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 140 | } |
| 141 | } |