@Test public void getConnectedDevicesTest() { Mockito.when(fileManager.fileExist(Mockito.anyString())).thenReturn(true); Mockito.when(getADBAttribute()).thenReturn(adbPath); androidDebugBridge = PowerMockito.mock(AndroidDebugBridge.class); IDevice aDevice1 = Mockito.mock(IDevice.class); IDevice[] devices = { aDevice1 }; PowerMockito.mockStatic(AndroidDebugBridge.class); PowerMockito.when(androidDebugBridge.hasInitialDeviceList()).thenReturn(false); PowerMockito.when(androidDebugBridge.getDevices()).thenReturn(devices); PowerMockito.mockStatic(AndroidDebugBridge.class, new Answer<AndroidDebugBridge>() { boolean activated = false; @Override public AndroidDebugBridge answer(InvocationOnMock invocation) throws Throwable { String method = invocation.getMethod().getName(); System.out.println("invocation:" + method); if (method.matches("getBridge")) { if (activated) { PowerMockito.when(androidDebugBridge.hasInitialDeviceList()).thenReturn(true); return androidDebugBridge; } return null; } else if (method.matches("initIfNeeded")) { activated = true; return null; } else if (method.matches("createBridge")) { if (activated) { PowerMockito.when(androidDebugBridge.hasInitialDeviceList()).thenReturn(true); return androidDebugBridge; } else { PowerMockito.when(androidDebugBridge.hasInitialDeviceList()).thenReturn(false); return androidDebugBridge; } } return null; } }); IDevice[] getDevices = null; try { getDevices = adbService.getConnectedDevices(); } catch (Exception e) { } assertTrue(getDevices == devices); }
@Override public IDevice[] getConnectedDevices() throws IOException{ AndroidDebugBridge adb = ensureADBServiceStarted(); if (adb == null) { LOGGER.debug("failed to connect to existing bridge, now trying running adb from environment"); throw new IOException("AndroidDebugBridge failed to start"); } int waitcount = 1; while (waitcount <= 20 && !adb.hasInitialDeviceList()) { try { Thread.sleep(100); } catch (InterruptedException ex) { } waitcount++; } return adb.getDevices(); }
AdbServiceImpl implements IAdbService { @Override public IDevice[] getConnectedDevices() throws IOException{ AndroidDebugBridge adb = ensureADBServiceStarted(); if (adb == null) { LOGGER.debug("failed to connect to existing bridge, now trying running adb from environment"); throw new IOException("AndroidDebugBridge failed to start"); } int waitcount = 1; while (waitcount <= 20 && !adb.hasInitialDeviceList()) { try { Thread.sleep(100); } catch (InterruptedException ex) { } waitcount++; } return adb.getDevices(); } }
AdbServiceImpl implements IAdbService { @Override public IDevice[] getConnectedDevices() throws IOException{ AndroidDebugBridge adb = ensureADBServiceStarted(); if (adb == null) { LOGGER.debug("failed to connect to existing bridge, now trying running adb from environment"); throw new IOException("AndroidDebugBridge failed to start"); } int waitcount = 1; while (waitcount <= 20 && !adb.hasInitialDeviceList()) { try { Thread.sleep(100); } catch (InterruptedException ex) { } waitcount++; } return adb.getDevices(); }  }
AdbServiceImpl implements IAdbService { @Override public IDevice[] getConnectedDevices() throws IOException{ AndroidDebugBridge adb = ensureADBServiceStarted(); if (adb == null) { LOGGER.debug("failed to connect to existing bridge, now trying running adb from environment"); throw new IOException("AndroidDebugBridge failed to start"); } int waitcount = 1; while (waitcount <= 20 && !adb.hasInitialDeviceList()) { try { Thread.sleep(100); } catch (InterruptedException ex) { } waitcount++; } return adb.getDevices(); }  @Autowired void setAndroid(IAndroid android); @Autowired void setExternalProcessRunner(IExternalProcessRunner runner); @Autowired void setFileExtactor(IReadWriteFileExtractor extractor); @Autowired void setFileManager(IFileManager fileManager); @Autowired void setAROConfigFile(Settings configFile); @Override boolean hasADBpath(); @Override String getAdbPath(); @Override String getAdbPath(boolean unfiltered); @Override boolean isAdbFileExist(); @Override AndroidDebugBridge initCreateBridge(String adbPath); @Override AndroidDebugBridge ensureADBServiceStarted(); @Override IDevice[] getConnectedDevices(); @Override FileEntry locate(IDevice device, FileEntry rootPath, String path); boolean pullFile(SyncService service, String remotePath, String file, String localFolder); @Override boolean installPayloadFile(IAroDevice aroDevice, String tempFolder, String payloadFileName, String remotepath); @Override boolean installPayloadFile(IDevice device, String tempFolder, String payloadFileName, String remotepath); @Override String[] getApplicationList(String id); }
AdbServiceImpl implements IAdbService { @Override public IDevice[] getConnectedDevices() throws IOException{ AndroidDebugBridge adb = ensureADBServiceStarted(); if (adb == null) { LOGGER.debug("failed to connect to existing bridge, now trying running adb from environment"); throw new IOException("AndroidDebugBridge failed to start"); } int waitcount = 1; while (waitcount <= 20 && !adb.hasInitialDeviceList()) { try { Thread.sleep(100); } catch (InterruptedException ex) { } waitcount++; } return adb.getDevices(); }  @Autowired void setAndroid(IAndroid android); @Autowired void setExternalProcessRunner(IExternalProcessRunner runner); @Autowired void setFileExtactor(IReadWriteFileExtractor extractor); @Autowired void setFileManager(IFileManager fileManager); @Autowired void setAROConfigFile(Settings configFile); @Override boolean hasADBpath(); @Override String getAdbPath(); @Override String getAdbPath(boolean unfiltered); @Override boolean isAdbFileExist(); @Override AndroidDebugBridge initCreateBridge(String adbPath); @Override AndroidDebugBridge ensureADBServiceStarted(); @Override IDevice[] getConnectedDevices(); @Override FileEntry locate(IDevice device, FileEntry rootPath, String path); boolean pullFile(SyncService service, String remotePath, String file, String localFolder); @Override boolean installPayloadFile(IAroDevice aroDevice, String tempFolder, String payloadFileName, String remotepath); @Override boolean installPayloadFile(IDevice device, String tempFolder, String payloadFileName, String remotepath); @Override String[] getApplicationList(String id);  }