From 5ef7980af78a3ae795da4f5d36435be27397a2fb Mon Sep 17 00:00:00 2001 From: Gijs Kruitbosch Date: Mon, 3 Jun 2013 22:12:00 +0200 Subject: [PATCH] Bug 877178 - Tests for unregisterArea, r=jaws --HG-- extra : rebase_source : a7ff14159918566306be0097ee0694941d07e30a --- .../customizableui/test/Makefile.in | 1 + .../test/browser_877178_unregisterArea.js | 64 +++++++++++++++++++ .../components/customizableui/test/head.js | 16 +++-- 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 browser/components/customizableui/test/browser_877178_unregisterArea.js diff --git a/browser/components/customizableui/test/Makefile.in b/browser/components/customizableui/test/Makefile.in index 40f2a0dff198..7a6a814e03b9 100644 --- a/browser/components/customizableui/test/Makefile.in +++ b/browser/components/customizableui/test/Makefile.in @@ -12,6 +12,7 @@ include $(DEPTH)/config/autoconf.mk MOCHITEST_BROWSER_FILES = \ browser_873501_handle_specials.js \ + browser_877178_unregisterArea.js \ head.js \ $(NULL) diff --git a/browser/components/customizableui/test/browser_877178_unregisterArea.js b/browser/components/customizableui/test/browser_877178_unregisterArea.js new file mode 100644 index 000000000000..ddaa7cd1aa53 --- /dev/null +++ b/browser/components/customizableui/test/browser_877178_unregisterArea.js @@ -0,0 +1,64 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +let gTests = [ + { + desc: "Sanity checks", + run: function() { + SimpleTest.doesThrow(function() CustomizableUI.registerArea("@foo"), + "Registering areas with an invalid ID should throw."); + + SimpleTest.doesThrow(function() CustomizableUI.registerArea([]), + "Registering areas with an invalid ID should throw."); + + SimpleTest.doesThrow(function() CustomizableUI.registerArea(CustomizableUI.AREA_NAVBAR), + "Registering an area with an ID that's already registered should throw."); + + SimpleTest.doesThrow(function() CustomizableUI.unregisterArea("@foo"), + "Unregistering areas with an invalid ID should throw."); + + SimpleTest.doesThrow(function() CustomizableUI.unregisterArea([]), + "Unregistering areas with an invalid ID should throw."); + + SimpleTest.doesThrow(function() CustomizableUI.unregisterArea("unknown"), + "Unregistering an area that's not registered should throw."); + } + }, + { + desc: "Check areas are loaded with their default placements.", + run: function() { + ok(CustomizableUI.inDefaultState, "Everything should be in its default state."); + } + }, + { + desc: "Check registering and unregistering a new area.", + run: function() { + const kToolbarId = "test-registration-toolbar"; + const kButtonId = "test-registration-button"; + createDummyXULButton(kButtonId); + createToolbarWithPlacements(kToolbarId, ["spring", kButtonId, "spring"]); + assertAreaPlacements(kToolbarId, + [/customizableui-special-spring\d+/, + kButtonId, + /customizableui-special-spring\d+/]); + ok(CustomizableUI.inDefaultState, "With a new toolbar and default placements, " + + "everything should still be in a default state."); + removeCustomToolbars(); // Will call unregisterArea for us + ok(CustomizableUI.inDefaultState, "When the toolbar is unregistered, " + + "everything should still be in a default state."); + } + } +]; + +function cleanup() { + removeCustomToolbars(); + resetCustomization(); +} + +function test() { + waitForExplicitFinish(); + registerCleanupFunction(cleanup); + runTests(gTests); +} + diff --git a/browser/components/customizableui/test/head.js b/browser/components/customizableui/test/head.js index e163039aeb12..e55e558e2370 100644 --- a/browser/components/customizableui/test/head.js +++ b/browser/components/customizableui/test/head.js @@ -11,22 +11,30 @@ let {Task, CustomizableUI} = tmp; const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; +function createDummyXULButton(id, label) { + let btn = document.createElementNS(kNSXUL, "toolbarbutton"); + btn.id = id; + btn.setAttribute("label", label || id); + btn.className = "toolbarbutton-1 chromeclass-toolbar-additional"; + window.gNavToolbox.palette.appendChild(btn); + return btn; +} + function createToolbarWithPlacements(id, placements) { let tb = document.createElementNS(kNSXUL, "toolbar"); tb.id = id; tb.setAttribute("customizable", "true"); document.getElementById("customToolbars").appendChild(tb); CustomizableUI.registerArea(id, { - type: CustomizableUI.TYPE_TOOLBAR + type: CustomizableUI.TYPE_TOOLBAR, + defaultPlacements: placements }); - for (let p of placements) { - CustomizableUI.addWidgetToArea(p, id); - } } function removeCustomToolbars() { let customToolbarSet = document.getElementById("customToolbars"); while (customToolbarSet.lastChild) { + CustomizableUI.unregisterArea(customToolbarSet.lastChild.id); customToolbarSet.lastChild.remove(); } }