use of com.intellij.util.download.FileDownloader in project android by JetBrains.
the class DistributionService method getInstance.
public static DistributionService getInstance() {
if (ourInstance == null) {
DownloadableFileDescription description = DownloadableFileService.getInstance().createFileDescription(STATS_URL, DOWNLOAD_FILENAME);
FileDownloader downloader = DownloadableFileService.getInstance().createDownloader(ImmutableList.of(description), "Distribution Stats");
ourInstance = new DistributionService(downloader, CACHE_PATH, FALLBACK_URL);
}
return ourInstance;
}
use of com.intellij.util.download.FileDownloader in project android by JetBrains.
the class DistributionServiceTest method testFallbackToPrevious.
/**
* Test that we pick up previously-downloaded distributions if a new one can't be loaded.
*/
public void testFallbackToPrevious() throws Exception {
File newFile = new File(CACHE_PATH, "distributions_2.json");
FileUtil.copy(new File(new File(myFixture.getTestDataPath(), DISTRIBUTION_PATH), "testPreviousDistributions.json"), newFile);
if (!newFile.setLastModified(20000)) {
fail();
}
File oldFile = new File(CACHE_PATH, "distributions.json");
FileUtil.copy(new File(new File(myFixture.getTestDataPath(), DISTRIBUTION_PATH), "testPreviousDistributions2.json"), oldFile);
if (!oldFile.setLastModified(10000)) {
fail();
}
FileDownloader downloader = Mockito.mock(FileDownloader.class);
Mockito.when(downloader.download(Matchers.any(File.class))).thenThrow(new RuntimeException("expected exception"));
DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
final FutureResult<Boolean> result = new FutureResult<>();
service.refresh(() -> result.set(true), () -> {
assert false;
});
assertTrue(result.get(5, TimeUnit.SECONDS));
assertEquals(.3, service.getSupportedDistributionForApiLevel(16), 0.0001);
}
use of com.intellij.util.download.FileDownloader in project android by JetBrains.
the class DistributionServiceTest method testSimpleCase.
/**
* Test that we get back the correct sum for an api level
*/
public void testSimpleCase() throws Exception {
FileDownloader downloader = Mockito.mock(FileDownloader.class);
Mockito.when(downloader.download(Matchers.any(File.class))).thenReturn(ImmutableList.of(Pair.create(myDistributionFile, myDescription)));
DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
assertEquals(0.7, service.getSupportedDistributionForApiLevel(16), 0.0001);
}
use of com.intellij.util.download.FileDownloader in project android by JetBrains.
the class DistributionServiceTest method testAsync.
/**
* Test that refresh will run asynchronously.
*/
public void testAsync() throws Exception {
final FileDownloader downloader = Mockito.mock(FileDownloader.class);
final Semaphore s = new Semaphore();
s.down();
Mockito.when(downloader.download(Matchers.any(File.class))).thenAnswer(new Answer<List<Pair<File, DownloadableFileDescription>>>() {
@Override
public List<Pair<File, DownloadableFileDescription>> answer(InvocationOnMock invocation) throws Throwable {
assertTrue(s.waitFor(5000));
return ImmutableList.of(Pair.create(myDistributionFile, myDescription));
}
});
final DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
service.refresh(() -> {
service.getSupportedDistributionForApiLevel(19);
service.getDistributionForApiLevel(21);
try {
Mockito.verify(downloader).download(Matchers.any(File.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}, null);
s.up();
}
use of com.intellij.util.download.FileDownloader in project android by JetBrains.
the class DistributionServiceTest method testAsync2.
/**
* Test that if we get another call to refresh while one is in progress, it's callbacks will be queued.
*/
public void testAsync2() throws Exception {
final FileDownloader downloader = Mockito.mock(FileDownloader.class);
final Semaphore s = new Semaphore();
s.down();
final Semaphore s2 = new Semaphore();
s2.down();
Mockito.when(downloader.download(Matchers.any(File.class))).thenAnswer(new Answer<List<Pair<File, DownloadableFileDescription>>>() {
@Override
public List<Pair<File, DownloadableFileDescription>> answer(InvocationOnMock invocation) throws Throwable {
assertTrue(s.waitFor(5000));
return ImmutableList.of(Pair.create(myDistributionFile, myDescription));
}
});
DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
final AtomicBoolean check = new AtomicBoolean(false);
service.refresh(() -> check.set(true), null);
service.refresh(() -> {
assertTrue(check.get());
s2.up();
}, null);
s.up();
assertTrue(s2.waitFor(5000));
service.getSupportedDistributionForApiLevel(19);
try {
Mockito.verify(downloader).download(Matchers.any(File.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations