|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.appdistribution.impl; |
| 16 | + |
| 17 | +import androidx.annotation.NonNull; |
| 18 | +import com.google.firebase.appdistribution.OnProgressListener; |
| 19 | +import com.google.firebase.appdistribution.UpdateProgress; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +/** |
| 26 | + * Helper listener that awaits a specific number of progress events on a {@code UpdateTask}. |
| 27 | + * |
| 28 | + * <p>This works around a limitation of the Tasks API where await() cannot be called on the main |
| 29 | + * thread. This listener works around it by running itself on a different thread, thus allowing the |
| 30 | + * main thread to be woken up when the Tasks complete. |
| 31 | + * |
| 32 | + * <p>Note: Calling {@link #await()} from a Robolectric test does block the main thread, since those |
| 33 | + * tests are executed on the main thread. |
| 34 | + */ |
| 35 | +class TestOnProgressListener implements OnProgressListener { |
| 36 | + private static final long TIMEOUT_MS = 5000; |
| 37 | + private final int expectedProgressCount; |
| 38 | + private final CountDownLatch latch; |
| 39 | + private final List<UpdateProgress> progressUpdates = new ArrayList<>(); |
| 40 | + |
| 41 | + TestOnProgressListener(int expectedProgressCount) { |
| 42 | + this.expectedProgressCount = expectedProgressCount; |
| 43 | + this.latch = new CountDownLatch(expectedProgressCount); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onProgressUpdate(@NonNull UpdateProgress updateProgress) { |
| 48 | + progressUpdates.add(updateProgress); |
| 49 | + latch.countDown(); |
| 50 | + } |
| 51 | + |
| 52 | + /** Blocks until the {@link #onProgressUpdate} is called the expected number of times. */ |
| 53 | + List<UpdateProgress> await() throws InterruptedException { |
| 54 | + if (!latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) { |
| 55 | + throw new InterruptedException( |
| 56 | + String.format( |
| 57 | + "Timed out waiting for progress events (expected = %d, actual = %d)", |
| 58 | + expectedProgressCount, progressUpdates.size())); |
| 59 | + } |
| 60 | + return progressUpdates; |
| 61 | + } |
| 62 | +} |
0 commit comments