Skip to content

Commit

Permalink
DeviceManager refactor3: codestyle format (#333)
Browse files Browse the repository at this point in the history
* style format

* Update EspressoTestInfoProcessorListener.java

* format with new rule
  • Loading branch information
zhou9584 authored Mar 7, 2023
1 parent 0d44e58 commit 4f3940a
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(AgentApplication.class)
.headless(headless)
.run(args);
log.info("*************************\nDevice Agent Startup success in %s\n*************************\n {}",
log.info("\n*************************\nDevice Agent Startup success in {}\n*************************\n",
System.currentTimeMillis() - time);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ public void testStarted(Description description) {

testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(15),
(imagePNGFile -> {
if (imagePNGFile == null) {
return;
}
if (!e.isStarted()) {
if (imagePNGFile == null || !e.isStarted()) {
return;
}
try {
Expand Down Expand Up @@ -237,14 +234,15 @@ public void testRunFinished(Result result) {
}
}
if (e.isStarted() && addedFrameCount < 2) {
try {
File imagePNGFile = testDeviceManager.getScreenShot(deviceInfo, logger);
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (Exception exception) {
logger.error(exception.getMessage(), e);
}
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(0),
(imagePNGFile -> {
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), logger);
}

}

logEnter("testRunEnded", elapsedTime, Thread.currentThread().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,15 @@ public void testPlanExecutionFinished(TestPlan testPlan) {
}
}
if (e.isStarted() && addedFrameCount < 2) {
try {
File imagePNGFile = testDeviceManager.getScreenShot(deviceInfo, logger);
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (Exception exception) {
logger.error(exception.getMessage(), e);
}
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(0),
(imagePNGFile -> {
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), logger);
}

}

performanceTestListener.testRunFinished();
Expand Down Expand Up @@ -213,23 +214,20 @@ public void executionStarted(TestIdentifier testIdentifier) {

testRun.addNewTestUnit(ongoingTestUnit);

testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(15), (imagePNGFile -> {
if (imagePNGFile == null) {
return;
}
if (!e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
addedFrameCount++;
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), logger);
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(15),
(imagePNGFile -> {
if (imagePNGFile == null || !e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
addedFrameCount++;
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), logger);

performanceTestListener.testStarted(ongoingTestUnit.getTitle());

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public class EspressoTestInfoProcessorListener extends XmlTestRunListener {
private int pid;
private int addedFrameCount;

public EspressoTestInfoProcessorListener(AgentManagementService agentManagementService,
ADBOperateUtil adbOperateUtil,
public EspressoTestInfoProcessorListener(AgentManagementService agentManagementService, ADBOperateUtil adbOperateUtil,
DeviceInfo deviceInfo, TestRun testRun, String pkgName,
PerformanceTestListener performanceTestListener) {
this.agentManagementService = agentManagementService;
Expand All @@ -63,8 +62,7 @@ public EspressoTestInfoProcessorListener(AgentManagementService agentManagementS
this.pkgName = pkgName;
this.performanceTestListener = performanceTestListener;
adbLogcatCollector = testDeviceManager.getLogCollector(deviceInfo, pkgName, testRun, logger);
adbDeviceScreenRecorder =
testDeviceManager.getScreenRecorder(deviceInfo, testRun.getResultFolder(), logger);
adbDeviceScreenRecorder = testDeviceManager.getScreenRecorder(deviceInfo, testRun.getResultFolder(), logger);
setReportDir(testRun.getResultFolder());
try {
setHostName(InetAddress.getLocalHost().getHostName());
Expand All @@ -84,8 +82,7 @@ public void startRecording(int maxTime) {
logger.info("Start record screen");
adbDeviceScreenRecorder.setupDevice();
adbDeviceScreenRecorder.startRecord(maxTime <= 0 ? 30 * 60 : maxTime);
recordingStartTimeMillis =
System.currentTimeMillis() + adbDeviceScreenRecorder.getPreSleepSeconds() * 1000L;
recordingStartTimeMillis = System.currentTimeMillis() + adbDeviceScreenRecorder.getPreSleepSeconds() * 1000L;
final String initializing = "Initializing";
deviceInfo.setRunningTestName(initializing);
testRun.addNewTimeTag(initializing, 0);
Expand Down Expand Up @@ -148,8 +145,7 @@ public void testStarted(TestIdentifier test) {
}
ongoingTestUnit.setTestedClass(test.getClassName());

testRun.addNewTimeTag(unitIndex + ". " + ongoingTestUnit.getTitle(),
System.currentTimeMillis() - recordingStartTimeMillis);
testRun.addNewTimeTag(unitIndex + ". " + ongoingTestUnit.getTitle(), System.currentTimeMillis() - recordingStartTimeMillis);
deviceInfo.setRunningTestName(ongoingTestUnit.getTitle());

ongoingTestUnit.setDeviceTestResultId(testRun.getId());
Expand All @@ -158,10 +154,7 @@ public void testStarted(TestIdentifier test) {
testRun.addNewTestUnit(ongoingTestUnit);

testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(5), (imagePNGFile -> {
if (imagePNGFile == null) {
return;
}
if (!e.isStarted()) {
if (imagePNGFile == null || !e.isStarted()) {
return;
}
try {
Expand All @@ -182,8 +175,7 @@ public void testFailed(TestIdentifier test, String trace) {
ongoingTestUnit.setStack(trace);
ongoingTestUnit.setStatusCode(AndroidTestUnit.StatusCodes.FAILURE);
performanceTestListener.testFailure(ongoingTestUnit.getTitle());
testRun.addNewTimeTag(ongoingTestUnit.getTitle() + ".fail",
System.currentTimeMillis() - recordingStartTimeMillis);
testRun.addNewTimeTag(ongoingTestUnit.getTitle() + ".fail", System.currentTimeMillis() - recordingStartTimeMillis);
testRun.oneMoreFailure();
}

Expand All @@ -192,8 +184,7 @@ public void testAssumptionFailure(TestIdentifier test, String trace) {
logEnter("testAssumptionFailure", test, trace);
super.testAssumptionFailure(test, trace);
ongoingTestUnit.setStack(trace);
testRun.addNewTimeTag(ongoingTestUnit.getTitle() + ".assumptionFail",
System.currentTimeMillis() - recordingStartTimeMillis);
testRun.addNewTimeTag(ongoingTestUnit.getTitle() + ".assumptionFail", System.currentTimeMillis() - recordingStartTimeMillis);
ongoingTestUnit.setStatusCode(AndroidTestUnit.StatusCodes.ASSUMPTION_FAILURE);
}

Expand All @@ -207,13 +198,10 @@ public void testIgnored(TestIdentifier test) {
@Override
public void testEnded(TestIdentifier test, Map<String, String> testMetrics) {
logEnter("testEnded", test, testMetrics);
testRun.addNewTimeTag(ongoingTestUnit.getTitle() + ".end",
System.currentTimeMillis() - recordingStartTimeMillis);
testRun.addNewTimeTag(ongoingTestUnit.getTitle() + ".end", System.currentTimeMillis() - recordingStartTimeMillis);
super.testEnded(test, testMetrics);
if (ongoingTestUnit.getStatusCode() == 0
|| ongoingTestUnit.getStatusCode() == AndroidTestUnit.StatusCodes.ASSUMPTION_FAILURE
|| ongoingTestUnit.getStatusCode() == AndroidTestUnit.StatusCodes.IGNORED
) {
if (ongoingTestUnit.getStatusCode() == 0 || ongoingTestUnit.getStatusCode() == AndroidTestUnit.StatusCodes.ASSUMPTION_FAILURE ||
ongoingTestUnit.getStatusCode() == AndroidTestUnit.StatusCodes.IGNORED) {
ongoingTestUnit.setStatusCode(AndroidTestUnit.StatusCodes.OK);
ongoingTestUnit.setSuccess(true);
performanceTestListener.testSuccess(ongoingTestUnit.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,17 @@ public long runMonkeyTestOnce(DeviceInfo deviceInfo, TestRun testRun, Logger log
ongoingMonkeyTest.setTestTaskId(testRun.getTestTaskId());

logger.info(ongoingMonkeyTest.getTitle());
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(5), (imagePNGFile -> {
if (imagePNGFile == null) {
return;
}
if (!e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), logger);
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(2),
(imagePNGFile -> {
if (imagePNGFile == null || !e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), logger);
//run monkey test
testRun.addNewTimeTag(unitIndex + ". " + ongoingMonkeyTest.getTitle(),
System.currentTimeMillis() - recordingStartTimeMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,17 @@ protected File runAndGetGif(File appiumJarFile, String appiumCommand, DeviceInfo
e.start(gifFile.getAbsolutePath());
e.setDelay(1000);
e.setRepeat(0);
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(5), (imagePNGFile -> {
if (imagePNGFile == null) {
return;
}
if (!e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), reportLogger);
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(5),
(imagePNGFile -> {
if (imagePNGFile == null || !e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), reportLogger);
testRun.setTestStartTimeMillis(System.currentTimeMillis());

performanceTestManagementService.testStarted(ongoingMonkeyTest.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,11 @@ public void runSmartTestOnce(int i, DeviceInfo deviceInfo, TestRun testRun, Logg
ongoingSmartTest.setDeviceTestResultId(testRun.getId());
ongoingSmartTest.setTestTaskId(testRun.getTestTaskId());

testRun.addNewTimeTag(unitIndex + ". " + ongoingSmartTest.getTitle(),
System.currentTimeMillis() - recordingStartTimeMillis);
testRun.addNewTimeTag(unitIndex + ". " + ongoingSmartTest.getTitle(), System.currentTimeMillis() - recordingStartTimeMillis);
deviceInfo.setRunningTestName(ongoingSmartTest.getTitle());
logger.info(ongoingSmartTest.getTitle());
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(1), (imagePNGFile -> {
if (imagePNGFile == null) {
return;
}
if (!e.isStarted()) {
if (imagePNGFile == null || !e.isStarted()) {
return;
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,17 @@ private void runT2CJsonTestCase(File jsonFile, DeviceInfo deviceInfo, TestRun te

performanceTestManagementService.testStarted(ongoingTest.getTitle());

testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(5), (imagePNGFile -> {
if (imagePNGFile == null) {
return;
}
if (!e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), reportLogger);
testDeviceManager.updateScreenshotImageAsyncDelay(deviceInfo, TimeUnit.SECONDS.toMillis(5),
(imagePNGFile -> {
if (imagePNGFile == null || !e.isStarted()) {
return;
}
try {
e.addFrame(ImgUtil.toBufferedImage(ImgUtil.scale(ImageIO.read(imagePNGFile), 0.3f)));
} catch (IOException ioException) {
ioException.printStackTrace();
}
}), reportLogger);

// Run Test
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import com.microsoft.hydralab.agent.config.AppOptions;
import com.microsoft.hydralab.common.entity.common.AgentUpdateTask;
import com.microsoft.hydralab.common.entity.common.Message;
import com.microsoft.hydralab.common.management.device.TestDeviceManager;
import com.microsoft.hydralab.common.management.device.impl.IOSTestDeviceManager;
import com.microsoft.hydralab.common.file.StorageServiceClientProxy;
import com.microsoft.hydralab.common.util.CommandOutputReceiver;
import com.microsoft.hydralab.common.util.Const;
import com.microsoft.hydralab.common.file.StorageServiceClientProxy;
import com.microsoft.hydralab.common.util.ThreadPoolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,8 +24,6 @@
public class AgentManageService {
private final Logger logger = LoggerFactory.getLogger(AgentManageService.class);
@Resource
TestDeviceManager testDeviceManager;
@Resource
AgentWebSocketClientService agentWebSocketClientService;
@Resource
private AppOptions appOptions;
Expand Down Expand Up @@ -63,8 +59,9 @@ public void restartAgent(String packageFileName, String path) {
sendMessageToCenter(true, "Init command Arr and check restart script exists or not.", "", path);

String packageName = packageFileName == null ? "" : packageFileName;
if (testDeviceManager instanceof IOSTestDeviceManager &&
!((IOSTestDeviceManager) testDeviceManager).isDeviceConnectedToWindows()) {
String osName = System.getProperty("os.name");
logger.info("Devices are connected to " + osName);
if (!osName.startsWith("Windows")) {
scriptPath = appOptions.getLocation() + File.separator + Const.AgentConfig.RESTART_FILE_MAC;
restartArgs = new String[]{"sh", scriptPath, packageName};
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void captureDevicesScreenSync(Collection<DeviceInfo> allDevices, boolean
AgentUser.BatteryStrategy batteryStrategy) {
DeviceTaskControl deviceTaskControl =
deviceTaskControlExecutor.runForAllDeviceAsync(allDevices, (deviceInfo, logger) -> {
testDeviceManager.getScreenShotWithStrategy(deviceInfo, DeviceControlService.log, batteryStrategy);
testDeviceManager.getScreenShotWithStrategy(deviceInfo, log, batteryStrategy);
return true;
}, null, logging, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ActionExecutorTest extends BaseTest {
@Test
void createAndExecuteActions() throws InvocationTargetException, IllegalAccessException {
AndroidTestDeviceManager deviceManager = Mockito.mock(AndroidTestDeviceManager.class);
DeviceInfo deviceInfo = new DeviceInfo();
DeviceInfo deviceInfo = new DeviceInfo(deviceManager);
JSONObject actionJson = new JSONObject();
actionJson.put("method", "setProperty");
actionJson.put("deviceType", "Android");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void testAttachCommandAction() {

AndroidTestDeviceManager deviceManager = Mockito.mock(AndroidTestDeviceManager.class);
ActionExecutor actionExecutor = new ActionExecutor();
DeviceInfo deviceInfo = new DeviceInfo();
DeviceInfo deviceInfo = new DeviceInfo(deviceManager);

actionExecutor.doActions(deviceManager, deviceInfo, baseLogger, testTask.getDeviceActions(),
DeviceAction.When.SET_UP);
Expand Down

0 comments on commit 4f3940a

Please sign in to comment.