Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.server.am; |
| 18 | |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 19 | import static android.os.SystemClock.sleep; |
Vishnu Nair | ce3ba63 | 2018-11-16 07:50:27 -0800 | [diff] [blame] | 20 | import static android.server.am.Components.ENTRY_POINT_ALIAS_ACTIVITY; |
| 21 | import static android.server.am.Components.NO_DISPLAY_ACTIVITY; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 22 | import static android.server.am.Components.REPORT_FULLY_DRAWN_ACTIVITY; |
Vishnu Nair | ce3ba63 | 2018-11-16 07:50:27 -0800 | [diff] [blame] | 23 | import static android.server.am.Components.SINGLE_TASK_ACTIVITY; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 24 | import static android.server.am.Components.TEST_ACTIVITY; |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 25 | import static android.server.am.Components.TRANSLUCENT_TEST_ACTIVITY; |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 26 | import static android.server.am.UiDeviceUtils.pressBackButton; |
| 27 | import static android.server.am.UiDeviceUtils.pressHomeButton; |
| 28 | import static android.server.am.UiDeviceUtils.waitForDeviceIdle; |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 29 | import static android.server.am.second.Components.SECOND_ACTIVITY; |
| 30 | import static android.server.am.third.Components.THIRD_ACTIVITY; |
Vishnu Nair | 86b03b8 | 2018-10-31 08:48:08 -0700 | [diff] [blame] | 31 | import static android.util.TimeUtils.formatDuration; |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 32 | |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 33 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION; |
| 34 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DELAY_MS; |
| 35 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS; |
| 36 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN; |
| 37 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN_MS; |
| 38 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS; |
| 39 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS; |
| 40 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CLASS_NAME; |
| 41 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_COLD_LAUNCH; |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 42 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_HOT_LAUNCH; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 43 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE; |
| 44 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_WARM_LAUNCH; |
| 45 | |
| 46 | import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | import static org.hamcrest.Matchers.containsString; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 48 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 49 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 50 | import static org.junit.Assert.assertEquals; |
| 51 | import static org.junit.Assert.assertNotNull; |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 52 | import static org.junit.Assert.assertNull; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 53 | import static org.junit.Assert.fail; |
| 54 | |
| 55 | import android.app.Activity; |
| 56 | import android.content.ComponentName; |
| 57 | import android.metrics.LogMaker; |
| 58 | import android.metrics.MetricsReader; |
| 59 | import android.os.SystemClock; |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 60 | import android.platform.test.annotations.Presubmit; |
| 61 | import android.support.test.filters.FlakyTest; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 62 | import android.support.test.metricshelper.MetricsAsserts; |
| 63 | import android.util.EventLog.Event; |
| 64 | |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame^] | 65 | import com.android.compatibility.common.util.SystemUtil; |
| 66 | |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 67 | import org.hamcrest.collection.IsIn; |
| 68 | import org.junit.Before; |
| 69 | import org.junit.Test; |
| 70 | |
| 71 | import java.util.Arrays; |
| 72 | import java.util.List; |
| 73 | import java.util.Queue; |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 74 | import java.util.concurrent.TimeUnit; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame^] | 77 | * CTS device tests for {@link com.android.server.wm.ActivityMetricsLogger}. |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 78 | * Build/Install/Run: |
| 79 | * atest CtsActivityManagerDeviceTestCases:ActivityMetricsLoggerTests |
| 80 | */ |
| 81 | public class ActivityMetricsLoggerTests extends ActivityManagerTestBase { |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 82 | private static final String TAG_ATM = "ActivityTaskManager"; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 83 | private final MetricsReader mMetricsReader = new MetricsReader(); |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 84 | private long mPreUptimeMs; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 85 | private LogSeparator mLogSeparator; |
| 86 | |
| 87 | @Before |
| 88 | public void setUp() throws Exception { |
| 89 | super.setUp(); |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 90 | mPreUptimeMs = SystemClock.uptimeMillis(); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 91 | 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 Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 103 | @Presubmit |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 104 | public void testAppLaunchIsLogged() { |
| 105 | getLaunchActivityBuilder() |
| 106 | .setUseInstrumentation() |
| 107 | .setTargetActivity(TEST_ACTIVITY) |
| 108 | .setWaitForLaunched(true) |
| 109 | .execute(); |
| 110 | |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 111 | final LogMaker metricsLog = getMetricsLog(TEST_ACTIVITY, APP_TRANSITION); |
| 112 | final String[] deviceLogs = getDeviceLogsForComponents(mLogSeparator, TAG_ATM); |
| 113 | final List<Event> eventLogs = getEventLogsForComponents(mLogSeparator, |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 114 | 30009 /* AM_ACTIVITY_LAUNCH_TIME */); |
| 115 | |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 116 | final long postUptimeMs = SystemClock.uptimeMillis(); |
| 117 | assertMetricsLogs(TEST_ACTIVITY, APP_TRANSITION, metricsLog, mPreUptimeMs, postUptimeMs); |
Vishnu Nair | ce3ba63 | 2018-11-16 07:50:27 -0800 | [diff] [blame] | 118 | assertTransitionIsStartingWindow(metricsLog); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 119 | final int windowsDrawnDelayMs = |
| 120 | (int) metricsLog.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS); |
| 121 | final String expectedLog = |
| 122 | "Displayed " + TEST_ACTIVITY.flattenToShortString() |
Vishnu Nair | 86b03b8 | 2018-10-31 08:48:08 -0700 | [diff] [blame] | 123 | + ": " + formatDuration(windowsDrawnDelayMs); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 124 | assertLogsContain(deviceLogs, expectedLog); |
| 125 | assertEventLogsContainsLaunchTime(eventLogs, TEST_ACTIVITY, windowsDrawnDelayMs); |
| 126 | } |
| 127 | |
| 128 | private void assertMetricsLogs(ComponentName componentName, |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 129 | int category, LogMaker log, long preUptimeMs, long postUptimeMs) { |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 130 | assertNotNull("did not find the metrics log for: " + componentName |
| 131 | + " category:" + category, log); |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 132 | int startUptimeSec = |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 133 | ((Number) log.getTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS)).intValue(); |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 134 | int preUptimeSec = (int) (TimeUnit.MILLISECONDS.toSeconds(preUptimeMs)); |
| 135 | int postUptimeSec = (int) (TimeUnit.MILLISECONDS.toSeconds(postUptimeMs)); |
| 136 | long testElapsedTimeMs = postUptimeMs - preUptimeMs; |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 137 | assertThat("must be either cold or warm launch", log.getType(), |
| 138 | IsIn.oneOf(TYPE_TRANSITION_COLD_LAUNCH, TYPE_TRANSITION_WARM_LAUNCH)); |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 139 | 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 Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 143 | assertNotNull("log should have delay", log.getTaggedData(APP_TRANSITION_DELAY_MS)); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 144 | assertNotNull("log should have windows drawn delay", |
| 145 | log.getTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS)); |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 146 | 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 Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Vishnu Nair | ce3ba63 | 2018-11-16 07:50:27 -0800 | [diff] [blame] | 151 | 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 Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 158 | 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 Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 182 | @Presubmit |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 183 | 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 Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 191 | sleep(1000); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 192 | |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 193 | final LogMaker metricsLog = getMetricsLog(REPORT_FULLY_DRAWN_ACTIVITY, |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 194 | APP_TRANSITION_REPORTED_DRAWN); |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 195 | final String[] deviceLogs = getDeviceLogsForComponents(mLogSeparator, TAG_ATM); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 196 | |
| 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 Nair | 86b03b8 | 2018-10-31 08:48:08 -0700 | [diff] [blame] | 208 | + ": " + formatDuration(fullyDrawnDelayMs); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 209 | assertLogsContain(deviceLogs, expectedLog); |
| 210 | } |
| 211 | |
| 212 | /** |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 213 | * Warm launch an activity with wait option and verify that {@link android.app.WaitResult#totalTime} |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 214 | * totalTime is set correctly. Make sure the reported value is consistent with value reported to |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 215 | * metrics logs. Verify we output the correct launch state. |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 216 | */ |
| 217 | @Test |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 218 | @Presubmit |
Vishnu Nair | 657aa967 | 2018-12-28 16:52:20 -0800 | [diff] [blame] | 219 | @FlakyTest(bugId = 122118854) |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 220 | public void testAppWarmLaunchSetsWaitResultDelayData() { |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame^] | 221 | SystemUtil.runShellCommand("am start -S -W " + TEST_ACTIVITY.flattenToShortString()); |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 222 | |
| 223 | // Test warm launch |
| 224 | pressBackButton(); |
| 225 | waitForDeviceIdle(1000); |
| 226 | mMetricsReader.checkpoint(); // clear out old logs |
| 227 | |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame^] | 228 | final String amStartOutput = SystemUtil.runShellCommand( |
| 229 | "am start -W " + TEST_ACTIVITY.flattenToShortString()); |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 230 | |
| 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 Nair | 657aa967 | 2018-12-28 16:52:20 -0800 | [diff] [blame] | 257 | @FlakyTest(bugId = 122118854) |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 258 | public void testAppHotLaunchSetsWaitResultDelayData() { |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame^] | 259 | SystemUtil.runShellCommand("am start -S -W " + TEST_ACTIVITY.flattenToShortString()); |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 260 | |
| 261 | // Test hot launch |
| 262 | pressHomeButton(); |
| 263 | waitForDeviceIdle(1000); |
| 264 | mMetricsReader.checkpoint(); // clear out old logs |
| 265 | |
Tadashi G. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame^] | 266 | final String amStartOutput = SystemUtil.runShellCommand( |
| 267 | "am start -W " + TEST_ACTIVITY.flattenToShortString()); |
Vishnu Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 268 | |
| 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. Takaoka | f280a9b | 2019-02-01 15:28:04 +0900 | [diff] [blame^] | 296 | final String amStartOutput = SystemUtil.runShellCommand( |
| 297 | "am start -S -W " + TEST_ACTIVITY.flattenToShortString()); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 298 | |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 299 | final LogMaker metricsLog = getMetricsLog(TEST_ACTIVITY, APP_TRANSITION); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 300 | 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 Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 312 | |
| 313 | assertThat("did not find launch state in am start output.", amStartOutput, |
| 314 | containsString("COLD")); |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 317 | /** |
| 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 Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 323 | @Presubmit |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 324 | public void testLaunchOfVisibleApp() { |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 325 | // Launch an activity. |
| 326 | getLaunchActivityBuilder() |
| 327 | .setUseInstrumentation() |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 328 | .setTargetActivity(SECOND_ACTIVITY) |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 329 | .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 Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 344 | .setTargetActivity(SECOND_ACTIVITY) |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 345 | .setWaitForLaunched(true) |
| 346 | .execute(); |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 347 | LogMaker metricsLog = getMetricsLog(SECOND_ACTIVITY, APP_TRANSITION); |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 348 | // 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 Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 352 | mPreUptimeMs = SystemClock.uptimeMillis(); |
| 353 | mMetricsReader.checkpoint(); // clear out old logs |
| 354 | |
| 355 | getLaunchActivityBuilder() |
| 356 | .setUseInstrumentation() |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 357 | .setTargetActivity(THIRD_ACTIVITY) |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 358 | .setWaitForLaunched(true) |
| 359 | .execute(); |
| 360 | |
| 361 | long postUptimeMs = SystemClock.uptimeMillis(); |
Vishnu Nair | 6caf417 | 2018-10-29 21:46:56 -0700 | [diff] [blame] | 362 | metricsLog = getMetricsLog(THIRD_ACTIVITY, APP_TRANSITION); |
| 363 | assertMetricsLogs(THIRD_ACTIVITY, APP_TRANSITION, metricsLog, mPreUptimeMs, |
Vishnu Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 364 | postUptimeMs); |
Vishnu Nair | ce3ba63 | 2018-11-16 07:50:27 -0800 | [diff] [blame] | 365 | 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 Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 374 | @Presubmit |
| 375 | @FlakyTest(bugId = 80380150) |
Vishnu Nair | ce3ba63 | 2018-11-16 07:50:27 -0800 | [diff] [blame] | 376 | 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 Nair | 9fc15c9 | 2018-12-21 08:28:24 -0800 | [diff] [blame] | 401 | @Presubmit |
Vishnu Nair | ce3ba63 | 2018-11-16 07:50:27 -0800 | [diff] [blame] | 402 | 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 Nair | 1cb588a | 2018-10-11 10:07:55 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Vishnu Nair | 8e3f5f7 | 2018-10-01 11:12:51 -0700 | [diff] [blame] | 415 | 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 | } |