browser/components/sessionstore/test/browser_telemetry.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/ */
     5 let tmp = {};
     6 Cu.import("resource:///modules/sessionstore/SessionFile.jsm", tmp);
     7 let {SessionFile} = tmp;
     9 // Shortcuts for histogram names
    10 let Keys = {};
    11 for (let k of ["HISTORY", "FORMDATA", "OPEN_WINDOWS", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS", "DOM_STORAGE"]) {
    12   Keys[k] = "FX_SESSION_RESTORE_TOTAL_" + k + "_SIZE_BYTES";
    13 }
    15 function lt(a, b, message) {
    16   isnot(a, undefined, message + " (sanity check)");
    17   isnot(b, undefined, message + " (sanity check)");
    18   ok(a < b, message + " ( " + a + " < " + b + ")");
    19 }
    20 function gt(a, b, message) {
    21   isnot(a, undefined, message + " (sanity check)");
    22   isnot(b, undefined, message + " (sanity check)");
    23   ok(a > b, message + " ( " + a + " > " + b + ")");
    24 }
    26 add_task(function init() {
    27   for (let i = ss.getClosedWindowCount() - 1; i >= 0; --i) {
    28     ss.forgetClosedWindow(i);
    29   }
    30   for (let i = ss.getClosedTabCount(window) - 1; i >= 0; --i) {
    31     ss.forgetClosedTab(window, i);
    32   }
    33 });
    35 /**
    36  * Test that Telemetry collection doesn't cause any error.
    37  */
    38 add_task(function() {
    39   info("Checking a little bit of consistency");
    40   let statistics = yield promiseStats();
    42   for (let k of Object.keys(statistics)) {
    43     let data = statistics[k];
    44     info("Data for " + k + ": " + data);
    45     if (Array.isArray(data)) {
    46       ok(data.every(x => x >= 0), "Data for " + k + " is >= 0");
    47     } else {
    48       ok(data >= 0, "Data for " + k + " is >= 0");
    49     }
    50   }
    51 });
    53 /**
    54  * Test HISTORY key.
    55  */
    56 add_task(function history() {
    57   let KEY = Keys.HISTORY;
    58   let tab = gBrowser.addTab("http://example.org:80/?");
    59   yield promiseBrowserLoaded(tab.linkedBrowser);
    60   try {
    61     SyncHandlers.get(tab.linkedBrowser).flush();
    62     let statistics = yield promiseStats();
    64     info("Now changing history");
    65     tab.linkedBrowser.loadURI("http://example.org:80/1");
    66     yield promiseBrowserLoaded(tab.linkedBrowser);
    67     SyncHandlers.get(tab.linkedBrowser).flush();
    68     let statistics2 = yield promiseStats();
    70     // We have changed history, so it must have increased
    71     isnot(statistics[KEY], undefined, "Key was defined");
    72     isnot(statistics2[KEY], undefined, "Key is still defined");
    73     gt(statistics2[KEY], statistics[KEY], "The total size of HISTORY has increased");
    75 // Almost nothing else should
    76     for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) {
    77       is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased");
    78     }
    79   } finally {
    80     if (tab) {
    81       gBrowser.removeTab(tab);
    82     }
    83   }
    84 });
    86 /**
    87  * Test CLOSED_TABS_IN_OPEN_WINDOWS key.
    88  */
    89 add_task(function close_tab() {
    90   let KEY = Keys.CLOSED_TABS_IN_OPEN_WINDOWS;
    91   let tab = gBrowser.addTab("http://example.org:80/?close_tab");
    92   yield promiseBrowserLoaded(tab.linkedBrowser);
    93   try {
    94     SyncHandlers.get(tab.linkedBrowser).flush();
    95     let statistics = yield promiseStats();
    97     info("Now closing a tab");
    98     gBrowser.removeTab(tab);
    99     tab = null;
   100     let statistics2 = yield promiseStats();
   102     isnot(statistics[KEY], undefined, "Key was defined");
   103     isnot(statistics2[KEY], undefined, "Key is still defined");
   104     gt(statistics2[KEY], statistics[KEY], "The total size of CLOSED_TABS_IN_OPEN_WINDOWS has increased");
   106     // Almost nothing else should change
   107     for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS"]) {
   108       is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased");
   109     }
   111   } finally {
   112     if (tab) {
   113       gBrowser.removeTab(tab);
   114     }
   115   }
   116 });
   118 /**
   119  * Test OPEN_WINDOWS key.
   120  */
   121 add_task(function open_window() {
   122   let KEY = Keys.OPEN_WINDOWS;
   123   let win;
   124   try {
   125     let statistics = yield promiseStats();
   126     win = yield promiseNewWindowLoaded("http://example.org:80/?open_window");
   127     let statistics2 = yield promiseStats();
   129     isnot(statistics[KEY], undefined, "Key was defined");
   130     isnot(statistics2[KEY], undefined, "Key is still defined");
   131     gt(statistics2[KEY], statistics[KEY], "The total size of OPEN_WINDOWS has increased");
   133     // Almost nothing else should change
   134     for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) {
   135       is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased");
   136     }
   138   } finally {
   139     if (win) {
   140       yield promiseWindowClosed(win);
   141     }
   142   }
   143 });
   145 /**
   146  * Test CLOSED_WINDOWS key.
   147  */
   148 add_task(function close_window() {
   149   let KEY = Keys.CLOSED_WINDOWS;
   150   let win = yield promiseNewWindowLoaded("http://example.org:80/?close_window");
   152   // We need to add something to the window, otherwise it won't be saved
   153   let tab = win.gBrowser.addTab("http://example.org:80/?close_tab");
   154   yield promiseBrowserLoaded(tab.linkedBrowser);
   155   try {
   156     let statistics = yield promiseStats();
   157     yield promiseWindowClosed(win);
   158     win = null;
   159     let statistics2 = yield promiseStats();
   161     isnot(statistics[KEY], undefined, "Key was defined");
   162     isnot(statistics2[KEY], undefined, "Key is still defined");
   163     gt(statistics2[KEY], statistics[KEY], "The total size of CLOSED_WINDOWS has increased");
   164     lt(statistics2[Keys.OPEN_WINDOWS], statistics[Keys.OPEN_WINDOWS], "The total size of OPEN_WINDOWS has decreased");
   166     // Almost nothing else should change
   167     for (let k of ["FORMDATA", "DOM_STORAGE", "CLOSED_TABS_IN_OPEN_WINDOWS"]) {
   168       is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased");
   169     }
   171   } finally {
   172     if (win) {
   173       yield promiseWindowClosed(win);
   174     }
   175   }
   176 });
   179 /**
   180  * Test DOM_STORAGE key.
   181  */
   182 add_task(function dom_storage() {
   183   let KEY = Keys.DOM_STORAGE;
   184   let tab = gBrowser.addTab("http://example.org:80/?dom_storage");
   185   yield promiseBrowserLoaded(tab.linkedBrowser);
   186   try {
   187     SyncHandlers.get(tab.linkedBrowser).flush();
   188     let statistics = yield promiseStats();
   190     info("Now adding some storage");
   191     yield modifySessionStorage(tab.linkedBrowser, {foo: "bar"});
   192     SyncHandlers.get(tab.linkedBrowser).flush();
   194     let statistics2 = yield promiseStats();
   196     isnot(statistics[KEY], undefined, "Key was defined");
   197     isnot(statistics2[KEY], undefined, "Key is still defined");
   198     gt(statistics2[KEY], statistics[KEY], "The total size of DOM_STORAGE has increased");
   200     // Almost nothing else should change
   201     for (let k of ["CLOSED_TABS_IN_OPEN_WINDOWS", "FORMDATA", "CLOSED_WINDOWS"]) {
   202       is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased");
   203     }
   205   } finally {
   206     if (tab) {
   207       gBrowser.removeTab(tab);
   208     }
   209   }
   210 });
   212 /**
   213  * Test FORMDATA key.
   214  */
   215 add_task(function formdata() {
   216   let KEY = Keys.FORMDATA;
   217   let tab = gBrowser.addTab("data:text/html;charset=utf-8,<input%20id='input'>");
   218   yield promiseBrowserLoaded(tab.linkedBrowser);
   219   try {
   220     SyncHandlers.get(tab.linkedBrowser).flush();
   221     let statistics = yield promiseStats();
   223     info("Now changing form data");
   225     yield setInputValue(tab.linkedBrowser, {id: "input", value: "This is some form data"});
   226     SyncHandlers.get(tab.linkedBrowser).flush();
   228     let statistics2 = yield promiseStats();
   230     isnot(statistics[KEY], undefined, "Key was defined");
   231     isnot(statistics2[KEY], undefined, "Key is still defined");
   232     gt(statistics2[KEY], statistics[KEY], "The total size of FORMDATA has increased");
   234     // Almost nothing else should
   235     for (let k of ["DOM_STORAGE", "CLOSED_WINDOWS", "CLOSED_TABS_IN_OPEN_WINDOWS"]) {
   236       is(statistics2[Keys[k]], statistics[Keys[k]], "The total size of " + k + " has not increased");
   237     }
   238   } finally {
   239     if (tab) {
   240       gBrowser.removeTab(tab);
   241     }
   242   }
   243 });
   245 add_task(function* test_sessionRestoreInit() {
   246    let info = Cc["@mozilla.org/toolkit/app-startup;1"].
   247      getService(Ci.nsIAppStartup).
   248      getStartupInfo();
   249   ok(info.sessionRestoreInit > info.process, "sessionRestoreInit is after process creation");
   250   ok(info.sessionRestoreInit <
   251     Date.now() + 10000 /* Date.now() is non-monotonic, let's play it paranoid*/,
   252     "sessionRestoreInit is before now");
   253 });
   255 /**
   256  * Get the latest statistics.
   257  */
   258 function promiseStats() {
   259   let state = ss.getBrowserState();
   260   info("Stats: " + state);
   261   return SessionFile.gatherTelemetry(state);
   262 }
   265 function modifySessionStorage(browser, data) {
   266   browser.messageManager.sendAsyncMessage("ss-test:modifySessionStorage", data);
   267   return promiseContentMessage(browser, "ss-test:MozStorageChanged");
   268 }
   270 function setInputValue(browser, data) {
   271   return sendMessage(browser, "ss-test:setInputValue", data);
   272 }

mercurial