<commit_before>/*
 * Copyright (C) 2016 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.google.android.exoplayer2.testutil;

import static org.junit.Assert.fail;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;

/**
 * A host activity for performing playback tests.
 */
public final class HostActivity extends Activity implements SurfaceHolder.Callback {

  /**
   * Interface for tests that run inside of a {@link HostActivity}.
   */
  public interface HostedTest {

    /**
     * Called on the main thread when the test is started.
     * <p>
     * The test will not be started until the {@link HostActivity} has been resumed and its
     * {@link Surface} has been created.
     *
     * @param host The {@link HostActivity} in which the test is being run.
     * @param surface The {@link Surface}.
     */
    void onStart(HostActivity host, Surface surface);

    /**
     * Called on the main thread to block until the test has stopped or {@link #forceStop()} is
     * called.
     *
     * @param timeoutMs The maximum time to block in milliseconds.
     * @return Whether the test has stopped successful.
     */
    boolean blockUntilStopped(long timeoutMs);

    /**
     * Called on the main thread to force stop the test (if it is not stopped already).
     *
     * @return Whether the test was forced stopped.
     */
    boolean forceStop();

    /**
     * Called on the test thread after the test has finished and been stopped.
     * <p>
     * Implementations may use this method to assert that test criteria were met.
     */
    void onFinished();

  }

  private static final String TAG = "HostActivity";

  private WakeLock wakeLock;
  private WifiLock wifiLock;
  private SurfaceView surfaceView;

  private HostedTest hostedTest;
  private boolean hostedTestStarted;
  private ConditionVariable hostedTestStartedCondition;
  private boolean forcedStopped;

  /**
   * Executes a {@link HostedTest} inside the host.
   *
   * @param hostedTest The test to execute.
   * @param timeoutMs The number of milliseconds to wait for the test to finish. If the timeout
   *     is exceeded then the test will fail.
   */
  public void runTest(HostedTest hostedTest, long timeoutMs) {
    runTest(hostedTest, timeoutMs, true);
  }

  /**
   * Executes a {@link HostedTest} inside the host.
   *
   * @param hostedTest The test to execute.
   * @param timeoutMs The number of milliseconds to wait for the test to finish.
   * @param failOnTimeout Whether the test fails when the timeout is exceeded.
   */
  public void runTest(final HostedTest hostedTest, long timeoutMs, boolean failOnTimeout) {
    Assertions.checkArgument(timeoutMs > 0);
    Assertions.checkState(Thread.currentThread() != getMainLooper().getThread());
    Assertions.checkState(this.hostedTest == null);
    Assertions.checkNotNull(hostedTest);
    hostedTestStartedCondition = new ConditionVariable();
    forcedStopped = false;
    hostedTestStarted = false;

    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        HostActivity.this.hostedTest = hostedTest;
        maybeStartHostedTest();
      }
    });
    hostedTestStartedCondition.block();

    if (hostedTest.blockUntilStopped(timeoutMs)) {
      if (!forcedStopped) {
        Log.d(TAG, "Checking test pass conditions.");
        hostedTest.onFinished();
        Log.d(TAG, "Pass conditions checked.");
      } else {
        String message = "Test force stopped. Activity may have been paused whilst "
            + "test was in progress.";
        Log.e(TAG, message);
        fail(message);
      }
    } else {
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          hostedTest.forceStop();
        }
      });
      String message = "Test timed out after " + timeoutMs + " ms.";<commit_msg>Fail tests quickly if they don't start within the host activity<commit_after>/*
 * Copyright (C) 2016 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.google.android.exoplayer2.testutil;

import static org.junit.Assert.fail;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;

/**
 * A host activity for performing playback tests.
 */
public final class HostActivity extends Activity implements SurfaceHolder.Callback {

  /**
   * Interface for tests that run inside of a {@link HostActivity}.
   */
  public interface HostedTest {

    /**
     * Called on the main thread when the test is started.
     * <p>
     * The test will not be started until the {@link HostActivity} has been resumed and its
     * {@link Surface} has been created.
     *
     * @param host The {@link HostActivity} in which the test is being run.
     * @param surface The {@link Surface}.
     */
    void onStart(HostActivity host, Surface surface);

    /**
     * Called on the main thread to block until the test has stopped or {@link #forceStop()} is
     * called.
     *
     * @param timeoutMs The maximum time to block in milliseconds.
     * @return Whether the test has stopped successful.
     */
    boolean blockUntilStopped(long timeoutMs);

    /**
     * Called on the main thread to force stop the test (if it is not stopped already).
     *
     * @return Whether the test was forced stopped.
     */
    boolean forceStop();

    /**
     * Called on the test thread after the test has finished and been stopped.
     * <p>
     * Implementations may use this method to assert that test criteria were met.
     */
    void onFinished();

  }

  private static final String TAG = "HostActivity";
  private static final long START_TIMEOUT_MS = 5000;

  private WakeLock wakeLock;
  private WifiLock wifiLock;
  private SurfaceView surfaceView;

  private HostedTest hostedTest;
  private boolean hostedTestStarted;
  private ConditionVariable hostedTestStartedCondition;
  private boolean forcedStopped;

  /**
   * Executes a {@link HostedTest} inside the host.
   *
   * @param hostedTest The test to execute.
   * @param timeoutMs The number of milliseconds to wait for the test to finish. If the timeout
   *     is exceeded then the test will fail.
   */
  public void runTest(HostedTest hostedTest, long timeoutMs) {
    runTest(hostedTest, timeoutMs, true);
  }

  /**
   * Executes a {@link HostedTest} inside the host.
   *
   * @param hostedTest The test to execute.
   * @param timeoutMs The number of milliseconds to wait for the test to finish.
   * @param failOnTimeout Whether the test fails when the timeout is exceeded.
   */
  public void runTest(final HostedTest hostedTest, long timeoutMs, boolean failOnTimeout) {
    Assertions.checkArgument(timeoutMs > 0);
    Assertions.checkState(Thread.currentThread() != getMainLooper().getThread());
    Assertions.checkState(this.hostedTest == null);
    Assertions.checkNotNull(hostedTest);
    hostedTestStartedCondition = new ConditionVariable();
    forcedStopped = false;
    hostedTestStarted = false;

    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        HostActivity.this.hostedTest = hostedTest;
        maybeStartHostedTest();
      }
    });

    if (!hostedTestStartedCondition.block(START_TIMEOUT_MS)) {
      String message =
          "Test failed to start. Display may be turned off or keyguard may be present.";
      Log.e(TAG, message);
      fail(message);
    }

    if (hostedTest.blockUntilStopped(timeoutMs)) {
      if (!forcedStopped) {
        Log.d(TAG, "Checking test pass conditions.");
        hostedTest.onFinished();
        Log.d(TAG, "Pass conditions checked.");
      } else {
        String message = "Test force stopped. Activity may have been paused whilst "
            + "test was in progress.";
        Log.e(TAG, message);
        fail(message);
      }
    } else {
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          hostedTest.forceStop();
        }
      });
      String message = "Test timed out after " + timeoutMs + " ms.";
{'fraction_non_alphanumeric': 0.0870390726491456, 'fraction_numerical': 0.0027980413710402717, 'mean_word_length': 3.130416838629798, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3319569', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2680, 'n_words': 1001}