private void changeCheckAllTests(boolean check) {
    TestGroup tg = (TestGroup) mTestGroupSpinner.getSelectedItem();
    List<TestCase> testCases = tg.getTestCases();

    for (TestCase testCase : testCases) {
      testCase.setEnabled(check);
    }

    fillTestList(testCases);
  }
  private void runTests() {

    MobileServiceClient client = null;

    try {
      client = createMobileServiceClient();
    } catch (MalformedURLException e) {
      createAndShowDialog(e, "Error");
    }

    // getMobileServiceRuntimeFeatures(client);

    final TestGroup group = (TestGroup) mTestGroupSpinner.getSelectedItem();
    logWithTimestamp(new Date(), "Tests for group \'" + group.getName() + "\'");

    logSeparator();

    final MobileServiceClient currentClient = client;

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1
        || Build.VERSION.SDK_INT == Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      // For android versions 4.0.x
      // Run a first Void AsyncTask on UI thread to enable the possibility
      // of running others on sub threads
      new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
          return null;
        }
      }.execute();
    }

    Thread thread =
        new Thread() {

          @Override
          public void run() {
            group.runTests(
                currentClient,
                new TestExecutionCallback() {

                  @Override
                  public void onTestStart(TestCase test) {
                    final TestCaseAdapter adapter = (TestCaseAdapter) mTestCaseList.getAdapter();

                    runOnUiThread(
                        new Runnable() {

                          @Override
                          public void run() {
                            adapter.notifyDataSetChanged();
                          }
                        });

                    log("TEST START", test.getName());
                  }

                  @Override
                  public void onTestGroupComplete(TestGroup group, List<TestResult> results) {
                    log(
                        "TEST GROUP COMPLETED",
                        group.getName() + " - " + group.getStatus().toString());
                    logSeparator();

                    if (group.getName().startsWith(TestGroup.AllTestsGroupName)) {

                      List<TestCase> tests = new ArrayList<TestCase>();

                      for (TestResult result : results) {
                        tests.add(result.getTestCase());
                      }

                      StorageLogger logger =
                          new StorageLogger(
                              getStorageURL(), getStorageBase64Token(), getStorageRuntime());

                      try {
                        logger.reportResults(
                            group.getFailedTestCount(),
                            group.getPassedTestCount(),
                            group.notRunTestCount(),
                            group.getStartTime(),
                            group.getEndTime(),
                            tests,
                            group.getSourceMap());
                      } catch (Throwable e) {
                        log(e.getMessage());
                      }
                    }

                    if (shouldRunUnattended()) {
                      // String logContent = mLog.toString();
                      // postLogs(logContent, true);

                      boolean passed = true;
                      for (TestResult result : results) {
                        if (result.getStatus() != TestStatus.Passed) {
                          passed = false;
                          break;
                        }
                      }

                      try {
                        String sdCard = Environment.getExternalStorageDirectory().getPath();
                        FileOutputStream fos =
                            new FileOutputStream(sdCard + "/done_android_e2e.txt");
                        OutputStreamWriter osw = new OutputStreamWriter(fos);
                        BufferedWriter bw = new BufferedWriter(osw);
                        bw.write("Completed successfully.\n");
                        bw.write(passed ? "PASSED" : "FAILED");
                        bw.write("\n");
                        bw.close();
                        osw.close();
                        fos.close();
                      } catch (IOException e) {
                        e.printStackTrace();
                      }
                    }
                  }

                  @Override
                  public void onTestComplete(TestCase test, TestResult result) {
                    Throwable e = result.getException();
                    if (e != null) {
                      StringBuilder sb = new StringBuilder();
                      while (e != null) {
                        sb.append(e.getClass().getSimpleName() + ": ");
                        sb.append(e.getMessage());
                        sb.append("\n");
                        sb.append(Log.getStackTraceString(e));
                        sb.append("\n\n");
                        e = e.getCause();
                      }

                      test.log("Exception: " + sb.toString());
                    }

                    final TestCaseAdapter adapter = (TestCaseAdapter) mTestCaseList.getAdapter();

                    runOnUiThread(
                        new Runnable() {

                          @Override
                          public void run() {
                            adapter.notifyDataSetChanged();
                          }
                        });
                    logWithTimestamp(
                        test.getStartTime(),
                        "Logs for test "
                            + test.getName()
                            + " ("
                            + result.getStatus().toString()
                            + ")");
                    String testLogs = test.getLog();
                    if (testLogs.length() > 0) {
                      if (testLogs.endsWith("\n")) {
                        testLogs = testLogs.substring(0, testLogs.length() - 1);
                      }
                      log(testLogs);
                    }

                    logWithTimestamp(test.getEndTime(), "Test " + result.getStatus().toString());
                    logWithTimestamp(test.getEndTime(), "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
                    logSeparator();
                  }
                });
          }
        };

    thread.start();
  }
  private void selectTestGroup(int pos) {
    TestGroup tg = (TestGroup) mTestGroupSpinner.getItemAtPosition(pos);
    List<TestCase> testCases = tg.getTestCases();

    fillTestList(testCases);
  }