Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Bug 817294 - Intermittent browser_styleeditor_loading.js | content do…
Browse files Browse the repository at this point in the history
…cument is still loading - Didn't expect complete, but got it | style editor root element has 'loading' class name | new style sheet button is disabled | import button is disabled r=jwalker a=bbajaj
  • Loading branch information
Michael Ratcliffe committed Feb 19, 2013
1 parent 88784b9 commit 8141819
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 166 deletions.
5 changes: 3 additions & 2 deletions browser/devtools/styleeditor/StyleEditor.jsm
Expand Up @@ -130,7 +130,7 @@ StyleEditor.prototype = {
{
let document = this.contentDocument;
if (this._styleSheetIndex == -1) {
for (let i = 0; i < document.styleSheets.length; ++i) {
for (let i = 0; i < document.styleSheets.length; i++) {
if (document.styleSheets[i] == this.styleSheet) {
this._styleSheetIndex = i;
break;
Expand Down Expand Up @@ -1004,8 +1004,9 @@ StyleEditor.prototype = {

// copy the list of listeners to allow adding/removing listeners in handlers
let listeners = this._actionListeners.concat();

// trigger all listeners that have this action handler
for (let i = 0; i < listeners.length; ++i) {
for (let i = 0; i < listeners.length; i++) {
let listener = listeners[i];
let actionHandler = listener["on" + aName];
if (actionHandler) {
Expand Down
131 changes: 58 additions & 73 deletions browser/devtools/styleeditor/StyleEditorChrome.jsm
Expand Up @@ -16,6 +16,7 @@ Cu.import("resource://gre/modules/PluralForm.jsm");
Cu.import("resource:///modules/devtools/StyleEditor.jsm");
Cu.import("resource:///modules/devtools/StyleEditorUtil.jsm");
Cu.import("resource:///modules/devtools/SplitView.jsm");
Cu.import("resource://gre/modules/commonjs/promise/core.js");

const STYLE_EDITOR_TEMPLATE = "stylesheet";

Expand Down Expand Up @@ -43,33 +44,42 @@ this.StyleEditorChrome = function StyleEditorChrome(aRoot, aContentWindow)
this._editors = [];
this._listeners = []; // @see addChromeListener

// Store the content window so that we can call the real contentWindow setter
// in the open method.
this._contentWindowTemp = aContentWindow;

this._contentWindow = null;
this._isContentAttached = false;
}

let initializeUI = function (aEvent) {
if (aEvent) {
this._window.removeEventListener("load", initializeUI, false);
}
StyleEditorChrome.prototype = {
_styleSheetToSelect: null,

let viewRoot = this._root.parentNode.querySelector(".splitview-root");
this._view = new SplitView(viewRoot);
open: function() {
let deferred = Promise.defer();
let initializeUI = function (aEvent) {
if (aEvent) {
this._window.removeEventListener("load", initializeUI, false);
}
let viewRoot = this._root.parentNode.querySelector(".splitview-root");
this._view = new SplitView(viewRoot);
this._setupChrome();

this._setupChrome();
// We need to juggle arount the contentWindow items because we need to
// trigger the setter at the appropriate time.
this.contentWindow = this._contentWindowTemp; // calls setter
this._contentWindowTemp = null;

// attach to the content window
this.contentWindow = aContentWindow;
this._contentWindowID = null;
}.bind(this);
deferred.resolve();
}.bind(this);

if (this._document.readyState == "complete") {
initializeUI();
} else {
this._window.addEventListener("load", initializeUI, false);
}
}
if (this._document.readyState == "complete") {
initializeUI();
} else {
this._window.addEventListener("load", initializeUI, false);
}

StyleEditorChrome.prototype = {
_styleSheetToSelect: null,
return deferred.promise;
},

/**
* Retrieve the content window attached to this chrome.
Expand Down Expand Up @@ -150,15 +160,6 @@ StyleEditorChrome.prototype = {
return this._contentWindow ? this._contentWindow.document : null;
},

/**
* Retrieve whether the content has been attached and StyleEditor instances
* exist for all of its stylesheets.
*
* @return boolean
* @see addChromeListener
*/
get isContentAttached() this._isContentAttached,

/**
* Retrieve an array with the StyleEditor instance for each live style sheet,
* ordered by style sheet index.
Expand All @@ -180,14 +181,6 @@ StyleEditorChrome.prototype = {
* Add a listener for StyleEditorChrome events.
*
* The listener implements IStyleEditorChromeListener := {
* onContentAttach: Called when a content window has been attached.
* All editors are instantiated, though they might
* not be loaded yet.
* Arguments: (StyleEditorChrome aChrome)
* @see contentWindow
* @see StyleEditor.isLoaded
* @see StyleEditor.addActionListener
*
* onContentDetach: Called when the content window has been detached.
* Arguments: (StyleEditorChrome aChrome)
* @see contentWindow
Expand Down Expand Up @@ -287,7 +280,7 @@ StyleEditorChrome.prototype = {

// (re)enable UI
let matches = this._root.querySelectorAll("toolbarbutton,input,select");
for (let i = 0; i < matches.length; ++i) {
for (let i = 0; i < matches.length; i++) {
matches[i].removeAttribute("disabled");
}
},
Expand All @@ -305,16 +298,14 @@ StyleEditorChrome.prototype = {
this._document.title = _("chromeWindowTitle",
document.title || document.location.href);

for (let i = 0; i < document.styleSheets.length; ++i) {
for (let i = 0; i < document.styleSheets.length; i++) {
let styleSheet = document.styleSheets[i];

let editor = new StyleEditor(document, styleSheet);
editor.addActionListener(this);
this._editors.push(editor);
}

this._triggerChromeListeners("ContentAttach");

// Queue editors loading so that ContentAttach is consistently triggered
// right after all editor instances are available (this.editors) but are
// NOT loaded/ready yet. This also helps responsivity during loading when
Expand Down Expand Up @@ -353,43 +344,36 @@ StyleEditorChrome.prototype = {

let select = function DEC_select(aEditor) {
let sheet = this._styleSheetToSelect.sheet;
let line = this._styleSheetToSelect.line;
let col = this._styleSheetToSelect.col;
let summary = sheet ? this.getSummaryElementForEditor(aEditor)
: this._view.getSummaryElementByOrdinal(0);

if (line) {
col = col || 1;

if (!aEditor.sourceEditor) {
// If a line or column was specified we move the caret appropriately.
let self = this;
aEditor.addActionListener({
onAttach: function SEC_selectSheet_onAttach()
{
aEditor.removeActionListener(this);
self.selectedStyleSheetIndex = aEditor.styleSheetIndex;
aEditor.sourceEditor.setCaretPosition(line - 1, col - 1);

let newSheet = self._styleSheetToSelect.sheet;
let newLine = self._styleSheetToSelect.line;
let newCol = self._styleSheetToSelect.col;
self._styleSheetToSelect = null;
if (newSheet != sheet) {
self._window.setTimeout(self.selectStyleSheet.bind(self, newSheet, newLine, newCol), 0);
}
}
});
} else {
// If a line or column was specified we move the caret appropriately.
let line = this._styleSheetToSelect.line || 1;
let col = this._styleSheetToSelect.col || 1;

if (!aEditor.sourceEditor) {
let onAttach = function SEC_selectSheet_onAttach() {
aEditor.removeActionListener(this);
this.selectedStyleSheetIndex = aEditor.styleSheetIndex;
aEditor.sourceEditor.setCaretPosition(line - 1, col - 1);

let newSheet = this._styleSheetToSelect.sheet;
let newLine = this._styleSheetToSelect.line;
let newCol = this._styleSheetToSelect.col;
this._styleSheetToSelect = null;
}
if (newSheet != sheet) {
this.selectStyleSheet.bind(this, newSheet, newLine, newCol);
}
}.bind(this);

aEditor.addActionListener({
onAttach: onAttach
});
} else {
// If a line or column was specified we move the caret appropriately.
aEditor.sourceEditor.setCaretPosition(line - 1, col - 1);
this._styleSheetToSelect = null;
}

this._view.activeSummary = summary;
let summary = sheet ? this.getSummaryElementForEditor(aEditor)
: this._view.getSummaryElementByOrdinal(0);
this._view.activeSummary = summary;
this.selectedStyleSheetIndex = aEditor.styleSheetIndex;
}.bind(this);

Expand All @@ -404,6 +388,7 @@ StyleEditorChrome.prototype = {
if ((sheet && aEditor.styleSheet == sheet) ||
(aEditor.styleSheetIndex == 0 && sheet == null)) {
aChrome.removeChromeListener(this);
aEditor.addActionListener(self);
select(aEditor);
}
}
Expand All @@ -430,7 +415,7 @@ StyleEditorChrome.prototype = {
_disableChrome: function SEC__disableChrome()
{
let matches = this._root.querySelectorAll("button,toolbarbutton,textbox");
for (let i = 0; i < matches.length; ++i) {
for (let i = 0; i < matches.length; i++) {
matches[i].setAttribute("disabled", "disabled");
}

Expand Down
16 changes: 12 additions & 4 deletions browser/devtools/styleeditor/StyleEditorPanel.jsm
Expand Up @@ -39,10 +39,14 @@ StyleEditorPanel.prototype = {
*/
open: function StyleEditor_open() {
let contentWin = this._toolbox.target.window;
this.setPage(contentWin);
this.isReady = true;
let deferred = Promise.defer();

return Promise.resolve(this);
this.setPage(contentWin).then(function() {
this.isReady = true;
deferred.resolve(this);
}.bind(this));

return deferred.promise;
},

/**
Expand All @@ -66,12 +70,16 @@ StyleEditorPanel.prototype = {
setPage: function StyleEditor_setPage(contentWindow) {
if (this._panelWin.styleEditorChrome) {
this._panelWin.styleEditorChrome.contentWindow = contentWindow;
this.selectStyleSheet(null, null, null);
} else {
let chromeRoot = this._panelDoc.getElementById("style-editor-chrome");
let chrome = new StyleEditorChrome(chromeRoot, contentWindow);
let promise = chrome.open();

this._panelWin.styleEditorChrome = chrome;
this.selectStyleSheet(null, null, null);
return promise;
}
this.selectStyleSheet(null, null, null);
},

/**
Expand Down
2 changes: 1 addition & 1 deletion browser/devtools/styleeditor/StyleEditorUtil.jsm
Expand Up @@ -152,7 +152,7 @@ this.wire = function wire(aRoot, aSelectorOrElement, aDescriptor)
aDescriptor = {events: {click: aDescriptor}};
}

for (let i = 0; i < matches.length; ++i) {
for (let i = 0; i < matches.length; i++) {
let element = matches[i];
forEach(aDescriptor.events, function (aName, aHandler) {
element.addEventListener(aName, aHandler, false);
Expand Down
1 change: 1 addition & 0 deletions browser/devtools/styleeditor/test/Makefile.in
Expand Up @@ -31,6 +31,7 @@ _BROWSER_TEST_FILES = \
four.html \
head.js \
helpers.js \
longload.html \
media.html \
media-small.css \
minified.html \
Expand Down
Expand Up @@ -19,12 +19,9 @@ function test()

addTabAndLaunchStyleEditorChromeWhenLoaded(function (aChrome) {
aChrome.addChromeListener({
onContentAttach: run,
onEditorAdded: testEditorAdded
});
if (aChrome.isContentAttached) {
run(aChrome);
}
run(aChrome);
});

content.location = TESTCASE_URI;
Expand Down
15 changes: 2 additions & 13 deletions browser/devtools/styleeditor/test/browser_styleeditor_init.js
Expand Up @@ -9,23 +9,17 @@ function test()
{
waitForExplicitFinish();

addTabAndLaunchStyleEditorChromeWhenLoaded(function (aChrome) {
launchStyleEditorChrome(function(aChrome) {
aChrome.addChromeListener({
onContentAttach: run,
onEditorAdded: testEditorAdded
});
if (aChrome.isContentAttached) {
run(aChrome);
}
run(aChrome);
});

content.location = TESTCASE_URI;
}

let gContentAttachHandled = false;
function run(aChrome)
{
gContentAttachHandled = true;
is(aChrome.contentWindow.document.readyState, "complete",
"content document is complete");

Expand All @@ -42,11 +36,6 @@ function run(aChrome)
let gEditorAddedCount = 0;
function testEditorAdded(aChrome, aEditor)
{
if (!gEditorAddedCount) {
is(gContentAttachHandled, true,
"ContentAttach event triggered before EditorAdded");
}

if (aEditor.styleSheetIndex == 0) {
gEditorAddedCount++;
testFirstStyleSheetEditor(aChrome, aEditor);
Expand Down

0 comments on commit 8141819

Please sign in to comment.