blob: 01101970519cce5652b50739bc97d66ba745542a [file] [log] [blame]
Steve McKaybbeba522016-01-13 17:17:39 -08001/*
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 org.junit.Assert.assertEquals;
20import static org.junit.Assert.fail;
21
22import android.support.annotation.Nullable;
23
24import com.android.documentsui.model.DocumentInfo;
25
26import java.util.ArrayList;
27import java.util.List;
28import java.util.concurrent.CountDownLatch;
29import java.util.concurrent.TimeUnit;
30
31public class TestJobListener implements Job.Listener {
32
33 private final CountDownLatch latch = new CountDownLatch(1);
34 private final List<Job> progress = new ArrayList<>();
35 @Nullable private Job started;
36 @Nullable private Job failed;
37 @Nullable private Job finished;
38
39 @Override
40 public void onStart(Job job) {
41 started = job;
42 }
43
44 @Override
45 public void onFailed(Job job) {
46 failed = job;
47 }
48
49 @Override
50 public void onFinished(Job job) {
51 this.finished = job;
52 latch.countDown();
53 }
54
55 @Override
56 public void onProgress(CopyJob job) {
57 progress.add(job);
58 }
59
60 public void assertStarted() {
61 if (started == null) {
62 fail("Job didn't start. onStart never called.");
63 }
64 }
65
66 public void assertFinished() {
67 if (finished == null) {
68 fail("Job didn't finish. onFinish never called.");
69 }
70 }
71
72 public void assertFailed() {
73 if (failed == null) {
74 fail("Job didn't fail. onFailed never called.");
75 }
76 }
77
78 public void assertFilesFailed(ArrayList<String> names) {
79 if (failed == null) {
80 fail("Can't test failed documetns. Job didn't fail.");
81 }
82
83 assertEquals(failed.failedFiles.size(), names.size());
84 for (String name : names) {
85 assertFileFailed(name);
86 }
87 }
88
89 public void assertFileFailed(String name) {
90 if (failed == null) {
91 fail("Can't test failed documetns. Job didn't fail.");
92 }
93
94 for (DocumentInfo failed : failed.failedFiles) {
95 if (name.equals(failed.displayName)) {
96 return;
97 }
98 }
99 fail("Couldn't find failed file: " + name);
100 }
101
102 public void assertCanceled() {
103 if (finished == null) {
104 fail("Can't determine if job was canceled. Job didn't finish.");
105 }
106 if (!finished.isCanceled()) {
107 fail("Job wasn't canceled. Job#isCanceled returned false.");
108 }
109 }
110
111 public void assertMadeProgress() {
112 if (progress.isEmpty()) {
113 fail("Job made no progress. onProgress never called.");
114 }
115 }
116
117 public void waitForFinished() throws InterruptedException {
118 latch.await(500, TimeUnit.MILLISECONDS);
119 }
120}