• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 com.android.documentsui.services;
18 
19 import static junit.framework.Assert.assertFalse;
20 import static junit.framework.Assert.fail;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.concurrent.Callable;
26 import java.util.concurrent.Delayed;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.ScheduledExecutorService;
30 import java.util.concurrent.ScheduledFuture;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33 
34 public class TestScheduledExecutorService implements ScheduledExecutorService {
35 
36     private List<TestFuture> scheduled = new ArrayList<>();
37     private boolean shutdown;
38 
39     @Override
shutdown()40     public void shutdown() {
41         this.shutdown = true;
42     }
43 
44     @Override
shutdownNow()45     public List<Runnable> shutdownNow() {
46         this.shutdown = true;
47         return new ArrayList<>();
48     }
49 
assertShutdown()50     void assertShutdown() {
51         if (!shutdown) {
52             fail("Executor wasn't shut down.");
53         }
54     }
55 
56     @Override
isShutdown()57     public boolean isShutdown() {
58         return shutdown;
59     }
60 
61     @Override
isTerminated()62     public boolean isTerminated() {
63         throw new UnsupportedOperationException();
64     }
65 
66     @Override
awaitTermination(long timeout, TimeUnit unit)67     public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
68         throw new UnsupportedOperationException();
69     }
70 
71     @Override
submit(Callable<T> task)72     public <T> Future<T> submit(Callable<T> task) {
73         throw new UnsupportedOperationException();
74     }
75 
76     @Override
submit(Runnable task, T result)77     public <T> Future<T> submit(Runnable task, T result) {
78         throw new UnsupportedOperationException();
79     }
80 
81     @Override
submit(Runnable task)82     public Future<?> submit(Runnable task) {
83         throw new UnsupportedOperationException();
84     }
85 
86     @Override
invokeAll(Collection<? extends Callable<T>> tasks)87     public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
88             throws InterruptedException {
89         throw new UnsupportedOperationException();
90     }
91 
92     @Override
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)93     public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout,
94             TimeUnit unit) throws InterruptedException {
95         throw new UnsupportedOperationException();
96     }
97 
98     @Override
invokeAny(Collection<? extends Callable<T>> tasks)99     public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
100             throws InterruptedException, ExecutionException {
101         throw new UnsupportedOperationException();
102     }
103 
104     @Override
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)105     public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
106             throws InterruptedException, ExecutionException, TimeoutException {
107         throw new UnsupportedOperationException();
108     }
109 
110     @Override
execute(Runnable command)111     public void execute(Runnable command) {
112         throw new UnsupportedOperationException();
113     }
114 
115     @Override
schedule(Runnable command, long delay, TimeUnit unit)116     public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
117         TestFuture future = new TestFuture(command, delay, unit);
118         scheduled.add(future);
119         return future;
120     }
121 
122     @Override
schedule(Callable<V> callable, long delay, TimeUnit unit)123     public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
124         throw new UnsupportedOperationException();
125     }
126 
127     @Override
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)128     public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period,
129             TimeUnit unit) {
130         throw new UnsupportedOperationException();
131     }
132 
133     @Override
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)134     public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay,
135             long delay, TimeUnit unit) {
136         throw new UnsupportedOperationException();
137     }
138 
runAll()139     void runAll() {
140         for (TestFuture future : scheduled) {
141             future.runnable.run();
142         }
143     }
144 
run(int taskIndex)145     void run(int taskIndex) {
146         scheduled.get(taskIndex).runnable.run();
147     }
148 
assertAlive()149     public void assertAlive() {
150         assertFalse(isShutdown());
151     }
152 
153     static class TestFuture implements ScheduledFuture<Void> {
154 
155         final Runnable runnable;
156         final long delay;
157         final TimeUnit unit;
158 
TestFuture(Runnable runnable, long delay, TimeUnit unit)159         public TestFuture(Runnable runnable, long delay, TimeUnit unit) {
160             this.runnable = runnable;
161             this.delay = delay;
162             this.unit = unit;
163         }
164 
165         @Override
getDelay(TimeUnit unit)166         public long getDelay(TimeUnit unit) {
167             return 0;
168         }
169 
170         @Override
compareTo(Delayed arg0)171         public int compareTo(Delayed arg0) {
172             return 0;
173         }
174 
175         @Override
cancel(boolean mayInterruptIfRunning)176         public boolean cancel(boolean mayInterruptIfRunning) {
177             return false;
178         }
179 
180         @Override
isCancelled()181         public boolean isCancelled() {
182             return false;
183         }
184 
185         @Override
isDone()186         public boolean isDone() {
187             return false;
188         }
189 
190         @Override
get()191         public Void get() throws InterruptedException, ExecutionException {
192             return null;
193         }
194 
195         @Override
get(long timeout, TimeUnit unit)196         public Void get(long timeout, TimeUnit unit)
197                 throws InterruptedException, ExecutionException, TimeoutException {
198             return null;
199         }
200     }
201 }
202