michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const TESTCASE_URI = TEST_BASE + "simple.html"; michael@0: michael@0: let TRANSITION_CLASS = "moz-styleeditor-transitioning"; michael@0: let TESTCASE_CSS_SOURCE = "body{background-color:red;"; michael@0: michael@0: let gUI; michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: addTabAndCheckOnStyleEditorAdded(panel => gUI = panel.UI, testEditorAdded); michael@0: michael@0: content.location = TESTCASE_URI; michael@0: } michael@0: michael@0: let gAddedCount = 0; // to add new stylesheet after the 2 initial stylesheets michael@0: let gNewEditor; // to make sure only one new stylesheet got created michael@0: let gOriginalHref; michael@0: michael@0: let checksCompleted = 0; michael@0: michael@0: function testEditorAdded(aEditor) michael@0: { michael@0: info("added " + gAddedCount + " editors"); michael@0: if (++gAddedCount == 2) { michael@0: waitForFocus(function () {// create a new style sheet michael@0: let newButton = gPanelWindow.document.querySelector(".style-editor-newButton"); michael@0: ok(newButton, "'new' button exists"); michael@0: michael@0: EventUtils.synthesizeMouseAtCenter(newButton, {}, gPanelWindow); michael@0: }, gPanelWindow); michael@0: } michael@0: if (gAddedCount < 3) { michael@0: return; michael@0: } michael@0: michael@0: ok(!gNewEditor, "creating a new stylesheet triggers one EditorAdded event"); michael@0: gNewEditor = aEditor; // above test will fail if we get a duplicate event michael@0: michael@0: is(gUI.editors.length, 3, michael@0: "creating a new stylesheet added a new StyleEditor instance"); michael@0: michael@0: aEditor.styleSheet.once("style-applied", function() { michael@0: // when changes have been completely applied to live stylesheet after transisiton michael@0: ok(!content.document.documentElement.classList.contains(TRANSITION_CLASS), michael@0: "StyleEditor's transition class has been removed from content"); michael@0: michael@0: if (++checksCompleted == 3) { michael@0: cleanup(); michael@0: } michael@0: }); michael@0: michael@0: aEditor.styleSheet.on("property-change", function(property) { michael@0: if (property == "ruleCount") { michael@0: let ruleCount = aEditor.summary.querySelector(".stylesheet-rule-count").textContent; michael@0: is(parseInt(ruleCount), 1, michael@0: "new editor shows 1 rule after modification"); michael@0: michael@0: if (++checksCompleted == 3) { michael@0: cleanup(); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: aEditor.getSourceEditor().then(testEditor); michael@0: } michael@0: michael@0: function testEditor(aEditor) { michael@0: waitForFocus(function () { michael@0: gOriginalHref = aEditor.styleSheet.href; michael@0: michael@0: let summary = aEditor.summary; michael@0: michael@0: ok(aEditor.sourceLoaded, michael@0: "new editor is loaded when attached"); michael@0: ok(aEditor.isNew, michael@0: "new editor has isNew flag"); michael@0: michael@0: ok(aEditor.sourceEditor.hasFocus(), michael@0: "new editor has focus"); michael@0: michael@0: let summary = aEditor.summary; michael@0: let ruleCount = summary.querySelector(".stylesheet-rule-count").textContent; michael@0: is(parseInt(ruleCount), 0, michael@0: "new editor initially shows 0 rules"); michael@0: michael@0: let computedStyle = content.getComputedStyle(content.document.body, null); michael@0: is(computedStyle.backgroundColor, "rgb(255, 255, 255)", michael@0: "content's background color is initially white"); michael@0: michael@0: for each (let c in TESTCASE_CSS_SOURCE) { michael@0: EventUtils.synthesizeKey(c, {}, gPanelWindow); michael@0: } michael@0: michael@0: ok(aEditor.unsaved, michael@0: "new editor has unsaved flag"); michael@0: michael@0: // we know that the testcase above will start a CSS transition michael@0: content.addEventListener("transitionend", onTransitionEnd, false); michael@0: }, gPanelWindow) ; michael@0: } michael@0: michael@0: function onTransitionEnd() { michael@0: content.removeEventListener("transitionend", onTransitionEnd, false); michael@0: michael@0: is(gNewEditor.sourceEditor.getText(), TESTCASE_CSS_SOURCE + "}", michael@0: "rule bracket has been auto-closed"); michael@0: michael@0: let computedStyle = content.getComputedStyle(content.document.body, null); michael@0: is(computedStyle.backgroundColor, "rgb(255, 0, 0)", michael@0: "content's background color has been updated to red"); michael@0: michael@0: if (gNewEditor) { michael@0: is(gNewEditor.styleSheet.href, gOriginalHref, michael@0: "style sheet href did not change"); michael@0: } michael@0: michael@0: if (++checksCompleted == 3) { michael@0: cleanup(); michael@0: } michael@0: } michael@0: michael@0: function cleanup() { michael@0: gNewEditor = null; michael@0: gUI = null; michael@0: finish(); michael@0: }