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 | |
| 64 | private TestActivityStack mStack; |
| 65 | private TaskRecord mTask; |
| 66 | private ActivityRecord mActivityRecord; |
| 67 | private ActivityRecord mActivityRecordTrampoline; |
| 68 | |
| 69 | @Before |
| 70 | public void setUpAMLO() throws Exception { |
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 | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 81 | mStack = mRootActivityContainer.getDefaultDisplay().createStack( |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 82 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */); |
| 83 | mTask = new TaskBuilder(mSupervisor).setStack(mStack).build(); |
| 84 | mActivityRecord = new ActivityBuilder(mService).setTask(mTask).build(); |
| 85 | mActivityRecordTrampoline = new ActivityBuilder(mService).setTask(mTask).build(); |
| 86 | } |
| 87 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 88 | @After |
| 89 | public void tearDownAMLO() throws Exception { |
| 90 | if (mLaunchObserverRegistry != null) { // Don't NPE if setUp failed. |
| 91 | mLaunchObserverRegistry.unregisterLaunchObserver(mLaunchObserver); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static class ActivityRecordMatcher implements ArgumentMatcher</*@ActivityRecordProto*/ byte[]> { |
| 96 | private final @ActivityRecordProto byte[] mExpected; |
| 97 | |
| 98 | public ActivityRecordMatcher(ActivityRecord activityRecord) { |
| 99 | mExpected = activityRecordToProto(activityRecord); |
| 100 | } |
| 101 | |
| 102 | public boolean matches(@ActivityRecordProto byte[] actual) { |
| 103 | return Arrays.equals(mExpected, actual); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static @ActivityRecordProto byte[] activityRecordToProto(ActivityRecord record) { |
| 108 | return ActivityMetricsLogger.convertActivityRecordToProto(record); |
| 109 | } |
| 110 | |
| 111 | static @ActivityRecordProto byte[] eqProto(ActivityRecord record) { |
| 112 | return argThat(new ActivityRecordMatcher(record)); |
| 113 | } |
| 114 | |
| 115 | static <T> T verifyAsync(T mock) { |
| 116 | // AMLO callbacks happen on a separate thread than AML calls, so we need to use a timeout. |
| 117 | return verify(mock, timeout(100)); |
| 118 | } |
| 119 | |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 120 | @Test |
| 121 | public void testOnIntentStarted() throws Exception { |
| 122 | Intent intent = new Intent("action 1"); |
| 123 | |
| 124 | mActivityMetricsLogger.notifyActivityLaunching(intent); |
| 125 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 126 | verifyAsync(mLaunchObserver).onIntentStarted(eq(intent)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 127 | verifyNoMoreInteractions(mLaunchObserver); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testOnIntentFailed() throws Exception { |
| 132 | testOnIntentStarted(); |
| 133 | |
| 134 | ActivityRecord activityRecord = null; |
| 135 | |
| 136 | // Bringing an intent that's already running 'to front' is not considered |
| 137 | // as an ACTIVITY_LAUNCHED state transition. |
| 138 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, |
| 139 | activityRecord); |
| 140 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 141 | verifyAsync(mLaunchObserver).onIntentFailed(); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 142 | verifyNoMoreInteractions(mLaunchObserver); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testOnActivityLaunched() throws Exception { |
| 147 | testOnIntentStarted(); |
| 148 | |
| 149 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 150 | mActivityRecord); |
| 151 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 152 | verifyAsync(mLaunchObserver).onActivityLaunched(eqProto(mActivityRecord), anyInt()); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 153 | verifyNoMoreInteractions(mLaunchObserver); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testOnActivityLaunchFinished() throws Exception { |
| 158 | testOnActivityLaunched(); |
| 159 | |
| 160 | mActivityMetricsLogger.notifyTransitionStarting(new SparseIntArray(), |
| 161 | SystemClock.uptimeMillis()); |
| 162 | |
| 163 | mActivityMetricsLogger.notifyWindowsDrawn(mActivityRecord.getWindowingMode(), |
| 164 | SystemClock.uptimeMillis()); |
| 165 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 166 | verifyAsync(mLaunchObserver).onActivityLaunchFinished(eqProto(mActivityRecord)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 167 | verifyNoMoreInteractions(mLaunchObserver); |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testOnActivityLaunchCancelled() throws Exception { |
| 172 | testOnActivityLaunched(); |
| 173 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 174 | mActivityRecord.mDrawn = true; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 175 | |
| 176 | // Cannot time already-visible activities. |
| 177 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, mActivityRecord); |
| 178 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 179 | verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(mActivityRecord)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 180 | verifyNoMoreInteractions(mLaunchObserver); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testOnActivityLaunchedTrampoline() throws Exception { |
| 185 | testOnIntentStarted(); |
| 186 | |
| 187 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 188 | mActivityRecord); |
| 189 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 190 | verifyAsync(mLaunchObserver).onActivityLaunched(eqProto(mActivityRecord), anyInt()); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 191 | |
| 192 | // A second, distinct, activity launch is coalesced into the the current app launch sequence |
| 193 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 194 | mActivityRecordTrampoline); |
| 195 | |
| 196 | verifyNoMoreInteractions(mLaunchObserver); |
| 197 | } |
| 198 | |
| 199 | @Test |
| 200 | public void testOnActivityLaunchFinishedTrampoline() throws Exception { |
| 201 | testOnActivityLaunchedTrampoline(); |
| 202 | |
| 203 | mActivityMetricsLogger.notifyTransitionStarting(new SparseIntArray(), |
| 204 | SystemClock.uptimeMillis()); |
| 205 | |
| 206 | mActivityMetricsLogger.notifyWindowsDrawn(mActivityRecordTrampoline.getWindowingMode(), |
| 207 | SystemClock.uptimeMillis()); |
| 208 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 209 | verifyAsync(mLaunchObserver).onActivityLaunchFinished(eqProto(mActivityRecordTrampoline)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 210 | verifyNoMoreInteractions(mLaunchObserver); |
| 211 | } |
| 212 | |
| 213 | @Test |
| 214 | public void testOnActivityLaunchCancelledTrampoline() throws Exception { |
| 215 | testOnActivityLaunchedTrampoline(); |
| 216 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 217 | mActivityRecordTrampoline.mDrawn = true; |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 218 | |
| 219 | // Cannot time already-visible activities. |
| 220 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, |
| 221 | mActivityRecordTrampoline); |
| 222 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 223 | verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(mActivityRecordTrampoline)); |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 224 | verifyNoMoreInteractions(mLaunchObserver); |
| 225 | } |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 226 | |
| 227 | @Test |
| 228 | public void testActivityRecordProtoIsNotTooBig() throws Exception { |
| 229 | // The ActivityRecordProto must not be too big, otherwise converting it at runtime |
| 230 | // will become prohibitively expensive. |
| 231 | assertWithMessage("mActivityRecord: %s", mActivityRecord). |
| 232 | that(activityRecordToProto(mActivityRecord).length). |
| 233 | isAtMost(ActivityMetricsLogger.LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE); |
| 234 | |
| 235 | assertWithMessage("mActivityRecordTrampoline: %s", mActivityRecordTrampoline). |
| 236 | that(activityRecordToProto(mActivityRecordTrampoline).length). |
| 237 | isAtMost(ActivityMetricsLogger.LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE); |
| 238 | } |
Igor Murashkin | cb76aac | 2018-11-02 14:46:28 -0700 | [diff] [blame] | 239 | } |