B2G/gecko/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadmonitor.js
// cleared when switching the private browsing mode on or off.
cleared when switching the private browsing mode on or off.
cleared when switching the private browsing mode on or off .
function test() {
  let pb = Cc["@mozilla.org/privatebrowsing;1"].
           getService(Ci.nsIPrivateBrowsingService);
  let dm = Cc["@mozilla.org/download-manager;1"].
           getService(Ci.nsIDownloadManager);
  if (!gDownloadMgr)
    gDownloadMgr = dm;
  let panel = document.getElementById("download-monitor");
  waitForExplicitFinish();
  let acceptDialog = 0;
  let confirmCalls = 0;
  function promptObserver(aSubject, aTopic, aData) {
    let dialogWin = aSubject.QueryInterface(Ci.nsIDOMWindow);
    confirmCalls++;
    if (acceptDialog-- > 0)
      dialogWin.document.documentElement.getButton("accept").click();
  }
  Services.obs.addObserver(promptObserver, "common-dialog-loaded", false);
  let [file, persist] = addDownload(dm, {
    isPrivate: window.gPrivateBrowsingUI.privateWindow,
    resultFileName: "pbtest-1",
    downloadName: "PB Test 1"
  });
  if (!DownloadMonitorPanel.inited())
    DownloadMonitorPanel.init();
  else
    DownloadMonitorPanel.updateStatus();
  ok(!panel.hidden, "The download panel should be successfully added initially");
  acceptDialog = 1;
  pb.privateBrowsingEnabled = true;
  is(confirmCalls, 1, "One prompt was accepted");
  ok(pb.privateBrowsingEnabled, "The private browsing transition was successful");
  executeSoon(function () {
    ok(panel.hidden, "The download panel should be hidden when entering the private browsing mode");
    let [file2, persist2] = addDownload(dm, {
      isPrivate: window.gPrivateBrowsingUI.privateWindow,
      resultFileName: "pbtest-2",
      downloadName: "PB Test 2"
    });
    DownloadMonitorPanel.updateStatus();
    ok(!panel.hidden, "The download panel should show up when a new download is added");
    acceptDialog = 1;
    pb.privateBrowsingEnabled = false;
    is(confirmCalls, 2, "One prompt was accepted");
    ok(!pb.privateBrowsingEnabled, "The private browsing transition was successful");
    executeSoon(function () {
      ok(panel.hidden, "The download panel should be hidden when leaving the private browsing mode");
      let dls = dm.activeDownloads;
      while (dls.hasMoreElements()) {
        let dl = dls.getNext().QueryInterface(Ci.nsIDownload);
        dm.removeDownload(dl.id);
        let file = dl.targetFile;
        if (file.exists())
          file.remove(false);
      }
      if (file.exists())
        file.remove(false);
      Services.obs.removeObserver(promptObserver, "common-dialog-loaded", false);
      finish();
    });
  });
}
function test ( ) { let pb = Cc [ "@mozilla.org/privatebrowsing;1" ] . getService ( Ci . nsIPrivateBrowsingService ) ; let dm = Cc [ "@mozilla.org/download-manager;1" ] . getService ( Ci . nsIDownloadManager ) ; if ( ! gDownloadMgr ) gDownloadMgr = dm ; let panel = document . getElementById ( "download-monitor" ) ; waitForExplicitFinish ( ) ; let acceptDialog = 0 ; let confirmCalls = 0 ; function promptObserver ( aSubject , aTopic , aData ) { let dialogWin = aSubject . QueryInterface ( Ci . nsIDOMWindow ) ; confirmCalls ++ ; if ( acceptDialog -- > 0 ) dialogWin . document . documentElement . getButton ( "accept" ) . click ( ) ; } Services . obs . addObserver ( promptObserver , "common-dialog-loaded" , false ) ; let [ file , persist ] = addDownload ( dm , { isPrivate : window . gPrivateBrowsingUI . privateWindow , resultFileName : "pbtest-1" , downloadName : "PB Test 1" } ) ; if ( ! DownloadMonitorPanel . inited ( ) ) DownloadMonitorPanel . init ( ) ; else DownloadMonitorPanel . updateStatus ( ) ; ok ( ! panel . hidden , "The download panel should be successfully added initially" ) ; acceptDialog = 1 ; pb . privateBrowsingEnabled = true ; is ( confirmCalls , 1 , "One prompt was accepted" ) ; ok ( pb . privateBrowsingEnabled , "The private browsing transition was successful" ) ; executeSoon ( function ( ) { ok ( panel . hidden , "The download panel should be hidden when entering the private browsing mode" ) ; let [ file2 , persist2 ] = addDownload ( dm , { isPrivate : window . gPrivateBrowsingUI . privateWindow , resultFileName : "pbtest-2" , downloadName : "PB Test 2" } ) ; DownloadMonitorPanel . updateStatus ( ) ; ok ( ! panel . hidden , "The download panel should show up when a new download is added" ) ; acceptDialog = 1 ; pb . privateBrowsingEnabled = false ; is ( confirmCalls , 2 , "One prompt was accepted" ) ; ok ( ! pb . privateBrowsingEnabled , "The private browsing transition was successful" ) ; executeSoon ( function ( ) { ok ( panel . hidden , "The download panel should be hidden when leaving the private browsing mode" ) ; let dls = dm . activeDownloads ; while ( dls . hasMoreElements ( ) ) { let dl = dls . getNext ( ) . QueryInterface ( Ci . nsIDownload ) ; dm . removeDownload ( dl . id ) ; let file = dl . targetFile ; if ( file . exists ( ) ) file . remove ( false ) ; } if ( file . exists ( ) ) file . remove ( false ) ; Services . obs . removeObserver ( promptObserver , "common-dialog-loaded" , false ) ; finish ( ) ; } ) ; } ) ; }
cleared when switching the private browsing mode on or off.
cleared when switching the private browsing mode on or off .
// initialization // Add a new download // Make sure that the download is being displayed in the monitor panel // Enter the private browsing mode // Add a new download // Update the panel // Make sure that the panel is visible // Exit the private browsing mode // cleanup