Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1471764 - Add unit tests for the toggling the flexbox and grid hi…
Browse files Browse the repository at this point in the history
…ghlighter from the markup display badges. r=jdescottes
  • Loading branch information
gabrielluong committed Sep 3, 2018
1 parent 95050e8 commit f01fd71
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 116 deletions.
1 change: 1 addition & 0 deletions devtools/.eslintrc.mochitests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"synthesizeKeyFromKeyTag": true,
"TargetFactory": true,
"waitForTick": true,
"waitUntilState": true,
},

"parserOptions": {
Expand Down
23 changes: 11 additions & 12 deletions devtools/client/inspector/computed/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

"use strict";

const promise = require("promise");
const flags = require("devtools/shared/flags");
const ToolDefinitions = require("devtools/client/definitions").Tools;
const CssLogic = require("devtools/shared/inspector/css-logic");
const {ELEMENT_STYLE} = require("devtools/shared/specs/styles");
const promise = require("promise");
const OutputParser = require("devtools/client/shared/output-parser");
const {PrefObserver} = require("devtools/client/shared/prefs");
const {createChild} = require("devtools/client/inspector/shared/utils");
Expand Down Expand Up @@ -183,14 +184,20 @@ function CssComputedView(inspector, document, pageStyle) {
this.styleDocument.addEventListener("mousedown", this.focusWindow);
this.element.addEventListener("click", this._onClick);
this.element.addEventListener("contextmenu", this._onContextMenu);
this.element.addEventListener("mousemove", () => {
this.addHighlightersToView();
}, { once: true });
this.searchField.addEventListener("input", this._onFilterStyles);
this.searchClearButton.addEventListener("click", this._onClearSearch);
this.includeBrowserStylesCheckbox.addEventListener("input",
this._onIncludeBrowserStyles);

if (flags.testing) {
// In tests, we start listening immediately to avoid having to simulate a mousemove.
this.highlighters.addToView(this);
} else {
this.element.addEventListener("mousemove", () => {
this.highlighters.addToView(this);
}, { once: true });
}

this.searchClearButton.hidden = true;

// No results text.
Expand Down Expand Up @@ -732,14 +739,6 @@ CssComputedView.prototype = {
}
},

/**
* Adds the highlighters overlay to the computed view. This is called by the "mousemove"
* event handler and in shared-head.js when opening and selecting the computed view.
*/
addHighlightersToView() {
this.highlighters.addToView(this);
},

/**
* Destructor for CssComputedView.
*/
Expand Down
133 changes: 59 additions & 74 deletions devtools/client/inspector/flexbox/flexbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"use strict";

const { throttle } = require("devtools/client/inspector/shared/utils");
const flags = require("devtools/shared/flags");

const {
clearFlexbox,
Expand Down Expand Up @@ -62,10 +63,16 @@ class FlexboxInspector {
return;
}

this.document.addEventListener("mousemove", () => {
if (flags.testing) {
// In tests, we start listening immediately to avoid having to simulate a mousemove.
this.highlighters.on("flexbox-highlighter-hidden", this.onHighlighterHidden);
this.highlighters.on("flexbox-highlighter-shown", this.onHighlighterShown);
}, { once: true });
} else {
this.document.addEventListener("mousemove", () => {
this.highlighters.on("flexbox-highlighter-hidden", this.onHighlighterHidden);
this.highlighters.on("flexbox-highlighter-shown", this.onHighlighterShown);
}, { once: true });
}

this.inspector.sidebar.on("select", this.onSidebarSelect);

Expand Down Expand Up @@ -304,99 +311,77 @@ class FlexboxInspector {
async update(flexboxFront) {
// Stop refreshing if the inspector or store is already destroyed or no node is
// selected.
if (!this.inspector || !this.store || !this.inspector.selection.nodeFront) {
if (!this.inspector ||
!this.store ||
!this.inspector.selection.nodeFront ||
!this.hasGetCurrentFlexbox) {
return;
}

// Fetch the current flexbox if no flexbox front was passed into this update.
if (!flexboxFront) {
try {
if (!this.hasGetCurrentFlexbox) {
return;
}

try {
// Fetch the current flexbox if no flexbox front was passed into this update.
if (!flexboxFront) {
flexboxFront = await this.layoutInspector.getCurrentFlexbox(
this.inspector.selection.nodeFront);
} catch (e) {
// This call might fail if called asynchrously after the toolbox is finished
// closing.
return;
}
}

// Clear the flexbox panel if there is no flex container for the current node
// selection.
if (!flexboxFront) {
try {
// Clear the flexbox panel if there is no flex container for the current node
// selection.
if (!flexboxFront) {
this.store.dispatch(clearFlexbox());
} catch (e) {
// This call might fail if called asynchrously after the toolbox is finished
// closing.
return;
}
return;
}

let containerNodeFront = flexboxFront.containerNodeFront;

// If the FlexboxFront doesn't yet have access to the NodeFront for its container,
// then get it from the walker. This happens when the walker hasn't seen this
// particular DOM Node in the tree yet or when we are connected to an older server.
if (!containerNodeFront) {
try {
// If the FlexboxFront doesn't yet have access to the NodeFront for its container,
// then get it from the walker. This happens when the walker hasn't seen this
// particular DOM Node in the tree yet or when we are connected to an older server.
let containerNodeFront = flexboxFront.containerNodeFront;
if (!containerNodeFront) {
containerNodeFront = await this.walker.getNodeFromActor(flexboxFront.actorID,
["containerEl"]);
} catch (e) {
// This call might fail if called asynchrously after the toolbox is finished
// closing.
return;
}
}

const highlighted = this._highlighters &&
containerNodeFront == this.highlighters.flexboxHighlighterShown;

// Fetch the flex items for the given flex container and the flex item NodeFronts.
const flexItems = [];
const flexItemFronts = await flexboxFront.getFlexItems();

for (const flexItemFront of flexItemFronts) {
let itemNodeFront = flexItemFront.nodeFront;
// Fetch the flex items for the given flex container and the flex item NodeFronts.
const flexItems = [];
const flexItemFronts = await flexboxFront.getFlexItems();

if (!itemNodeFront) {
try {
for (const flexItemFront of flexItemFronts) {
let itemNodeFront = flexItemFront.nodeFront;
if (!itemNodeFront) {
itemNodeFront = await this.walker.getNodeFromActor(flexItemFront.actorID,
["element"]);
} catch (e) {
// This call might fail if called asynchrously after the toolbox is finished
// closing.
return;
}

flexItems.push({
actorID: flexItemFront.actorID,
shown: false,
flexItemSizing: flexItemFront.flexItemSizing,
nodeFront: itemNodeFront,
properties: flexItemFront.properties,
});
}

flexItems.push({
actorID: flexItemFront.actorID,
shown: false,
flexItemSizing: flexItemFront.flexItemSizing,
nodeFront: itemNodeFront,
properties: flexItemFront.properties,
});
const highlighted = this._highlighters &&
containerNodeFront == this.highlighters.flexboxHighlighterShown;
const currentUrl = this.inspector.target.url;
// Get the hostname, if there is no hostname, fall back on protocol
// ex: `data:` uri, and `about:` pages
const hostname = parseURL(currentUrl).hostname || parseURL(currentUrl).protocol;
const customColors = await this.getCustomFlexboxColors();
const color = customColors[hostname] ? customColors[hostname] : FLEXBOX_COLOR;

this.store.dispatch(updateFlexbox({
actorID: flexboxFront.actorID,
color,
flexItems,
highlighted,
nodeFront: containerNodeFront,
properties: flexboxFront.properties,
}));
} catch (e) {
// This call might fail if called asynchrously after the toolbox is finished
// closing.
}

const currentUrl = this.inspector.target.url;
// Get the hostname, if there is no hostname, fall back on protocol
// ex: `data:` uri, and `about:` pages
const hostname = parseURL(currentUrl).hostname || parseURL(currentUrl).protocol;
const customColors = await this.getCustomFlexboxColors();
const color = customColors[hostname] ? customColors[hostname] : FLEXBOX_COLOR;

this.store.dispatch(updateFlexbox({
actorID: flexboxFront.actorID,
color,
flexItems,
highlighted,
nodeFront: containerNodeFront,
properties: flexboxFront.properties,
}));
}
}

Expand Down
11 changes: 9 additions & 2 deletions devtools/client/inspector/grids/grid-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const Services = require("Services");
const { throttle } = require("devtools/client/inspector/shared/utils");
const flags = require("devtools/shared/flags");

const {
updateGridColor,
Expand Down Expand Up @@ -97,10 +98,16 @@ class GridInspector {
return;
}

this.document.addEventListener("mousemove", () => {
if (flags.testing) {
// In tests, we start listening immediately to avoid having to simulate a mousemove.
this.highlighters.on("grid-highlighter-hidden", this.onHighlighterHidden);
this.highlighters.on("grid-highlighter-shown", this.onHighlighterShown);
}, { once: true });
} else {
this.document.addEventListener("mousemove", () => {
this.highlighters.on("grid-highlighter-hidden", this.onHighlighterHidden);
this.highlighters.on("grid-highlighter-shown", this.onHighlighterShown);
}, { once: true });
}

this.inspector.sidebar.on("select", this.onSidebarSelect);
this.inspector.on("new-root", this.onNavigate);
Expand Down
3 changes: 0 additions & 3 deletions devtools/client/inspector/grids/test/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@
module.exports = {
// Extend from the shared list of defined globals for mochitests.
"extends": "../../../../.eslintrc.mochitests.js",
"globals": {
"waitUntilState": true
}
};
2 changes: 1 addition & 1 deletion devtools/client/inspector/markup/test/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module.exports = {
// Extend from the shared list of defined globals for mochitests.
"extends": "../../../../.eslintrc.mochitests.js"
"extends": "../../../../.eslintrc.mochitests.js",
};
3 changes: 3 additions & 0 deletions devtools/client/inspector/markup/test/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ support-files =
!/devtools/client/inspector/test/head.js
!/devtools/client/inspector/test/shared-head.js
!/devtools/client/shared/test/shared-head.js
!/devtools/client/shared/test/shared-redux-head.js
!/devtools/client/shared/test/telemetry-test-helpers.js
!/devtools/client/shared/test/test-actor.js
!/devtools/client/shared/test/test-actor-registry.js
Expand Down Expand Up @@ -130,6 +131,8 @@ skip-if = true # Bug 1177550
[browser_markup_events_react_production_16.2.0_jsx.js]
[browser_markup_events_source_map.js]
[browser_markup_events-windowed-host.js]
[browser_markup_flex_display_badge.js]
[browser_markup_grid_display_badge.js]
[browser_markup_links_01.js]
[browser_markup_links_02.js]
[browser_markup_links_03.js]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that the flex display badge toggles on the flexbox highlighter.

const TEST_URI = `
<style type="text/css">
#flex {
display: flex;
}
</style>
<div id="flex"></div>
`;

const HIGHLIGHTER_TYPE = "FlexboxHighlighter";

add_task(async function() {
await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
const { inspector } = await openLayoutView();
const { highlighters, store } = inspector;

info("Check the flex display badge is shown and not active.");
await selectNode("#flex", inspector);
const flexContainer = await getContainerForSelector("#flex", inspector);
const flexDisplayBadge = flexContainer.elt.querySelector(".markup-badge[data-display]");
ok(!flexDisplayBadge.classList.contains("active"), "flex display badge is not active.");

info("Check the initial state of the flex highlighter.");
ok(!highlighters.highlighters[HIGHLIGHTER_TYPE],
"No flexbox highlighter exists in the highlighters overlay.");
ok(!highlighters.flexboxHighlighterShown, "No flexbox highlighter is shown.");

info("Toggling ON the flexbox highlighter from the flex display badge.");
const onHighlighterShown = highlighters.once("flexbox-highlighter-shown");
let onCheckboxChange = waitUntilState(store, state => state.flexbox.highlighted);
flexDisplayBadge.click();
await onHighlighterShown;
await onCheckboxChange;

info("Check the flexbox highlighter is created and flex display badge state.");
ok(highlighters.highlighters[HIGHLIGHTER_TYPE],
"Flexbox highlighter is created in the highlighters overlay.");
ok(highlighters.flexboxHighlighterShown, "Flexbox highlighter is shown.");
ok(flexDisplayBadge.classList.contains("active"), "flex display badge is active.");

info("Toggling OFF the flexbox highlighter from the flex display badge.");
const onHighlighterHidden = highlighters.once("flexbox-highlighter-hidden");
onCheckboxChange = waitUntilState(store, state => !state.flexbox.highlighted);
flexDisplayBadge.click();
await onHighlighterHidden;
await onCheckboxChange;

ok(!flexDisplayBadge.classList.contains("active"), "flex display badge is not active.");
});
Loading

0 comments on commit f01fd71

Please sign in to comment.