From 140e460f6396972ed0e386f79a64b09c7213dbba Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Sat, 15 Oct 2011 23:12:45 +0200 Subject: [PATCH] add a test, use Services --- content/mandelbrot.js | 75 +++++++++++++++--------------- content/mandelbrot.xul | 2 +- tests/Makefile.in | 1 + tests/browser_mandelbrot_basics.js | 72 ++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 38 deletions(-) create mode 100644 tests/browser_mandelbrot_basics.js diff --git a/content/mandelbrot.js b/content/mandelbrot.js index 68cdffe..7fd6b6e 100644 --- a/content/mandelbrot.js +++ b/content/mandelbrot.js @@ -37,10 +37,9 @@ * * ***** END LICENSE BLOCK ***** */ +Components.utils.import("resource://gre/modules/Services.jsm"); + var gColorPalette = []; -var gPref = Components.classes["@mozilla.org/preferences-service;1"] - .getService(Components.interfaces.nsIPrefService) - .getBranch(null); var gStartTime = 0; var gMbrotBundle; var gCurrentImageData; @@ -61,6 +60,8 @@ function Startup() { img.addEventListener("touchcancel", imgEvHandler, false); img.addEventListener("touchleave", imgEvHandler, false); img.addEventListener("touchmove", imgEvHandler, false); + + Services.obs.notifyObservers(window, "mandelbrot-loaded", null); } function getAdjustPref(prefname) { @@ -70,48 +71,48 @@ function getAdjustPref(prefname) { case "image.height": value = 0; try { - value = gPref.getIntPref("mandelbrot." + prefname); + value = Services.prefs.getIntPref("mandelbrot." + prefname); } catch (e) { } if ((value < 10) || (value > 5000)) { value = 300; - gPref.setIntPref("mandelbrot." + prefname, value); + Services.prefs.setIntPref("mandelbrot." + prefname, value); } return value; case "last_image.Cr_*": let Cr_min = -2.0; let Cr_max = 1.0; try { - Cr_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min")); - Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max")); + Cr_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_min")); + Cr_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_max")); } catch (e) { } if ((Cr_min < -3) || (Cr_min > 2) || (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) { Cr_min = -2.0; Cr_max = 1.0; } - gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min); - gPref.setCharPref("mandelbrot.last_image.Cr_max", Cr_max); + Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", Cr_min); + Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", Cr_max); return {Cr_min: Cr_min, Cr_max: Cr_max}; case "last_image.Ci_*": let Ci_min = -1.5; let Ci_max = 1.5; try { - Ci_min = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min")); - Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max")); + Ci_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_min")); + Ci_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_max")); } catch (e) { } if ((Ci_min < -2.5) || (Ci_min > 2.5) || (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) { Ci_min = -1.5; Ci_max = 1.5; } - gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min); - gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max); + Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", Ci_min); + Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", Ci_max); return {Ci_min: Ci_min, Ci_max: Ci_max}; case "iteration_max": value = 500; try { - value = gPref.getIntPref("mandelbrot." + prefname); + value = Services.prefs.getIntPref("mandelbrot." + prefname); } catch (e) { setIter(value); @@ -124,7 +125,7 @@ function getAdjustPref(prefname) { case "use_algorithm": value = "numeric"; try { - value = gPref.getCharPref("mandelbrot." + prefname); + value = Services.prefs.getCharPref("mandelbrot." + prefname); } catch (e) { setAlgorithm(value); @@ -133,7 +134,7 @@ function getAdjustPref(prefname) { case "color_palette": value = "kairo"; try { - value = gPref.getCharPref("mandelbrot." + prefname); + value = Services.prefs.getCharPref("mandelbrot." + prefname); } catch(e) { setPalette(value); @@ -142,10 +143,10 @@ function getAdjustPref(prefname) { case "syncProportions": value = true; try { - value = gPref.getBoolPref("mandelbrot." + prefname); + value = Services.prefs.getBoolPref("mandelbrot." + prefname); } catch(e) { - gPref.setBoolPref("mandelbrot." + prefname, value); + Services.prefs.setBoolPref("mandelbrot." + prefname, value); } return value; default: @@ -179,10 +180,10 @@ function adjustCoordsAndDraw(aC_min, aC_max) { let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight); - gPref.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2); - gPref.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2); - gPref.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2); - gPref.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2); + Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2); + Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2); + Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2); + Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2); drawImage(); } @@ -620,7 +621,7 @@ function callBookmark(evtarget) { connection.close(); if (iterMax && C_min && C_max) { - gPref.setIntPref("mandelbrot.iteration_max", iterMax); + Services.prefs.setIntPref("mandelbrot.iteration_max", iterMax); adjustCoordsAndDraw(C_min, C_max); } } @@ -686,7 +687,7 @@ function updateIterMenu() { } function setIter(aIter) { - gPref.setIntPref("mandelbrot.iteration_max", aIter); + Services.prefs.setIntPref("mandelbrot.iteration_max", aIter); } function updatePaletteMenu() { @@ -708,7 +709,7 @@ function updatePaletteMenu() { } function setPalette(aPaletteID) { - gPref.setCharPref("mandelbrot.color_palette", aPaletteID); + Services.prefs.setCharPref("mandelbrot.color_palette", aPaletteID); gColorPalette = getColorPalette(aPaletteID); } @@ -729,12 +730,12 @@ function updateDebugMenu() { let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome"; try { // This throws in versions that don't have JaegerMonkey yet --> catch block - gPref.getBoolPref("javascript.options.methodjit." + scope); + Services.prefs.getBoolPref("javascript.options.methodjit." + scope); // We have JaegerMonkey, i.e. two prefs for trace/method JIT for each (let type in ["tracejit", "methodjit"]) { let jitMenuItem = document.getElementById(type + "Enabled"); - jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options." + type + "." + scope)); + jitMenuItem.setAttribute("checked", Services.prefs.getBoolPref("javascript.options." + type + "." + scope)); } } catch (e) { @@ -743,15 +744,15 @@ function updateDebugMenu() { document.getElementById(type + "Enabled").hidden = true; let jitMenuItem = document.getElementById("jitEnabled"); jitMenuItem.hidden = false; - jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit." + scope)); + jitMenuItem.setAttribute("checked", Services.prefs.getBoolPref("javascript.options.jit." + scope)); } } function toggleJITState(jitMenuItem, jittype) { let scope = (document.getElementById("mandelbrotWindow").nodeName == "page") ? "content" : "chrome"; let jitpref = "javascript.options." + jittype + "jit." + scope; - let jitEnabled = !gPref.getBoolPref(jitpref); - gPref.setBoolPref(jitpref, jitEnabled) + let jitEnabled = !Services.prefs.getBoolPref(jitpref); + Services.prefs.setBoolPref(jitpref, jitEnabled) jitMenuItem.setAttribute("checked", jitEnabled ? "true" : "false"); } @@ -772,7 +773,7 @@ function updateAlgoMenu() { } function setAlgorithm(algoID) { - gPref.setCharPref("mandelbrot.use_algorithm", algoID); + Services.prefs.setCharPref("mandelbrot.use_algorithm", algoID); } function initImgSettings() { @@ -806,15 +807,15 @@ function closeImgSettings() { function saveImgSettings() { // Get values to prefs. for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) { - gPref.setCharPref("mandelbrot.last_image." + coord, - document.getElementById("is_" + coord).value); + Services.prefs.setCharPref("mandelbrot.last_image." + coord, + document.getElementById("is_" + coord).value); } for each (let dim in ["width", "height"]) { - gPref.setIntPref("mandelbrot.image." + dim, - document.getElementById("is_img_" + dim).value); + Services.prefs.setIntPref("mandelbrot.image." + dim, + document.getElementById("is_img_" + dim).value); } - gPref.setBoolPref("mandelbrot.syncProportions", - document.getElementById("is_syncProp").checked); + Services.prefs.setBoolPref("mandelbrot.syncProportions", + document.getElementById("is_syncProp").checked); } function checkISValue(textbox, type) { diff --git a/content/mandelbrot.xul b/content/mandelbrot.xul index 47f8c59..b397c8c 100644 --- a/content/mandelbrot.xul +++ b/content/mandelbrot.xul @@ -51,7 +51,7 @@ diff --git a/tests/Makefile.in b/tests/Makefile.in index 20a13ee..273ee7a 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -44,6 +44,7 @@ relativesrcdir = extensions/mandelbrot/tests include $(topsrcdir)/config/rules.mk _BROWSER_FILES = \ + browser_mandelbrot_basics.js \ $(NULL) libs:: $(_BROWSER_FILES) diff --git a/tests/browser_mandelbrot_basics.js b/tests/browser_mandelbrot_basics.js new file mode 100644 index 0000000..76c2f3c --- /dev/null +++ b/tests/browser_mandelbrot_basics.js @@ -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(); +} +]; -- 2.35.3