All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deque.axe.android.Axe Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.deque.axe.android;

import com.deque.axe.android.utils.AxeTree;
import com.deque.axe.android.wrappers.AxeProps;
import com.deque.networking.analytics.AmplitudeEventConstant;
import com.deque.networking.analytics.AnalyticsService;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Main class for the Axe library. Is in charge of managing AxeRules and returning AxeResults.
 */
public class Axe {

  final AxeConf axeConf;

  public Axe(final AxeConf axeConf) {

    this.axeConf = axeConf;
  }

  /**
   * Main axe run function.
   * @param axeContext The {@link AxeContext} for the run.
   * @return An {@link AxeResult} object.
   */
  public AxeResult run(final AxeContext axeContext) {

    AxeResult axeResult = new AxeResult(axeConf, axeContext);

    List rules = new LinkedList<>();

    List statefulRules = new LinkedList<>();

    for (AxeRule rule : axeConf.ruleInstances()) {
      if (rule instanceof AxeRuleStateful) {
        statefulRules.add((AxeRuleStateful) rule);
      } else if (rule instanceof AxeRuleViewHierarchy) {
        rules.add((AxeRuleViewHierarchy) rule);
      }
    }

    final Map contextProps = new HashMap<>();

    for (AxeRuleViewHierarchy axeRule : rules) {
      final AxeProps axeProps = new AxeProps();
      contextProps.put(axeRule, axeProps);
      axeRule.setup(axeContext, axeProps);
    }

    axeContext.axeView.forEachRecursive(axeView -> {

      for (AxeRuleViewHierarchy axeRule : rules) {

        try {

          final AxeProps viewProps = new AxeProps();

          viewProps.putAll(contextProps.get(axeRule));

          axeRule.collectProps(axeView, viewProps);

          if (axeRule.isApplicable(viewProps)) {
            final String status = axeRule.runRule(viewProps);
            axeResult.add(new AxeRuleResult(status, axeRule, viewProps, axeView));
          }
        } catch (Exception e) {
          handleException(e, axeRule, axeView);
        }
      }

      return AxeTree.CallBackResponse.CONTINUE;
    });

    for (AxeRuleViewHierarchy axeRule : rules) {
      axeRule.tearDown();
    }

    for (AxeRuleStateful axeRule : statefulRules) {
      try {
        axeResult.add(axeRule.run(axeContext.axeEventStream));
      } catch (Exception e) {
        handleException(e, axeRule, axeContext.axeView);
      }
    }

    AnalyticsService.sendRuleErrorLog(AmplitudeEventConstant.SCAN_TYPE_IMPERATIVE);

    return axeResult;
  }

  private void handleException(final Exception e,
                               final AxeRule axeRule,
                               final AxeView axeView) {
    String errorMessage = "rule: " + axeRule.id + " on view id: " + axeView.viewIdResourceName + ", error: " + e.toString();
    System.err.println("Axe DevTools Android: " + errorMessage);
    System.err.println("Axe DevTools Android: " + Arrays.toString(e.getStackTrace()));
    AnalyticsService.addRuleError(errorMessage);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy