Skip to content

Instantly share code, notes, and snippets.

@JoseAlcerreca
Last active August 14, 2016 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoseAlcerreca/c27a0becd6d92310fe674c3c7b21c96d to your computer and use it in GitHub Desktop.
Save JoseAlcerreca/c27a0becd6d92310fe674c3c7b21c96d to your computer and use it in GitHub Desktop.
An ExecutorService for Espresso
/*
* Copyright 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.example.android.testing.espresso.AdvancedIdlingResource;
import android.support.test.espresso.idling.CountingIdlingResource;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
public class IdlingResourceExecutorService extends GuavasWrappingExecutorService {
private final CountingIdlingResource mIdlingResource;
public IdlingResourceExecutorService(ExecutorService delegate,
CountingIdlingResource idlingResource) {
super(delegate);
mIdlingResource = idlingResource;
}
@Override
protected <T> Callable<T> wrapTask(Callable<T> callable) {
return new WrappedCallable<>(callable);
}
private final class WrappedCallable<T> implements Callable<T> {
private final Callable<T> delegate;
public WrappedCallable(Callable<T> delegate) {
this.delegate = delegate;
}
@Override
public T call() throws Exception {
mIdlingResource.increment();
T call;
try {
call = delegate.call();
} finally {
mIdlingResource.decrement();
}
return call;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment