Home | History | Annotate | Download | only in services
      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.support.annotation.Nullable;
     23 
     24 import com.android.documentsui.model.DocumentInfo;
     25 
     26 import java.util.ArrayList;
     27 import java.util.List;
     28 import java.util.concurrent.CountDownLatch;
     29 import java.util.concurrent.TimeUnit;
     30 
     31 public 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 finished;
     37 
     38     @Override
     39     public void onStart(Job job) {
     40         started = job;
     41     }
     42 
     43     @Override
     44     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() {
     67         if (finished == null || !finished.hasFailures()) {
     68             fail("Job didn't fail. onFailed never called.");
     69         }
     70     }
     71 
     72     public void assertFilesFailed(ArrayList<String> names) {
     73         if (finished == null || !finished.hasFailures()) {
     74             fail("Can't test failed documetns. Job didn't fail.");
     75         }
     76 
     77         assertEquals(finished.failedFiles.size(), names.size());
     78         for (String name : names) {
     79             assertFileFailed(name);
     80         }
     81     }
     82 
     83     public void assertFileFailed(String name) {
     84         if (finished == null || !finished.hasFailures()) {
     85             fail("Can't test failed documetns. Job didn't fail.");
     86         }
     87 
     88         for (DocumentInfo failed : finished.failedFiles) {
     89             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 }
    115