blob: 52ab80493bdefc54485924654c3594c2f53295cd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*
* 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.
*
* <p>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 <internal> for additional information on
* providing data files for tests.
*
* <p>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<Void> startDownloading(DownloadRequest downloadRequest) {
return executionSequencer.submitAsync(
() -> startDownloadingInternal(downloadRequest), executor);
}
private ListenableFuture<Void> 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());
}
}
|