Search in sources :

Example 6 with FileDownloader

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);
    }
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FileDownloader(com.intellij.util.download.FileDownloader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) File(java.io.File)

Example 7 with FileDownloader

use of com.intellij.util.download.FileDownloader in project android by JetBrains.

the class DistributionServiceTest method testCache.

/**
   * Test that we don't download on every request
   *
   * @throws Exception
   */
public void testCache() 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);
    service.getSupportedDistributionForApiLevel(19);
    service.getDistributionForApiLevel(21);
    Mockito.verify(downloader).download(Matchers.any(File.class));
}
Also used : FileDownloader(com.intellij.util.download.FileDownloader) File(java.io.File)

Example 8 with FileDownloader

use of com.intellij.util.download.FileDownloader in project android by JetBrains.

the class DistributionServiceTest method testFailure.

/**
   * Test that the failure callback will be called if the download fails, and then the fallback data will be used.
   */
public void testFailure() throws Exception {
    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(() -> {
        assert false;
    }, () -> result.set(true));
    assertTrue(result.get(5, TimeUnit.SECONDS));
    assertEquals(0.4, service.getSupportedDistributionForApiLevel(17), 0.001);
}
Also used : FutureResult(com.intellij.util.concurrency.FutureResult) FileDownloader(com.intellij.util.download.FileDownloader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File)

Aggregations

FileDownloader (com.intellij.util.download.FileDownloader)8 File (java.io.File)6 DownloadableFileDescription (com.intellij.util.download.DownloadableFileDescription)4 List (java.util.List)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ImmutableList (com.google.common.collect.ImmutableList)2 FutureResult (com.intellij.util.concurrency.FutureResult)2 Semaphore (com.intellij.util.concurrency.Semaphore)2 IOException (java.io.IOException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ExecutionException (com.intellij.execution.ExecutionException)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ActionLink (com.intellij.ui.components.labels.ActionLink)1 DownloadableFileService (com.intellij.util.download.DownloadableFileService)1