1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.testing; 17 18 import static com.google.common.util.concurrent.Futures.immediateVoidFuture; 19 20 import android.net.Uri; 21 import android.os.Environment; 22 import com.google.android.libraries.mobiledatadownload.downloader.DownloadConstraints; 23 import com.google.android.libraries.mobiledatadownload.downloader.DownloadRequest; 24 import com.google.android.libraries.mobiledatadownload.downloader.FileDownloader; 25 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 26 import com.google.android.libraries.mobiledatadownload.file.backends.FileUri; 27 import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil; 28 import com.google.common.util.concurrent.ListenableFuture; 29 import com.google.common.util.concurrent.ListeningExecutorService; 30 31 /** 32 * A {@link FileDownloader} that "downloads" by copying the file from the testdata folder. 33 * 34 * <p>The filename is the Last Path Segment of the urlToDownload. For example, the URL 35 * https://www.gstatic.com/icing/idd/sample_group/step1.txt will be mapped to the file 36 * testDataAbsolutePath/step1.txt. 37 * 38 * <p>Note that TestFileDownloader ignores the DownloadConditions. 39 */ 40 public final class TestFileDownloader implements FileDownloader { 41 42 private static final String TAG = "TestDataFileDownloader"; 43 44 private static final String GOOGLE3_ABSOLUTE_PATH = 45 Environment.getExternalStorageDirectory() + "/googletest/test_runfiles/google3/"; 46 47 private final String testDataAbsolutePath; 48 private final FileDownloader delegateDownloader; 49 TestFileDownloader( String testDataRelativePath, SynchronousFileStorage fileStorage, ListeningExecutorService executor)50 public TestFileDownloader( 51 String testDataRelativePath, 52 SynchronousFileStorage fileStorage, 53 ListeningExecutorService executor) { 54 this.testDataAbsolutePath = GOOGLE3_ABSOLUTE_PATH + testDataRelativePath; 55 this.delegateDownloader = new LocalFileDownloader(fileStorage, executor); 56 } 57 58 @Override startDownloading(DownloadRequest downloadRequest)59 public ListenableFuture<Void> startDownloading(DownloadRequest downloadRequest) { 60 Uri fileUri = downloadRequest.fileUri(); 61 String urlToDownload = downloadRequest.urlToDownload(); 62 DownloadConstraints downloadConstraints = downloadRequest.downloadConstraints(); 63 64 // We need to translate the real urlToDownload to the one representing the local file in 65 // testdata folder. 66 Uri uriToDownload = Uri.parse(urlToDownload.trim()); 67 if (uriToDownload == null) { 68 LogUtil.e("%s: Invalid urlToDownload %s", TAG, urlToDownload); 69 return immediateVoidFuture(); 70 } 71 72 String testDataUrl = 73 FileUri.builder() 74 .setPath(testDataAbsolutePath + uriToDownload.getLastPathSegment()) 75 .build() 76 .toString(); 77 78 return delegateDownloader.startDownloading( 79 DownloadRequest.newBuilder() 80 .setFileUri(fileUri) 81 .setUrlToDownload(testDataUrl) 82 .setDownloadConstraints(downloadConstraints) 83 .build()); 84 } 85 } 86