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