1 /* 2 * Copyright (C) 2017 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.tests.servicecrashtest; 18 19 import android.app.UiAutomation; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.RemoteException; 23 import android.provider.Settings; 24 import android.test.InstrumentationTestCase; 25 26 import com.android.compatibility.common.util.SystemUtil; 27 28 import java.io.IOException; 29 import java.util.concurrent.TimeUnit; 30 31 public class ServiceCrashTest extends InstrumentationTestCase { 32 33 private static final String TAG = ServiceCrashTest.class.getSimpleName(); 34 35 private String mResetConstants = "foo=bar"; 36 37 @Override setUp()38 protected void setUp() throws Exception { 39 super.setUp(); 40 mResetConstants = Settings.Global.getString( 41 getInstrumentation().getContext().getContentResolver(), 42 Settings.Global.ACTIVITY_MANAGER_CONSTANTS); 43 setAMConstants("service_crash_restart_duration=5000,service_crash_max_retry=4"); 44 } 45 46 @Override tearDown()47 protected void tearDown() throws Exception { 48 // Reset the activity manager constants 49 setAMConstants(mResetConstants); 50 super.tearDown(); 51 } 52 setAMConstants(String value)53 private void setAMConstants(String value) throws IOException { 54 // Set the activity manager constants 55 if (value == null) { 56 SystemUtil.runShellCommand(getInstrumentation(), 57 "settings delete global activity_manager_constants"); 58 } else { 59 SystemUtil.runShellCommand(getInstrumentation(), "settings put global " 60 + "activity_manager_constants " + value); 61 } 62 } 63 testCrashQuickly()64 public void testCrashQuickly() throws RemoteException { 65 Context ctx = getInstrumentation().getContext(); 66 // Start the activity, which will bind the crashing service 67 Intent intent = new Intent(); 68 intent.setAction(Intent.ACTION_MAIN); 69 intent.setClass(ctx, MainActivity.class); 70 ctx.startActivity(intent); 71 try { 72 assertTrue(MainActivity.sBindingDiedLatch.await(200, TimeUnit.SECONDS)); 73 } catch (InterruptedException ie) { 74 } 75 } 76 } 77