blob: 4d278c3c2d9a5998e2370d51f974f9e0ab681630 [file] [log] [blame]
Riddle Hsu54a86c62019-07-10 21:37:08 +08001/*
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
17package android.wm;
18
19import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20
Riddle Hsuf5622d22019-09-24 17:56:05 -060021import android.app.Activity;
Riddle Hsu54a86c62019-07-10 21:37:08 +080022import android.app.UiAutomation;
Riddle Hsuf5622d22019-09-24 17:56:05 -060023import android.content.Intent;
24import android.perftests.utils.PerfTestActivity;
Riddle Hsu54a86c62019-07-10 21:37:08 +080025
Riddle Hsuf5622d22019-09-24 17:56:05 -060026import androidx.test.rule.ActivityTestRule;
27import androidx.test.runner.lifecycle.ActivityLifecycleCallback;
28import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
29import androidx.test.runner.lifecycle.Stage;
30
31import org.junit.BeforeClass;
32import org.junit.runner.Description;
33import org.junit.runners.model.Statement;
34
35import java.util.concurrent.TimeUnit;
Riddle Hsu54a86c62019-07-10 21:37:08 +080036
37public class WindowManagerPerfTestBase {
38 static final UiAutomation sUiAutomation = getInstrumentation().getUiAutomation();
39 static final long NANOS_PER_S = 1000L * 1000 * 1000;
Riddle Hsu5ef56dd62019-07-26 21:28:51 -060040 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 Hsu54a86c62019-07-10 21:37:08 +080042
Riddle Hsuf5622d22019-09-24 17:56:05 -060043 @BeforeClass
44 public static void setUpOnce() {
Riddle Hsu54a86c62019-07-10 21:37:08 +080045 // In order to be closer to the real use case.
46 sUiAutomation.executeShellCommand("input keyevent KEYCODE_WAKEUP");
47 sUiAutomation.executeShellCommand("wm dismiss-keyguard");
Riddle Hsuf5622d22019-09-24 17:56:05 -060048 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 Hsu54a86c62019-07-10 21:37:08 +0800140 }
141}