Refactorings to makeEventOrderTest() interface to make the tests easier to read. Hopefully. Maybe.
authorAtul Varma <varmaa@gmail.com>
Tue, 30 Nov 2010 11:43:35 -0500
changeset 1147 72a1521c2de76d2f318589858fbbaf4e6703bbb6
parent 1146 39c67ae4303d7aba3688731115b937ee6716be5b
child 1148 4a3c87093ba3e61c10a970eb8fe58facd5fbb62a
push id471
push userbwarner@mozilla.com
push dateTue, 30 Nov 2010 22:30:12 +0000
Refactorings to makeEventOrderTest() interface to make the tests easier to read. Hopefully. Maybe.
packages/addon-kit/tests/test-panel.js
--- a/packages/addon-kit/tests/test-panel.js
+++ b/packages/addon-kit/tests/test-panel.js
@@ -131,79 +131,61 @@ tests.testSeveralShowHides = function(te
   });
   panel.on('error', function(e) {
     test.fail('error was emitted:' + e.message + '\n' + e.stack);
   });
   panel.show();
 };
 
 function makeEventOrderTest(options) {
-  let expectedEvents = options.expect.slice();
+  let expectedEvents = [];
 
   return function(test) {
     let panel = panels.Panel({ contentURL: "about:buildconfig" });
 
-    function when(event, cb) {
+    function expect(event, cb) {
+      expectedEvents.push(event);
       panel.on(event, function() {
         test.assertEqual(event, expectedEvents.shift());
-        require("timer").setTimeout(cb, 1);
+        if (cb)
+          require("timer").setTimeout(cb, 1);
       });
+      return {then: expect};
     }
 
     test.waitUntilDone();
-    options.test(test, when, panel);
+    options.test(test, expect, panel);
   }
 }
 
 tests.testWaitForInitThenShowThenDestroy = makeEventOrderTest({
-  expect: ['inited', 'show', 'hide'],
-  test: function(test, when, panel) {
-    when('inited', function() { panel.show(); });
-    when('show', function() { panel.destroy(); });
-    when('hide', function() { test.done(); });
+  test: function(test, expect, panel) {
+    expect('inited', function() { panel.show(); }).
+      then('show', function() { panel.destroy(); }).
+      then('hide', function() { test.done(); });
   }
 });
 
 tests.testShowThenWaitForInitThenDestroy = makeEventOrderTest({
-  expect: ['inited', 'show', 'hide'],
-  test: function(test, when, panel) {
+  test: function(test, expect, panel) {
     panel.show();
-    when('inited', function() {});
-    when('show', function() { panel.destroy(); });
-    when('hide', function() { test.done(); });
+    expect('inited').
+      then('show', function() { panel.destroy(); }).
+      then('hide', function() { test.done(); });
   }
 });
 
 tests.testShowThenHideThenDestroy = makeEventOrderTest({
-  expect: ['show', 'hide'],
-  test: function(test, when, panel) {
+  test: function(test, expect, panel) {
     panel.show();
-    when('show', function() { panel.hide(); });
-    when('hide', function() { panel.destroy(); test.done(); });
+    expect('show', function() { panel.hide(); }).
+      then('hide', function() { panel.destroy(); test.done(); });
   }
 });
 
-function(test) {
-  let panel = panels.Panel({ contentURL: "about:buildconfig" });
-  let expectedEvents = ['inited', 'show', 'hide'];
-
-  function when(event, cb) {
-    panel.on(event, function() {
-      test.assertEqual(event, expectedEvents.shift());
-      require("timer").setTimeout(cb, 1);
-    });
-  }
-
-  test.waitUntilDone();
-
-  when('inited', function() { panel.show(); });
-  when('show', function() { panel.destroy(); });
-  when('hide', function() { test.done(); });
-};
-
 tests.testContentURLOption = function(test) {
   const URL_STRING = "about:buildconfig";
   const HTML_CONTENT = "<html><title>Test</title><p>This is a test.</p></html>";
 
   let (panel = Panel({ contentURL: URL_STRING })) {
     test.pass("contentURL accepts a string URL.");
     test.assert(panel.contentURL instanceof URL,
                 "contentURL is a URL object.");