blob: 29449c80425a978cd60f39aa319e45fc1851a46f [file] [log] [blame]
Vishnu Nair8e3f5f72018-10-01 11:12:51 -07001/*
2 * Copyright (C) 2018 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.server.am;
18
Vishnu Nair6caf4172018-10-29 21:46:56 -070019import static android.os.SystemClock.sleep;
Vishnu Nairce3ba632018-11-16 07:50:27 -080020import static android.server.am.Components.ENTRY_POINT_ALIAS_ACTIVITY;
21import static android.server.am.Components.NO_DISPLAY_ACTIVITY;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070022import static android.server.am.Components.REPORT_FULLY_DRAWN_ACTIVITY;
Vishnu Nairce3ba632018-11-16 07:50:27 -080023import static android.server.am.Components.SINGLE_TASK_ACTIVITY;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070024import static android.server.am.Components.TEST_ACTIVITY;
Vishnu Nair1cb588a2018-10-11 10:07:55 -070025import static android.server.am.Components.TRANSLUCENT_TEST_ACTIVITY;
Vishnu Nair9fc15c92018-12-21 08:28:24 -080026import static android.server.am.UiDeviceUtils.pressBackButton;
27import static android.server.am.UiDeviceUtils.pressHomeButton;
28import static android.server.am.UiDeviceUtils.waitForDeviceIdle;
Vishnu Nair6caf4172018-10-29 21:46:56 -070029import static android.server.am.second.Components.SECOND_ACTIVITY;
30import static android.server.am.third.Components.THIRD_ACTIVITY;
Vishnu Nair86b03b82018-10-31 08:48:08 -070031import static android.util.TimeUtils.formatDuration;
Vishnu Nair9fc15c92018-12-21 08:28:24 -080032
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070033import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION;
34import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DELAY_MS;
35import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS;
36import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN;
37import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN_MS;
38import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS;
39import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS;
40import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CLASS_NAME;
41import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_COLD_LAUNCH;
Vishnu Nair9fc15c92018-12-21 08:28:24 -080042import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_HOT_LAUNCH;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070043import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE;
44import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_WARM_LAUNCH;
45
46import static org.hamcrest.MatcherAssert.assertThat;
47import static org.hamcrest.Matchers.containsString;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070048import static org.hamcrest.Matchers.greaterThanOrEqualTo;
49import static org.hamcrest.Matchers.lessThanOrEqualTo;
50import static org.junit.Assert.assertEquals;
51import static org.junit.Assert.assertNotNull;
Vishnu Nair1cb588a2018-10-11 10:07:55 -070052import static org.junit.Assert.assertNull;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070053import static org.junit.Assert.fail;
54
55import android.app.Activity;
56import android.content.ComponentName;
57import android.metrics.LogMaker;
58import android.metrics.MetricsReader;
59import android.os.SystemClock;
Vishnu Nair9fc15c92018-12-21 08:28:24 -080060import android.platform.test.annotations.Presubmit;
61import android.support.test.filters.FlakyTest;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070062import android.support.test.metricshelper.MetricsAsserts;
63import android.util.EventLog.Event;
64
Tadashi G. Takaokaf280a9b2019-02-01 15:28:04 +090065import com.android.compatibility.common.util.SystemUtil;
66
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070067import org.hamcrest.collection.IsIn;
68import org.junit.Before;
69import org.junit.Test;
70
71import java.util.Arrays;
72import java.util.List;
73import java.util.Queue;
Vishnu Nair1cb588a2018-10-11 10:07:55 -070074import java.util.concurrent.TimeUnit;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070075
76/**
Tadashi G. Takaokaf280a9b2019-02-01 15:28:04 +090077 * CTS device tests for {@link com.android.server.wm.ActivityMetricsLogger}.
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070078 * Build/Install/Run:
79 * atest CtsActivityManagerDeviceTestCases:ActivityMetricsLoggerTests
80 */
81public class ActivityMetricsLoggerTests extends ActivityManagerTestBase {
Vishnu Nair6caf4172018-10-29 21:46:56 -070082 private static final String TAG_ATM = "ActivityTaskManager";
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070083 private final MetricsReader mMetricsReader = new MetricsReader();
Vishnu Nair1cb588a2018-10-11 10:07:55 -070084 private long mPreUptimeMs;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070085 private LogSeparator mLogSeparator;
86
87 @Before
88 public void setUp() throws Exception {
89 super.setUp();
Vishnu Nair1cb588a2018-10-11 10:07:55 -070090 mPreUptimeMs = SystemClock.uptimeMillis();
Vishnu Nair8e3f5f72018-10-01 11:12:51 -070091 mMetricsReader.checkpoint(); // clear out old logs
92 mLogSeparator = separateLogs(); // add a new separator for logs
93 }
94
95 /**
96 * Launch an app and verify:
97 * - appropriate metrics logs are added
98 * - "Displayed activity ..." log is added to logcat
99 * - am_activity_launch_time event is generated
100 * In all three cases, verify the delay measurements are the same.
101 */
102 @Test
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800103 @Presubmit
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700104 public void testAppLaunchIsLogged() {
105 getLaunchActivityBuilder()
106 .setUseInstrumentation()
107 .setTargetActivity(TEST_ACTIVITY)
108 .setWaitForLaunched(true)
109 .execute();
110
Vishnu Nair6caf4172018-10-29 21:46:56 -0700111 final LogMaker metricsLog = getMetricsLog(TEST_ACTIVITY, APP_TRANSITION);
112 final String[] deviceLogs = getDeviceLogsForComponents(mLogSeparator, TAG_ATM);
113 final List<Event> eventLogs = getEventLogsForComponents(mLogSeparator,
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700114 30009 /* AM_ACTIVITY_LAUNCH_TIME */);
115
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700116 final long postUptimeMs = SystemClock.uptimeMillis();
117 assertMetricsLogs(TEST_ACTIVITY, APP_TRANSITION, metricsLog, mPreUptimeMs, postUptimeMs);
Vishnu Nairce3ba632018-11-16 07:50:27 -0800118 assertTransitionIsStartingWindow(metricsLog);
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700119 final int windowsDrawnDelayMs =
120 (int) metricsLog.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS);
121 final String expectedLog =
122 "Displayed " + TEST_ACTIVITY.flattenToShortString()
Vishnu Nair86b03b82018-10-31 08:48:08 -0700123 + ": " + formatDuration(windowsDrawnDelayMs);
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700124 assertLogsContain(deviceLogs, expectedLog);
125 assertEventLogsContainsLaunchTime(eventLogs, TEST_ACTIVITY, windowsDrawnDelayMs);
126 }
127
128 private void assertMetricsLogs(ComponentName componentName,
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700129 int category, LogMaker log, long preUptimeMs, long postUptimeMs) {
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700130 assertNotNull("did not find the metrics log for: " + componentName
131 + " category:" + category, log);
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700132 int startUptimeSec =
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700133 ((Number) log.getTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS)).intValue();
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700134 int preUptimeSec = (int) (TimeUnit.MILLISECONDS.toSeconds(preUptimeMs));
135 int postUptimeSec = (int) (TimeUnit.MILLISECONDS.toSeconds(postUptimeMs));
136 long testElapsedTimeMs = postUptimeMs - preUptimeMs;
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700137 assertThat("must be either cold or warm launch", log.getType(),
138 IsIn.oneOf(TYPE_TRANSITION_COLD_LAUNCH, TYPE_TRANSITION_WARM_LAUNCH));
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700139 assertThat("reported uptime should be after the app was started", startUptimeSec,
140 greaterThanOrEqualTo(preUptimeSec));
141 assertThat("reported uptime should be before assertion time", startUptimeSec,
142 lessThanOrEqualTo(postUptimeSec));
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700143 assertNotNull("log should have delay", log.getTaggedData(APP_TRANSITION_DELAY_MS));
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700144 assertNotNull("log should have windows drawn delay",
145 log.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS));
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700146 long windowsDrawnDelayMs = (int) log.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS);
147 assertThat("windows drawn delay should be less that total elapsed time",
148 windowsDrawnDelayMs, lessThanOrEqualTo(testElapsedTimeMs));
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700149 }
150
Vishnu Nairce3ba632018-11-16 07:50:27 -0800151 private void assertTransitionIsStartingWindow(LogMaker log) {
152 assertEquals("transition should be started because of starting window",
153 1 /* APP_TRANSITION_STARTING_WINDOW */, log.getSubtype());
154 assertNotNull("log should have starting window delay",
155 log.getTaggedData(APP_TRANSITION_STARTING_WINDOW_DELAY_MS));
156 }
157
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700158 private void assertEventLogsContainsLaunchTime(List<Event> events, ComponentName componentName,
159 int windowsDrawnDelayMs) {
160 for (Event event : events) {
161 Object[] arr = (Object[]) event.getData();
162 assertEquals(4, arr.length);
163 final String name = (String) arr[2];
164 final int delay = (int) arr[3];
165 if (name.equals(componentName.flattenToShortString())) {
166 assertEquals("Unexpected windows drawn delay for " + componentName,
167 delay, windowsDrawnDelayMs);
168 return;
169 }
170 }
171 fail("Could not find am_activity_launch_time for " + componentName);
172 }
173
174 /**
175 * Start an activity that reports full drawn and verify:
176 * - fully drawn metrics are added to metrics logs
177 * - "Fully drawn activity ..." log is added to logcat
178 * In both cases verify fully drawn delay measurements are equal.
179 * See {@link Activity#reportFullyDrawn()}
180 */
181 @Test
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800182 @Presubmit
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700183 public void testAppFullyDrawnReportIsLogged() {
184 getLaunchActivityBuilder()
185 .setUseInstrumentation()
186 .setTargetActivity(REPORT_FULLY_DRAWN_ACTIVITY)
187 .setWaitForLaunched(true)
188 .execute();
189
190 // Sleep until activity under test has reported drawn (after 500ms)
Vishnu Nair6caf4172018-10-29 21:46:56 -0700191 sleep(1000);
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700192
Vishnu Nair6caf4172018-10-29 21:46:56 -0700193 final LogMaker metricsLog = getMetricsLog(REPORT_FULLY_DRAWN_ACTIVITY,
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700194 APP_TRANSITION_REPORTED_DRAWN);
Vishnu Nair6caf4172018-10-29 21:46:56 -0700195 final String[] deviceLogs = getDeviceLogsForComponents(mLogSeparator, TAG_ATM);
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700196
197 assertNotNull("did not find the metrics log for: " + REPORT_FULLY_DRAWN_ACTIVITY
198 + " category:" + APP_TRANSITION_REPORTED_DRAWN, metricsLog);
199 assertThat("test activity has a 500ms delay before reporting fully drawn",
200 (long) metricsLog.getTaggedData(APP_TRANSITION_REPORTED_DRAWN_MS),
201 greaterThanOrEqualTo(500L));
202 assertEquals(TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE, metricsLog.getType());
203
204 final long fullyDrawnDelayMs =
205 (long) metricsLog.getTaggedData(APP_TRANSITION_REPORTED_DRAWN_MS);
206 final String expectedLog =
207 "Fully drawn " + REPORT_FULLY_DRAWN_ACTIVITY.flattenToShortString()
Vishnu Nair86b03b82018-10-31 08:48:08 -0700208 + ": " + formatDuration(fullyDrawnDelayMs);
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700209 assertLogsContain(deviceLogs, expectedLog);
210 }
211
212 /**
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800213 * Warm launch an activity with wait option and verify that {@link android.app.WaitResult#totalTime}
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700214 * totalTime is set correctly. Make sure the reported value is consistent with value reported to
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800215 * metrics logs. Verify we output the correct launch state.
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700216 */
217 @Test
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800218 @Presubmit
Vishnu Nair657aa9672018-12-28 16:52:20 -0800219 @FlakyTest(bugId = 122118854)
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800220 public void testAppWarmLaunchSetsWaitResultDelayData() {
Tadashi G. Takaokaf280a9b2019-02-01 15:28:04 +0900221 SystemUtil.runShellCommand("am start -S -W " + TEST_ACTIVITY.flattenToShortString());
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800222
223 // Test warm launch
224 pressBackButton();
225 waitForDeviceIdle(1000);
226 mMetricsReader.checkpoint(); // clear out old logs
227
Tadashi G. Takaokaf280a9b2019-02-01 15:28:04 +0900228 final String amStartOutput = SystemUtil.runShellCommand(
229 "am start -W " + TEST_ACTIVITY.flattenToShortString());
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800230
231 final LogMaker metricsLog = getMetricsLog(TEST_ACTIVITY, APP_TRANSITION);
232 assertNotNull("log should have windows drawn delay", metricsLog);
233
234 final int windowsDrawnDelayMs =
235 (int) metricsLog.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS);
236
237 assertEquals("Expected a cold launch.", metricsLog.getType(), TYPE_TRANSITION_WARM_LAUNCH);
238
239 assertThat("did not find component in am start output.", amStartOutput,
240 containsString(TEST_ACTIVITY.flattenToShortString()));
241
242 assertThat("did not find windows drawn delay time in am start output.", amStartOutput,
243 containsString(Integer.toString(windowsDrawnDelayMs)));
244
245 assertThat("did not find launch state in am start output.", amStartOutput,
246 containsString("WARM"));
247 }
248
249
250 /**
251 * Hot launch an activity with wait option and verify that {@link android.app.WaitResult#totalTime}
252 * totalTime is set correctly. Make sure the reported value is consistent with value reported to
253 * metrics logs. Verify we output the correct launch state.
254 */
255 @Test
256 @Presubmit
Vishnu Nair657aa9672018-12-28 16:52:20 -0800257 @FlakyTest(bugId = 122118854)
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800258 public void testAppHotLaunchSetsWaitResultDelayData() {
Tadashi G. Takaokaf280a9b2019-02-01 15:28:04 +0900259 SystemUtil.runShellCommand("am start -S -W " + TEST_ACTIVITY.flattenToShortString());
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800260
261 // Test hot launch
262 pressHomeButton();
263 waitForDeviceIdle(1000);
264 mMetricsReader.checkpoint(); // clear out old logs
265
Tadashi G. Takaokaf280a9b2019-02-01 15:28:04 +0900266 final String amStartOutput = SystemUtil.runShellCommand(
267 "am start -W " + TEST_ACTIVITY.flattenToShortString());
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800268
269 final LogMaker metricsLog = getMetricsLog(TEST_ACTIVITY, APP_TRANSITION);
270 assertNotNull("log should have windows drawn delay", metricsLog);
271
272 final int windowsDrawnDelayMs =
273 (int) metricsLog.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS);
274
275 assertEquals("Expected a cold launch.", metricsLog.getType(), TYPE_TRANSITION_HOT_LAUNCH);
276
277 assertThat("did not find component in am start output.", amStartOutput,
278 containsString(TEST_ACTIVITY.flattenToShortString()));
279
280 // TODO (b/120981435) use ActivityMetricsLogger to populate hot launch times
281 // assertThat("did not find windows drawn delay time in am start output.", amStartOutput,
282 // containsString(Integer.toString(windowsDrawnDelayMs)));
283
284 assertThat("did not find launch state in am start output.", amStartOutput,
285 containsString("HOT"));
286 }
287
288 /**
289 * Cold launch an activity with wait option and verify that {@link android.app.WaitResult#totalTime}
290 * totalTime is set correctly. Make sure the reported value is consistent with value reported to
291 * metrics logs. Verify we output the correct launch state.
292 */
293 @Test
294 @Presubmit
295 public void testAppColdLaunchSetsWaitResultDelayData() {
Tadashi G. Takaokaf280a9b2019-02-01 15:28:04 +0900296 final String amStartOutput = SystemUtil.runShellCommand(
297 "am start -S -W " + TEST_ACTIVITY.flattenToShortString());
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700298
Vishnu Nair6caf4172018-10-29 21:46:56 -0700299 final LogMaker metricsLog = getMetricsLog(TEST_ACTIVITY, APP_TRANSITION);
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700300 assertNotNull("log should have windows drawn delay", metricsLog);
301
302 final int windowsDrawnDelayMs =
303 (int) metricsLog.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS);
304
305 assertEquals("Expected a cold launch.", metricsLog.getType(), TYPE_TRANSITION_COLD_LAUNCH);
306
307 assertThat("did not find component in am start output.", amStartOutput,
308 containsString(TEST_ACTIVITY.flattenToShortString()));
309
310 assertThat("did not find windows drawn delay time in am start output.", amStartOutput,
311 containsString(Integer.toString(windowsDrawnDelayMs)));
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800312
313 assertThat("did not find launch state in am start output.", amStartOutput,
314 containsString("COLD"));
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700315 }
316
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700317 /**
318 * Launch an app that is already visible and verify we handle cases where we will not
319 * receive a windows drawn message.
320 * see b/117148004
321 */
322 @Test
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800323 @Presubmit
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700324 public void testLaunchOfVisibleApp() {
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700325 // Launch an activity.
326 getLaunchActivityBuilder()
327 .setUseInstrumentation()
Vishnu Nair6caf4172018-10-29 21:46:56 -0700328 .setTargetActivity(SECOND_ACTIVITY)
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700329 .setWaitForLaunched(true)
330 .execute();
331
332 // Launch a translucent activity on top.
333 getLaunchActivityBuilder()
334 .setUseInstrumentation()
335 .setTargetActivity(TRANSLUCENT_TEST_ACTIVITY)
336 .setWaitForLaunched(true)
337 .execute();
338
339 // Launch the first activity again. This will not trigger a windows drawn message since
340 // its windows were visible before launching.
341 mMetricsReader.checkpoint(); // clear out old logs
342 getLaunchActivityBuilder()
343 .setUseInstrumentation()
Vishnu Nair6caf4172018-10-29 21:46:56 -0700344 .setTargetActivity(SECOND_ACTIVITY)
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700345 .setWaitForLaunched(true)
346 .execute();
Vishnu Nair6caf4172018-10-29 21:46:56 -0700347 LogMaker metricsLog = getMetricsLog(SECOND_ACTIVITY, APP_TRANSITION);
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700348 // Verify transition logs are absent since we cannot measure windows drawn delay.
349 assertNull("transition logs should be reset.", metricsLog);
350
351 // Verify metrics for subsequent launches are generated as expected.
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700352 mPreUptimeMs = SystemClock.uptimeMillis();
353 mMetricsReader.checkpoint(); // clear out old logs
354
355 getLaunchActivityBuilder()
356 .setUseInstrumentation()
Vishnu Nair6caf4172018-10-29 21:46:56 -0700357 .setTargetActivity(THIRD_ACTIVITY)
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700358 .setWaitForLaunched(true)
359 .execute();
360
361 long postUptimeMs = SystemClock.uptimeMillis();
Vishnu Nair6caf4172018-10-29 21:46:56 -0700362 metricsLog = getMetricsLog(THIRD_ACTIVITY, APP_TRANSITION);
363 assertMetricsLogs(THIRD_ACTIVITY, APP_TRANSITION, metricsLog, mPreUptimeMs,
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700364 postUptimeMs);
Vishnu Nairce3ba632018-11-16 07:50:27 -0800365 assertTransitionIsStartingWindow(metricsLog);
366 }
367
368 /**
369 * Launch a NoDisplay activity and verify it does not affect subsequent activity launch
370 * metrics. NoDisplay activities do not draw any windows and may be incorrectly identified as a
371 * trampoline activity. See b/80380150 (Long warm launch times reported in dev play console)
372 */
373 @Test
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800374 @Presubmit
375 @FlakyTest(bugId = 80380150)
Vishnu Nairce3ba632018-11-16 07:50:27 -0800376 public void testNoDisplayActivityLaunch() {
377 getLaunchActivityBuilder()
378 .setUseInstrumentation()
379 .setTargetActivity(NO_DISPLAY_ACTIVITY)
380 .setWaitForLaunched(true)
381 .execute();
382
383 mPreUptimeMs = SystemClock.uptimeMillis();
384 getLaunchActivityBuilder()
385 .setUseInstrumentation()
386 .setTargetActivity(SECOND_ACTIVITY)
387 .setWaitForLaunched(true)
388 .execute();
389 final LogMaker metricsLog = getMetricsLog(SECOND_ACTIVITY, APP_TRANSITION);
390 final long postUptimeMs = SystemClock.uptimeMillis();
391 assertMetricsLogs(SECOND_ACTIVITY, APP_TRANSITION, metricsLog, mPreUptimeMs, postUptimeMs);
392 assertTransitionIsStartingWindow(metricsLog);
393 }
394
395 /**
396 * Launch an activity with a trampoline activity and verify launch metrics measures the complete
397 * launch sequence from when the trampoline activity is launching to when the target activity
398 * draws on screen.
399 */
400 @Test
Vishnu Nair9fc15c92018-12-21 08:28:24 -0800401 @Presubmit
Vishnu Nairce3ba632018-11-16 07:50:27 -0800402 public void testTrampolineActivityLaunch() {
403 // Launch a trampoline activity that will launch single task activity.
404 getLaunchActivityBuilder()
405 .setUseInstrumentation()
406 .setTargetActivity(ENTRY_POINT_ALIAS_ACTIVITY)
407 .setWaitForLaunched(true)
408 .execute();
409 final LogMaker metricsLog = getMetricsLog(SINGLE_TASK_ACTIVITY, APP_TRANSITION);
410 final long postUptimeMs = SystemClock.uptimeMillis();
411 assertMetricsLogs(SINGLE_TASK_ACTIVITY, APP_TRANSITION, metricsLog, mPreUptimeMs,
412 postUptimeMs);
Vishnu Nair1cb588a2018-10-11 10:07:55 -0700413 }
414
Vishnu Nair8e3f5f72018-10-01 11:12:51 -0700415 private LogMaker getMetricsLog(ComponentName componentName, int category) {
416 final Queue<LogMaker> startLogs = MetricsAsserts.findMatchingLogs(mMetricsReader,
417 new LogMaker(category));
418 for (LogMaker log : startLogs) {
419 final String actualClassName = (String) log.getTaggedData(FIELD_CLASS_NAME);
420 final String actualPackageName = log.getPackageName();
421 if (componentName.getClassName().equals(actualClassName) &&
422 componentName.getPackageName().equals(actualPackageName)) {
423 return log;
424 }
425 }
426 return null;
427 }
428
429 private void assertLogsContain(String[] logs, String expectedLog) {
430 for (String line : logs) {
431 if (line.contains(expectedLog)) {
432 return;
433 }
434 }
435 fail("Expected to find '" + expectedLog + "' in " + Arrays.toString(logs));
436 }
437}