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 | |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
| 26 | import static org.mockito.Mockito.mock; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 29 | |
| 30 | import android.content.Intent; |
| 31 | import android.os.SystemClock; |
| 32 | import android.platform.test.annotations.Presubmit; |
| 33 | import android.util.SparseIntArray; |
| 34 | |
| 35 | import androidx.test.filters.FlakyTest; |
| 36 | import androidx.test.filters.SmallTest; |
| 37 | import androidx.test.runner.AndroidJUnit4; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | |
| 42 | /** |
| 43 | * Tests for the {@link ActivityMetricsLaunchObserver} class. |
| 44 | * |
| 45 | * Build/Install/Run: |
| 46 | * atest WmTests:ActivityMetricsLaunchObserverTests |
| 47 | */ |
| 48 | @SmallTest |
| 49 | @Presubmit |
| 50 | @FlakyTest(detail="promote once confirmed non-flaky") |
| 51 | public class ActivityMetricsLaunchObserverTests extends ActivityTestsBase { |
| 52 | private ActivityMetricsLogger mActivityMetricsLogger; |
| 53 | private ActivityMetricsLaunchObserver mLaunchObserver; |
| 54 | |
| 55 | private TestActivityStack mStack; |
| 56 | private TaskRecord mTask; |
| 57 | private ActivityRecord mActivityRecord; |
| 58 | private ActivityRecord mActivityRecordTrampoline; |
| 59 | |
| 60 | @Before |
| 61 | public void setUpAMLO() throws Exception { |
| 62 | setupActivityTaskManagerService(); |
| 63 | |
| 64 | mActivityMetricsLogger = |
| 65 | new ActivityMetricsLogger(mSupervisor, mService.mContext, mService.mH.getLooper()); |
| 66 | |
| 67 | mLaunchObserver = mock(ActivityMetricsLaunchObserver.class); |
| 68 | |
| 69 | // TODO: Use ActivityMetricsLaunchObserverRegistry . |
| 70 | java.lang.reflect.Field f = |
| 71 | mActivityMetricsLogger.getClass().getDeclaredField("mLaunchObserver"); |
| 72 | f.setAccessible(true); |
| 73 | f.set(mActivityMetricsLogger, mLaunchObserver); |
| 74 | |
| 75 | // Sometimes we need an ActivityRecord for ActivityMetricsLogger to do anything useful. |
| 76 | // This seems to be the easiest way to create an ActivityRecord. |
| 77 | mStack = mSupervisor.getDefaultDisplay().createStack( |
| 78 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */); |
| 79 | mTask = new TaskBuilder(mSupervisor).setStack(mStack).build(); |
| 80 | mActivityRecord = new ActivityBuilder(mService).setTask(mTask).build(); |
| 81 | mActivityRecordTrampoline = new ActivityBuilder(mService).setTask(mTask).build(); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testOnIntentStarted() throws Exception { |
| 86 | Intent intent = new Intent("action 1"); |
| 87 | |
| 88 | mActivityMetricsLogger.notifyActivityLaunching(intent); |
| 89 | |
| 90 | verify(mLaunchObserver).onIntentStarted(eq(intent)); |
| 91 | verifyNoMoreInteractions(mLaunchObserver); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testOnIntentFailed() throws Exception { |
| 96 | testOnIntentStarted(); |
| 97 | |
| 98 | ActivityRecord activityRecord = null; |
| 99 | |
| 100 | // Bringing an intent that's already running 'to front' is not considered |
| 101 | // as an ACTIVITY_LAUNCHED state transition. |
| 102 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, |
| 103 | activityRecord); |
| 104 | |
| 105 | verify(mLaunchObserver).onIntentFailed(); |
| 106 | verifyNoMoreInteractions(mLaunchObserver); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testOnActivityLaunched() throws Exception { |
| 111 | testOnIntentStarted(); |
| 112 | |
| 113 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 114 | mActivityRecord); |
| 115 | |
| 116 | verify(mLaunchObserver).onActivityLaunched(eq(mActivityRecord), anyInt()); |
| 117 | verifyNoMoreInteractions(mLaunchObserver); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testOnActivityLaunchFinished() throws Exception { |
| 122 | testOnActivityLaunched(); |
| 123 | |
| 124 | mActivityMetricsLogger.notifyTransitionStarting(new SparseIntArray(), |
| 125 | SystemClock.uptimeMillis()); |
| 126 | |
| 127 | mActivityMetricsLogger.notifyWindowsDrawn(mActivityRecord.getWindowingMode(), |
| 128 | SystemClock.uptimeMillis()); |
| 129 | |
| 130 | verify(mLaunchObserver).onActivityLaunchFinished(eq(mActivityRecord)); |
| 131 | verifyNoMoreInteractions(mLaunchObserver); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testOnActivityLaunchCancelled() throws Exception { |
| 136 | testOnActivityLaunched(); |
| 137 | |
| 138 | mActivityRecord.nowVisible = true; |
| 139 | |
| 140 | // Cannot time already-visible activities. |
| 141 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, mActivityRecord); |
| 142 | |
| 143 | verify(mLaunchObserver).onActivityLaunchCancelled(eq(mActivityRecord)); |
| 144 | verifyNoMoreInteractions(mLaunchObserver); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testOnActivityLaunchedTrampoline() throws Exception { |
| 149 | testOnIntentStarted(); |
| 150 | |
| 151 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 152 | mActivityRecord); |
| 153 | |
| 154 | verify(mLaunchObserver).onActivityLaunched(eq(mActivityRecord), anyInt()); |
| 155 | |
| 156 | // A second, distinct, activity launch is coalesced into the the current app launch sequence |
| 157 | mActivityMetricsLogger.notifyActivityLaunched(START_SUCCESS, |
| 158 | mActivityRecordTrampoline); |
| 159 | |
| 160 | verifyNoMoreInteractions(mLaunchObserver); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testOnActivityLaunchFinishedTrampoline() throws Exception { |
| 165 | testOnActivityLaunchedTrampoline(); |
| 166 | |
| 167 | mActivityMetricsLogger.notifyTransitionStarting(new SparseIntArray(), |
| 168 | SystemClock.uptimeMillis()); |
| 169 | |
| 170 | mActivityMetricsLogger.notifyWindowsDrawn(mActivityRecordTrampoline.getWindowingMode(), |
| 171 | SystemClock.uptimeMillis()); |
| 172 | |
| 173 | verify(mLaunchObserver).onActivityLaunchFinished(eq(mActivityRecordTrampoline)); |
| 174 | verifyNoMoreInteractions(mLaunchObserver); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testOnActivityLaunchCancelledTrampoline() throws Exception { |
| 179 | testOnActivityLaunchedTrampoline(); |
| 180 | |
| 181 | mActivityRecordTrampoline.nowVisible = true; |
| 182 | |
| 183 | // Cannot time already-visible activities. |
| 184 | mActivityMetricsLogger.notifyActivityLaunched(START_TASK_TO_FRONT, |
| 185 | mActivityRecordTrampoline); |
| 186 | |
| 187 | verify(mLaunchObserver).onActivityLaunchCancelled(eq(mActivityRecordTrampoline)); |
| 188 | verifyNoMoreInteractions(mLaunchObserver); |
| 189 | } |
| 190 | } |