blob: 46b093d4f24a53e9d7dd1ac79404bdc017605ca2 [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;
Steve McKaybbeba522016-01-13 17:17:39 -080036 @Nullable private Job finished;
37
38 @Override
39 public void onStart(Job job) {
40 started = job;
41 }
42
43 @Override
Steve McKaybbeba522016-01-13 17:17:39 -080044 public void onFinished(Job job) {
45 this.finished = job;
46 latch.countDown();
47 }
48
49 @Override
50 public void onProgress(CopyJob job) {
51 progress.add(job);
52 }
53
54 public void assertStarted() {
55 if (started == null) {
56 fail("Job didn't start. onStart never called.");
57 }
58 }
59
60 public void assertFinished() {
61 if (finished == null) {
62 fail("Job didn't finish. onFinish never called.");
63 }
64 }
65
66 public void assertFailed() {
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090067 if (finished == null || !finished.hasFailures()) {
Steve McKaybbeba522016-01-13 17:17:39 -080068 fail("Job didn't fail. onFailed never called.");
69 }
70 }
71
72 public void assertFilesFailed(ArrayList<String> names) {
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090073 if (finished == null || !finished.hasFailures()) {
Steve McKaybbeba522016-01-13 17:17:39 -080074 fail("Can't test failed documetns. Job didn't fail.");
75 }
76
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090077 assertEquals(finished.failedFiles.size(), names.size());
Steve McKaybbeba522016-01-13 17:17:39 -080078 for (String name : names) {
79 assertFileFailed(name);
80 }
81 }
82
83 public void assertFileFailed(String name) {
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090084 if (finished == null || !finished.hasFailures()) {
Steve McKaybbeba522016-01-13 17:17:39 -080085 fail("Can't test failed documetns. Job didn't fail.");
86 }
87
Tomasz Mikolajewskidd2b31c2016-01-22 16:22:51 +090088 for (DocumentInfo failed : finished.failedFiles) {
Steve McKaybbeba522016-01-13 17:17:39 -080089 if (name.equals(failed.displayName)) {
90 return;
91 }
92 }
93 fail("Couldn't find failed file: " + name);
94 }
95
96 public void assertCanceled() {
97 if (finished == null) {
98 fail("Can't determine if job was canceled. Job didn't finish.");
99 }
100 if (!finished.isCanceled()) {
101 fail("Job wasn't canceled. Job#isCanceled returned false.");
102 }
103 }
104
105 public void assertMadeProgress() {
106 if (progress.isEmpty()) {
107 fail("Job made no progress. onProgress never called.");
108 }
109 }
110
111 public void waitForFinished() throws InterruptedException {
112 latch.await(500, TimeUnit.MILLISECONDS);
113 }
114}