-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathLiveDevelopmentMultiBrowser-test.js
More file actions
2189 lines (1797 loc) · 101 KB
/
Copy pathLiveDevelopmentMultiBrowser-test.js
File metadata and controls
2189 lines (1797 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2014 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, beforeAll, afterAll, awaitsFor, it, awaitsForDone, expect, awaits, jsPromise*/
define(function (require, exports, module) {
const SpecRunnerUtils = require("spec/SpecRunnerUtils"),
KeyEvent = require("utils/KeyEvent"),
StringUtils = require("utils/StringUtils"),
Strings = require("strings");
describe("livepreview:MultiBrowser Live Preview", function () {
async function _waitForIframeSrc(name) {
await awaitsFor(() => {
let outerIFrame = testWindow.document.getElementById("panel-live-preview-frame");
let srcURL = new URL(outerIFrame.src);
return srcURL.pathname.endsWith(name) === true;
}, "waiting for name- " + name);
}
if (Phoenix.isTestWindowPlaywright && !Phoenix.browser.desktop.isChromeBased) {
it("All tests requiring virtual server is disabled in playwright/firefox/safari", async function () {
// we dont spawn virtual server in iframe playwright linux/safari as playwright linux/safari fails badly
// we dont need virtual server for tests except for live preview and custom extension load tests,
// which are disabled in playwright. We test in chrome atleast as chromium support is a baseline.
});
return;
}
var testWindow,
brackets,
DocumentManager,
LiveDevMultiBrowser,
LiveDevProtocol,
EditorManager,
CommandManager,
BeautificationManager,
Commands,
MainViewManager,
WorkspaceManager,
PreferencesManager,
Dialogs,
NativeApp;
let testFolder = SpecRunnerUtils.getTestPath("/spec/LiveDevelopment-MultiBrowser-test-files"),
prettierTestFolder = SpecRunnerUtils.getTestPath("/spec/prettier-test-files"),
allSpacesRE = /\s+/gi;
const SVG_IMAGE_PATH = "sub/phoenix-logo.svg";
function fixSpaces(str) {
return str.replace(allSpacesRE, " ");
}
async function getSourceFromBrowser(liveDoc) {
let doneSyncing = false, browserText;
liveDoc.getSourceFromBrowser().done(function (text) {
browserText = text;
}).always(function () {
doneSyncing = true;
});
await awaitsFor(function () { return doneSyncing; }, "Browser to sync changes", 20000);
return browserText;
}
let savedNativeAppOpener;
beforeAll(async function () {
// Create a new window that will be shared by ALL tests in this spec.
if (!testWindow) {
// we have to popout a new window and cant use the embedded iframe for live preview integ tests
// as Firefox sandbox prevents service worker access from nexted iframes.
// In tauri, we use node server, so this limitation doesn't apply in tauri, and we stick to iframes.
const useWindowInsteadOfIframe = (Phoenix.browser.desktop.isFirefox && !window.__TAURI__);
testWindow = await SpecRunnerUtils.createTestWindowAndRun({
forceReload: false, useWindowInsteadOfIframe
});
brackets = testWindow.brackets;
DocumentManager = brackets.test.DocumentManager;
LiveDevMultiBrowser = brackets.test.LiveDevMultiBrowser;
LiveDevProtocol = brackets.test.LiveDevProtocol;
CommandManager = brackets.test.CommandManager;
Commands = brackets.test.Commands;
EditorManager = brackets.test.EditorManager;
WorkspaceManager = brackets.test.WorkspaceManager;
BeautificationManager = brackets.test.BeautificationManager;
PreferencesManager = brackets.test.PreferencesManager;
NativeApp = brackets.test.NativeApp;
Dialogs = brackets.test.Dialogs;
MainViewManager = brackets.test.MainViewManager;
savedNativeAppOpener = NativeApp.openURLInDefaultBrowser;
await SpecRunnerUtils.loadProjectInTestWindow(testFolder);
await SpecRunnerUtils.deletePathAsync(testFolder + "/.phcode.json", true);
// Disable edit mode features for core live preview tests
// This ensures tests focus on basic live preview functionality without
// edit mode interference (hover/click handlers)
if (LiveDevMultiBrowser && LiveDevMultiBrowser.config) {
LiveDevMultiBrowser.config.isProUser = false;
// Also update the remote browser configuration
if (LiveDevMultiBrowser.updateConfig) {
LiveDevMultiBrowser.updateConfig(JSON.stringify(LiveDevMultiBrowser.config));
}
}
if (!WorkspaceManager.isPanelVisible('live-preview-panel')) {
await awaitsForDone(CommandManager.execute(Commands.FILE_LIVE_FILE_PREVIEW));
}
}
}, 30000);
afterAll(async function () {
NativeApp.openURLInDefaultBrowser = savedNativeAppOpener;
// we dont await SpecRunnerUtils.closeTestWindow(); here as tests fail eraticaly if we do this in intel macs
testWindow = null;
brackets = null;
LiveDevMultiBrowser = null;
CommandManager = null;
Commands = null;
EditorManager = null;
MainViewManager = null;
savedNativeAppOpener = null;
Dialogs = null;
NativeApp = null;
PreferencesManager = null;
BeautificationManager = null;
DocumentManager = null;
WorkspaceManager = null;
}, 30000);
async function _enableLiveHighlights(enable) {
PreferencesManager.setViewState("livedevHighlight", enable);
}
async function endPreviewSession() {
await _enableLiveHighlights(true);
LiveDevMultiBrowser.close();
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL, { _forceClose: true }),
"closing all file");
}
async function waitsForLiveDevelopmentFileSwitch() {
await awaitsFor(
function isLiveDevelopmentActive() {
return LiveDevMultiBrowser.status === LiveDevMultiBrowser.STATUS_ACTIVE;
},
"livedevelopment.done.opened",
20000
);
let editor = EditorManager.getActiveEditor();
editor && editor.setCursorPos({ line: 0, ch: 0 });
}
async function waitsForLiveDevelopmentToOpen() {
// Ensure edit mode is disabled before opening live preview
if (LiveDevMultiBrowser && LiveDevMultiBrowser.config) {
LiveDevMultiBrowser.config.isProUser = false;
// Update the remote browser configuration to sync the disabled state
if (LiveDevMultiBrowser.updateConfig) {
LiveDevMultiBrowser.updateConfig(JSON.stringify(LiveDevMultiBrowser.config));
}
}
LiveDevMultiBrowser.open();
await waitsForLiveDevelopmentFileSwitch();
}
it("should establish a browser connection for an opened html file", async function () {
//open a file
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
expect(LiveDevMultiBrowser.status).toBe(LiveDevMultiBrowser.STATUS_ACTIVE);
await endPreviewSession();
}, 30000);
it("should establish a browser connection for an opened html file that has no 'head' tag", async function () {
//open a file
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["withoutHead.html"]),
"SpecRunnerUtils.openProjectFiles withoutHead.html");
await waitsForLiveDevelopmentToOpen();
expect(LiveDevMultiBrowser.status).toBe(LiveDevMultiBrowser.STATUS_ACTIVE);
await endPreviewSession();
}, 30000);
it("should send all external stylesheets as related docs on start-up", async function () {
let liveDoc;
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
liveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
await awaitsFor(
function relatedDocsReceived() {
return (Object.getOwnPropertyNames(liveDoc.getRelated().stylesheets).length > 0);
},
"relatedDocuments.done.received",
10000
);
expect(liveDoc.isRelated(testFolder + "/simple1.css")).toBeTruthy();
expect(liveDoc.isRelated(testFolder + "/simpleShared.css")).toBeTruthy();
await endPreviewSession();
}, 30000);
it("should send all import-ed stylesheets as related docs on start-up", async function () {
let liveDoc;
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
liveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
await awaitsFor(
function relatedDocsReceived() {
return (Object.getOwnPropertyNames(liveDoc.getRelated().scripts).length > 0) &&
liveDoc.isRelated(testFolder + "/import1.css");
},
"relatedDocuments.done.received",
10000
);
expect(liveDoc.isRelated(testFolder + "/import1.css")).toBeTruthy();
await endPreviewSession();
}, 30000);
it("should send all external javascript files as related docs on start-up", async function () {
let liveDoc;
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
liveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
await awaitsFor(
function relatedDocsReceived() {
return (Object.getOwnPropertyNames(liveDoc.getRelated().scripts).length > 0);
},
"relatedDocuments.done.received",
10000
);
expect(liveDoc.isRelated(testFolder + "/simple1.js")).toBeTruthy();
await endPreviewSession();
}, 30000);
it("should render partial arabic html with correct utf-8 encoding", async function () {
// https://github.com/orgs/phcode-dev/discussions/1676
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["arabicPartial.html"]),
"SpecRunnerUtils.openProjectFiles arabicPartial.html");
await waitsForLiveDevelopmentToOpen();
let result;
await awaitsFor(
function isArabicTextProperlyRendered() {
LiveDevProtocol.evaluate(`document.getElementById('arabic-text').textContent`)
.done((response) => {
result = JSON.parse(response.result || "");
});
return result === " 1 يناير 2021 بواسطة ";
},
`arabic text to be read correctly`,
5000,
50
);
await endPreviewSession();
}, 30000);
function _isRelatedStyleSheet(liveDoc, fileName) {
let relatedSheets = Object.keys(liveDoc.getRelated().stylesheets);
for (let relatedPath of relatedSheets) {
if (relatedPath.endsWith(fileName)) {
return true;
}
}
return false;
}
it("should send notifications for added/removed stylesheets through link nodes", async function () {
let liveDoc;
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
liveDoc = LiveDevMultiBrowser.getCurrentLiveDoc();
let curDoc = DocumentManager.getCurrentDocument();
curDoc.replaceRange('<link href="blank.css" rel="stylesheet">\n', { line: 8, ch: 0 });
await awaitsFor(
function relatedDocsReceived() {
return _isRelatedStyleSheet(liveDoc, "blank.css");
},
"relatedDocuments.done.received",
20000
);
// blank.css exist and hence part of live doc. o fix this
expect(liveDoc.isRelated(testFolder + "/blank.css")).toBeTruthy();
// todo #remove_not_working
// curDoc = DocumentManager.getCurrentDocument();
// curDoc.replaceRange('', {line: 8, ch: 0}, {line: 8, ch: 50});
//
// await awaitsFor(
// function relatedDocsReceived() {
// return (Object.getOwnPropertyNames(liveDoc.getRelated().stylesheets).length === 3);
// },
// "relatedDocuments.done.received",
// 10000
// );
//
// expect(liveDoc.isRelated(testFolder + "/blank.css")).toBeFalsy();
await endPreviewSession();
}, 30000);
// search for todo #remove_not_working fix in future
// xit(" todo should send notifications for removed stylesheets through link nodes", async function () {
// });
it("should push changes through browser connection when editing a related CSS", async function () {
let localText,
browserText,
liveDoc,
curDoc;
const styleTextAdd = "\n .testClass { background-color:#090; }\n";
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.css"]),
"SpecRunnerUtils.openProjectFiles simple1.css");
curDoc = DocumentManager.getCurrentDocument();
localText = curDoc.getText();
localText += styleTextAdd;
curDoc.setText(localText);
liveDoc = LiveDevMultiBrowser.getLiveDocForPath(testFolder + "/simple1.css");
browserText = await getSourceFromBrowser(liveDoc);
browserText = browserText.replace(/url\('http:\/\/127\.0\.0\.1:\d+\/import1\.css'\);/, "url('import1.css');");
expect(fixSpaces(browserText).includes(fixSpaces(styleTextAdd))).toBeTrue();
await endPreviewSession();
}, 30000);
it("should show error banner if there is syntax error in html", async function () {
let savedText,
curDoc;
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
curDoc = DocumentManager.getCurrentDocument();
savedText = curDoc.getText();
// split file in half to and add syntax error then see if error banner shows up
const mid = Math.ceil(savedText.length / 2);
curDoc.setText(savedText.slice(0, mid));
await awaitsFor(
function isBannerVisible() {
return testWindow.$(".live-preview-status-overlay").is(":visible") &&
testWindow.$(".live-preview-status-overlay").text()
.includes(Strings.LIVE_DEV_STATUS_TIP_SYNC_ERROR);
},
"waiting for syntax error banner to appear",
5000,
50
);
curDoc.setText(savedText);
await endPreviewSession();
}, 30000);
it("should make CSS-relative URLs absolute", async function () {
var localText,
browserText,
liveDoc,
curDoc;
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["index.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["sub/test.css"]),
"SpecRunnerUtils.openProjectFiles simple1.css");
curDoc = DocumentManager.getCurrentDocument();
localText = curDoc.getText();
localText += "\n .testClass { background-color:#090; }\n";
curDoc.setText(localText);
liveDoc = LiveDevMultiBrowser.getLiveDocForPath(testFolder + "/sub/test.css");
browserText = await getSourceFromBrowser(liveDoc);
// Drop the port from 127.0.0.1:port so it's easier to work with
browserText = browserText.replace(/127\.0\.0\.1:\d+/, "127.0.0.1");
// expect relative URL to have been made absolute
expect(browserText).toContain("icon_chevron.png); }");
// expect absolute URL to stay unchanged
expect(browserText).toContain(".sub { background: url(file:///fake.png); }");
await endPreviewSession();
}, 30000);
async function _editFileAndVerifyLivePreview(fileName, location, editText, verifyID, verifyText) {
await awaitsForDone(SpecRunnerUtils.openProjectFiles([fileName]),
"SpecRunnerUtils.openProjectFiles " + fileName);
await awaits(1000); // todo can we remove this
await awaitsFor(
function isTextChanged() {
return LiveDevMultiBrowser.status === LiveDevMultiBrowser.STATUS_ACTIVE;
},
"waiting for live preview active",
5000,
50
);
let curDoc = DocumentManager.getCurrentDocument();
curDoc.replaceRange(editText, location);
let result;
await awaitsFor(
function isTextChanged() {
LiveDevProtocol.evaluate(`document.getElementById('${verifyID}').textContent`)
.done((response) => {
result = JSON.parse(response.result || "");
});
return result === verifyText;
},
`relatedDocuments.done.received verifying ${verifyID} to have ${verifyText}`,
5000,
50
);
}
it("should Live preview push html file changes to browser", async function () {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await _editFileAndVerifyLivePreview("simple1.html", { line: 11, ch: 45 }, 'hello world ',
"testId", "Brackets is hello world awesome!");
await endPreviewSession();
}, 30000);
it("should Live preview push html class changes to browser", async function () {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"simple1.html");
await awaitsFor(() => LiveDevMultiBrowser.status === LiveDevMultiBrowser.STATUS_ACTIVE,
"status active");
let curDoc = DocumentManager.getCurrentDocument();
// add a class
curDoc.replaceRange("addClass ", { line: 11, ch: 22 });
let hasClass;
await awaitsFor(
function isClassChanged() {
LiveDevProtocol.evaluate(`document.getElementById('testId').classList.contains("addClass")`)
.done((response) => {
hasClass = JSON.parse(response.result || "");
});
return hasClass;
},
"replaceClass",
5000,
50
);
// remove a class
curDoc.replaceRange("", { line: 11, ch: 22 }, { line: 11, ch: 31 });
await awaitsFor(
function isClassChanged() {
LiveDevProtocol.evaluate(`document.getElementById('testId').classList.contains("addClass")`)
.done((response) => {
hasClass = JSON.parse(response.result || "");
});
return hasClass;
},
"replaceClass",
5000,
50
);
await endPreviewSession();
}, 30000);
it("should Live preview push html attribute changes to browser", async function () {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"simple1.html");
await awaitsFor(() => LiveDevMultiBrowser.status === LiveDevMultiBrowser.STATUS_ACTIVE,
"status active");
let curDoc = DocumentManager.getCurrentDocument();
// add an attribute
curDoc.replaceRange(' hello="world" ', { line: 11, ch: 15 });
let result;
await awaitsFor(
function isAttributeAdded() {
LiveDevProtocol.evaluate(`document.getElementById('testId').getAttribute("hello")`)
.done((response) => {
result = JSON.parse(response.result || "");
});
return result === "world";
},
"attribute add",
5000,
50
);
// remove the attribute
curDoc.replaceRange("", { line: 11, ch: 15 }, { line: 11, ch: 30 });
await awaitsFor(
function isClassChanged() {
LiveDevProtocol.evaluate(`document.getElementById('testId').getAttribute("hello")`)
.done((response) => {
result = JSON.parse(response.result || "");
});
return result !== "world";
},
"attribute remove",
5000,
50
);
await endPreviewSession();
}, 30000);
async function _openCodeHints(cursor, expectedSomeHintsArray) {
let editor = EditorManager.getActiveEditor();
editor.setCursorPos(cursor);
await awaitsForDone(CommandManager.execute(Commands.SHOW_CODE_HINTS),
"show code hints");
await awaitsFor(function () {
return testWindow.$(".codehint-menu").is(":visible");
}, "codehints to be shown");
await awaitsFor(function () {
for (let hint of expectedSomeHintsArray) {
if (!testWindow.$(".codehint-menu").text().includes(hint)) {
return false;
}
}
return true;
}, "expected hints to be there");
}
async function _waitForLivePreviewElementColor(elementID, color) {
let result;
await awaitsFor(
async function isColorChanged() {
const response = await LiveDevProtocol.evaluate(
`window.getComputedStyle(document.getElementById('${elementID}')).color`);
result = JSON.parse(response.result||"");
return result === color;
},
async ()=>{
const response = await LiveDevProtocol.evaluate(
`window.getComputedStyle(document.getElementById('${elementID}')).color`);
result = JSON.parse(response.result||"");
return `element #${elementID} to color ${color} but was ${result}`;
},
5000,
50
);
}
async function _livePreviewCodeHintsHTML() {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["inline-style.html"]),
"SpecRunnerUtils.openProjectFiles inline-style.html");
await waitsForLiveDevelopmentToOpen();
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["inline-style.html"]),
"inline-style.html");
await awaitsFor(() => LiveDevMultiBrowser.status === LiveDevMultiBrowser.STATUS_ACTIVE,
"status active");
await _openCodeHints({ line: 9, ch: 18 }, ["red"]);
let editor = EditorManager.getActiveEditor();
const initialHistoryLength = editor.getHistory().done.length;
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", testWindow.document.body);
await awaitsFor(function () {
return editor.getSelectedText() === "indianred";
}, "expected live hints to update selection to indianred");
await _waitForLivePreviewElementColor("testId2", "rgb(205, 92, 92)"); // indian red
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", testWindow.document.body);
await awaitsFor(function () {
return editor.getSelectedText() === "mediumvioletred";
}, "expected live hints to update selection to mediumvioletred");
await _waitForLivePreviewElementColor("testId2", "rgb(199, 21, 133)");
return initialHistoryLength;
}
it("should Live preview push css code hints selection changes to browser(inline html)", async function () {
const expectedHistoryLength = await _livePreviewCodeHintsHTML();
let editor = EditorManager.getActiveEditor();
// now dismiss with escape
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_ESCAPE, "keydown", testWindow.document.body);
await awaitsFor(function () {
return !testWindow.$(".codehint-menu").is(":visible");
}, "codehints to be hidden");
await awaitsFor(function () {
return editor.getSelectedText() === "";
}, "to restore the text to old state");
expect(editor.getToken().string).toBe("red");
// the undo history should be same as when we started
expect(editor.getHistory().done.length).toBe(expectedHistoryLength);
await endPreviewSession();
}, 30000);
it("should Live preview push css code hints selection changes to browser and commit(inline html)", async function () {
const expectedHistoryLength = await _livePreviewCodeHintsHTML();
let editor = EditorManager.getActiveEditor();
// commit with enter key
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_RETURN, "keydown", testWindow.document.body);
await awaitsFor(function () {
return !testWindow.$(".codehint-menu").is(":visible");
}, "codehints to be hidden");
await awaitsFor(function () {
return editor.getSelectedText() === "";
}, "to restore the text to old state");
// check if we have the new value
expect(editor.getToken().string).toBe("mediumvioletred");
// the undo history should be just one above
expect(editor.getHistory().done.length).toBe(expectedHistoryLength + 3);
await endPreviewSession();
}, 30000);
async function _livePreviewCodeHintsCSS() {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["inline-style.html"]),
"SpecRunnerUtils.openProjectFiles inline-style.html");
await waitsForLiveDevelopmentToOpen();
await awaitsFor(() => LiveDevMultiBrowser.status === LiveDevMultiBrowser.STATUS_ACTIVE,
"status active");
// Ensure we get a clean copy of simple1.css from disk, not a modified cached version
// from previous tests.
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL, { _forceClose: true }),
"closing all files before opening simple1.css");
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.css"]),
"simple1.css");
const doc = DocumentManager.getCurrentDocument();
const text = doc.getText();
// The original simple1.css should NOT contain background-color:#090
// That gets added by a previous test and must be cleaned up
// We verify the file doesn't contain #090 background-color and if it does, change expectations
// in linux, or if system slow, it will take some time for file system change event to catch
// up and update document. so we need to do this below. This is a bug in tests as why is the test not
// resetting file properly constantly?
const has90 = text.includes("background-color:#090");
const firstColor = has90 ? "#090" : "aliceblue";
const firstColorRGB = has90 ? "rgb(0, 153, 0)" : "rgb(240, 248, 255)";
await _openCodeHints({ line: 3, ch: 8 }, ["antiquewhite"]);
let editor = EditorManager.getActiveEditor();
const initialHistoryLength = editor.getHistory().done.length;
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", testWindow.document.body);
await awaitsFor(function () {
// #090 is the content from simple1.css file
// this appears as the 2nd item in the codehint menu, from "suggest previously used color" feature
return editor.getSelectedText() === firstColor;
}, `expected live hints to update selection to ${firstColor}`);
await _waitForLivePreviewElementColor("testId", firstColorRGB);
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", testWindow.document.body);
const secondColor = has90 ? "aliceblue" : "antiquewhite";
const secondColorRGB = has90 ? "rgb(240, 248, 255)" : "rgb(250, 235, 215)";
await awaitsFor(function () {
return editor.getSelectedText() === secondColor;
}, `expected live hints to update selection to ${secondColor}`);
await _waitForLivePreviewElementColor("testId", secondColorRGB); // antiquewhite
return initialHistoryLength;
}
it("should Live preview push css code hints selection changes to browser(linked css)", async function () {
const expectedHistoryLength = await _livePreviewCodeHintsCSS();
let editor = EditorManager.getActiveEditor();
// now dismiss with escape
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_ESCAPE, "keydown", testWindow.document.body);
await awaitsFor(function () {
return !testWindow.$(".codehint-menu").is(":visible");
}, "codehints to be hidden");
await awaitsFor(function () {
return editor.getSelectedText() === "";
}, "to restore the text to old state");
expect(editor.getToken().string).toBe(" ");
// the undo history should be same as when we started
expect(editor.getHistory().done.length).toBe(expectedHistoryLength);
await endPreviewSession();
}, 30000);
it("should Live preview push css code hints selection changes to browser and commit(linked css)", async function () {
const expectedHistoryLength = await _livePreviewCodeHintsCSS();
let editor = EditorManager.getActiveEditor();
// now dismiss with escape
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_RETURN, "keydown", testWindow.document.body);
await awaitsFor(function () {
return !testWindow.$(".codehint-menu").is(":visible");
}, "codehints to be hidden");
await awaitsFor(function () {
return editor.getSelectedText() === "";
}, "to restore the text to old state");
// check if we have the new value
if(!["antiquewhite", "aliceblue"].includes(editor.getToken().string)){
// so depends on the bug in _livePreviewCodeHintsCSS which color is at present.
expect("color should have beein either aliceblue or antiquewhite").toBeTrue();
}
// the undo history should be just one above
expect(editor.getHistory().done.length).toBe(expectedHistoryLength + 3);
await endPreviewSession();
}, 30000);
async function _waitForLivePreviewElementClass(elementID, classExpected) {
let result;
await awaitsFor(
async function isColorChanged() {
const response = await LiveDevProtocol.evaluate(
`document.getElementById('${elementID}').classList.contains('${classExpected}')`);
result = JSON.parse(response.result || "");
return result === true;
},
`element #${elementID} to have class ${classExpected}`,
5000,
50
);
}
async function _livePreviewCodeHintsHTMLCSSClass(onlyOnce, position = { line: 15, ch: 24 }) {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["inline-style.html"]),
"SpecRunnerUtils.openProjectFiles inline-style.html");
await waitsForLiveDevelopmentToOpen();
await awaitsFor(() => LiveDevMultiBrowser.status === LiveDevMultiBrowser.STATUS_ACTIVE,
"status active");
await _openCodeHints(position, ["testClass2", "testClass"]);
let editor = EditorManager.getActiveEditor();
const initialHistoryLength = editor.getHistory().done.length;
const $ = testWindow.$;
let initialSelectedCodeHint = $($(".code-hints-list-item .highlight .brackets-html-hints")).text();
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", testWindow.document.body);
await awaitsFor(function () {
let newSelectedCodeHint = $($(".code-hints-list-item .highlight .brackets-html-hints")).text();
return newSelectedCodeHint !== initialSelectedCodeHint &&
editor.getSelectedText() === newSelectedCodeHint;
}, "expected live hints to update selection to next code hint");
let newSelectedCodeHint = $($(".code-hints-list-item .highlight .brackets-html-hints")).text();
await _waitForLivePreviewElementClass("testId", newSelectedCodeHint);
if (onlyOnce) {
return initialHistoryLength;
}
SpecRunnerUtils.simulateKeyEvent(KeyEvent.DOM_VK_DOWN, "keydown", testWindow.document.body);
await awaitsFor(function () {
let newSelectedCodeHint2 = $($(".code-hints-list-item .highlight .brackets-html-hints")).text();
return newSelectedCodeHint !== newSelectedCodeHint2 &&
editor.getSelectedText() === newSelectedCodeHint2;
}, "expected live hints to update selection");
let newSelectedCodeHint2 = $($(".code-hints-list-item .highlight .brackets-html-hints")).text();
await _waitForLivePreviewElementClass("testId", newSelectedCodeHint2);
return initialHistoryLength;
}
async function _testAtPos(pos, endKey = KeyEvent.DOM_VK_ESCAPE, onlyOnce = false,
additionalHistoryLengthExpected = 0) {
const expectedHistoryLength = await _livePreviewCodeHintsHTMLCSSClass(onlyOnce, pos);
let editor = EditorManager.getActiveEditor();
// now dismiss with escape
const $ = testWindow.$;
let selectedCodeHint = $($(".code-hints-list-item .highlight .brackets-html-hints")).text();
expect(selectedCodeHint).toBeDefined();
SpecRunnerUtils.simulateKeyEvent(endKey, "keydown", testWindow.document.body);
await awaitsFor(function () {
return !testWindow.$(".codehint-menu").is(":visible");
}, "codehints to be hidden");
await awaitsFor(function () {
return editor.getSelectedText() === "";
}, "to restore the text to old state");
// the undo history should be what we expect
expect(editor.getHistory().done.length).toBe(expectedHistoryLength + additionalHistoryLengthExpected);
return selectedCodeHint;
}
it("should Live preview push html css class code hints selection changes to browser", async function () {
//<p id="testId" class="t<cursor>estClass ">Brackets is awesome!</p>
await _testAtPos({ line: 15, ch: 24 });
let editor = EditorManager.getActiveEditor();
expect(editor.getToken().string).toBe('"testClass "');
await endPreviewSession();
}, 30000);
it("should Live preview push html css class code hints on empty input selection changes to browser", async function () {
//<p id="testId" class="testClass <cursor>">Brackets is awesome!</p>
await _testAtPos({ line: 15, ch: 32 });
let editor = EditorManager.getActiveEditor();
expect(editor.getToken().string).toBe('"testClass "');
await endPreviewSession();
}, 30000);
it("should Live preview push html css class code hints selection changes to browser and commit", async function () {
//<p id="testId" class="t<cursor>estClass ">Brackets is awesome!</p>
await _testAtPos({ line: 15, ch: 24 }, KeyEvent.DOM_VK_RETURN, true, 3);
let editor = EditorManager.getActiveEditor();
expect(editor.getToken().string).toBe('"testClass2 "');
await endPreviewSession();
}, 30000);
it("should Live preview push html css class code hints on empty input selection changes to browser and commit", async function () {
//<p id="testId" class="testClass <cursor>">Brackets is awesome!</p>
const selectedHint = await _testAtPos({ line: 15, ch: 32 }, KeyEvent.DOM_VK_RETURN, true, 2);
let editor = EditorManager.getActiveEditor();
expect(editor.getToken().string).toBe(`"testClass ${selectedHint}"`);
await endPreviewSession();
}, 30000);
it("should Live preview work even if we switch html files", async function () {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await _editFileAndVerifyLivePreview("simple1.html", { line: 11, ch: 45 }, 'hello world ',
"testId", "Brackets is hello world awesome!");
await _editFileAndVerifyLivePreview("simple2.html", { line: 11, ch: 45 }, 'hello world ',
"simpId", "Brackets is hello world awesome!");
// now switch back to old file
await _editFileAndVerifyLivePreview("simple1.html", { line: 11, ch: 45 }, 'hello world ',
"testId", "Brackets is hello world hello world awesome!");
await endPreviewSession();
}, 30000);
it("should Markdown/svg image files be previewed and switched between live previews", async function () {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await _editFileAndVerifyLivePreview("simple1.html", { line: 11, ch: 45 }, 'hello world ',
"testId", "Brackets is hello world awesome!");
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["readme.md"]),
"readme.md");
await awaits(300);
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
expect(iFrame.src.endsWith("readme.md")).toBeTrue();
await awaitsForDone(SpecRunnerUtils.openProjectFiles([SVG_IMAGE_PATH]),
SVG_IMAGE_PATH);
await awaits(500);
iFrame = testWindow.document.getElementById("panel-live-preview-frame");
let srcURL = new URL(iFrame.src);
expect(srcURL.pathname.endsWith(SVG_IMAGE_PATH)).toBeTrue();
// now switch back to old file
await _editFileAndVerifyLivePreview("simple1.html", { line: 11, ch: 45 }, 'hello world ',
"testId", "Brackets is hello world hello world awesome!");
await endPreviewSession();
}, 30000);
it("should not live preview binary image files", async function () {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await _editFileAndVerifyLivePreview("simple1.html", { line: 11, ch: 45 }, 'hello world ',
"testId", "Brackets is hello world awesome!");
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["sub/icon_chevron.png"]),
"sub/icon_chevron.png");
await awaits(500);
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
expect(iFrame.src.endsWith("simple1.html")).toBeTrue();
await endPreviewSession();
}, 30000);
it("focus test: should html live previews never take focus from editor", async function () {
// this test may fail if the test window doesn't have focus
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
await _editFileAndVerifyLivePreview("simple1.html", { line: 11, ch: 45 }, 'hello world ',
"testId", "Brackets is hello world awesome!");
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
expect(iFrame.src.endsWith("simple1.html")).toBeTrue();
iFrame.focus(); // live preview has focus but it can take html focus, the live preview has to
// delegate focus to editor explicitly in case of html files.
expect(testWindow.document.activeElement).toEqual(iFrame);
// for html, it can take focus, but clicking on any non- text elemnt will make it loose focus to editor
await forRemoteExec(`document.getElementById("testId2").click()`);
await awaits(500);
const activeElement = testWindow.document.activeElement;
const editorHolder = testWindow.document.getElementById("editor-holder");
expect(editorHolder.contains(activeElement)).toBeTrue();
await endPreviewSession();
}, 30000);
async function openPreviewAndClickTextInputsInPreview() {
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["hyperlink.html"]),
"SpecRunnerUtils.openProjectFiles simple1.html");
await waitsForLiveDevelopmentToOpen();
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
expect(iFrame.src.endsWith("hyperlink.html")).toBeTrue();
iFrame.focus(); // live preview has focus but it can take html focus, the live preview has to
// delegate focus to editor explicitly in case of html files.
expect(testWindow.document.activeElement).toEqual(iFrame);
// for html, it can take focus, but clicking on any text elemnt will focus file preview
await forRemoteExec(`document.getElementById("textArea").click()`);
await awaits(300);
expect(testWindow.document.activeElement).toEqual(iFrame);
await forRemoteExec(`document.getElementById("inputText").click()`);
await awaits(300);
expect(testWindow.document.activeElement).toEqual(iFrame);
}
it("focus test: should html live previews take focus from editor on text filed click in live preview", async function () {
// this test may fail if the test window doesn't have focus
await openPreviewAndClickTextInputsInPreview();
await endPreviewSession();
}, 30000);
async function triggerEscapeKeyEvent() {
// Create a new KeyboardEvent
const jsExec = `document.getElementById("inputText").dispatchEvent(new KeyboardEvent("keydown", {
key: "Escape",
keyCode: 27, // keyCode for Escape key
code: "Escape",
which: 27,
bubbles: true, // Event bubbles up through the DOM
cancelable: true // Event can be canceled
}))`;
await forRemoteExec(jsExec);
}
it("focus test: should pressing escape key on live preview focued input focus editor", async function () {
// this test may fail if the test window doesn't have focus
await openPreviewAndClickTextInputsInPreview();
await triggerEscapeKeyEvent();
// Editor will gain focus on escape key press
await awaits(500);
const activeElement = testWindow.document.activeElement;
const editorHolder = testWindow.document.getElementById("editor-holder");
expect(editorHolder.contains(activeElement)).toBeTrue();
await endPreviewSession();
}, 30000);