<commit_before>package com.google.android.apps.common.testing.testrunner;

import android.app.Activity;
import android.app.Application;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.test.AndroidTestRunner;
import android.test.InstrumentationTestRunner;
import android.test.TestSuiteProvider;
import android.util.Log;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestListener;
import junit.framework.TestSuite;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Custom instrumentation runner for google android test cases.
 *
 */
public class
GoogleInstrumentationTestRunner
  extends
GoogleInstrumentation
    implements TestSuiteProvider {

  private static final long MILLIS_TO_WAIT_FOR_ACTIVITY_TO_STOP = TimeUnit.SECONDS.toMillis(2);
  private static final String LOG_TAG = "GoogleInstrTest";
  private BridgeTestRunner bridgeTestRunner = new BridgeTestRunner();


  @Override
  public void finish(int resultCode, Bundle results) {

    try {
      UsageTrackerRegistry.getInstance().sendUsages();
    } catch (RuntimeException re) {
      Log.w(LOG_TAG, "Failed to send analytics.", re);
    }
    super.finish(resultCode, results);
  }

  // ActivityUnitTestCase defaults to building the ComponentName via
  // Activity.getClass().getPackage().getName(). This will cause a problem if the Java Package of
  // the Activity is not the Android Package of the application, specifically
  // Activity.getPackageName() will return an incorrect value. For example, android compatibility
  // lib rev 19 is broken by this behaviour because it will eventually call through to
  // PackageManager with Activity.getComponentName().getPackageName() and which PM will know nothing
  // about.
  @Override
  public Activity newActivity(Class<?> clazz,
      Context context,
      IBinder token,
      Application application,
      Intent intent,
      ActivityInfo info,
      CharSequence title,
      Activity parent,
      String id,
      Object lastNonConfigurationInstance) throws InstantiationException, IllegalAccessException {
    String activityClassPackageName = clazz.getPackage().getName();
    String contextPackageName = context.getPackageName();
    ComponentName intentComponentName = intent.getComponent();
    if (!contextPackageName.equals(intentComponentName.getPackageName())) {
      if (activityClassPackageName.equals(intentComponentName.getPackageName())) {
        intent.setComponent(
            new ComponentName(contextPackageName, intentComponentName.getClassName()));
      }
    }
    return super.newActivity(clazz,
        context,
        token,
        application,
        intent,
        info,
        title,
        parent,
        id,
        lastNonConfigurationInstance);
  }

  @Override
  public void onCreate(Bundle arguments) {
    super.onCreate(arguments);
    mockitoWorkarounds();

    String disableAnalyticsStringValue = arguments.getString(
        "disableAnalytics");
    boolean disableAnalytics = Boolean.parseBoolean(disableAnalyticsStringValue);

    if (!disableAnalytics) {
      UsageTracker tracker = new AnalyticsBasedUsageTracker.Builder(
          getTargetContext()).buildIfPossible();

      if (null != tracker) {
        UsageTrackerRegistry.registerInstance(tracker);
      }
    }

    Log.i(LOG_TAG, "Test Started!");


    // bridge will call start()
    bridgeTestRunner.onCreate(arguments);
  }


  @Override
  public TestSuite getTestSuite() {
    return bridgeTestRunner.getTestSuite();
  }

  @Override
  public void start() {
    List<TestCase> testCases = bridgeTestRunner.getAndroidTestRunner().getTestCases();

    // Register a listener to update the current test description.
    bridgeTestRunner.getAndroidTestRunner().addTestListener(new TestListener() {
      @Override
      public void startTest(Test test) {
        runOnMainSync(new ActivityFinisher());
      }

      @Override
      public void endTest(Test test) {
      }

      @Override
      public void addFailure(Test test, AssertionFailedError ae) {
      }

      @Override
      public void addError(Test test, Throwable t) {
      }<commit_msg>Espresso: provide getter for AndroidTestRunner.<commit_after>package com.google.android.apps.common.testing.testrunner;

import android.app.Activity;
import android.app.Application;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.test.AndroidTestRunner;
import android.test.InstrumentationTestRunner;
import android.test.TestSuiteProvider;
import android.util.Log;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestListener;
import junit.framework.TestSuite;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Custom instrumentation runner for google android test cases.
 *
 */
public class
GoogleInstrumentationTestRunner
  extends
GoogleInstrumentation
    implements TestSuiteProvider {

  private static final long MILLIS_TO_WAIT_FOR_ACTIVITY_TO_STOP = TimeUnit.SECONDS.toMillis(2);
  private static final String LOG_TAG = "GoogleInstrTest";
  private BridgeTestRunner bridgeTestRunner = new BridgeTestRunner();


  @Override
  public void finish(int resultCode, Bundle results) {

    try {
      UsageTrackerRegistry.getInstance().sendUsages();
    } catch (RuntimeException re) {
      Log.w(LOG_TAG, "Failed to send analytics.", re);
    }
    super.finish(resultCode, results);
  }

  // ActivityUnitTestCase defaults to building the ComponentName via
  // Activity.getClass().getPackage().getName(). This will cause a problem if the Java Package of
  // the Activity is not the Android Package of the application, specifically
  // Activity.getPackageName() will return an incorrect value. For example, android compatibility
  // lib rev 19 is broken by this behaviour because it will eventually call through to
  // PackageManager with Activity.getComponentName().getPackageName() and which PM will know nothing
  // about.
  @Override
  public Activity newActivity(Class<?> clazz,
      Context context,
      IBinder token,
      Application application,
      Intent intent,
      ActivityInfo info,
      CharSequence title,
      Activity parent,
      String id,
      Object lastNonConfigurationInstance) throws InstantiationException, IllegalAccessException {
    String activityClassPackageName = clazz.getPackage().getName();
    String contextPackageName = context.getPackageName();
    ComponentName intentComponentName = intent.getComponent();
    if (!contextPackageName.equals(intentComponentName.getPackageName())) {
      if (activityClassPackageName.equals(intentComponentName.getPackageName())) {
        intent.setComponent(
            new ComponentName(contextPackageName, intentComponentName.getClassName()));
      }
    }
    return super.newActivity(clazz,
        context,
        token,
        application,
        intent,
        info,
        title,
        parent,
        id,
        lastNonConfigurationInstance);
  }

  @Override
  public void onCreate(Bundle arguments) {
    super.onCreate(arguments);
    mockitoWorkarounds();

    String disableAnalyticsStringValue = arguments.getString(
        "disableAnalytics");
    boolean disableAnalytics = Boolean.parseBoolean(disableAnalyticsStringValue);

    if (!disableAnalytics) {
      UsageTracker tracker = new AnalyticsBasedUsageTracker.Builder(
          getTargetContext()).buildIfPossible();

      if (null != tracker) {
        UsageTrackerRegistry.registerInstance(tracker);
      }
    }

    Log.i(LOG_TAG, "Test Started!");


    // bridge will call start()
    bridgeTestRunner.onCreate(arguments);
  }


  @Override
  public TestSuite getTestSuite() {
    return bridgeTestRunner.getTestSuite();
  }

  /**
   * Provides access to the underlying AndroidTestRunner.
   */
  public AndroidTestRunner getAndroidTestRunner() {
    return bridgeTestRunner.getAndroidTestRunner();
  }

  @Override
  public void start() {
    List<TestCase> testCases = bridgeTestRunner.getAndroidTestRunner().getTestCases();

    // Register a listener to update the current test description.
    bridgeTestRunner.getAndroidTestRunner().addTestListener(new TestListener() {
      @Override
      public void startTest(Test test) {
        runOnMainSync(new ActivityFinisher());
      }

      @Override
      public void endTest(Test test) {
      }

      @Override
      public void addFailure(Test test, AssertionFailedError ae) {
      }

      @Override
      public void addError(Test test, Throwable t) {
      }
{'fraction_non_alphanumeric': 0.08442776735459662, 'fraction_numerical': 0.0006621785674870323, 'mean_word_length': 3.766964755391899, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7595795', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2314, 'n_words': 609}