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