• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.wm;
18 
19 import static android.server.wm.app.Components.HOST_ACTIVITY;
20 import static android.server.wm.app.Components.UNRESPONSIVE_ACTIVITY;
21 import static android.server.wm.app.Components.UnresponsiveActivity;
22 import static android.server.wm.app.Components.UnresponsiveActivity.EXTRA_DELAY_UI_THREAD_MS;
23 import static android.server.wm.app.Components.UnresponsiveActivity.EXTRA_ON_CREATE_DELAY_MS;
24 import static android.server.wm.app.Components.UnresponsiveActivity.EXTRA_ON_KEYDOWN_DELAY_MS;
25 import static android.server.wm.app.Components.UnresponsiveActivity.EXTRA_ON_MOTIONEVENT_DELAY_MS;
26 
27 import static org.junit.Assert.fail;
28 import static org.junit.Assume.assumeTrue;
29 
30 import android.content.ComponentName;
31 import android.os.SystemClock;
32 import android.platform.test.annotations.Presubmit;
33 import android.provider.Settings;
34 import android.server.wm.app.Components.RenderService;
35 import android.server.wm.settings.SettingsSession;
36 import android.support.test.uiautomator.By;
37 import android.support.test.uiautomator.UiDevice;
38 import android.support.test.uiautomator.UiObject2;
39 import android.support.test.uiautomator.Until;
40 import android.util.EventLog;
41 import android.util.Log;
42 import android.view.KeyEvent;
43 
44 import androidx.test.core.app.ActivityScenario;
45 import androidx.test.filters.FlakyTest;
46 import androidx.test.platform.app.InstrumentationRegistry;
47 
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 
52 import java.util.List;
53 import java.util.concurrent.CountDownLatch;
54 
55 /**
56  * Test scenarios that lead to ANR dialog being shown.
57  *
58  * <p>Build/Install/Run:
59  *     atest CtsWindowManagerDeviceTestCases:AnrTests
60  */
61 @Presubmit
62 @FlakyTest(bugId = 143047723)
63 @android.server.wm.annotation.Group3
64 public class AnrTests extends ActivityManagerTestBase {
65     private static final String TAG = "AnrTests";
66     private LogSeparator mLogSeparator;
67     private SettingsSession<Integer> mHideDialogSetting;
68 
69     @Before
setup()70     public void setup() throws Exception {
71         super.setUp();
72         assumeTrue(mAtm.currentUiModeSupportsErrorDialogs(mContext));
73 
74         mLogSeparator = separateLogs(); // add a new separator for logs
75         mHideDialogSetting = new SettingsSession<>(
76                 Settings.Global.getUriFor(Settings.Global.HIDE_ERROR_DIALOGS),
77                 Settings.Global::getInt, Settings.Global::putInt);
78         mHideDialogSetting.set(0);
79     }
80 
81     @After
teardown()82     public void teardown() {
83         if (mHideDialogSetting != null) mHideDialogSetting.close();
84         stopTestPackage(UNRESPONSIVE_ACTIVITY.getPackageName());
85         stopTestPackage(HOST_ACTIVITY.getPackageName());
86     }
87 
88     @Test
slowOnCreateWithKeyEventTriggersAnr()89     public void slowOnCreateWithKeyEventTriggersAnr() {
90         startUnresponsiveActivity(EXTRA_ON_CREATE_DELAY_MS, false /* waitForCompletion */,
91                 UNRESPONSIVE_ACTIVITY);
92         // wait for app to be focused
93         mWmState.waitAndAssertAppFocus(UNRESPONSIVE_ACTIVITY.getPackageName(),
94                 2000 /* waitTime_ms */);
95         // wait for input manager to get the new focus app. This sleep can be removed once we start
96         // listing to input about the focused app.
97         SystemClock.sleep(500);
98         injectKey(KeyEvent.KEYCODE_BACK, false /* longpress */, false /* sync */);
99         clickCloseAppOnAnrDialog();
100         assertEventLogsContainsAnr(UnresponsiveActivity.PROCESS_NAME);
101     }
102 
103     @Test
slowUiThreadWithKeyEventTriggersAnr()104     public void slowUiThreadWithKeyEventTriggersAnr() {
105         startUnresponsiveActivity(EXTRA_DELAY_UI_THREAD_MS, true /* waitForCompletion */,
106                 UNRESPONSIVE_ACTIVITY);
107         injectKey(KeyEvent.KEYCODE_BACK, false /* longpress */, false /* sync */);
108         clickCloseAppOnAnrDialog();
109         assertEventLogsContainsAnr(UnresponsiveActivity.PROCESS_NAME);
110     }
111 
112     @Test
slowOnKeyEventHandleTriggersAnr()113     public void slowOnKeyEventHandleTriggersAnr() {
114         startUnresponsiveActivity(EXTRA_ON_KEYDOWN_DELAY_MS, true /* waitForCompletion */,
115                 UNRESPONSIVE_ACTIVITY);
116         injectKey(KeyEvent.KEYCODE_BACK, false /* longpress */, false /* sync */);
117         clickCloseAppOnAnrDialog();
118         assertEventLogsContainsAnr(UnresponsiveActivity.PROCESS_NAME);
119     }
120 
121     @Test
slowOnTouchEventHandleTriggersAnr()122     public void slowOnTouchEventHandleTriggersAnr() {
123         startUnresponsiveActivity(EXTRA_ON_MOTIONEVENT_DELAY_MS, true /* waitForCompletion */,
124                 UNRESPONSIVE_ACTIVITY);
125 
126         mWmState.computeState();
127         // Tap on the UnresponsiveActivity
128         final WindowManagerState.Task unresponsiveActivityTask =
129                 mWmState.getTaskByActivity(UNRESPONSIVE_ACTIVITY);
130         mTouchHelper.tapOnTaskCenterAsync(unresponsiveActivityTask);
131         clickCloseAppOnAnrDialog();
132         assertEventLogsContainsAnr(UnresponsiveActivity.PROCESS_NAME);
133     }
134 
135     /**
136      * Verify embedded windows can trigger ANR and the verify embedded app is blamed.
137      */
138     @Test
embeddedWindowTriggersAnr()139     public void embeddedWindowTriggersAnr() {
140         try (ActivityScenario<HostActivity> scenario =
141                      ActivityScenario.launch(HostActivity.class)) {
142             CountDownLatch[] latch = new CountDownLatch[1];
143             scenario.onActivity(activity -> latch[0] = activity.mEmbeddedViewAttachedLatch);
144             latch[0].await();
145             mWmState.computeState();
146             final WindowManagerState.Task hostActivityTask =
147                     mWmState.getTaskByActivity(new ComponentName("android.server.wm.cts",
148                             "android.server.wm.HostActivity"));
149             mTouchHelper.tapOnTaskCenterAsync(hostActivityTask);
150             clickCloseAppOnAnrDialog();
151         } catch (InterruptedException ignored) {
152         }
153         assertEventLogsContainsAnr(RenderService.PROCESS_NAME);
154     }
155 
assertEventLogsContainsAnr(String processName)156     private void assertEventLogsContainsAnr(String processName) {
157         final List<EventLog.Event> events = getEventLogsForComponents(mLogSeparator,
158                 android.util.EventLog.getTagCode("am_anr"));
159         for (EventLog.Event event : events) {
160             Object[] arr = (Object[]) event.getData();
161             final String name = (String) arr[2];
162             if (name.equals(processName)) {
163                 return;
164             }
165         }
166         fail("Could not find anr kill event for " + processName);
167     }
168 
clickCloseAppOnAnrDialog()169     private void clickCloseAppOnAnrDialog() {
170         // Find anr dialog and kill app
171         UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
172         UiObject2 closeAppButton = uiDevice.wait(Until.findObject(By.res("android:id/aerr_close")),
173                 20000);
174         if (closeAppButton != null) {
175             Log.d(TAG, "found permission dialog after searching all windows, clicked");
176             closeAppButton.click();
177             return;
178         }
179         fail("Could not find anr dialog");
180     }
181 
startUnresponsiveActivity(String delayTypeExtra, boolean waitForCompletion, ComponentName activity)182     private void startUnresponsiveActivity(String delayTypeExtra, boolean waitForCompletion,
183             ComponentName activity) {
184         String flags = waitForCompletion ? " -W -n " : " -n ";
185         String startCmd = "am start" + flags + activity.flattenToString() +
186                 " --ei " + delayTypeExtra + " 30000";
187         executeShellCommand(startCmd);
188     }
189 }
190