Task executor rules for flatfoot

This CL adds 2 new public task executor rules that can be used
to control flatfoot background threads in developer's host side
and Instrumentation tests.

Bug: 38066329
Test: :arch:core-testing:test :arch:core-testing:cC
Change-Id: I32f55cd2a07072c51f22e55844eb7a878ea522d3
diff --git a/app-toolkit/core-testing/build.gradle b/app-toolkit/core-testing/build.gradle
index e3f0f84..52ff125 100644
--- a/app-toolkit/core-testing/build.gradle
+++ b/app-toolkit/core-testing/build.gradle
@@ -20,11 +20,12 @@
     compileSdkVersion tools.current_sdk
     buildToolsVersion tools.build_tools_version
 
-        defaultConfig {
+    defaultConfig {
         minSdkVersion flatfoot.min_sdk
         targetSdkVersion tools.current_sdk
         versionCode 1
         versionName "1.0"
+        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
     }
     buildTypes {
         release {
@@ -47,6 +48,17 @@
         exclude module: 'hamcrest-core'
     }
     compile libs.mockito_core
+
+    testCompile libs.junit
+    testCompile libs.support.annotations
+
+    androidTestCompile libs.junit
+    androidTestCompile(libs.test_runner) {
+        exclude module: 'support-annotations'
+    }
+    androidTestCompile(libs.espresso_core, {
+        exclude group: 'com.android.support', module: 'support-annotations'
+    })
 }
 
 archivesBaseName = "core-testing"
diff --git a/app-toolkit/core-testing/src/androidTest/java/android/arch/core/executor/CountingTaskExecutorRuleTest.java b/app-toolkit/core-testing/src/androidTest/java/android/arch/core/executor/CountingTaskExecutorRuleTest.java
new file mode 100644
index 0000000..c695d63
--- /dev/null
+++ b/app-toolkit/core-testing/src/androidTest/java/android/arch/core/executor/CountingTaskExecutorRuleTest.java
@@ -0,0 +1,180 @@
+/*
+ * 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 android.arch.core.executor;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import android.support.test.filters.MediumTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+@RunWith(AndroidJUnit4.class)
+@MediumTest
+public class CountingTaskExecutorRuleTest {
+    private final Semaphore mOnIdleCount = new Semaphore(0);
+
+    @Rule
+    public CountingTaskExecutorRule mRule = new CountingTaskExecutorRule() {
+        @Override
+        protected void onIdle() {
+            super.onIdle();
+            mOnIdleCount.release(1);
+        }
+    };
+
+    @Test
+    public void initialIdle() {
+        assertThat(mRule.isIdle(), is(true));
+    }
+
+    @Test
+    public void busyIO() throws InterruptedException {
+        LatchRunnable task = runOnIO();
+        singleTaskTest(task);
+    }
+
+    @Test
+    public void busyMain() throws InterruptedException {
+        LatchRunnable task = runOnMain();
+        singleTaskTest(task);
+    }
+
+    @Test
+    public void multipleTasks() throws InterruptedException {
+        List<LatchRunnable> latches = new ArrayList<>(10);
+        for (int i = 0; i < 5; i++) {
+            latches.add(runOnIO());
+            latches.add(runOnMain());
+        }
+        assertNotIdle();
+        for (int i = 0; i < 9; i++) {
+            latches.get(i).start();
+        }
+        for (int i = 0; i < 9; i++) {
+            latches.get(i).await();
+        }
+        assertNotIdle();
+
+        LatchRunnable another = runOnIO();
+        latches.get(9).startAndFinish();
+        assertNotIdle();
+
+        another.startAndFinish();
+        assertBecomeIdle();
+
+        LatchRunnable oneMore = runOnMain();
+
+        assertNotIdle();
+
+        oneMore.startAndFinish();
+        assertBecomeIdle();
+    }
+
+    private void assertNotIdle() throws InterruptedException {
+        assertThat(mOnIdleCount.tryAcquire(300, TimeUnit.MILLISECONDS), is(false));
+        assertThat(mRule.isIdle(), is(false));
+    }
+
+    private void assertBecomeIdle() throws InterruptedException {
+        assertThat(mOnIdleCount.tryAcquire(1, TimeUnit.SECONDS), is(true));
+        assertThat(mRule.isIdle(), is(true));
+    }
+
+    private void singleTaskTest(LatchRunnable task)
+            throws InterruptedException {
+        assertNotIdle();
+        task.startAndFinish();
+        assertBecomeIdle();
+    }
+
+    private LatchRunnable runOnIO() {
+        LatchRunnable latchRunnable = new LatchRunnable();
+        AppToolkitTaskExecutor.getInstance().executeOnDiskIO(latchRunnable);
+        return latchRunnable;
+    }
+
+    private LatchRunnable runOnMain() {
+        LatchRunnable latchRunnable = new LatchRunnable();
+        AppToolkitTaskExecutor.getInstance().executeOnMainThread(latchRunnable);
+        return latchRunnable;
+    }
+
+    @Test
+    public void drainFailure() throws InterruptedException {
+        runOnIO();
+        try {
+            mRule.drainTasks(300, TimeUnit.MILLISECONDS);
+            throw new AssertionError("drain should fail");
+        } catch (TimeoutException timeout) {
+        }
+    }
+
+    @Test
+    public void drainSuccess() throws TimeoutException, InterruptedException {
+        final LatchRunnable task = runOnIO();
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    Thread.sleep(300);
+                } catch (InterruptedException e) {
+                }
+                task.start();
+            }
+        }).start();
+        mRule.drainTasks(1, TimeUnit.SECONDS);
+    }
+
+    private static class LatchRunnable implements Runnable {
+        private final CountDownLatch mStart = new CountDownLatch(1);
+        private final CountDownLatch mEnd = new CountDownLatch(1);
+
+        @Override
+        public void run() {
+            try {
+                mStart.await(10, TimeUnit.SECONDS);
+                mEnd.countDown();
+            } catch (InterruptedException e) {
+                throw new AssertionError(e);
+            }
+        }
+
+        public void await() throws InterruptedException {
+            mEnd.await(10, TimeUnit.SECONDS);
+        }
+
+        public void start() {
+            mStart.countDown();
+        }
+
+        private void startAndFinish() throws InterruptedException {
+            start();
+            await();
+        }
+    }
+}
diff --git a/app-toolkit/core-testing/src/main/java/android/arch/core/executor/CountingTaskExecutorRule.java b/app-toolkit/core-testing/src/main/java/android/arch/core/executor/CountingTaskExecutorRule.java
new file mode 100644
index 0000000..b89bbff
--- /dev/null
+++ b/app-toolkit/core-testing/src/main/java/android/arch/core/executor/CountingTaskExecutorRule.java
@@ -0,0 +1,139 @@
+/*
+ * 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 android.arch.core.executor;
+
+import android.os.SystemClock;
+
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A JUnit Test Rule that swaps the background executor used by the Architecture Components with a
+ * different one which counts the tasks as they are start and finish.
+ * <p>
+ * You can use this rule for your host side tests that use Architecture Components.
+ */
+public class CountingTaskExecutorRule extends TestWatcher {
+    private final Object mCountLock = new Object();
+    private int mTaskCount = 0;
+
+    @Override
+    protected void starting(Description description) {
+        super.starting(description);
+        AppToolkitTaskExecutor.getInstance().setDelegate(new DefaultTaskExecutor() {
+            @Override
+            public void executeOnDiskIO(Runnable runnable) {
+                super.executeOnDiskIO(new CountingRunnable(runnable));
+            }
+
+            @Override
+            public void postToMainThread(Runnable runnable) {
+                super.postToMainThread(new CountingRunnable(runnable));
+            }
+        });
+    }
+
+    @Override
+    protected void finished(Description description) {
+        super.finished(description);
+        AppToolkitTaskExecutor.getInstance().setDelegate(null);
+    }
+
+    private void increment() {
+        synchronized (mCountLock) {
+            mTaskCount++;
+        }
+    }
+
+    private void decrement() {
+        synchronized (mCountLock) {
+            mTaskCount--;
+            if (mTaskCount == 0) {
+                onIdle();
+                mCountLock.notifyAll();
+            }
+        }
+    }
+
+    /**
+     * Called when the number of awaiting tasks reaches to 0.
+     *
+     * @see #isIdle()
+     */
+    protected void onIdle() {
+
+    }
+
+    /**
+     * Returns false if there are tasks waiting to be executed, true otherwise.
+     *
+     * @return False if there are tasks waiting to be executed, true otherwise.
+     *
+     * @see #onIdle()
+     */
+    public boolean isIdle() {
+        synchronized (mCountLock) {
+            return mTaskCount == 0;
+        }
+    }
+
+    /**
+     * Waits until all active tasks are finished.
+     *
+     * @param time The duration to wait
+     * @param timeUnit The time unit for the {@code time} parameter
+     *
+     * @throws InterruptedException If thread is interrupted while waiting
+     * @throws TimeoutException If tasks cannot be drained at the given time
+     */
+    public void drainTasks(int time, TimeUnit timeUnit)
+            throws InterruptedException, TimeoutException {
+        long end = SystemClock.uptimeMillis() + timeUnit.toMillis(time);
+        synchronized (mCountLock) {
+            while (mTaskCount != 0) {
+                long now = SystemClock.uptimeMillis();
+                long remaining = end - now;
+                if (remaining > 0) {
+                    mCountLock.wait(remaining);
+                } else {
+                    throw new TimeoutException("could not drain tasks");
+                }
+            }
+        }
+    }
+
+    class CountingRunnable implements Runnable {
+        final Runnable mWrapped;
+
+        CountingRunnable(Runnable wrapped) {
+            mWrapped = wrapped;
+            increment();
+        }
+
+        @Override
+        public void run() {
+            try {
+                mWrapped.run();
+            } finally {
+                decrement();
+            }
+        }
+    }
+}
diff --git a/app-toolkit/core-testing/src/main/java/android/arch/core/executor/InstantTaskExecutorRule.java b/app-toolkit/core-testing/src/main/java/android/arch/core/executor/InstantTaskExecutorRule.java
new file mode 100644
index 0000000..2c59927
--- /dev/null
+++ b/app-toolkit/core-testing/src/main/java/android/arch/core/executor/InstantTaskExecutorRule.java
@@ -0,0 +1,55 @@
+/*
+ * 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 android.arch.core.executor;
+
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+/**
+ * A JUnit Test Rule that swaps the background executor used by the Architecture Components with a
+ * different one which executes each task synchronously.
+ * <p>
+ * You can use this rule for your host side tests that use Architecture Components.
+ */
+public class InstantTaskExecutorRule extends TestWatcher {
+    @Override
+    protected void starting(Description description) {
+        super.starting(description);
+        AppToolkitTaskExecutor.getInstance().setDelegate(new TaskExecutor() {
+            @Override
+            public void executeOnDiskIO(Runnable runnable) {
+                runnable.run();
+            }
+
+            @Override
+            public void postToMainThread(Runnable runnable) {
+                runnable.run();
+            }
+
+            @Override
+            public boolean isMainThread() {
+                return true;
+            }
+        });
+    }
+
+    @Override
+    protected void finished(Description description) {
+        super.finished(description);
+        AppToolkitTaskExecutor.getInstance().setDelegate(null);
+    }
+}
diff --git a/app-toolkit/core-testing/src/test/java/android/arch/core/executor/InstantTaskExecutorRuleTest.java b/app-toolkit/core-testing/src/test/java/android/arch/core/executor/InstantTaskExecutorRuleTest.java
new file mode 100644
index 0000000..e42bf1c3
--- /dev/null
+++ b/app-toolkit/core-testing/src/test/java/android/arch/core/executor/InstantTaskExecutorRuleTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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 android.arch.core.executor;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+@RunWith(JUnit4.class)
+public class InstantTaskExecutorRuleTest {
+    @Rule
+    public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
+
+    @Test
+    public void executeOnMain() throws ExecutionException, InterruptedException, TimeoutException {
+        final Thread current = Thread.currentThread();
+        FutureTask<Void> check = new FutureTask<>(new Callable<Void>() {
+            @Override
+            public Void call() throws Exception {
+                assertTrue(Thread.currentThread() == current);
+                return null;
+            }
+        });
+        AppToolkitTaskExecutor.getInstance().executeOnMainThread(check);
+        check.get(1, TimeUnit.SECONDS);
+    }
+
+    @Test
+    public void executeOnIO() throws ExecutionException, InterruptedException, TimeoutException {
+        final Thread current = Thread.currentThread();
+        FutureTask<Void> check = new FutureTask<>(new Callable<Void>() {
+            @Override
+            public Void call() throws Exception {
+                assertTrue(Thread.currentThread() == current);
+                return null;
+            }
+        });
+        AppToolkitTaskExecutor.getInstance().executeOnDiskIO(check);
+        check.get(1, TimeUnit.SECONDS);
+    }
+}
diff --git a/app-toolkit/core/src/main/java/android/arch/core/executor/AppToolkitTaskExecutor.java b/app-toolkit/core/src/main/java/android/arch/core/executor/AppToolkitTaskExecutor.java
index 9b60c86..29d9c1b 100644
--- a/app-toolkit/core/src/main/java/android/arch/core/executor/AppToolkitTaskExecutor.java
+++ b/app-toolkit/core/src/main/java/android/arch/core/executor/AppToolkitTaskExecutor.java
@@ -16,15 +16,10 @@
 
 package android.arch.core.executor;
 
-import android.os.Handler;
-import android.os.Looper;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.annotation.RestrictTo;
 
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
 /**
  * A static class that serves as a central point to execute common tasks.
  * <p>
@@ -42,36 +37,7 @@
     private TaskExecutor mDefaultTaskExecutor;
 
     private AppToolkitTaskExecutor() {
-        mDefaultTaskExecutor = new TaskExecutor() {
-            private final Object mLock = new Object();
-            private ExecutorService mDiskIO = Executors.newFixedThreadPool(2);
-
-            @Nullable
-            private volatile Handler mMainHandler;
-
-            @Override
-            public void executeOnDiskIO(Runnable runnable) {
-                mDiskIO.execute(runnable);
-            }
-
-            @Override
-            public void postToMainThread(Runnable runnable) {
-                if (mMainHandler == null) {
-                    synchronized (mLock) {
-                        if (mMainHandler == null) {
-                            mMainHandler = new Handler(Looper.getMainLooper());
-                        }
-                    }
-                }
-                //noinspection ConstantConditions
-                mMainHandler.post(runnable);
-            }
-
-            @Override
-            public boolean isMainThread() {
-                return Looper.getMainLooper().getThread() == Thread.currentThread();
-            }
-        };
+        mDefaultTaskExecutor = new DefaultTaskExecutor();
         mDelegate = mDefaultTaskExecutor;
     }
 
diff --git a/app-toolkit/core/src/main/java/android/arch/core/executor/DefaultTaskExecutor.java b/app-toolkit/core/src/main/java/android/arch/core/executor/DefaultTaskExecutor.java
new file mode 100644
index 0000000..f6ce7d3
--- /dev/null
+++ b/app-toolkit/core/src/main/java/android/arch/core/executor/DefaultTaskExecutor.java
@@ -0,0 +1,55 @@
+/*
+ * 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 android.arch.core.executor;
+
+import android.os.Handler;
+import android.os.Looper;
+import android.support.annotation.Nullable;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+class DefaultTaskExecutor extends TaskExecutor {
+    private final Object mLock = new Object();
+    private ExecutorService mDiskIO = Executors.newFixedThreadPool(2);
+
+    @Nullable
+    private volatile Handler mMainHandler;
+
+    @Override
+    public void executeOnDiskIO(Runnable runnable) {
+        mDiskIO.execute(runnable);
+    }
+
+    @Override
+    public void postToMainThread(Runnable runnable) {
+        if (mMainHandler == null) {
+            synchronized (mLock) {
+                if (mMainHandler == null) {
+                    mMainHandler = new Handler(Looper.getMainLooper());
+                }
+            }
+        }
+        //noinspection ConstantConditions
+        mMainHandler.post(runnable);
+    }
+
+    @Override
+    public boolean isMainThread() {
+        return Looper.getMainLooper().getThread() == Thread.currentThread();
+    }
+}