michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * Tests the functions located directly in the "Downloads" object. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Tests michael@0: michael@0: /** michael@0: * Tests that the createDownload function exists and can be called. More michael@0: * detailed tests are implemented separately for the DownloadCore module. michael@0: */ michael@0: add_task(function test_createDownload() michael@0: { michael@0: // Creates a simple Download object without starting the download. michael@0: yield Downloads.createDownload({ michael@0: source: { url: "about:blank" }, michael@0: target: { path: getTempFile(TEST_TARGET_FILE_NAME).path }, michael@0: saver: { type: "copy" }, michael@0: }); michael@0: }); michael@0: michael@0: /** michael@0: * Tests createDownload for private download. michael@0: */ michael@0: add_task(function test_createDownload_private() michael@0: { michael@0: let download = yield Downloads.createDownload({ michael@0: source: { url: "about:blank", isPrivate: true }, michael@0: target: { path: getTempFile(TEST_TARGET_FILE_NAME).path }, michael@0: saver: { type: "copy" } michael@0: }); michael@0: do_check_true(download.source.isPrivate); michael@0: }); michael@0: michael@0: /** michael@0: * Tests createDownload for normal (public) download. michael@0: */ michael@0: add_task(function test_createDownload_public() michael@0: { michael@0: let tempPath = getTempFile(TEST_TARGET_FILE_NAME).path; michael@0: let download = yield Downloads.createDownload({ michael@0: source: { url: "about:blank", isPrivate: false }, michael@0: target: { path: tempPath }, michael@0: saver: { type: "copy" } michael@0: }); michael@0: do_check_false(download.source.isPrivate); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: { url: "about:blank" }, michael@0: target: { path: tempPath }, michael@0: saver: { type: "copy" } michael@0: }); michael@0: do_check_false(download.source.isPrivate); michael@0: }); michael@0: michael@0: /** michael@0: * Tests "fetch" with nsIURI and nsIFile as arguments. michael@0: */ michael@0: add_task(function test_fetch_uri_file_arguments() michael@0: { michael@0: let targetFile = getTempFile(TEST_TARGET_FILE_NAME); michael@0: yield Downloads.fetch(NetUtil.newURI(httpUrl("source.txt")), targetFile); michael@0: yield promiseVerifyContents(targetFile.path, TEST_DATA_SHORT); michael@0: }); michael@0: michael@0: /** michael@0: * Tests "fetch" with DownloadSource and DownloadTarget as arguments. michael@0: */ michael@0: add_task(function test_fetch_object_arguments() michael@0: { michael@0: let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; michael@0: yield Downloads.fetch({ url: httpUrl("source.txt") }, { path: targetPath }); michael@0: yield promiseVerifyContents(targetPath, TEST_DATA_SHORT); michael@0: }); michael@0: michael@0: /** michael@0: * Tests "fetch" with string arguments. michael@0: */ michael@0: add_task(function test_fetch_string_arguments() michael@0: { michael@0: let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; michael@0: yield Downloads.fetch(httpUrl("source.txt"), targetPath); michael@0: yield promiseVerifyContents(targetPath, TEST_DATA_SHORT); michael@0: michael@0: targetPath = getTempFile(TEST_TARGET_FILE_NAME).path; michael@0: yield Downloads.fetch(new String(httpUrl("source.txt")), michael@0: new String(targetPath)); michael@0: yield promiseVerifyContents(targetPath, TEST_DATA_SHORT); michael@0: }); michael@0: michael@0: /** michael@0: * Tests that the getList function returns the same list when called multiple michael@0: * times with the same argument, but returns different lists when called with michael@0: * different arguments. More detailed tests are implemented separately for the michael@0: * DownloadList module. michael@0: */ michael@0: add_task(function test_getList() michael@0: { michael@0: let publicListOne = yield Downloads.getList(Downloads.PUBLIC); michael@0: let privateListOne = yield Downloads.getList(Downloads.PRIVATE); michael@0: michael@0: let publicListTwo = yield Downloads.getList(Downloads.PUBLIC); michael@0: let privateListTwo = yield Downloads.getList(Downloads.PRIVATE); michael@0: michael@0: do_check_eq(publicListOne, publicListTwo); michael@0: do_check_eq(privateListOne, privateListTwo); michael@0: michael@0: do_check_neq(publicListOne, privateListOne); michael@0: }); michael@0: michael@0: /** michael@0: * Tests that the getSummary function returns the same summary when called michael@0: * multiple times with the same argument, but returns different summaries when michael@0: * called with different arguments. More detailed tests are implemented michael@0: * separately for the DownloadSummary object in the DownloadList module. michael@0: */ michael@0: add_task(function test_getSummary() michael@0: { michael@0: let publicSummaryOne = yield Downloads.getSummary(Downloads.PUBLIC); michael@0: let privateSummaryOne = yield Downloads.getSummary(Downloads.PRIVATE); michael@0: michael@0: let publicSummaryTwo = yield Downloads.getSummary(Downloads.PUBLIC); michael@0: let privateSummaryTwo = yield Downloads.getSummary(Downloads.PRIVATE); michael@0: michael@0: do_check_eq(publicSummaryOne, publicSummaryTwo); michael@0: do_check_eq(privateSummaryOne, privateSummaryTwo); michael@0: michael@0: do_check_neq(publicSummaryOne, privateSummaryOne); michael@0: }); michael@0: michael@0: /** michael@0: * Tests that the getSystemDownloadsDirectory returns a non-empty download michael@0: * directory string. michael@0: */ michael@0: add_task(function test_getSystemDownloadsDirectory() michael@0: { michael@0: let downloadDir = yield Downloads.getSystemDownloadsDirectory(); michael@0: do_check_neq(downloadDir, ""); michael@0: }); michael@0: michael@0: /** michael@0: * Tests that the getPreferredDownloadsDirectory returns a non-empty download michael@0: * directory string. michael@0: */ michael@0: add_task(function test_getPreferredDownloadsDirectory() michael@0: { michael@0: let downloadDir = yield Downloads.getPreferredDownloadsDirectory(); michael@0: do_check_neq(downloadDir, ""); michael@0: }); michael@0: michael@0: /** michael@0: * Tests that the getTemporaryDownloadsDirectory returns a non-empty download michael@0: * directory string. michael@0: */ michael@0: add_task(function test_getTemporaryDownloadsDirectory() michael@0: { michael@0: let downloadDir = yield Downloads.getTemporaryDownloadsDirectory(); michael@0: do_check_neq(downloadDir, ""); michael@0: }); michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Termination michael@0: michael@0: let tailFile = do_get_file("tail.js"); michael@0: Services.scriptloader.loadSubScript(NetUtil.newURI(tailFile).spec);