Bug 1237576 - DownloadAction.getDestinationFile(): Create directory if needed. r?rnewman
--- 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));
}