Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2013 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.android.builder.internal.testing;
 
 
 import java.io.File;
 import java.util.Map;
Basic Callable to run tests on a given com.android.builder.testing.api.DeviceConnector using com.android.ddmlib.testrunner.RemoteAndroidTestRunner. The boolean return value is true if success.
 
 public class SimpleTestCallable implements Callable<Boolean> {
 
     public static final String FILE_COVERAGE_EC = "coverage.ec";
 
     @NonNull
     private final String projectName;
     @NonNull
     private final DeviceConnector device;
     @NonNull
     private final String flavorName;
     @NonNull
     private final TestData testData;
     @NonNull
     private final File resultsDir;
     @NonNull
     private final File coverageDir;
     @NonNull
     private final File testApk;
     @Nullable
     private final File testedApk;
     private final int timeout;
     @NonNull
     private final ILogger logger;
 
     public SimpleTestCallable(
             @NonNull  DeviceConnector device,
             @NonNull  String projectName,
             @NonNull  String flavorName,
             @NonNull  File testApk,
             @Nullable File testedApk,
             @NonNull  TestData testData,
             @NonNull  File resultsDir,
             @NonNull  File coverageDir,
                       int timeout,
             @NonNull  ILogger logger) {
         this. = projectName;
         this. = device;
         this. = flavorName;
         this. = resultsDir;
         this. = coverageDir;
         this. = testApk;
         this. = testedApk;
         this. = testData;
         this. = timeout;
         this. = logger;
     }
 
     @Override
     public Boolean call() throws Exception {
         String deviceName = .getName();
         boolean isInstalled = false;
 
        CustomTestRunListener runListener = new CustomTestRunListener(
                deviceName);
        runListener.setReportDir();
        long time = System.currentTimeMillis();
        boolean success = false;
        String coverageFile = "/data/data/" + .getTestedApplicationId() + "/" + ;
        try {
            .connect();
            if ( != null) {
                .verbose("DeviceConnector '%s': installing %s"deviceName);
                .installPackage();
            }
            .verbose("DeviceConnector '%s': installing %s"deviceName);
            .installPackage();
            isInstalled = true;
            RemoteAndroidTestRunner runner = new RemoteAndroidTestRunner(
                    .getApplicationId(),
                    .getInstrumentationRunner(),
                    );
            if (.isTestCoverageEnabled()) {
                runner.addInstrumentationArg("coverage""true");
                runner.addInstrumentationArg("coverageFile"coverageFile);
            }
            runner.setRunName(deviceName);
            runner.setMaxtimeToOutputResponse();
            runner.run(runListener);
            TestRunResult testRunResult = runListener.getRunResult();
            success = true;
            // for now throw an exception if no tests.
            // TODO return a status instead of allow merging of multi-variants/multi-device reports.
            if (testRunResult.getNumTests() == 0) {
                CustomTestRunListener fakeRunListener = new CustomTestRunListener(
                        deviceName);
                fakeRunListener.setReportDir();
                // create a fake test output
                Map<StringStringemptyMetrics = Collections.emptyMap();
                TestIdentifier fakeTest = new TestIdentifier(.getClass().getName(), "hasTests");
                fakeRunListener.testStarted(fakeTest);
                fakeRunListener.testFailed(..fakeTest , "No tests found.");
                fakeRunListener.testEnded(fakeTestemptyMetrics);
                // end the run to generate the XML file.
                fakeRunListener.testRunEnded(System.currentTimeMillis() - timeemptyMetrics);
                return false;
            }
            return !testRunResult.hasFailedTests();
        } catch (Exception e) {
            Map<StringStringemptyMetrics = Collections.emptyMap();
            // create a fake test output
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintWriter pw = new PrintWriter(baostrue);
            e.printStackTrace(pw);
            TestIdentifier fakeTest = new TestIdentifier(.getClass().getName(), "runTests");
            runListener.testStarted(fakeTest);
            runListener.testFailed(..fakeTest , baos.toString());
            runListener.testEnded(fakeTestemptyMetrics);
            // end the run to generate the XML file.
            runListener.testRunEnded(System.currentTimeMillis() - timeemptyMetrics);
            // and throw
            throw e;
        } finally {
            if (isInstalled) {
                // Get the coverage if needed.
                if (success && .isTestCoverageEnabled()) {
                    .executeShellCommand(
                            "run-as " + .getTestedApplicationId() + " chmod 644 " + coverageFile,
                            new NullOutputReceiver(),
                            30, .);
                    .pullFile(
                            coverageFile,
                            new File().getPath());
                }
                // uninstall the apps
                // This should really not be null, because if it was the build
                // would have broken before.
                uninstall(.getApplicationId(), deviceName);
                if ( != null) {
                   uninstall(.getTestedApplicationId(), deviceName);
                }
            }
            .disconnect();
        }
    }
    private void uninstall(@NonNull File apkFile, @Nullable String packageName,
                           @NonNull String deviceName)
            throws DeviceException {
        if (packageName != null) {
            .verbose("DeviceConnector '%s': uninstalling %s"deviceNamepackageName);
            .uninstallPackage(packageName);
        } else {
            .verbose("DeviceConnector '%s': unable to uninstall %s: unable to get package name",
                    deviceNameapkFile);
        }
    }
New to GrepCode? Check out our FAQ X