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 org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 22 import android.net.Uri; 23 import androidx.annotation.Nullable; 24 25 import com.android.documentsui.base.DocumentInfo; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.TimeUnit; 31 32 public class TestJobListener implements Job.Listener { 33 34 private final CountDownLatch latch = new CountDownLatch(1); 35 private final List<Job> progress = new ArrayList<>(); 36 @Nullable private Job started; 37 @Nullable private Job finished; 38 39 @Override onStart(Job job)40 public void onStart(Job job) { 41 started = job; 42 } 43 44 @Override onFinished(Job job)45 public void onFinished(Job job) { 46 this.finished = job; 47 latch.countDown(); 48 } 49 assertStarted()50 public void assertStarted() { 51 if (started == null) { 52 fail("Job didn't start. onStart never called."); 53 } 54 } 55 assertFinished()56 public void assertFinished() { 57 if (finished == null) { 58 fail("Job didn't finish. onFinish never called."); 59 } 60 } 61 assertFailed()62 public void assertFailed() { 63 if (finished == null || !finished.hasFailures()) { 64 fail("Job didn't fail. onFailed never called."); 65 } 66 } 67 assertFilesFailed(List<String> names)68 public void assertFilesFailed(List<String> names) { 69 if (finished == null || !finished.hasFailures()) { 70 fail("Can't test failed documetns. Job didn't fail."); 71 } 72 73 assertEquals(finished.failedDocs.size(), names.size()); 74 for (String name : names) { 75 assertFileFailed(name); 76 } 77 } 78 assertFileFailed(String name)79 public void assertFileFailed(String name) { 80 if (finished == null || !finished.hasFailures()) { 81 fail("Can't test failed documetns. Job didn't fail."); 82 } 83 84 for (DocumentInfo failed : finished.failedDocs) { 85 if (name.equals(failed.displayName)) { 86 return; 87 } 88 } 89 fail("Couldn't find failed file: " + name); 90 } 91 assertUrisFailed(List<Uri> uris)92 public void assertUrisFailed(List<Uri> uris) { 93 if (finished == null || !finished.hasFailures()) { 94 fail("Can't test failed documetns. Job didn't fail."); 95 } 96 97 assertEquals(finished.failedDocs.size(), uris.size()); 98 for (Uri uri : uris) { 99 assertUriFailed(uri); 100 } 101 } 102 assertUriFailed(Uri uri)103 public void assertUriFailed(Uri uri) { 104 if (finished == null || !finished.hasFailures()) { 105 fail("Can't test failed documetns. Job didn't fail."); 106 } 107 108 for (Uri failed : finished.failedUris) { 109 if (uri.equals(failed)) { 110 return; 111 } 112 } 113 fail("Couldn't find failed uri: " + uri); 114 } 115 assertFailureCount(int expected)116 public void assertFailureCount(int expected) { 117 if (finished == null) { 118 fail("No job to test."); 119 } 120 121 assertEquals(expected, finished.failureCount); 122 } 123 assertCanceled()124 public void assertCanceled() { 125 if (finished == null) { 126 fail("Can't determine if job was canceled. Job didn't finish."); 127 } 128 if (!finished.isCanceled()) { 129 fail("Job wasn't canceled. Job#isCanceled returned false."); 130 } 131 } 132 assertMadeProgress()133 public void assertMadeProgress() { 134 if (progress.isEmpty()) { 135 fail("Job made no progress. onProgress never called."); 136 } 137 } 138 waitForFinished()139 public void waitForFinished() throws InterruptedException { 140 latch.await(500, TimeUnit.MILLISECONDS); 141 } 142 } 143