Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.arch.core.executor.testing;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import androidx.arch.core.executor.ArchTaskExecutor;
     22 
     23 import org.junit.Rule;
     24 import org.junit.Test;
     25 import org.junit.runner.RunWith;
     26 import org.junit.runners.JUnit4;
     27 
     28 import java.util.concurrent.Callable;
     29 import java.util.concurrent.ExecutionException;
     30 import java.util.concurrent.FutureTask;
     31 import java.util.concurrent.TimeUnit;
     32 import java.util.concurrent.TimeoutException;
     33 
     34 @RunWith(JUnit4.class)
     35 public class InstantTaskExecutorRuleTest {
     36     @Rule
     37     public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
     38 
     39     @Test
     40     public void executeOnMain() throws ExecutionException, InterruptedException, TimeoutException {
     41         final Thread current = Thread.currentThread();
     42         FutureTask<Void> check = new FutureTask<>(new Callable<Void>() {
     43             @Override
     44             public Void call() throws Exception {
     45                 assertTrue(Thread.currentThread() == current);
     46                 return null;
     47             }
     48         });
     49         ArchTaskExecutor.getInstance().executeOnMainThread(check);
     50         check.get(1, TimeUnit.SECONDS);
     51     }
     52 
     53     @Test
     54     public void executeOnIO() throws ExecutionException, InterruptedException, TimeoutException {
     55         final Thread current = Thread.currentThread();
     56         FutureTask<Void> check = new FutureTask<>(new Callable<Void>() {
     57             @Override
     58             public Void call() throws Exception {
     59                 assertTrue(Thread.currentThread() == current);
     60                 return null;
     61             }
     62         });
     63         ArchTaskExecutor.getInstance().executeOnDiskIO(check);
     64         check.get(1, TimeUnit.SECONDS);
     65     }
     66 }
     67