/*
 * Copyright (C) 2017 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 androidx.test.espresso.idling.concurrent;

import android.util.Log;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.IdlingResource;
import androidx.test.espresso.idling.CountingIdlingResource;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * A {@link ScheduledThreadPoolExecutor} that can be registered as an {@link IdlingResource} with
 * Espresso.
 *
 * <p>Overrides parent methods to monitor threads starting, and finishing execution. Uses {@link
 * CountingIdlingResource} to track number of active tasks.
 *
 * <p><b>This API is currently in beta.</b>
 */
public class IdlingScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor
    implements IdlingResource {

  private static final String LOG_TAG = "IdlingThreadPoolExec";

  private CountingIdlingResource countingIdlingResource;

  /**
   * Creates a new {@code IdlingScheduledThreadPoolExecutor} with the given initial parameters.
   *
   * @param resourceName the name of the executor (used for logging and idempotency of
   *     registration).
   * @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless
   *     allowCoreThreadTimeOut is set.
   * @param threadFactory the factory to use when the executor creates a new thread.
   */
  public IdlingScheduledThreadPoolExecutor(
      String resourceName, int corePoolSize, ThreadFactory threadFactory) {
    super(corePoolSize, threadFactory);
    countingIdlingResource = new CountingIdlingResource(resourceName);
    Log.i(LOG_TAG, "Register idling resource for scheduled thread pool " + resourceName);
    IdlingRegistry.getInstance().register(this);
  }

  @Override
  public String getName() {
    return countingIdlingResource.getName();
  }

  @Override
  public boolean isIdleNow() {
    return countingIdlingResource.isIdleNow();
  }

  @Override
  public void registerIdleTransitionCallback(ResourceCallback callback) {
    countingIdlingResource.registerIdleTransitionCallback(callback);
  }

  @Override
  public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
    countingIdlingResource.increment();
    return super.schedule(command, delay, unit);
  }

  @Override
  public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
    countingIdlingResource.increment();
    return super.schedule(callable, delay, unit);
  }

  @Override
  protected void beforeExecute(Thread t, Runnable r) {}

  @Override
  protected void afterExecute(Runnable r, Throwable t) {
    countingIdlingResource.decrement();
  }

  @Override
  protected void terminated() {
    super.terminated();
    Log.i(LOG_TAG, "Thread pool terminated, unregistering " + countingIdlingResource.getName());
    IdlingRegistry.getInstance().unregister(this);
  }
}

@@ -2191,24 +2191,779 @@
 dFactory) %7B%0A
+    this(resourceName, corePoolSize, threadFactory, false);%0A  %7D%0A%0A  /**%0A   * Creates a new %7B@code IdlingScheduledThreadPoolExecutor%7D with the given initial parameters.%0A   *%0A   * @param resourceName the name of the executor (used for logging and idempotency of%0A   *     registration).%0A   * @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless%0A   *     allowCoreThreadTimeOut is set.%0A   * @param threadFactory the factory to use when the executor creates a new thread.%0A   * @param debugCounting if true increment & decrement calls will print trace information to logs.%0A   */%0A  public IdlingScheduledThreadPoolExecutor(%0A      String resourceName, int corePoolSize, ThreadFactory threadFactory, boolean debugCounting) %7B%0A
     super(co
@@ -3054,24 +3054,39 @@
 resourceName
+, debugCounting
 );%0A    Log.i

Add an overloaded constructor to IdlingScheduledThreadPoolExecutor to also include a debug flag that will allow the underlying CountingIdlingResource to print increment/decrement information to logs