browser/devtools/netmonitor/test/browser_net_prefs-reload.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 /**
     5  * Tests if the prefs that should survive across tool reloads work.
     6  */
     8 function test() {
     9   initNetMonitor(SIMPLE_URL).then(([aTab, aDebuggee, aMonitor]) => {
    10     info("Starting test... ");
    12     // This test reopens the network monitor a bunch of times, for different
    13     // hosts (bottom, side, window). This seems to be slow on debug builds.
    14     requestLongerTimeout(3);
    16     // Use these getters instead of caching instances inside the panel win,
    17     // since the tool is reopened a bunch of times during this test
    18     // and the instances will differ.
    19     let getView = () => aMonitor.panelWin.NetMonitorView;
    20     let getController = () => aMonitor.panelWin.NetMonitorController;
    22     let prefsToCheck = {
    23       filters: {
    24         // A custom new value to be used for the verified preference.
    25         newValue: ["html", "css"],
    26         // Getter used to retrieve the current value from the frontend, in order
    27         // to verify that the pref was applied properly.
    28         validateValue: ($) => getView().RequestsMenu._activeFilters,
    29         // Predicate used to modify the frontend when setting the new pref value,
    30         // before trying to validate the changes.
    31         modifyFrontend: ($, aValue) => aValue.forEach(e => getView().RequestsMenu.filterOn(e))
    32       },
    33       networkDetailsWidth: {
    34         newValue: ~~(Math.random() * 200 + 100),
    35         validateValue: ($) => ~~$("#details-pane").getAttribute("width"),
    36         modifyFrontend: ($, aValue) => $("#details-pane").setAttribute("width", aValue)
    37       },
    38       networkDetailsHeight: {
    39         newValue: ~~(Math.random() * 300 + 100),
    40         validateValue: ($) => ~~$("#details-pane").getAttribute("height"),
    41         modifyFrontend: ($, aValue) => $("#details-pane").setAttribute("height", aValue)
    42       }
    43       /* add more prefs here... */
    44     };
    46     function storeFirstPrefValues() {
    47       info("Caching initial pref values.");
    49       for (let name in prefsToCheck) {
    50         let currentValue = aMonitor.panelWin.Prefs[name];
    51         prefsToCheck[name].firstValue = currentValue;
    52       }
    53     }
    55     function validateFirstPrefValues() {
    56       info("Validating current pref values to the UI elements.");
    58       for (let name in prefsToCheck) {
    59         let currentValue = aMonitor.panelWin.Prefs[name];
    60         let firstValue = prefsToCheck[name].firstValue;
    61         let validateValue = prefsToCheck[name].validateValue;
    63         is(currentValue.toSource(), firstValue.toSource(),
    64           "Pref " + name + " should be equal to first value: " + firstValue);
    65         is(currentValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
    66           "Pref " + name + " should validate: " + currentValue);
    67       }
    68     }
    70     function modifyFrontend() {
    71       info("Modifying UI elements to the specified new values.");
    73       for (let name in prefsToCheck) {
    74         let currentValue = aMonitor.panelWin.Prefs[name];
    75         let firstValue = prefsToCheck[name].firstValue;
    76         let newValue = prefsToCheck[name].newValue;
    77         let validateValue = prefsToCheck[name].validateValue;
    78         let modifyFrontend = prefsToCheck[name].modifyFrontend;
    80         modifyFrontend(aMonitor.panelWin.$, newValue);
    81         info("Modified UI element affecting " + name + " to: " + newValue);
    83         is(currentValue.toSource(), firstValue.toSource(),
    84           "Pref " + name + " should still be equal to first value: " + firstValue);
    85         isnot(currentValue.toSource(), newValue.toSource(),
    86           "Pref " + name + " should't yet be equal to second value: " + newValue);
    87         is(newValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
    88           "The UI element affecting " + name + " should validate: " + newValue);
    89       }
    90     }
    92     function validateNewPrefValues() {
    93       info("Invalidating old pref values to the modified UI elements.");
    95       for (let name in prefsToCheck) {
    96         let currentValue = aMonitor.panelWin.Prefs[name];
    97         let firstValue = prefsToCheck[name].firstValue;
    98         let newValue = prefsToCheck[name].newValue;
    99         let validateValue = prefsToCheck[name].validateValue;
   101         isnot(currentValue.toSource(), firstValue.toSource(),
   102           "Pref " + name + " should't be equal to first value: " + firstValue);
   103         is(currentValue.toSource(), newValue.toSource(),
   104           "Pref " + name + " should now be equal to second value: " + newValue);
   105         is(newValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
   106           "The UI element affecting " + name + " should validate: " + newValue);
   107       }
   108     }
   110     function resetFrontend() {
   111       info("Resetting UI elements to the cached initial pref values.");
   113       for (let name in prefsToCheck) {
   114         let currentValue = aMonitor.panelWin.Prefs[name];
   115         let firstValue = prefsToCheck[name].firstValue;
   116         let newValue = prefsToCheck[name].newValue;
   117         let validateValue = prefsToCheck[name].validateValue;
   118         let modifyFrontend = prefsToCheck[name].modifyFrontend;
   120         modifyFrontend(aMonitor.panelWin.$, firstValue);
   121         info("Modified UI element affecting " + name + " to: " + firstValue);
   123         isnot(currentValue.toSource(), firstValue.toSource(),
   124           "Pref " + name + " should't yet be equal to first value: " + firstValue);
   125         is(currentValue.toSource(), newValue.toSource(),
   126           "Pref " + name + " should still be equal to second value: " + newValue);
   127         is(firstValue.toSource(), validateValue(aMonitor.panelWin.$).toSource(),
   128           "The UI element affecting " + name + " should validate: " + firstValue);
   129       }
   130     }
   132     function testBottom() {
   133       info("Testing prefs reload for a bottom host.");
   134       storeFirstPrefValues();
   136       // Validate and modify while toolbox is on the bottom.
   137       validateFirstPrefValues();
   138       modifyFrontend();
   140       return restartNetMonitor(aMonitor)
   141         .then(([,, aNewMonitor]) => {
   142           aMonitor = aNewMonitor;
   144           // Revalidate and reset frontend while toolbox is on the bottom.
   145           validateNewPrefValues();
   146           resetFrontend();
   148           return restartNetMonitor(aMonitor);
   149         })
   150         .then(([,, aNewMonitor]) => {
   151           aMonitor = aNewMonitor;
   153           // Revalidate.
   154           validateFirstPrefValues();
   155         });
   156     }
   158     function testSide() {
   159       info("Moving toolbox to the side...");
   161       return aMonitor._toolbox.switchHost(Toolbox.HostType.SIDE)
   162         .then(() => {
   163           info("Testing prefs reload for a side host.");
   164           storeFirstPrefValues();
   166           // Validate and modify frontend while toolbox is on the side.
   167           validateFirstPrefValues();
   168           modifyFrontend();
   170           return restartNetMonitor(aMonitor);
   171         })
   172         .then(([,, aNewMonitor]) => {
   173           aMonitor = aNewMonitor;
   175           // Revalidate and reset frontend while toolbox is on the side.
   176           validateNewPrefValues();
   177           resetFrontend();
   179           return restartNetMonitor(aMonitor);
   180         })
   181         .then(([,, aNewMonitor]) => {
   182           aMonitor = aNewMonitor;
   184           // Revalidate.
   185           validateFirstPrefValues();
   186         });
   187     }
   189     function testWindow() {
   190       info("Moving toolbox into a window...");
   192       return aMonitor._toolbox.switchHost(Toolbox.HostType.WINDOW)
   193         .then(() => {
   194           info("Testing prefs reload for a window host.");
   195           storeFirstPrefValues();
   197           // Validate and modify frontend while toolbox is in a window.
   198           validateFirstPrefValues();
   199           modifyFrontend();
   201           return restartNetMonitor(aMonitor);
   202         })
   203         .then(([,, aNewMonitor]) => {
   204           aMonitor = aNewMonitor;
   206           // Revalidate and reset frontend while toolbox is in a window.
   207           validateNewPrefValues();
   208           resetFrontend();
   210           return restartNetMonitor(aMonitor);
   211         })
   212         .then(([,, aNewMonitor]) => {
   213           aMonitor = aNewMonitor;
   215           // Revalidate.
   216           validateFirstPrefValues();
   217         });
   218     }
   220     function cleanupAndFinish() {
   221       info("Moving toolbox back to the bottom...");
   223       aMonitor._toolbox.switchHost(Toolbox.HostType.BOTTOM)
   224         .then(() => teardown(aMonitor))
   225         .then(finish);
   226     }
   228     testBottom()
   229       .then(testSide)
   230       .then(testWindow)
   231       .then(cleanupAndFinish);
   232   });
   233 }

mercurial