1 /* 2 * Copyright (C) 2018 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.assertNull; 21 import static org.junit.Assert.assertTrue; 22 23 import android.app.Notification; 24 25 import com.android.documentsui.services.CopyJob.CopyJobProgressTracker; 26 27 import java.util.function.Function; 28 import java.util.function.LongSupplier; 29 30 class TestCopyJobProcessTracker<T extends CopyJobProgressTracker> { 31 private T mProcessTracker; 32 private Notification.Builder mProgressBuilder; 33 private final Function<Double, String> mProgressFormatter; 34 private final Function<Long, String> mRemainTimeFormatter; 35 36 private static class TestLongSupplier implements LongSupplier { 37 long mValue = 0; 38 boolean mCalled; 39 40 @Override getAsLong()41 public long getAsLong() { 42 mCalled = true; 43 return mValue; 44 } 45 } 46 private TestLongSupplier mTimeSupplier = new TestLongSupplier(); 47 TestCopyJobProcessTracker(Class<T> trackerClass, long requiredData, CopyJob job, Function<Double, String> progressFormatter, Function<Long, String> remainTimeFormatter)48 TestCopyJobProcessTracker(Class<T> trackerClass, 49 long requiredData, CopyJob job, Function<Double, String> progressFormatter, 50 Function<Long, String> remainTimeFormatter) throws Exception { 51 52 mProcessTracker = trackerClass.getDeclaredConstructor(long.class, 53 LongSupplier.class).newInstance(requiredData, mTimeSupplier); 54 55 mProgressBuilder = job.mProgressBuilder; 56 mProgressFormatter = progressFormatter; 57 mRemainTimeFormatter = remainTimeFormatter; 58 } 59 getProcessTracker()60 T getProcessTracker() { 61 return mProcessTracker; 62 } 63 assertProgressTrackStarted()64 void assertProgressTrackStarted() { 65 assertTrue(mTimeSupplier.mCalled); 66 } 67 assertStartedProgressEquals(int expectedProgress)68 void assertStartedProgressEquals(int expectedProgress) { 69 assertEquals(expectedProgress, (int) mProcessTracker.getProgress()); 70 } 71 assertStartedRemainingTimeEquals(long expectedRemainingTime)72 void assertStartedRemainingTimeEquals(long expectedRemainingTime) { 73 assertEquals(expectedRemainingTime, mProcessTracker.getRemainingTimeEstimate()); 74 } 75 updateProgressAndRemainingTime(long elapsedTime)76 void updateProgressAndRemainingTime(long elapsedTime) { 77 mTimeSupplier.mValue = elapsedTime; 78 mProcessTracker.update(mProgressBuilder, mRemainTimeFormatter); 79 } 80 assertProgressEquals(double progress)81 void assertProgressEquals(double progress) { 82 assertEquals(mProgressFormatter.apply(progress), 83 mProgressBuilder.build().extras.get(Notification.EXTRA_SUB_TEXT)); 84 } 85 assertReminingTimeEquals(long remainingTime)86 void assertReminingTimeEquals(long remainingTime) { 87 assertEquals(mRemainTimeFormatter.apply(remainingTime), 88 mProgressBuilder.build().extras.get(Notification.EXTRA_TEXT)); 89 } 90 assertNoRemainingTime()91 void assertNoRemainingTime() { 92 assertNull(mProgressBuilder.build().extras.get(Notification.EXTRA_TEXT)); 93 } 94 } 95