1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_sanitize-download-history.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 + 1.9 +function test() 1.10 +{ 1.11 + ////////////////////////////////////////////////////////////////////////////// 1.12 + //// Tests (defined locally for scope's sake) 1.13 + 1.14 + function test_checkedAndDisabledAtStart(aWin) 1.15 + { 1.16 + let doc = aWin.document; 1.17 + let downloads = doc.getElementById("downloads-checkbox"); 1.18 + let history = doc.getElementById("history-checkbox"); 1.19 + 1.20 + ok(history.checked, "history checkbox is checked"); 1.21 + ok(downloads.disabled, "downloads checkbox is disabled"); 1.22 + ok(downloads.checked, "downloads checkbox is checked"); 1.23 + } 1.24 + 1.25 + function test_checkedAndDisabledOnHistoryToggle(aWin) 1.26 + { 1.27 + let doc = aWin.document; 1.28 + let downloads = doc.getElementById("downloads-checkbox"); 1.29 + let history = doc.getElementById("history-checkbox"); 1.30 + 1.31 + EventUtils.synthesizeMouse(history, 0, 0, {}, aWin); 1.32 + ok(!history.checked, "history checkbox is not checked"); 1.33 + ok(downloads.disabled, "downloads checkbox is disabled"); 1.34 + ok(downloads.checked, "downloads checkbox is checked"); 1.35 + } 1.36 + 1.37 + function test_checkedAfterAddingDownload(aWin) 1.38 + { 1.39 + let doc = aWin.document; 1.40 + let downloads = doc.getElementById("downloads-checkbox"); 1.41 + let history = doc.getElementById("history-checkbox"); 1.42 + 1.43 + // Add download to DB 1.44 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.45 + getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); 1.46 + file.append("sanitize-dm-test.file"); 1.47 + file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 1.48 + let testPath = Services.io.newFileURI(file).spec; 1.49 + let data = { 1.50 + name: "381603.patch", 1.51 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.52 + target: testPath, 1.53 + startTime: 1180493839859230, 1.54 + endTime: 1180493839859239, 1.55 + state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED, 1.56 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0, 1.57 + guid: "a1bcD23eF4g5" 1.58 + }; 1.59 + let db = Cc["@mozilla.org/download-manager;1"]. 1.60 + getService(Ci.nsIDownloadManager).DBConnection; 1.61 + let stmt = db.createStatement( 1.62 + "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " + 1.63 + "state, currBytes, maxBytes, preferredAction, autoResume, guid) " + 1.64 + "VALUES (:name, :source, :target, :startTime, :endTime, :state, " + 1.65 + ":currBytes, :maxBytes, :preferredAction, :autoResume, :guid)"); 1.66 + try { 1.67 + for (let prop in data) 1.68 + stmt.params[prop] = data[prop]; 1.69 + stmt.execute(); 1.70 + } 1.71 + finally { 1.72 + stmt.finalize(); 1.73 + } 1.74 + 1.75 + // Toggle history to get everything to update 1.76 + EventUtils.synthesizeMouse(history, 0, 0, {}, aWin); 1.77 + EventUtils.synthesizeMouse(history, 0, 0, {}, aWin); 1.78 + 1.79 + ok(!history.checked, "history checkbox is not checked"); 1.80 + ok(!downloads.disabled, "downloads checkbox is not disabled"); 1.81 + ok(downloads.checked, "downloads checkbox is checked"); 1.82 + } 1.83 + 1.84 + function test_checkedAndDisabledWithHistoryChecked(aWin) 1.85 + { 1.86 + let doc = aWin.document; 1.87 + let downloads = doc.getElementById("downloads-checkbox"); 1.88 + let history = doc.getElementById("history-checkbox"); 1.89 + 1.90 + EventUtils.synthesizeMouse(history, 0, 0, {}, aWin); 1.91 + ok(history.checked, "history checkbox is checked"); 1.92 + ok(downloads.disabled, "downloads checkbox is disabled"); 1.93 + ok(downloads.checked, "downloads checkbox is checked"); 1.94 + } 1.95 + 1.96 + let tests = [ 1.97 + test_checkedAndDisabledAtStart, 1.98 + test_checkedAndDisabledOnHistoryToggle, 1.99 + test_checkedAfterAddingDownload, 1.100 + test_checkedAndDisabledWithHistoryChecked, 1.101 + ]; 1.102 + 1.103 + ////////////////////////////////////////////////////////////////////////////// 1.104 + //// Run the tests 1.105 + 1.106 + let dm = Cc["@mozilla.org/download-manager;1"]. 1.107 + getService(Ci.nsIDownloadManager); 1.108 + let db = dm.DBConnection; 1.109 + 1.110 + // Empty any old downloads 1.111 + db.executeSimpleSQL("DELETE FROM moz_downloads"); 1.112 + 1.113 + // Close the UI if necessary 1.114 + let win = Services.ww.getWindowByName("Sanitize", null); 1.115 + if (win && (win instanceof Ci.nsIDOMWindow)) 1.116 + win.close(); 1.117 + 1.118 + // Start the test when the sanitize window loads 1.119 + Services.ww.registerNotification(function (aSubject, aTopic, aData) { 1.120 + Services.ww.unregisterNotification(arguments.callee); 1.121 + aSubject.QueryInterface(Ci.nsIDOMEventTarget) 1.122 + .addEventListener("DOMContentLoaded", doTest, false); 1.123 + }); 1.124 + 1.125 + // Let the methods that run onload finish before we test 1.126 + let doTest = function() setTimeout(function() { 1.127 + let win = Services.ww.getWindowByName("Sanitize", null) 1.128 + .QueryInterface(Ci.nsIDOMWindow); 1.129 + 1.130 + for (let i = 0; i < tests.length; i++) 1.131 + tests[i](win); 1.132 + 1.133 + win.close(); 1.134 + finish(); 1.135 + }, 0); 1.136 + 1.137 + // Show the UI 1.138 + Services.ww.openWindow(window, 1.139 + "chrome://browser/content/sanitize.xul", 1.140 + "Sanitize", 1.141 + "chrome,titlebar,centerscreen", 1.142 + null); 1.143 + 1.144 + waitForExplicitFinish(); 1.145 +}