/* * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.libraries.mobiledatadownload.testing; import static com.google.common.util.concurrent.Futures.immediateFailedFuture; import static com.google.common.util.concurrent.Futures.immediateVoidFuture; import android.net.Uri; import com.google.android.libraries.mobiledatadownload.downloader.DownloadConstraints; import com.google.android.libraries.mobiledatadownload.downloader.DownloadRequest; import com.google.android.libraries.mobiledatadownload.downloader.FileDownloader; import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; import com.google.android.libraries.mobiledatadownload.file.backends.FileUri; import com.google.common.util.concurrent.ExecutionSequencer; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.devtools.build.runtime.RunfilesPaths; import java.io.IOException; import java.nio.file.Path; /** * A {@link FileDownloader} suitable for use in Robolectric tests that "downloads" by copying the * file from the testdata folder. * *

The filename is the Last Path Segment of the urlToDownload. For example, the URL * https://www.gstatic.com/icing/idd/sample_group/step1.txt will be mapped to the file * testSrcDirectory/testDataRelativePath/step1.txt. See for additional information on * providing data files for tests. * *

Note that TestFileDownloader ignores the DownloadConditions. */ public final class RobolectricFileDownloader implements FileDownloader { private final String testDataRelativePath; private final SynchronousFileStorage fileStorage; private final ListeningExecutorService executor; private final FileDownloader delegateDownloader; // Sequence downloads to prevent any potential overwrites private final ExecutionSequencer executionSequencer = ExecutionSequencer.create(); public RobolectricFileDownloader( String testDataRelativePath, SynchronousFileStorage fileStorage, ListeningExecutorService executor) { this.testDataRelativePath = testDataRelativePath; this.fileStorage = fileStorage; this.executor = executor; this.delegateDownloader = new LocalFileDownloader(fileStorage, executor); } @Override public ListenableFuture startDownloading(DownloadRequest downloadRequest) { return executionSequencer.submitAsync( () -> startDownloadingInternal(downloadRequest), executor); } private ListenableFuture startDownloadingInternal(DownloadRequest downloadRequest) { Uri fileUri = downloadRequest.fileUri(); String urlToDownload = downloadRequest.urlToDownload(); DownloadConstraints downloadConstraints = downloadRequest.downloadConstraints(); // If the file already exists, return immediately try { if (fileStorage.exists(fileUri)) { return immediateVoidFuture(); } } catch (IOException e) { return immediateFailedFuture(e); } // We need to translate the real urlToDownload to the one representing the local file in // testdata folder. Uri uriToDownload = Uri.parse(urlToDownload.trim()); if (uriToDownload == null) { return immediateVoidFuture(); } Path testDataPath = RunfilesPaths.resolve(testDataRelativePath); Path uriToDownloadPath = testDataPath.resolve(uriToDownload.getLastPathSegment()); String testDataUrl = FileUri.builder().setPath(uriToDownloadPath.toString()).build().toString(); return delegateDownloader.startDownloading( DownloadRequest.newBuilder() .setFileUri(fileUri) .setUrlToDownload(testDataUrl) .setDownloadConstraints(downloadConstraints) .build()); } }