add a test, use Services
[mandelbrot.git] / tests / browser_mandelbrot_basics.js
diff --git a/tests/browser_mandelbrot_basics.js b/tests/browser_mandelbrot_basics.js
new file mode 100644 (file)
index 0000000..76c2f3c
--- /dev/null
@@ -0,0 +1,72 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// Test some basic functionality of KaiRo-Mandelbrot.
+
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+const MBROT_LOADED = "mandelbrot-loaded";
+const TEST_DONE = "mandelbrot-test-done";
+
+function test() {
+  gBrowser.addTab();
+  // Open KaiRo-Mandelbrot, testing the menu item.
+  let menuitem = document.getElementById("tasksMenuMandelbrot") ||
+                 document.getElementById("menu_openMandelbrot");
+  menuitem.click();
+
+  var testIndex = 0;
+  var win;
+
+  let testObs = {
+    observe: function(aSubject, aTopic, aData) {
+      if (aTopic == MBROT_LOADED) {
+        Services.obs.removeObserver(testObs, MBROT_LOADED);
+        ok(true, "KaiRo-Mandelbrot is loaded");
+
+        win = content.wrappedJSObject;
+        Services.obs.addObserver(testObs, TEST_DONE, false);
+        // Trigger the first test now!
+        Services.obs.notifyObservers(window, TEST_DONE, null);
+      }
+      else {
+        // TEST_DONE triggered, run next test
+        info("run test #" + (testIndex + 1) + " of " + testFuncs.length +
+             " (" + testFuncs[testIndex].name + ")");
+        setTimeout(testFuncs[testIndex++], 0, win);
+
+        if (testIndex >= testFuncs.length) {
+          // Finish this up!
+          Services.obs.removeObserver(testObs, TEST_DONE);
+          Services.cookies.removeAll();
+          gLocSvc.fhist.removeAllEntries();
+          setTimeout(finish, 0);
+        }
+      }
+    }
+  };
+  waitForExplicitFinish();
+  Services.obs.addObserver(testObs, MBROT_LOADED, false);
+}
+
+var testFuncs = [
+function test_open_state(aWin) {
+  is(aWin.document.documentElement.id, "mandelbrot-page",
+     "The active tab is KaiRo-Mandelbrot");
+  is(aWin.document.getElementById("drawButton").hidden, false,
+     "The draw button is not hidden");
+  Services.obs.notifyObservers(window, TEST_DONE, null);
+},
+
+function test_close(aWin) {
+  function dmWindowClosedListener() {
+    aWin.removeEventListener("unload", dmWindowClosedListener, false);
+    isnot(content.document.documentElement.id, "mandelbrot-page",
+       "The active tab is not KaiRo-Mandelbrot");
+    Services.obs.notifyObservers(window, TEST_DONE, null);
+  }
+  aWin.addEventListener("unload", dmWindowClosedListener, false);
+  aWin.close();
+}
+];