Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -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 com.android.server.wm; |
| 18 | |
| 19 | import static android.app.ActivityManager.START_SUCCESS; |
| 20 | import static android.app.ActivityManager.START_TASK_TO_FRONT; |
| 21 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 23 | |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 25 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
| 26 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyNoMoreInteractions; |
Yunfan Chen | d552c16 | 2019-02-08 16:51:28 +0900 | [diff] [blame] | 27 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 28 | import static com.google.common.truth.Truth.assertWithMessage; |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 29 | |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.anyInt; |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame^] | 31 | import static org.mockito.ArgumentMatchers.anyLong; |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 32 | import static org.mockito.ArgumentMatchers.argThat; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 33 | import static org.mockito.ArgumentMatchers.eq; |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 34 | import static org.mockito.Mockito.timeout; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 35 | |
| 36 | import android.content.Intent; |
| 37 | import android.os.SystemClock; |
| 38 | import android.platform.test.annotations.Presubmit; |
| 39 | import android.util.SparseIntArray; |
| 40 | |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 41 | import androidx.test.filters.SmallTest; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 42 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 43 | import com.android.server.wm.ActivityMetricsLaunchObserver.ActivityRecordProto; |
| 44 | |
| 45 | import org.junit.After; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 48 | import org.mockito.ArgumentMatcher; |
| 49 | |
| 50 | import java.util.Arrays; |
Riddle Hsu | 7a2fec5 | 2019-09-04 00:12:08 +0800 | [diff] [blame] | 51 | import java.util.concurrent.TimeUnit; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Tests for the {@link ActivityMetricsLaunchObserver} class. |
| 55 | * |
| 56 | * Build/Install/Run: |
| 57 | * atest WmTests:ActivityMetricsLaunchObserverTests |
| 58 | */ |
| 59 | @SmallTest |
| 60 | @Presubmit |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 61 | public class ActivityMetricsLaunchObserverTests extends ActivityTestsBase { |
| 62 | private ActivityMetricsLogger mActivityMetricsLogger; |
| 63 | private ActivityMetricsLaunchObserver mLaunchObserver; |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 64 | private ActivityMetricsLaunchObserverRegistry mLaunchObserverRegistry; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 65 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 66 | private ActivityStack mStack; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 67 | private TaskRecord mTask; |
| 68 | private ActivityRecord mActivityRecord; |
| 69 | private ActivityRecord mActivityRecordTrampoline; |
| 70 | |
| 71 | @Before |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 72 | public void setUpAMLO() { |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 73 | mLaunchObserver = mock(ActivityMetricsLaunchObserver.class); |
| 74 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 75 | // ActivityStackSupervisor always creates its own instance of ActivityMetricsLogger. |
| 76 | mActivityMetricsLogger = mSupervisor.getActivityMetricsLogger(); |
| 77 | |
| 78 | mLaunchObserverRegistry = mActivityMetricsLogger.getLaunchObserverRegistry(); |
| 79 | mLaunchObserverRegistry.registerLaunchObserver(mLaunchObserver); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 80 | |
| 81 | // Sometimes we need an ActivityRecord for ActivityMetricsLogger to do anything useful. |
| 82 | // This seems to be the easiest way to create an ActivityRecord. |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 83 | mStack = new StackBuilder(mRootActivityContainer) |
| 84 | .setActivityType(ACTIVITY_TYPE_STANDARD) |
| 85 | .setWindowingMode(WINDOWING_MODE_FULLSCREEN) |
| 86 | .setOnTop(true) |
| 87 | .setCreateActivity(true) |
| 88 | .build(); |
| 89 | mTask = mStack.topTask(); |
| 90 | mActivityRecord = mTask.getTopActivity(); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 91 | mActivityRecordTrampoline = new ActivityBuilder(mService).setTask(mTask).build(); |
| 92 | } |
| 93 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 94 | @After |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 95 | public void tearDownAMLO() { |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 96 | if (mLaunchObserverRegistry != null) { // Don't NPE if setUp failed. |
| 97 | mLaunchObserverRegistry.unregisterLaunchObserver(mLaunchObserver); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static class ActivityRecordMatcher implements ArgumentMatcher</*@ActivityRecordProto*/ byte[]> { |
| 102 | private final @ActivityRecordProto byte[] mExpected; |
| 103 | |
| 104 | public ActivityRecordMatcher(ActivityRecord activityRecord) { |
| 105 | mExpected = activityRecordToProto(activityRecord); |
| 106 | } |
| 107 | |
| 108 | public boolean matches(@ActivityRecordProto byte[] actual) { |
| 109 | return Arrays.equals(mExpected, actual); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static @ActivityRecordProto byte[] activityRecordToProto(ActivityRecord record) { |
| 114 | return ActivityMetricsLogger.convertActivityRecordToProto(record); |
| 115 | } |
| 116 | |
| 117 | static @ActivityRecordProto byte[] eqProto(ActivityRecord record) { |
| 118 | return argThat(new ActivityRecordMatcher(record)); |
| 119 | } |
| 120 | |
| 121 | static <T> T verifyAsync(T mock) { |
| 122 | // AMLO callbacks happen on a separate thread than AML calls, so we need to use a timeout. |
Riddle Hsu | 7a2fec5 | 2019-09-04 00:12:08 +0800 | [diff] [blame] | 123 | return verify(mock, timeout(TimeUnit.SECONDS.toMillis(5))); |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 126 | @Test |
| 127 | public void testOnIntentStarted() throws Exception { |
| 128 | Intent intent = new Intent("action 1"); |
| 129 | |
| 130 | mActivityMetricsLogger.notifyActivityLaunching(intent); |
| 131 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 132 | verifyAsync(mLaunchObserver).onIntentStarted(eq(intent)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 133 | verifyNoMoreInteractions(mLaunchObserver); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testOnIntentFailed() throws Exception { |
| 138 | testOnIntentStarted(); |
| 139 | |
| 140 | ActivityRecord activityRecord = null; |
| 141 | |
| 142 | // Bringing an intent that's already running 'to front' is not considered |
| 143 | // as an ACTIVITY_LAUNCHED state transition. |
| 144 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, |
| 145 | activityRecord); |
| 146 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 147 | verifyAsync(mLaunchObserver).onIntentFailed(); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 148 | verifyNoMoreInteractions(mLaunchObserver); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testOnActivityLaunched() throws Exception { |
| 153 | testOnIntentStarted(); |
| 154 | |
| 155 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 156 | mActivityRecord); |
| 157 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 158 | verifyAsync(mLaunchObserver).onActivityLaunched(eqProto(mActivityRecord), anyInt()); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 159 | verifyNoMoreInteractions(mLaunchObserver); |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void testOnActivityLaunchFinished() throws Exception { |
| 164 | testOnActivityLaunched(); |
| 165 | |
| 166 | mActivityMetricsLogger.notifyTransitionStarting(new SparseIntArray(), |
| 167 | SystemClock.uptimeMillis()); |
| 168 | |
| 169 | mActivityMetricsLogger.notifyWindowsDrawn(mActivityRecord.getWindowingMode(), |
| 170 | SystemClock.uptimeMillis()); |
| 171 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 172 | verifyAsync(mLaunchObserver).onActivityLaunchFinished(eqProto(mActivityRecord)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 173 | verifyNoMoreInteractions(mLaunchObserver); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testOnActivityLaunchCancelled() throws Exception { |
| 178 | testOnActivityLaunched(); |
| 179 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 180 | mActivityRecord.mDrawn = true; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 181 | |
| 182 | // Cannot time already-visible activities. |
| 183 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, mActivityRecord); |
| 184 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 185 | verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(mActivityRecord)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 186 | verifyNoMoreInteractions(mLaunchObserver); |
| 187 | } |
| 188 | |
| 189 | @Test |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame^] | 190 | public void testOnReportFullyDrawn() throws Exception { |
| 191 | testOnActivityLaunched(); |
| 192 | |
| 193 | mActivityMetricsLogger.logAppTransitionReportedDrawn(mActivityRecord, false); |
| 194 | |
| 195 | verifyAsync(mLaunchObserver).onReportFullyDrawn(eqProto(mActivityRecord), anyLong()); |
| 196 | verifyNoMoreInteractions(mLaunchObserver); |
| 197 | } |
| 198 | |
| 199 | @Test |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 200 | public void testOnActivityLaunchedTrampoline() throws Exception { |
| 201 | testOnIntentStarted(); |
| 202 | |
| 203 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 204 | mActivityRecord); |
| 205 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 206 | verifyAsync(mLaunchObserver).onActivityLaunched(eqProto(mActivityRecord), anyInt()); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 207 | |
| 208 | // A second, distinct, activity launch is coalesced into the the current app launch sequence |
| 209 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 210 | mActivityRecordTrampoline); |
| 211 | |
| 212 | verifyNoMoreInteractions(mLaunchObserver); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testOnActivityLaunchFinishedTrampoline() throws Exception { |
| 217 | testOnActivityLaunchedTrampoline(); |
| 218 | |
| 219 | mActivityMetricsLogger.notifyTransitionStarting(new SparseIntArray(), |
| 220 | SystemClock.uptimeMillis()); |
| 221 | |
| 222 | mActivityMetricsLogger.notifyWindowsDrawn(mActivityRecordTrampoline.getWindowingMode(), |
| 223 | SystemClock.uptimeMillis()); |
| 224 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 225 | verifyAsync(mLaunchObserver).onActivityLaunchFinished(eqProto(mActivityRecordTrampoline)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 226 | verifyNoMoreInteractions(mLaunchObserver); |
| 227 | } |
| 228 | |
| 229 | @Test |
| 230 | public void testOnActivityLaunchCancelledTrampoline() throws Exception { |
| 231 | testOnActivityLaunchedTrampoline(); |
| 232 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 233 | mActivityRecordTrampoline.mDrawn = true; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 234 | |
| 235 | // Cannot time already-visible activities. |
| 236 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, |
| 237 | mActivityRecordTrampoline); |
| 238 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 239 | verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(mActivityRecordTrampoline)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 240 | verifyNoMoreInteractions(mLaunchObserver); |
| 241 | } |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 242 | |
| 243 | @Test |
| 244 | public void testActivityRecordProtoIsNotTooBig() throws Exception { |
| 245 | // The ActivityRecordProto must not be too big, otherwise converting it at runtime |
| 246 | // will become prohibitively expensive. |
| 247 | assertWithMessage("mActivityRecord: %s", mActivityRecord). |
| 248 | that(activityRecordToProto(mActivityRecord).length). |
| 249 | isAtMost(ActivityMetricsLogger.LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE); |
| 250 | |
| 251 | assertWithMessage("mActivityRecordTrampoline: %s", mActivityRecordTrampoline). |
| 252 | that(activityRecordToProto(mActivityRecordTrampoline).length). |
| 253 | isAtMost(ActivityMetricsLogger.LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE); |
| 254 | } |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 255 | } |