예제 #1
0
 /**
  * Detects if emulator is running using adb.
  *
  * @param serialNo
  * @return true if running, false otherwise.
  */
 public boolean adbIsRunning(String serialNo) {
   AndroidDebugBridge adb = mTestManager.getAndroidDebugBridge();
   for (IDevice device : adb.getDevices()) {
     if (device.getSerialNumber().endsWith(serialNo)) {
       return true;
     }
   }
   return false;
 }
예제 #2
0
 /**
  * Checks the attached devices looking for one whose device id matches the specified regex.
  *
  * @param deviceIdRegex the regular expression to match against
  * @return the Device (if found), or null (if not found).
  */
 private IDevice findAttacedDevice(String deviceIdRegex) {
   Pattern pattern = Pattern.compile(deviceIdRegex);
   for (IDevice device : bridge.getDevices()) {
     String serialNumber = device.getSerialNumber();
     if (pattern.matcher(serialNumber).matches()) {
       return device;
     }
   }
   return null;
 }
예제 #3
0
  public boolean stop(String serialNo) { // NOPMD
    /* check adb connection */
    if (!mTestManager.isAdbConnected()) {
      throw new IllegalStateException("ADB is not connected");
    }

    if (!isRunning(serialNo)) {
      mLog.v("Emualtor \"%s\" is not running. Nothing to stop.", serialNo);
      return true;
    }

    /* verify serial represents emulator, and not a real device */
    AndroidDebugBridge adb = mTestManager.getAndroidDebugBridge();
    boolean skipAdbStop = true;
    for (IDevice curDevice : adb.getDevices()) {
      if (curDevice.getSerialNumber().endsWith(serialNo)) {
        if (!curDevice.isEmulator()) {
          throw new IllegalArgumentException("Not an emulator: " + serialNo);
        }
        skipAdbStop = false;
        break;
      }
    }

    if (!skipAdbStop) { // NOPMD
      /* Killing emulator using adb's embedded control over emulator console */
      mLog.v("Killing emulator \"%s\" using adb.", serialNo);
      if (adbStop(serialNo)) {
        if (waitForEmualtorStopped(serialNo, DEFAULT_TIMEOUT)) {
          mLog.v("Emulator \"%s\" is no longer running.", serialNo);
          return true;
        }
      } else {
        mLog.v("Failed to stop emulator \"%s\" using avd.", serialNo);
      }
    } else {
      mLog.w("Emulator \"%s\" is not visible from adb.", serialNo);
    }

    /* Let's kill emulator using emulator console */
    mLog.v("Killing emulator \"%s\" using emulator console.", serialNo);
    if (mConsole.consoleStop(serialNo)) {
      if (waitForEmualtorStopped(serialNo, DEFAULT_TIMEOUT)) {
        mLog.v("Emulator \"%s\" is no longer running.", serialNo);
        return true;
      }
    } else {
      mLog.v("Failed to stop emulator \"%s\" using emulator console.", serialNo);
    }

    mLog.e(null, "I really tried to kill \"%s\" emulator. But failed.", serialNo);
    return false;
  }
예제 #4
0
 @NotNull
 private IDevice[] getFilteredDevices(AndroidDebugBridge bridge) {
   final List<IDevice> filteredDevices = new ArrayList<IDevice>();
   for (IDevice device : bridge.getDevices()) {
     if (myFilter == null || myFilter.value(device)) {
       filteredDevices.add(device);
     }
   }
   // Do not filter launching cloud devices as they are just unselectable progress markers
   // that are replaced with the actual cloud devices as soon as they are up and the actual cloud
   // devices will be filtered above.
   return filteredDevices.toArray(new IDevice[filteredDevices.size()]);
 }
  /**
   * Start the Android Emulator with the specified options.
   *
   * @throws org.apache.maven.plugin.MojoExecutionException
   * @see #emulatorAvd
   * @see #emulatorWait
   * @see #emulatorOptions
   */
  protected void startAndroidEmulator() throws MojoExecutionException {
    parseParameters();

    CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
    executor.setLogger(this.getLog());

    try {
      String filename;
      if (isWindows()) {
        filename = writeEmulatorStartScriptWindows();
      } else {
        filename = writeEmulatorStartScriptUnix();
      }

      final AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();
      if (androidDebugBridge.isConnected()) {
        List<IDevice> devices = Arrays.asList(androidDebugBridge.getDevices());
        int numberOfDevices = devices.size();
        getLog()
            .info("Found " + numberOfDevices + " devices connected with the Android Debug Bridge");

        IDevice existingEmulator = null;

        for (IDevice device : devices) {
          if (device.isEmulator()) {
            if (isExistingEmulator(device)) {
              existingEmulator = device;
              break;
            }
          }
        }

        if (existingEmulator == null) {
          getLog().info(START_EMULATOR_MSG + filename);
          executor.executeCommand(filename, null);

          getLog().info(START_EMULATOR_WAIT_MSG + parsedWait);
          // wait for the emulator to start up
          Thread.sleep(new Long(parsedWait));
        } else {
          getLog()
              .info(
                  String.format(
                      "Emulator already running [Serial No: '%s', AVD Name '%s']. Skipping start and wait.",
                      existingEmulator.getSerialNumber(), existingEmulator.getAvdName()));
        }
      }
    } catch (Exception e) {
      throw new MojoExecutionException("", e);
    }
  }
  private void setBridge(@Nullable AndroidDebugBridge bridge) {
    myBridge = bridge;
    myComboBoxModel.removeAllElements();

    IDevice[] devices = myBridge == null ? null : myBridge.getDevices();
    if (devices == null || devices.length == 0) {
      myComboBoxModel.addElement(NO_DEVICES);
    } else {
      for (IDevice device : devices) {
        myComboBoxModel.addElement(device);
      }
    }
    myDevicesComboBox.setSelectedIndex(0);
  }
  /**
   * Performs the callback action on the devices determined by {@link
   * #shouldDoWithThisDevice(com.android.ddmlib.IDevice)}
   *
   * @param deviceCallback the action to perform on each device
   * @throws org.apache.maven.plugin.MojoExecutionException in case there is a problem
   * @throws org.apache.maven.plugin.MojoFailureException in case there is a problem
   */
  protected void doWithDevices(final DeviceCallback deviceCallback)
      throws MojoExecutionException, MojoFailureException {
    final AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();

    if (!androidDebugBridge.isConnected()) {
      throw new MojoExecutionException("Android Debug Bridge is not connected.");
    }

    waitForInitialDeviceList(androidDebugBridge);
    List<IDevice> devices = Arrays.asList(androidDebugBridge.getDevices());
    int numberOfDevices = devices.size();
    getLog().info("Found " + numberOfDevices + " devices connected with the Android Debug Bridge");
    if (devices.size() == 0) {
      throw new MojoExecutionException("No online devices attached.");
    }

    boolean shouldRunOnAllDevices = StringUtils.isBlank(device);
    if (shouldRunOnAllDevices) {
      getLog().info("android.device parameter not set, using all attached devices");
    } else {
      getLog().info("android.device parameter set to " + device);
    }

    ArrayList<DoThread> doThreads = new ArrayList<DoThread>();
    for (final IDevice idevice : devices) {
      if (shouldRunOnAllDevices) {
        String deviceType = idevice.isEmulator() ? "Emulator " : "Device ";
        getLog().info(deviceType + DeviceHelper.getDescriptiveName(idevice) + " found.");
      }
      if (shouldRunOnAllDevices || shouldDoWithThisDevice(idevice)) {
        DoThread deviceDoThread =
            new DoThread() {
              public void runDo() throws MojoFailureException, MojoExecutionException {
                deviceCallback.doWithDevice(idevice);
              }
            };
        doThreads.add(deviceDoThread);
        deviceDoThread.start();
      }
    }

    joinAllThreads(doThreads);
    throwAnyDoThreadErrors(doThreads);

    if (!shouldRunOnAllDevices && doThreads.isEmpty()) {
      throw new MojoExecutionException("No device found for android.device=" + device);
    }
  }
  /**
   * Stop the running Android Emulators.
   *
   * @throws org.apache.maven.plugin.MojoExecutionException
   */
  protected void stopAndroidEmulators() throws MojoExecutionException {
    final AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();
    if (androidDebugBridge.isConnected()) {
      List<IDevice> devices = Arrays.asList(androidDebugBridge.getDevices());
      int numberOfDevices = devices.size();
      getLog()
          .info("Found " + numberOfDevices + " devices connected with the Android Debug Bridge");

      for (IDevice device : devices) {
        if (device.isEmulator()) {
          stopEmulator(device);
        } else {
          getLog()
              .info("Skipping stop. Not an emulator. " + DeviceHelper.getDescriptiveName(device));
        }
      }
    }
  }
  /**
   * Performs the callback action on the devices determined by {@link
   * #shouldDoWithThisDevice(com.android.ddmlib.IDevice)}
   *
   * @param deviceCallback the action to perform on each device
   * @throws org.apache.maven.plugin.MojoExecutionException in case there is a problem
   * @throws org.apache.maven.plugin.MojoFailureException in case there is a problem
   */
  protected void doWithDevices(final DeviceCallback deviceCallback)
      throws MojoExecutionException, MojoFailureException {
    final AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();

    if (!androidDebugBridge.isConnected()) {
      throw new MojoExecutionException("Android Debug Bridge is not connected.");
    }

    waitForInitialDeviceList(androidDebugBridge);
    List<IDevice> devices = Arrays.asList(androidDebugBridge.getDevices());
    int numberOfDevices = devices.size();
    getLog().debug("Found " + numberOfDevices + " devices connected with the Android Debug Bridge");
    if (devices.size() == 0) {
      throw new MojoExecutionException("No online devices attached.");
    }

    int threadCount = getDeviceThreads();
    if (getDeviceThreads() == 0) {
      getLog()
          .info(
              "android.devicesThreads parameter not set, using a thread for each attached device");
      threadCount = numberOfDevices;
    } else {
      getLog().info("android.devicesThreads parameter set to " + getDeviceThreads());
    }

    boolean shouldRunOnAllDevices = getDevices().size() == 0;
    if (shouldRunOnAllDevices) {
      getLog().info("android.devices parameter not set, using all attached devices");
    } else {
      getLog().info("android.devices parameter set to " + getDevices().toString());
    }

    ArrayList<DoThread> doThreads = new ArrayList<DoThread>();
    ExecutorService executor = Executors.newFixedThreadPool(threadCount);
    for (final IDevice idevice : devices) {
      if (shouldRunOnAllDevices) {
        String deviceType = idevice.isEmulator() ? "Emulator " : "Device ";
        getLog().info(deviceType + DeviceHelper.getDescriptiveName(idevice) + " found.");
      }
      if (shouldRunOnAllDevices || shouldDoWithThisDevice(idevice)) {
        DoThread deviceDoThread =
            new DoThread() {
              public void runDo() throws MojoFailureException, MojoExecutionException {
                deviceCallback.doWithDevice(idevice);
              }
            };
        doThreads.add(deviceDoThread);
        executor.execute(deviceDoThread);
      }
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
      // waiting for threads finish
    }
    throwAnyDoThreadErrors(doThreads);

    if (!shouldRunOnAllDevices && doThreads.isEmpty()) {
      throw new MojoExecutionException(
          "No device found for android.device=" + getDevices().toString());
    }
  }
 /** {@inheritDoc} */
 public IDevice[] getDevices() {
   if (mAdbBridge == null) {
     throw new IllegalStateException("getDevices called before init");
   }
   return mAdbBridge.getDevices();
 }