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 package com.android.tradefed.targetprep; 17 18 import com.android.tradefed.build.IBuildInfo; 19 import com.android.tradefed.config.Option; 20 import com.android.tradefed.config.OptionClass; 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.device.ITestDevice; 23 24 /** Target preparer that restarts the system server without rebooting the device. */ 25 @OptionClass(alias = "restart-system-server") 26 public class RestartSystemServerTargetPreparer extends BaseTargetPreparer 27 implements ITargetCleaner { 28 @Option(name = "restart-setup", description = "Restart system server on setup.") 29 private boolean mRestartBefore = true; 30 31 @Option(name = "restart-teardown", description = "Restart system server on tear down.") 32 private boolean mRestartAfter = false; 33 34 // Exposed for testing. 35 static final String KILL_SERVER_COMMAND = "pkill system_server"; 36 37 private void restartSystemServer(ITestDevice device) throws DeviceNotAvailableException { 38 device.enableAdbRoot(); 39 device.executeShellCommand("setprop dev.bootcomplete 0"); 40 device.executeShellCommand(KILL_SERVER_COMMAND); 41 device.waitForDeviceAvailable(); 42 } 43 44 @Override 45 public void setUp(ITestDevice device, IBuildInfo buildInfo) 46 throws TargetSetupError, BuildError, DeviceNotAvailableException { 47 if (!mRestartBefore) { 48 return; 49 } 50 restartSystemServer(device); 51 } 52 53 @Override 54 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e) 55 throws DeviceNotAvailableException { 56 if (!mRestartAfter) { 57 return; 58 } 59 restartSystemServer(device); 60 } 61 } 62