dom/tests/browser/browser_ConsoleStorageAPITests.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 const TEST_URI = "http://example.com/browser/dom/tests/browser/test-console-api.html";
     5 const TEST_URI_NAV = "http://example.com/browser/dom/tests/browser/";
     7 let ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
     8                           .getService(Ci.nsIConsoleAPIStorage);
    10 var apiCallCount;
    12 var ConsoleObserver = {
    13   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
    15   init: function CO_init()
    16   {
    17     Services.obs.addObserver(this, "console-storage-cache-event", false);
    18     apiCallCount = 0;
    19   },
    21   observe: function CO_observe(aSubject, aTopic, aData)
    22   {
    23     if (aTopic == "console-storage-cache-event") {
    24       apiCallCount ++;
    25       if (apiCallCount == 4) {
    26         Services.obs.removeObserver(this, "console-storage-cache-event");
    28         try {
    29         let tab = gBrowser.selectedTab;
    30         let browser = gBrowser.selectedBrowser;
    31         let win = browser.contentWindow;
    32         let windowID = getWindowId(win);
    33         let messages = ConsoleAPIStorage.getEvents(windowID);
    34         ok(messages.length >= 4, "Some messages found in the storage service");
    36         ConsoleAPIStorage.clearEvents();
    37         messages = ConsoleAPIStorage.getEvents(windowID);
    38         is(messages.length, 0, "Cleared Storage");
    40         // make sure a closed window's events are in fact removed from the
    41         // storage cache
    42         win.console.log("adding a new event");
    43         // Close the window.
    44         gBrowser.removeTab(tab, {animate: false});
    45         // Ensure actual window destruction is not delayed (too long).
    46         SpecialPowers.DOMWindowUtils.garbageCollect();
    47         // Ensure "inner-window-destroyed" event is processed,
    48         // so the storage cache is cleared.
    49         executeSoon(function () {
    50           // use the old windowID again to see if we have any stray cached messages
    51           messages = ConsoleAPIStorage.getEvents(windowID);
    52           is(messages.length, 0, "tab close is clearing the cache");
    53           finish();
    54         });
    55         } catch (ex) {
    56           dump(ex + "\n\n\n");
    57           dump(ex.stack + "\n\n\n");
    58           ok(false, "We got an unexpected exception");
    59         }
    60       }
    61     }
    62   }
    63 };
    65 function tearDown()
    66 {
    67   while (gBrowser.tabs.length > 1)
    68     gBrowser.removeCurrentTab();
    69 }
    71 function test()
    72 {
    73   // Don't cache removed tabs, so "clear console cache on tab close" triggers.
    74   Services.prefs.setIntPref("browser.tabs.max_tabs_undo", 0);
    75   registerCleanupFunction(function() {
    76     Services.prefs.clearUserPref("browser.tabs.max_tabs_undo");
    77   });
    79   registerCleanupFunction(tearDown);
    81   ConsoleObserver.init();
    83   waitForExplicitFinish();
    85   var tab = gBrowser.addTab(TEST_URI);
    86   gBrowser.selectedTab = tab;
    87   var browser = gBrowser.selectedBrowser;
    88   browser.addEventListener("DOMContentLoaded", function onLoad(event) {
    89     browser.removeEventListener("DOMContentLoaded", onLoad, false);
    90     executeSoon(function test_executeSoon() {
    91       let win = browser.contentWindow;
    92       win.console.log("this", "is", "a", "log message");
    93       win.console.info("this", "is", "a", "info message");
    94       win.console.warn("this", "is", "a", "warn message");
    95       win.console.error("this", "is", "a", "error message");
    96     });
    97   }, false);
    98 }
   100 function getWindowId(aWindow)
   101 {
   102   return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
   103                 .getInterface(Ci.nsIDOMWindowUtils)
   104                 .currentInnerWindowID;
   105 }

mercurial