Bug 1154874 - Make the damp test easier to read and extend;r=fitzgen draft
authorBrian Grinstead <bgrinstead@mozilla.com>
Tue, 12 Jan 2016 15:23:33 -0800
changeset 321166 56369657ae2d30da5bcf7da1afe9ba835bf46543
parent 321164 efb769687f96cfe21b7e1baab7d662e2ac0b68c9
child 512858 90e1f60d0b108746ea49741e4ac99c8457d3d177
push id9333
push userbgrinstead@mozilla.com
push dateTue, 12 Jan 2016 23:23:39 +0000
reviewersfitzgen
bugs1154874
milestone46.0a1
Bug 1154874 - Make the damp test easier to read and extend;r=fitzgen This provides some helpers for testSetup and testTeardown, gets rid of the method which built the tests with arrays of functions that returned promises and instead converts each one into a single function with Task.async
testing/talos/talos/tests/devtools/addon/content/damp.js
--- a/testing/talos/talos/tests/devtools/addon/content/damp.js
+++ b/testing/talos/talos/tests/devtools/addon/content/damp.js
@@ -1,16 +1,17 @@
 Components.utils.import("resource://devtools/client/framework/gDevTools.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm");
 
 const {devtools} =
   Components.utils.import("resource://devtools/shared/Loader.jsm", {});
 const { getActiveTab } = devtools.require("sdk/tabs/utils");
 const { getMostRecentBrowserWindow } = devtools.require("sdk/window/utils");
 const ThreadSafeChromeUtils = devtools.require("ThreadSafeChromeUtils");
+const {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
 
 const webserver = Services.prefs.getCharPref("addon.test.damp.webserver");
 
 const SIMPLE_URL = "chrome://damp/content/pages/simple.html";
 const COMPLICATED_URL = webserver + "/tests/tp5n/bild.de/www.bild.de/index.html";
 
 function Damp() {
   // Path to the temp file where the heap snapshot file is saved. Set by
@@ -138,126 +139,122 @@ Damp.prototype = {
     this._results.push({
       name: label + ".takeCensus",
       value: end - start
     });
 
     return Promise.resolve();
   },
 
-  _startTest: function() {
+  _getToolLoadingTests: function(url, label) {
+    let subtests = {
+      webconsoleOpen: Task.async(function*() {
+        yield this.testSetup(url);
+        yield this.openToolbox(label + ".webconsole", "webconsole");
+        yield this.reloadPage(label + ".webconsole");
+        yield this.closeToolbox(label + ".webconsole");
+        yield this.testTeardown();
+      }),
 
-    var self = this;
-    var openToolbox = this.openToolbox.bind(this);
-    var closeToolbox = this.closeToolbox.bind(this);
-    var reloadPage = this.reloadPage.bind(this);
-    var next = this._nextCommand.bind(this);
-    var saveHeapSnapshot = this.saveHeapSnapshot.bind(this);
-    var readHeapSnapshot = this.readHeapSnapshot.bind(this);
-    var takeCensus = this.takeCensus.bind(this);
-    var config = this._config;
-    var rest = config.rest; // How long to wait in between opening the tab and starting the test.
-
-    let tests = getTestsForURL(SIMPLE_URL, "simple");
-    tests = tests.concat(getTestsForURL(COMPLICATED_URL, "complicated"));
-
-    this._doSequence(tests, this._doneInternal);
-
-    function getTestsForURL(url, label) {
+      inspectorOpen: Task.async(function*() {
+        yield this.testSetup(url);
+        yield this.openToolbox(label + ".inspector", "inspector");
+        yield this.reloadPage(label + ".inspector");
+        yield this.closeToolbox(label + ".inspector");
+        yield this.testTeardown();
+      }),
 
-      // This is called before each subtest
-      let init = [
-        () => { self.addTab(url).then(next); },
-        () => { setTimeout(next, rest); },
-      ];
-
-      // This is called after each subtest
-      let restore = [
-        () => { self.closeCurrentTab(); next(); }
-      ];
+      debuggerOpen: Task.async(function*() {
+        yield this.testSetup(url);
+        yield this.openToolbox(label + ".jsdebugger", "jsdebugger");
+        yield this.reloadPage(label + ".jsdebugger");
+        yield this.closeToolbox(label + ".jsdebugger");
+        yield this.testTeardown();
+      }),
 
-      var subtests = {
+      styleEditorOpen: Task.async(function*() {
+        yield this.testSetup(url);
+        yield this.openToolbox(label + ".styleeditor", "styleeditor");
+        yield this.reloadPage(label + ".styleeditor");
+        yield this.closeToolbox(label + ".styleeditor");
+        yield this.testTeardown();
+      }),
 
-        webconsoleOpen: [
-          () => { openToolbox(label + ".webconsole", "webconsole").then(next); },
-          () => { reloadPage(label + ".webconsole").then(next); },
-          () => { closeToolbox(label + ".webconsole").then(next); },
-        ],
-
-        inspectorOpen: [
-          () => { openToolbox(label + ".inspector", "inspector").then(next); },
-          () => { reloadPage(label + ".inspector").then(next); },
-          () => { closeToolbox(label + ".inspector").then(next); },
-        ],
+      performanceOpen: Task.async(function*() {
+        yield this.testSetup(url);
+        yield this.openToolbox(label + ".performance", "performance");
+        yield this.reloadPage(label + ".performance");
+        yield this.closeToolbox(label + ".performance");
+        yield this.testTeardown();
+      }),
 
-        debuggerOpen: [
-          () => { openToolbox(label + ".jsdebugger", "jsdebugger").then(next); },
-          () => { reloadPage(label + ".jsdebugger").then(next); },
-          () => { closeToolbox(label + ".jsdebugger").then(next); },
-        ],
-
-        styleEditorOpen: [
-          () => { openToolbox(label + ".styleeditor", "styleeditor").then(next); },
-          () => { reloadPage(label + ".styleeditor").then(next); },
-          () => { closeToolbox(label + ".styleeditor").then(next); },
-        ],
+      netmonitorOpen: Task.async(function*() {
+        yield this.testSetup(url);
+        yield this.openToolbox(label + ".netmonitor", "netmonitor");
+        yield this.reloadPage(label + ".netmonitor");
+        yield this.closeToolbox(label + ".netmonitor");
+        yield this.testTeardown();
+      }),
 
-        performanceOpen: [
-          () => { openToolbox(label + ".performance", "performance").then(next); },
-          () => { reloadPage(label + ".performance").then(next); },
-          () => { closeToolbox(label + ".performance").then(next); },
-        ],
-
-        netmonitorOpen: [
-          () => { openToolbox(label + ".netmonitor", "netmonitor").then(next); },
-          () => { reloadPage(label + ".netmonitor").then(next); },
-          () => { closeToolbox(label + ".netmonitor").then(next); },
-        ],
+      saveAndReadHeapSnapshot: Task.async(function*() {
+        yield this.testSetup(url);
+        yield this.openToolbox(label + ".memory", "memory");
+        yield this.reloadPage(label + ".memory");
+        yield this.saveHeapSnapshot(label);
+        yield this.readHeapSnapshot(label);
+        yield this.takeCensus(label);
+        yield this.closeToolbox(label + ".memory");
+        yield this.testTeardown();
+      }),
+    };
 
-        saveAndReadHeapSnapshot: [
-          () => { openToolbox(label + ".memory", "memory").then(next); },
-          () => { reloadPage(label + ".memory").then(next); },
-          () => { saveHeapSnapshot(label).then(next); },
-          () => { readHeapSnapshot(label).then(next); },
-          () => { takeCensus(label).then(next); },
-          () => { closeToolbox(label + ".memory").then(next); },
-        ]
-      };
+    // Construct the sequence array: config.repeat times config.subtests
+    let config = this._config;
+    let sequenceArray = [];
+    for (var i in config.subtests) {
+      for (var r = 0; r < config.repeat; r++) {
+        if (!config.subtests[i] || !subtests[config.subtests[i]]) {
+          continue;
+        }
+
+        sequenceArray.push(subtests[config.subtests[i]]);
+      }
+    }
 
-      // Construct the sequence array: config.repeat times config.subtests,
-      // where each subtest implicitly starts with init.
-      sequenceArray = [];
-      for (var i in config.subtests) {
-        for (var r = 0; r < config.repeat; r++) {
-          sequenceArray = sequenceArray.concat(init);
-          sequenceArray = sequenceArray.concat(subtests[config.subtests[i]]);
-          sequenceArray = sequenceArray.concat(restore);
-        }
-      }
+    return sequenceArray;
+  },
 
-      return sequenceArray;
-    }
-  },
+  testSetup: Task.async(function*(url) {
+    yield this.addTab(url);
+    yield new Promise(resolve => {
+      setTimeout(resolve, this._config.rest);
+    });
+  }),
+
+  testTeardown: Task.async(function*(url) {
+    this.closeCurrentTab();
+    this._nextCommand();
+  }),
 
   // Everything below here are common pieces needed for the test runner to function,
   // just copy and pasted from Tart with /s/TART/DAMP
 
   _win: undefined,
   _dampTab: undefined,
   _results: [],
   _config: {subtests: [], repeat: 1, rest: 100},
   _nextCommandIx: 0,
   _commands: [],
   _onSequenceComplete: 0,
   _nextCommand: function() {
     if (this._nextCommandIx >= this._commands.length) {
       this._onSequenceComplete();
       return;
     }
-    this._commands[this._nextCommandIx++]();
+    this._commands[this._nextCommandIx++].call(this);
   },
   // Each command at the array a function which must call nextCommand once it's done
   _doSequence: function(commands, onComplete) {
     this._commands = commands;
     this._onSequenceComplete = onComplete;
     this._results = [];
     this._nextCommandIx = 0;
 
@@ -321,11 +318,14 @@ Damp.prototype = {
     const Ci = Components.interfaces;
     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
     this._win = wm.getMostRecentWindow("navigator:browser");
     this._dampTab = this._win.gBrowser.selectedTab;
     this._win.gBrowser.selectedBrowser.focus(); // Unfocus the URL bar to avoid caret blink
 
     Profiler.mark("DAMP - start", true);
 
-    return this._startTest();
+    let tests = [];
+    tests = tests.concat(this._getToolLoadingTests(SIMPLE_URL, "simple"));
+    tests = tests.concat(this._getToolLoadingTests(COMPLICATED_URL, "complicated"));
+    this._doSequence(tests, this._doneInternal);
   }
 }