|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | + * https://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 | +package androidx.media3.test.utils.robolectric; |
| 17 | + |
| 18 | +import static androidx.media3.test.utils.robolectric.RobolectricUtil.createRobolectricConditionVariable; |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import androidx.annotation.Nullable; |
| 22 | +import androidx.media3.common.C; |
| 23 | +import androidx.media3.common.StreamKey; |
| 24 | +import androidx.media3.common.util.ConditionVariable; |
| 25 | +import androidx.media3.common.util.UnstableApi; |
| 26 | +import androidx.media3.exoplayer.offline.DownloadRequest; |
| 27 | +import androidx.media3.exoplayer.offline.Downloader; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
| 30 | + |
| 31 | +/** A fake {@link Downloader}. */ |
| 32 | +@UnstableApi |
| 33 | +public class FakeDownloader implements Downloader { |
| 34 | + |
| 35 | + /** Timeout to use when blocking on conditions that we expect to become unblocked. */ |
| 36 | + private static final int TIMEOUT_MS = 10_000; |
| 37 | + |
| 38 | + private final DownloadRequest request; |
| 39 | + private final ConditionVariable downloadStarted; |
| 40 | + private final ConditionVariable removeStarted; |
| 41 | + private final ConditionVariable finished; |
| 42 | + private final ConditionVariable blocker; |
| 43 | + private final AtomicInteger startCount; |
| 44 | + private final AtomicInteger bytesDownloaded; |
| 45 | + |
| 46 | + private volatile boolean canceled; |
| 47 | + private volatile boolean enableDownloadIoException; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a {@link FakeDownloader}. |
| 51 | + * |
| 52 | + * @param request The {@linkplain DownloadRequest download request}. |
| 53 | + */ |
| 54 | + public FakeDownloader(DownloadRequest request) { |
| 55 | + this.request = request; |
| 56 | + downloadStarted = createRobolectricConditionVariable(); |
| 57 | + removeStarted = createRobolectricConditionVariable(); |
| 58 | + finished = createRobolectricConditionVariable(); |
| 59 | + blocker = createRobolectricConditionVariable(); |
| 60 | + startCount = new AtomicInteger(); |
| 61 | + bytesDownloaded = new AtomicInteger(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void cancel() { |
| 66 | + canceled = true; |
| 67 | + blocker.open(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void download(@Nullable ProgressListener progressListener) throws IOException { |
| 72 | + startCount.incrementAndGet(); |
| 73 | + downloadStarted.open(); |
| 74 | + try { |
| 75 | + block(); |
| 76 | + if (canceled) { |
| 77 | + return; |
| 78 | + } |
| 79 | + int bytesDownloaded = this.bytesDownloaded.get(); |
| 80 | + if (progressListener != null && bytesDownloaded > 0) { |
| 81 | + progressListener.onProgress(C.LENGTH_UNSET, bytesDownloaded, C.PERCENTAGE_UNSET); |
| 82 | + } |
| 83 | + if (enableDownloadIoException) { |
| 84 | + enableDownloadIoException = false; |
| 85 | + throw new IOException(); |
| 86 | + } |
| 87 | + } finally { |
| 88 | + finished.open(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void remove() { |
| 94 | + startCount.incrementAndGet(); |
| 95 | + removeStarted.open(); |
| 96 | + try { |
| 97 | + block(); |
| 98 | + } finally { |
| 99 | + finished.open(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** Finishes the {@link #download} or {@link #remove} without an error. */ |
| 104 | + public void finish() throws InterruptedException { |
| 105 | + blocker.open(); |
| 106 | + blockUntilFinished(); |
| 107 | + } |
| 108 | + |
| 109 | + /** Fails {@link #download} or {@link #remove} with an error. */ |
| 110 | + public void fail() throws InterruptedException { |
| 111 | + enableDownloadIoException = true; |
| 112 | + blocker.open(); |
| 113 | + blockUntilFinished(); |
| 114 | + } |
| 115 | + |
| 116 | + /** Increments the number of bytes that the fake downloader has downloaded. */ |
| 117 | + public void incrementBytesDownloaded() { |
| 118 | + bytesDownloaded.incrementAndGet(); |
| 119 | + } |
| 120 | + |
| 121 | + /** Asserts that the {@link FakeDownloader} instance was created with the given {@code id}. */ |
| 122 | + public void assertId(String id) { |
| 123 | + assertThat(request.id).isEqualTo(id); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Asserts that the {@link FakeDownloader} instance was created with the given {@code streamKeys}. |
| 128 | + */ |
| 129 | + public void assertStreamKeys(StreamKey... streamKeys) { |
| 130 | + assertThat(request.streamKeys).containsExactlyElementsIn(streamKeys); |
| 131 | + } |
| 132 | + |
| 133 | + /** Asserts that {@link #download} has started. */ |
| 134 | + public void assertDownloadStarted() throws InterruptedException { |
| 135 | + assertThat(downloadStarted.block(TIMEOUT_MS)).isTrue(); |
| 136 | + downloadStarted.close(); |
| 137 | + } |
| 138 | + |
| 139 | + /** Asserts that {@link #remove} has started. */ |
| 140 | + public void assertRemoveStarted() throws InterruptedException { |
| 141 | + assertThat(removeStarted.block(TIMEOUT_MS)).isTrue(); |
| 142 | + removeStarted.close(); |
| 143 | + } |
| 144 | + |
| 145 | + /** Asserts that {@link #download} or {@link #remove} has been cancelled. */ |
| 146 | + public void assertCanceled() throws InterruptedException { |
| 147 | + blockUntilFinished(); |
| 148 | + assertThat(canceled).isTrue(); |
| 149 | + } |
| 150 | + |
| 151 | + // Internal methods. |
| 152 | + |
| 153 | + private void block() { |
| 154 | + try { |
| 155 | + blocker.block(); |
| 156 | + } catch (InterruptedException e) { |
| 157 | + throw new IllegalStateException(e); // Never happens. |
| 158 | + } finally { |
| 159 | + blocker.close(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private void blockUntilFinished() throws InterruptedException { |
| 164 | + assertThat(finished.block(TIMEOUT_MS)).isTrue(); |
| 165 | + finished.close(); |
| 166 | + } |
| 167 | +} |
0 commit comments