B2G/gecko/browser/components/sessionstore/test/browser_248970_a.js
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
This Source Code Form is subject to the terms of the Mozilla Public
License, v.
This Source Code Form is subject to the terms of the Mozilla Public License v .
function test() {
  waitForExplicitFinish();
  let pb = Cc["@mozilla.org/privatebrowsing;1"].
           getService(Ci.nsIPrivateBrowsingService);
  gPrefService.setBoolPref("browser.privatebrowsing.keep_current_session", true);
  let profilePath = Cc["@mozilla.org/file/directory_service;1"].
                    getService(Ci.nsIProperties).
                    get("ProfD", Ci.nsIFile);
  function getSessionstoreFile() {
    let sessionStoreJS = profilePath.clone();
    sessionStoreJS.append("sessionstore.js");
    return sessionStoreJS;
  }
  function getSessionstorejsModificationTime() {
    let file = getSessionstoreFile();
    if (file.exists())
      return file.lastModifiedTime;
    else
      return -1;
  }
  function waitForFileExistence(aMessage, aDoNext) {
    const TOPIC = "sessionstore-state-write-complete";
    Services.obs.addObserver(function (aSubject, aTopic, aData) {
      Services.obs.removeObserver(arguments.callee, TOPIC);
      ok(getSessionstoreFile().exists(), aMessage);
      aDoNext();
    }, TOPIC, false);
  }
  function actualTest() {
    const testURL_A = "http://example.org/";
    let tab_A = gBrowser.addTab(testURL_A);
    tab_A.linkedBrowser.addEventListener("load", function (aEvent) {
      this.removeEventListener("load", arguments.callee, true);
      let sessionStoreJS = getSessionstoreFile();
      sessionStoreJS.remove(false);
      pb.privateBrowsingEnabled = true;
      ok(pb.privateBrowsingEnabled, "private browsing enabled");
      waitForFileExistence("file should be created after private browsing entered",
                           function() {
        let startPBModeTimeStamp = getSessionstorejsModificationTime();
        const testURL_B = "http://test1.example.org/";
        let tab_B = gBrowser.addTab(testURL_B);
        tab_B.linkedBrowser.addEventListener("load", function (aEvent) {
          this.removeEventListener("load", arguments.callee, true);
          const testURL_C = "http://mochi.test:8888/";
          let tab_C = gBrowser.addTab(testURL_C);
          tab_C.linkedBrowser.addEventListener("load", function (aEvent) {
            this.removeEventListener("load", arguments.callee, true);
            gBrowser.removeTab(tab_C);
            gBrowser.removeTab(tab_B);
            gBrowser.removeTab(tab_A);
            if (gPrefService.prefHasUserValue("browser.sessionstore.interval"))
              gPrefService.clearUserPref("browser.sessionstore.interval");
            gPrefService.setIntPref("browser.sessionstore.interval", 0);
            let endPBModeTimeStamp = getSessionstorejsModificationTime();
            pb.privateBrowsingEnabled = false;
            ok(!pb.privateBrowsingEnabled, "private browsing disabled");
            is(startPBModeTimeStamp, endPBModeTimeStamp,
              "outside private browsing - sessionStore.js timestamp has not changed");
            gPrefService.clearUserPref("browser.sessionstore.interval");
            gPrefService.clearUserPref("browser.privatebrowsing.keep_current_session");
            finish();
          }, true);
        }, true);
      }); 
    }, true);
  }
  let sessionStoreJS = getSessionstoreFile();
  if (sessionStoreJS.exists())
    sessionStoreJS.remove(false);
  gPrefService.setIntPref("browser.sessionstore.interval", 0);
  waitForFileExistence("file should be created after setting interval to 0",
                       actualTest);
}
function test ( ) { waitForExplicitFinish ( ) ; let pb = Cc [ "@mozilla.org/privatebrowsing;1" ] . getService ( Ci . nsIPrivateBrowsingService ) ; gPrefService . setBoolPref ( "browser.privatebrowsing.keep_current_session" , true ) ; let profilePath = Cc [ "@mozilla.org/file/directory_service;1" ] . getService ( Ci . nsIProperties ) . get ( "ProfD" , Ci . nsIFile ) ; function getSessionstoreFile ( ) { let sessionStoreJS = profilePath . clone ( ) ; sessionStoreJS . append ( "sessionstore.js" ) ; return sessionStoreJS ; } function getSessionstorejsModificationTime ( ) { let file = getSessionstoreFile ( ) ; if ( file . exists ( ) ) return file . lastModifiedTime ; else return - 1 ; } function waitForFileExistence ( aMessage , aDoNext ) { const TOPIC = "sessionstore-state-write-complete" ; Services . obs . addObserver ( function ( aSubject , aTopic , aData ) { Services . obs . removeObserver ( arguments . callee , TOPIC ) ; ok ( getSessionstoreFile ( ) . exists ( ) , aMessage ) ; aDoNext ( ) ; } , TOPIC , false ) ; } function actualTest ( ) { const testURL_A = "http://example.org/" ; let tab_A = gBrowser . addTab ( testURL_A ) ; tab_A . linkedBrowser . addEventListener ( "load" , function ( aEvent ) { this . removeEventListener ( "load" , arguments . callee , true ) ; let sessionStoreJS = getSessionstoreFile ( ) ; sessionStoreJS . remove ( false ) ; pb . privateBrowsingEnabled = true ; ok ( pb . privateBrowsingEnabled , "private browsing enabled" ) ; waitForFileExistence ( "file should be created after private browsing entered" , function ( ) { let startPBModeTimeStamp = getSessionstorejsModificationTime ( ) ; const testURL_B = "http://test1.example.org/" ; let tab_B = gBrowser . addTab ( testURL_B ) ; tab_B . linkedBrowser . addEventListener ( "load" , function ( aEvent ) { this . removeEventListener ( "load" , arguments . callee , true ) ; const testURL_C = "http://mochi.test:8888/" ; let tab_C = gBrowser . addTab ( testURL_C ) ; tab_C . linkedBrowser . addEventListener ( "load" , function ( aEvent ) { this . removeEventListener ( "load" , arguments . callee , true ) ; gBrowser . removeTab ( tab_C ) ; gBrowser . removeTab ( tab_B ) ; gBrowser . removeTab ( tab_A ) ; if ( gPrefService . prefHasUserValue ( "browser.sessionstore.interval" ) ) gPrefService . clearUserPref ( "browser.sessionstore.interval" ) ; gPrefService . setIntPref ( "browser.sessionstore.interval" , 0 ) ; let endPBModeTimeStamp = getSessionstorejsModificationTime ( ) ; pb . privateBrowsingEnabled = false ; ok ( ! pb . privateBrowsingEnabled , "private browsing disabled" ) ; is ( startPBModeTimeStamp , endPBModeTimeStamp , "outside private browsing - sessionStore.js timestamp has not changed" ) ; gPrefService . clearUserPref ( "browser.sessionstore.interval" ) ; gPrefService . clearUserPref ( "browser.privatebrowsing.keep_current_session" ) ; finish ( ) ; } , true ) ; } , true ) ; } ) ; } , true ) ; } let sessionStoreJS = getSessionstoreFile ( ) ; if ( sessionStoreJS . exists ( ) ) sessionStoreJS . remove ( false ) ; gPrefService . setIntPref ( "browser.sessionstore.interval" , 0 ) ; waitForFileExistence ( "file should be created after setting interval to 0" , actualTest ) ; }
This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0.
This Source Code Form is subject to the terms of the Mozilla Public License v . 2 . 0 .
/** Test (A) for Bug 248970 **/ // test setup // private browsing service // Remove the observer so we do not leak. // Check that the file exists. // Run our next set of work. ////////////////////////////////////////////////////////////////// // Test (A) : No data recording while in private browsing mode  // ////////////////////////////////////////////////////////////////// // public session, add a new tab: (A) // remove sessionstore.js to make sure it's created again when entering // the private browsing mode. // enter private browsing mode // sessionstore.js should be re-created at this point // record the time stamp of sessionstore.js in the private session // private browsing session, add new tab: (B) // private browsing session, add new tab: (C) // private browsing session, close tab: (C) // private browsing session, close tab: (B) // private browsing session, close tab: (A) // record the timestamp of sessionstore.js at the end of the private session // exit private browsing mode // compare timestamps: pre and post private browsing session // cleanup // End of anonymous waitForFileExistence function. // Remove the sessionstore.js file before setting the interval to 0 // Make sure that sessionstore.js can be forced to be created by setting // the interval pref to 0 // sessionstore.js should be re-created at this point