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 TestCase createLogoutTest() {

    TestCase test =
        new TestCase() {

          @Override
          protected void executeTest(MobileServiceClient client, TestExecutionCallback callback) {

            client.logout();
            log("Logged out");
            TestResult result = new TestResult();
            result.setTestCase(this);
            result.setStatus(
                client.getCurrentUser() == null ? TestStatus.Passed : TestStatus.Failed);

            callback.onTestComplete(this, result);
          }
        };

    test.setName("Logout");

    return test;
  }
  public CustomApiTests() {
    super("Custom API tests");

    apiNames = new HashMap<CustomApiTests.ApiPermissions, String>();
    apiNames.put(ApiPermissions.Admin, ADMIN_API_NAME);
    apiNames.put(ApiPermissions.User, USER_API_NAME);
    apiNames.put(ApiPermissions.Application, APP_API_NAME);
    apiNames.put(ApiPermissions.Public, PUBLIC_API_NAME);

    Random rndGen = new Random();

    this.addTest(LoginTests.createLogoutTest());

    for (ApiPermissions permission : ApiPermissions.values()) {
      for (int i = 0; i < 10; i++) {
        this.addTest(createJsonApiTest(permission, false, rndGen, i));
      }
    }

    TestCase loginTest = LoginTests.createLoginTest(MobileServiceAuthenticationProvider.Facebook);
    loginTest.setCanRunUnattended(false);
    this.addTest(loginTest);
    TestCase apiAuthenticatedTest = createJsonApiTest(ApiPermissions.User, true, rndGen, 0);
    apiAuthenticatedTest.setCanRunUnattended(false);
    this.addTest(apiAuthenticatedTest);
    this.addTest(LoginTests.createLogoutTest());

    for (TypedTestType testType : TypedTestType.values()) {
      this.addTest(createTypedApiTest(rndGen, testType));
    }

    for (DataFormat inputFormat : DataFormat.values()) {
      for (DataFormat outputFormat : DataFormat.values()) {
        this.addTest(createHttpContentApiTest(inputFormat, outputFormat, rndGen));
      }
    }
  }
  private TestCase createLoginTest(final MobileServiceAuthenticationProvider provider) {
    TestCase test =
        new TestCase() {

          @Override
          protected void executeTest(
              final MobileServiceClient client, final TestExecutionCallback callback) {
            final TestCase testCase = this;
            client.login(
                provider,
                new UserAuthenticationCallback() {

                  @Override
                  public void onCompleted(
                      MobileServiceUser user, Exception exception, ServiceFilterResponse response) {
                    TestResult result = new TestResult();
                    String userName;
                    if (user == null) {
                      userName = "******";
                    } else {
                      userName = user.getUserId();
                    }

                    log("Logged in as " + userName);
                    result.setStatus(
                        client.getCurrentUser() != null ? TestStatus.Passed : TestStatus.Failed);
                    result.setTestCase(testCase);

                    callback.onTestComplete(testCase, result);
                  }
                });
          }
        };

    test.setName("Login with " + provider.toString());
    return test;
  }
  private TestCase createCRUDTest(
      final String tableName,
      final MobileServiceAuthenticationProvider provider,
      final TablePermission tableType,
      final boolean userIsAuthenticated) {
    final TestCase test =
        new TestCase() {

          @Override
          protected void executeTest(
              MobileServiceClient client, final TestExecutionCallback callback) {

            final TestResult result = new TestResult();
            result.setStatus(TestStatus.Passed);
            result.setTestCase(this);
            final TestCase testCase = this;

            MobileServiceClient logClient = client.withFilter(new LogServiceFilter());

            final MobileServiceJsonTable table = logClient.getTable(tableName);
            final boolean crudShouldWork =
                tableType == TablePermission.Public
                    || tableType == TablePermission.Application
                    || (tableType == TablePermission.User && userIsAuthenticated);
            final JsonObject item = new JsonObject();
            item.addProperty("name", "John Doe");
            log("insert item");
            table.insert(
                item,
                new TableJsonOperationCallback() {

                  @Override
                  public void onCompleted(
                      JsonObject jsonEntity, Exception exception, ServiceFilterResponse response) {
                    int id = 1;
                    if (exception == null) {
                      id = jsonEntity.get("id").getAsInt();
                    }

                    item.addProperty("id", id);
                    if (!validateExecution(crudShouldWork, exception, result)) {
                      callback.onTestComplete(testCase, result);
                      return;
                    }

                    item.addProperty("name", "Jane Doe");
                    log("update item");
                    table.update(
                        item,
                        new TableJsonOperationCallback() {

                          @Override
                          public void onCompleted(
                              JsonObject jsonEntity,
                              Exception exception,
                              ServiceFilterResponse response) {

                            if (!validateExecution(crudShouldWork, exception, result)) {
                              callback.onTestComplete(testCase, result);
                              return;
                            }

                            log("lookup item");
                            table.lookUp(
                                item.get("id").getAsInt(),
                                new TableJsonOperationCallback() {

                                  @Override
                                  public void onCompleted(
                                      JsonObject jsonEntity,
                                      Exception exception,
                                      ServiceFilterResponse response) {
                                    if (!validateExecution(crudShouldWork, exception, result)) {
                                      callback.onTestComplete(testCase, result);
                                      return;
                                    }

                                    log("delete item");
                                    table.delete(
                                        item.get("id").getAsInt(),
                                        new TableDeleteCallback() {

                                          @Override
                                          public void onCompleted(
                                              Exception exception, ServiceFilterResponse response) {
                                            validateExecution(crudShouldWork, exception, result);

                                            callback.onTestComplete(testCase, result);
                                            return;
                                          }
                                        });
                                  }
                                });
                          }
                        });
                  }
                });
          }

          private boolean validateExecution(
              boolean crudShouldWork, Exception exception, TestResult result) {
            if (crudShouldWork && exception != null || !crudShouldWork && exception == null) {
              createResultFromException(result, exception);
              result.setStatus(TestStatus.Failed);
              return false;
            } else {
              return true;
            }
          }
        };

    String testKind;
    if (userIsAuthenticated) {
      testKind = "auth by " + provider.toString();
    } else {
      testKind = "unauthenticated";
    }

    String testName =
        String.format(
            Locale.getDefault(),
            "CRUD, %s, table with %s permissions",
            testKind,
            tableType.toString());
    test.setName(testName);

    return test;
  }