Skip to content

Commit acaadae

Browse files
committed
test: markdown live preview focus/selection integ tests
1 parent 039a465 commit acaadae

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/extensionsIntegrated/Phoenix-live-preview/markdown.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
};
3232
} else if(event.data.type === "_TEST_FOCUS_CLICK") { // for integ tests
3333
document.body.click();
34+
} else if(event.data.type === "_TEST_SELECT_TEXT_AND_CLICK") { // for integ tests
35+
const selection = window.getSelection();
36+
const range = document.createRange();
37+
range.selectNodeContents(document.body);
38+
selection.removeAllRanges(); // Clear any existing selections
39+
selection.addRange(range);
40+
document.body.click();
41+
} else if(event.data.type === "_TEST_UNSELECT_TEXT_AND_CLICK") { // for integ tests
42+
window.getSelection().removeAllRanges();
43+
document.body.click();
3444
}
3545
});
3646
if(window.self !== window.parent){

test/spec/LiveDevelopmentMultiBrowser-test.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ define(function (require, exports, module) {
630630
isTauri: Phoenix.browser.isTauri
631631
}, "*"); // this is not sensitive info, and is only dispatched if requested by the iframe
632632

633-
// Editor lost focus, it will gain back as the editor detects it lost focus to live preview pane in 100 ms
633+
// Editor lost focus, it will gain back on click on markdown live preview
634634
await awaits(500);
635635

636636
const activeElement = testWindow.document.activeElement;
@@ -640,6 +640,53 @@ define(function (require, exports, module) {
640640
await endPreviewSession();
641641
}, 30000);
642642

643+
it("focus test: should markdown previews take focus from editor if there is selection", async function () {
644+
// this test may fail if the test window doesn't have focus
645+
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
646+
"SpecRunnerUtils.openProjectFiles simple1.html");
647+
648+
await waitsForLiveDevelopmentToOpen();
649+
await _editFileAndVerifyLivePreview("simple1.html", {line: 11, ch: 45}, 'hello world ',
650+
"testId", "Brackets is hello world awesome!");
651+
652+
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
653+
expect(iFrame.src.endsWith("simple1.html")).toBeTrue();
654+
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["readme.md"]),
655+
"readme.md");
656+
657+
// now make the active editor loose focus and click on the markdown md for it to
658+
// trigger focus.
659+
await awaits(300);
660+
let outerIFrame = testWindow.document.getElementById("panel-live-preview-frame");
661+
expect(outerIFrame.src.endsWith("readme.md")).toBeTrue();
662+
outerIFrame.focus();
663+
expect(testWindow.document.activeElement).toEqual(outerIFrame);
664+
// now select some /all text in the markdown and click
665+
outerIFrame.contentWindow.postMessage({
666+
type: "_TEST_SELECT_TEXT_AND_CLICK",
667+
isTauri: Phoenix.browser.isTauri
668+
}, "*");
669+
670+
// Editor lost focus, it should not gain focus as there is an active selection in the markdown
671+
await awaits(200);
672+
expect(testWindow.document.activeElement).toEqual(outerIFrame);
673+
674+
// now clear all selections in the markdown and click
675+
outerIFrame.contentWindow.postMessage({
676+
type: "_TEST_UNSELECT_TEXT_AND_CLICK",
677+
isTauri: Phoenix.browser.isTauri
678+
}, "*");
679+
680+
// Editor lost focus, it should not gain focus as there is an active selection in the markdown
681+
await awaits(400);
682+
683+
const activeElement = testWindow.document.activeElement;
684+
const editorHolder = testWindow.document.getElementById("editor-holder");
685+
expect(editorHolder.contains(activeElement)).toBeTrue();
686+
687+
await endPreviewSession();
688+
}, 30000);
689+
643690
it("should Markdown preview hyperlinks be proper", async function () {
644691
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
645692
"SpecRunnerUtils.openProjectFiles simple1.html");

0 commit comments

Comments
 (0)