Bug 1237576 - DownloadAction.getDestinationFile(): Create directory if needed. r?rnewman draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Mon, 11 Jan 2016 17:44:30 +0100
changeset 320545 f1515fac00a5c0637e65cac4a2bb40f65fb61fce
parent 320542 15c5eb70ba7385270f1caa8a94746fd1320d4b8a
child 320546 2f7226e0a4e745e5aaa5efebd097545f61cf98f3
push id9224
push users.kaspari@gmail.com
push dateMon, 11 Jan 2016 18:26:09 +0000
reviewersrnewman
bugs1237576
milestone46.0a1
Bug 1237576 - DownloadAction.getDestinationFile(): Create directory if needed. r?rnewman
mobile/android/base/java/org/mozilla/gecko/dlc/BaseAction.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/dlc/TestDownloadAction.java
--- a/mobile/android/base/java/org/mozilla/gecko/dlc/BaseAction.java
+++ b/mobile/android/base/java/org/mozilla/gecko/dlc/BaseAction.java
@@ -80,19 +80,27 @@ public abstract class BaseAction {
 
         public UnrecoverableDownloadContentException(Throwable cause) {
             super(cause);
         }
     }
 
     public abstract void perform(Context context, DownloadContentCatalog catalog);
 
-    protected File getDestinationFile(Context context, DownloadContent content) throws UnrecoverableDownloadContentException {
+    protected File getDestinationFile(Context context, DownloadContent content)
+            throws UnrecoverableDownloadContentException, RecoverableDownloadContentException {
         if (content.isFont()) {
-            return new File(new File(context.getApplicationInfo().dataDir, "fonts"), content.getFilename());
+            File destinationDirectory = new File(context.getApplicationInfo().dataDir, "fonts");
+
+            if (!destinationDirectory.exists() && !destinationDirectory.mkdirs()) {
+                throw new RecoverableDownloadContentException(RecoverableDownloadContentException.DISK_IO,
+                        "Destination directory does not exist and cannot be created");
+            }
+
+            return new File(destinationDirectory, content.getFilename());
         }
 
         // Unrecoverable: We downloaded a file and we don't know what to do with it (Should not happen)
         throw new UnrecoverableDownloadContentException("Can't determine destination for kind: " + content.getKind());
     }
 
     protected boolean verify(File file, String expectedChecksum)
             throws RecoverableDownloadContentException, UnrecoverableDownloadContentException {
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/dlc/TestDownloadAction.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/dlc/TestDownloadAction.java
@@ -384,49 +384,49 @@ public class TestDownloadAction {
 
     /**
      * Scenario: Not enough storage space for temporary file available.
      *
      * Verify that:
      *  * hasEnoughDiskSpace() returns false
      */
     @Test
-    public void testWithNotEnoughSpaceForTemporaryFile() {
+    public void testWithNotEnoughSpaceForTemporaryFile() throws Exception{
         DownloadContent content = createFontWithSize(2048);
         File destinationFile = mockNotExistingFile();
         File temporaryFile = mockNotExistingFileWithUsableSpace(1024);
 
         DownloadAction action = new DownloadAction(null);
         Assert.assertFalse(action.hasEnoughDiskSpace(content, destinationFile, temporaryFile));
     }
 
     /**
      * Scenario: Not enough storage space for destination file available.
      *
      * Verify that:
      *  * hasEnoughDiskSpace() returns false
      */
     @Test
-    public void testWithNotEnoughSpaceForDestinationFile() {
+    public void testWithNotEnoughSpaceForDestinationFile() throws Exception {
         DownloadContent content = createFontWithSize(2048);
         File destinationFile = mockNotExistingFileWithUsableSpace(1024);
         File temporaryFile = mockNotExistingFile();
 
         DownloadAction action = new DownloadAction(null);
         Assert.assertFalse(action.hasEnoughDiskSpace(content, destinationFile, temporaryFile));
     }
 
     /**
      * Scenario: Enough storage space for temporary and destination file available.
      *
      * Verify that:
      *  * hasEnoughDiskSpace() returns true
      */
     @Test
-    public void testWithEnoughSpaceForEverything() {
+    public void testWithEnoughSpaceForEverything() throws Exception {
         DownloadContent content = createFontWithSize(2048);
         File destinationFile = mockNotExistingFileWithUsableSpace(4096);
         File temporaryFile = mockNotExistingFileWithUsableSpace(4096);
 
         DownloadAction action = new DownloadAction(null);
         Assert.assertTrue(action.hasEnoughDiskSpace(content, destinationFile, temporaryFile));
     }