summaryrefslogtreecommitdiff
path: root/Tools/DumpRenderTree/gtk/DumpRenderTree.cpp
blob: 73d710a04db8402e8d5dbd479bcd57267cc9ff67 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
/*
 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2008 Alp Toker <alp@nuanti.com>
 * Copyright (C) 2009 Jan Alonzo <jmalonzo@gmail.com>
 * Copyright (C) 2010, 2011 Igalia S.L.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "DumpRenderTree.h"

#include "AccessibilityController.h"
#include "EditingCallbacks.h"
#include "EventSender.h"
#include "GCController.h"
#include "PixelDumpSupport.h"
#include "SelfScrollingWebKitWebView.h"
#include "TestRunner.h"
#include "TextInputController.h"
#include "WebCoreSupport/DumpRenderTreeSupportGtk.h"
#include "WebCoreTestSupport.h"
#include "WorkQueue.h"
#include "WorkQueueItem.h"
#include <JavaScriptCore/JavaScript.h>
#include <WebCore/platform/network/soup/GUniquePtrSoup.h>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <getopt.h>
#include <gtk/gtk.h>
#include <locale.h>
#include <webkit/webkit.h>
#include <wtf/Assertions.h>
#include <wtf/gobject/GlibUtilities.h>
#include <wtf/text/WTFString.h>

#if PLATFORM(X11)
#include <fontconfig/fontconfig.h>
#endif


using namespace std;

extern "C" {
// This API is not yet public.
extern gchar* webkit_web_history_item_get_target(WebKitWebHistoryItem*);
extern gboolean webkit_web_history_item_is_target_item(WebKitWebHistoryItem*);
extern GList* webkit_web_history_item_get_children(WebKitWebHistoryItem*);
extern void webkit_web_settings_add_extra_plugin_directory(WebKitWebView* view, const gchar* directory);
extern gchar* webkit_web_frame_get_response_mime_type(WebKitWebFrame* frame);
}

volatile bool done;
static bool printSeparators;
static int dumpPixelsForAllTests = false;
static bool dumpPixelsForCurrentTest;
static int dumpTree = 1;
static int useTimeoutWatchdog = 1;

#if HAVE(ACCESSIBILITY)
AccessibilityController* axController = 0;
#endif
RefPtr<TestRunner> gTestRunner;
static GCController* gcController = 0;
static WebKitWebView* webView;
static GtkWidget* window;
static GtkWidget* container;
static GtkWidget* webInspectorWindow;
WebKitWebFrame* mainFrame = 0;
WebKitWebFrame* topLoadingFrame = 0;
guint waitToDumpWatchdog = 0;
bool waitForPolicy = false;

// This is a list of opened webviews
GSList* webViewList = 0;

// current b/f item at the end of the previous test
static WebKitWebHistoryItem* prevTestBFItem = NULL;

const unsigned historyItemIndent = 8;

static void runTest(const string& inputLine);

static void didRunInsecureContent(WebKitWebFrame*, WebKitSecurityOrigin*, const char* url);

static bool shouldLogFrameLoadDelegates(const string& pathOrURL)
{
    return pathOrURL.find("loading/") != string::npos;
}

static bool shouldOpenWebInspector(const string& pathOrURL)
{
    return pathOrURL.find("inspector/") != string::npos;
}

static bool shouldDumpAsText(const string& pathOrURL)
{
    return pathOrURL.find("dumpAsText/") != string::npos;
}

static bool shouldEnableDeveloperExtras(const string& pathOrURL)
{
    return true;
}

void dumpFrameScrollPosition(WebKitWebFrame* frame)
{
    WebKitDOMDocument* document = webkit_web_frame_get_dom_document(frame);
    if (!document)
        return;

    WebKitDOMDOMWindow* domWindow = webkit_dom_document_get_default_view(document);
    if (!domWindow)
        return;

    glong x = webkit_dom_dom_window_get_page_x_offset(domWindow);
    glong y = webkit_dom_dom_window_get_page_y_offset(domWindow);

    if (abs(x) > 0 || abs(y) > 0) {
        if (webkit_web_frame_get_parent(frame))
            printf("frame '%s' ", webkit_web_frame_get_name(frame));
        printf("scrolled to %ld,%ld\n", x, y);
    }

    if (gTestRunner->dumpChildFrameScrollPositions()) {
        GSList* children = DumpRenderTreeSupportGtk::getFrameChildren(frame);
        for (GSList* child = children; child; child = g_slist_next(child))
            dumpFrameScrollPosition(static_cast<WebKitWebFrame*>(child->data));
        g_slist_free(children);
    }
}

void displayWebView()
{
    DumpRenderTreeSupportGtk::forceWebViewPaint(webView);
    DumpRenderTreeSupportGtk::setTracksRepaints(mainFrame, true);
    DumpRenderTreeSupportGtk::resetTrackedRepaints(mainFrame);
}

static void appendString(gchar*& target, const gchar* string)
{
    gchar* oldString = target;
    target = g_strconcat(target, string, NULL);
    g_free(oldString);
}

static void initializeGtkFontSettings(const char* testURL)
{
    GtkSettings* settings = gtk_settings_get_default();
    if (!settings)
        return;
    g_object_set(settings,
                 "gtk-xft-dpi", 98304, // This is 96 * 1024 or 96 DPI according to the GTK+ docs.
                 "gtk-xft-antialias", 1,
                 "gtk-xft-hinting", 0,
                 "gtk-font-name", "Liberation Sans 12",
                 "gtk-icon-theme-name", "gnome",
                 NULL);
    gdk_screen_set_resolution(gdk_screen_get_default(), 96.0);

    // One test needs subpixel anti-aliasing turned on, but generally we
    // want all text in other tests to use to grayscale anti-aliasing.
    if (testURL && strstr(testURL, "xsettings_antialias_settings.html"))
        g_object_set(settings, "gtk-xft-rgba", "rgb", NULL);
    else
        g_object_set(settings, "gtk-xft-rgba", "none", NULL);
}

CString getTopLevelPath()
{
    if (const gchar* topLevel = g_getenv("WEBKIT_TOP_LEVEL"))
        return topLevel;

    g_setenv("WEBKIT_TOP_LEVEL", TOP_LEVEL_DIR, FALSE);
    return TOP_LEVEL_DIR;
}

CString getOutputDir()
{
    const char* webkitOutputDir = g_getenv("WEBKIT_OUTPUTDIR");
    if (webkitOutputDir)
        return webkitOutputDir;

    CString topLevelPath = getTopLevelPath();
    GUniquePtr<char> outputDir(g_build_filename(topLevelPath.data(), "WebKitBuild", NULL));
    return outputDir.get();
}

static CString getFontsPath()
{
    CString webkitOutputDir = getOutputDir();
    GUniquePtr<char> fontsPath(g_build_filename(webkitOutputDir.data(), "Dependencies", "Root", "webkitgtk-test-fonts", NULL));
    if (g_file_test(fontsPath.get(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
        return fontsPath.get();

    // Try alternative fonts path.
    fontsPath.reset(g_build_filename(webkitOutputDir.data(), "webkitgtk-test-fonts", NULL));
    if (g_file_test(fontsPath.get(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
        return fontsPath.get();

    return CString();
}

static void initializeFonts(const char* testURL = 0)
{
#if PLATFORM(X11)
    initializeGtkFontSettings(testURL);

    FcInit();

    // If a test resulted a font being added or removed via the @font-face rule, then
    // we want to reset the FontConfig configuration to prevent it from affecting other tests.
    static int numFonts = 0;
    FcFontSet* appFontSet = FcConfigGetFonts(0, FcSetApplication);
    if (appFontSet && numFonts && appFontSet->nfont == numFonts)
        return;

    // Load our configuration file, which sets up proper aliases for family
    // names like sans, serif and monospace.
    FcConfig* config = FcConfigCreate();
    GUniquePtr<gchar> fontConfigFilename(g_build_filename(FONTS_CONF_DIR, "fonts.conf", nullptr));
    if (!FcConfigParseAndLoad(config, reinterpret_cast<FcChar8*>(fontConfigFilename.get()), true))
        g_error("Couldn't load font configuration file from: %s", fontConfigFilename.get());

    CString fontsPath = getFontsPath();
    if (fontsPath.isNull())
        g_error("Could not locate test fonts at $WEBKIT_TOP_LEVEL/WebKitBuild/Dependencies/Root/webkitgtk-test-fonts. "
            "WEBKIT_TOP_LEVEL is your WebKit checkout by default, and can be overridden by setting it as an environment variable.");

    GUniquePtr<GDir> fontsDirectory(g_dir_open(fontsPath.data(), 0, nullptr));
    while (const char* directoryEntry = g_dir_read_name(fontsDirectory.get())) {
        if (!g_str_has_suffix(directoryEntry, ".ttf") && !g_str_has_suffix(directoryEntry, ".otf"))
            continue;
        GUniquePtr<gchar> fontPath(g_build_filename(fontsPath.data(), directoryEntry, nullptr));
        if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontPath.get())))
            g_error("Could not load font at %s!", fontPath.get());

    }

    // Ahem is used by many layout tests.
    GUniquePtr<gchar> ahemFontFilename(g_build_filename(FONTS_CONF_DIR, "AHEM____.TTF", nullptr));
    if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(ahemFontFilename.get())))
        g_error("Could not load font at %s!", ahemFontFilename.get()); 

    for (int i = 1; i <= 9; i++) {
        GUniquePtr<gchar> fontFilename(g_strdup_printf("WebKitWeightWatcher%i00.ttf", i));
        GUniquePtr<gchar> fontPath(g_build_filename(FONTS_CONF_DIR, "..", "..", "fonts", fontFilename.get(), nullptr));
        if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontPath.get())))
            g_error("Could not load font at %s!", fontPath.get()); 
    }

    // A font with no valid Fontconfig encoding to test https://bugs.webkit.org/show_bug.cgi?id=47452
    GUniquePtr<gchar> fontWithNoValidEncodingFilename(g_build_filename(FONTS_CONF_DIR, "FontWithNoValidEncoding.fon", nullptr));
    if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontWithNoValidEncodingFilename.get())))
        g_error("Could not load font at %s!", fontWithNoValidEncodingFilename.get()); 

    if (!FcConfigSetCurrent(config))
        g_error("Could not set the current font configuration!");

    numFonts = FcConfigGetFonts(config, FcSetApplication)->nfont;
#endif
}

static gchar* dumpFramesAsText(WebKitWebFrame* frame)
{
    gchar* result = 0;

    // Add header for all but the main frame.
    bool isMainFrame = (webkit_web_view_get_main_frame(webView) == frame);

    CString innerText = DumpRenderTreeSupportGtk::getInnerText(frame);
    if (isMainFrame)
        result = g_strdup_printf("%s\n", innerText.data());
    else {
        const gchar* frameName = webkit_web_frame_get_name(frame);
        result = g_strdup_printf("\n--------\nFrame: '%s'\n--------\n%s\n", frameName, innerText.data());
    }

    if (gTestRunner->dumpChildFramesAsText()) {
        GSList* children = DumpRenderTreeSupportGtk::getFrameChildren(frame);
        for (GSList* child = children; child; child = g_slist_next(child)) {
            GUniquePtr<gchar> childData(dumpFramesAsText(static_cast<WebKitWebFrame*>(child->data)));
            appendString(result, childData.get());
        }
        g_slist_free(children);
    }

    return result;
}

static gint compareHistoryItems(gpointer* item1, gpointer* item2)
{
    GUniquePtr<gchar> firstItemTarget(webkit_web_history_item_get_target(WEBKIT_WEB_HISTORY_ITEM(item1)));
    GUniquePtr<gchar> secondItemTarget(webkit_web_history_item_get_target(WEBKIT_WEB_HISTORY_ITEM(item2)));
    return g_ascii_strcasecmp(firstItemTarget.get(), secondItemTarget.get());
}

static void dumpHistoryItem(WebKitWebHistoryItem* item, int indent, bool current)
{
    ASSERT(item != NULL);
    int start = 0;
    g_object_ref(item);
    if (current) {
        printf("curr->");
        start = 6;
    }
    for (int i = start; i < indent; i++)
        putchar(' ');

    // normalize file URLs.
    const gchar* uri = webkit_web_history_item_get_uri(item);
    gchar* uriScheme = g_uri_parse_scheme(uri);
    if (g_strcmp0(uriScheme, "file") == 0) {
        gchar* pos = g_strstr_len(uri, -1, "/LayoutTests/");
        if (!pos) {
            g_free(uriScheme);
            return;
        }

        GString* result = g_string_sized_new(strlen(uri));
        result = g_string_append(result, "(file test):");
        result = g_string_append(result, pos + strlen("/LayoutTests/"));
        printf("%s", result->str);
        g_string_free(result, TRUE);
    } else
        printf("%s", uri);

    g_free(uriScheme);

    GUniquePtr<gchar> target(webkit_web_history_item_get_target(item));
    if (target.get() && strlen(target.get()) > 0)
        printf(" (in frame \"%s\")", target.get());
    if (webkit_web_history_item_is_target_item(item))
        printf("  **nav target**");
    putchar('\n');

    if (GList* kids = webkit_web_history_item_get_children(item)) {
        // must sort to eliminate arbitrary result ordering which defeats reproducible testing
        for (GList* kid = g_list_sort(kids, (GCompareFunc) compareHistoryItems); kid; kid = g_list_next(kid)) {
            WebKitWebHistoryItem* item = WEBKIT_WEB_HISTORY_ITEM(kid->data);
            dumpHistoryItem(item, indent + 4, FALSE);
            g_object_unref(item);
        }
        g_list_free(kids);
    }
    g_object_unref(item);
}

static void dumpBackForwardListForWebView(WebKitWebView* view)
{
    printf("\n============== Back Forward List ==============\n");
    WebKitWebBackForwardList* bfList = webkit_web_view_get_back_forward_list(view);

    // Print out all items in the list after prevTestBFItem, which was from the previous test
    // Gather items from the end of the list, the print them out from oldest to newest
    GList* itemsToPrint = NULL;
    gint forwardListCount = webkit_web_back_forward_list_get_forward_length(bfList);
    for (int i = forwardListCount; i > 0; i--) {
        WebKitWebHistoryItem* item = webkit_web_back_forward_list_get_nth_item(bfList, i);
        // something is wrong if the item from the last test is in the forward part of the b/f list
        ASSERT(item != prevTestBFItem);
        g_object_ref(item);
        itemsToPrint = g_list_prepend(itemsToPrint, item);
    }

    WebKitWebHistoryItem* currentItem = webkit_web_back_forward_list_get_current_item(bfList);
    g_object_ref(currentItem);
    itemsToPrint = g_list_prepend(itemsToPrint, currentItem);

    gint backListCount = webkit_web_back_forward_list_get_back_length(bfList);
    for (int i = -1; i >= -(backListCount); i--) {
        WebKitWebHistoryItem* item = webkit_web_back_forward_list_get_nth_item(bfList, i);
        if (item == prevTestBFItem)
            break;
        g_object_ref(item);
        itemsToPrint = g_list_prepend(itemsToPrint, item);
    }

    for (GList* itemToPrint = itemsToPrint; itemToPrint; itemToPrint = g_list_next(itemToPrint)) {
        WebKitWebHistoryItem* item = WEBKIT_WEB_HISTORY_ITEM(itemToPrint->data);
        dumpHistoryItem(item, historyItemIndent, item == currentItem);
        g_object_unref(item);
    }

    g_list_free(itemsToPrint);
    printf("===============================================\n");
}

static void dumpBackForwardListForAllWebViews()
{
    // Dump the back forward list of the main WebView first
    dumpBackForwardListForWebView(webView);

    // The view list is prepended. Reverse the list so we get the order right.
    for (GSList* currentView = g_slist_reverse(webViewList); currentView; currentView = g_slist_next(currentView))
        dumpBackForwardListForWebView(WEBKIT_WEB_VIEW(currentView->data));
}

void setWaitToDumpWatchdog(guint timer)
{
    waitToDumpWatchdog = timer;
}

bool shouldSetWaitToDumpWatchdog()
{
    return !waitToDumpWatchdog && useTimeoutWatchdog;
}

CString soupURIToStringPreservingPassword(SoupURI* soupURI)
{
    if (!soupURI->password) {
        GUniquePtr<char> uriString(soup_uri_to_string(soupURI, FALSE));
        return uriString.get();
    }

    // soup_uri_to_string does not insert the password into the string, so we need to create the
    // URI string and then reinsert any credentials that were present in the SoupURI. All tests that
    // use URL-embedded credentials use HTTP, so it's safe here.
    GUniquePtr<char> password(soupURI->password);
    GUniquePtr<char> user(soupURI->user);
    soupURI->password = 0;
    soupURI->user = 0;

    GUniquePtr<char> uriString(soup_uri_to_string(soupURI, FALSE));
    String absoluteURIWithoutCredentialString = String::fromUTF8(uriString.get());
    String protocolAndCredential = String::format("http://%s:%s@", user ? user.get() : "", password.get());
    return absoluteURIWithoutCredentialString.replace("http://", protocolAndCredential).utf8();
}

static void invalidateAnyPreviousWaitToDumpWatchdog()
{
    if (waitToDumpWatchdog) {
        g_source_remove(waitToDumpWatchdog);
        waitToDumpWatchdog = 0;
    }

    waitForPolicy = false;
}

static void resetDefaultsToConsistentValues()
{
    WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
    GUniquePtr<gchar> localStoragePath(g_build_filename(g_get_user_data_dir(), "DumpRenderTreeGtk", "databases", nullptr));
    g_object_set(G_OBJECT(settings),
        "enable-accelerated-compositing", FALSE,
        "enable-private-browsing", FALSE,
        "enable-developer-extras", FALSE,
        "enable-spell-checking", TRUE,
        "enable-html5-database", TRUE,
        "enable-html5-local-storage", TRUE,
        "html5-local-storage-database-path", localStoragePath.get(),
        "enable-xss-auditor", FALSE,
        "enable-spatial-navigation", FALSE,
        "javascript-can-access-clipboard", TRUE,
        "javascript-can-open-windows-automatically", TRUE,
        "enable-offline-web-application-cache", TRUE,
        "enable-universal-access-from-file-uris", TRUE,
        "enable-file-access-from-file-uris", TRUE,
        "enable-scripts", TRUE,
        "enable-dom-paste", TRUE,
        "default-font-family", "Times",
        "monospace-font-family", "Courier",
        "serif-font-family", "Times",
        "sans-serif-font-family", "Helvetica",
        "cursive-font-family", "cursive",
        "fantasy-font-family", "fantasy",
        "default-font-size", 12,
        "default-monospace-font-size", 10,
        "minimum-font-size", 0,
        "enable-caret-browsing", FALSE,
        "enable-page-cache", FALSE,
        "auto-resize-window", TRUE,
        "auto-load-images", TRUE,
        "enable-java-applet", FALSE,
        "enable-plugins", TRUE,
        "enable-hyperlink-auditing", FALSE,
        "editing-behavior", WEBKIT_EDITING_BEHAVIOR_UNIX,
        "enable-fullscreen", TRUE,
        "enable-mediasource", TRUE,
        NULL);
    webkit_web_view_set_settings(webView, settings);
    webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER);

    DumpRenderTreeSupportGtk::clearMainFrameName(mainFrame);
    DumpRenderTreeSupportGtk::scalePageBy(webView, 1, 0, 0);

    WebKitWebInspector* inspector = webkit_web_view_get_inspector(webView);
    g_object_set(G_OBJECT(inspector), "javascript-profiling-enabled", FALSE, NULL);

    webkit_web_view_set_zoom_level(webView, 1.0);

    DumpRenderTreeSupportGtk::resetOriginAccessWhiteLists();

    WebKitWebBackForwardList* list = webkit_web_view_get_back_forward_list(webView);
    webkit_web_back_forward_list_clear(list);

    SoupSession* session = webkit_get_default_session();
    SoupCookieJar* jar = reinterpret_cast<SoupCookieJar*>(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));

    // We only create the jar when the soup backend needs to do
    // HTTP. Should we initialize it earlier, perhaps?
    if (jar)
        g_object_set(G_OBJECT(jar), SOUP_COOKIE_JAR_ACCEPT_POLICY, SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY, NULL);

    setlocale(LC_ALL, "");

    DumpRenderTreeSupportGtk::setLinksIncludedInFocusChain(true);
    webkit_icon_database_set_path(webkit_get_icon_database(), 0);
    DumpRenderTreeSupportGtk::setDefersLoading(webView, false);
    DumpRenderTreeSupportGtk::setSerializeHTTPLoads(false);

#if HAVE(ACCESSIBILITY)
    if (axController)
        axController->resetToConsistentState();
#endif

    DumpRenderTreeSupportGtk::clearOpener(mainFrame);
    DumpRenderTreeSupportGtk::setTracksRepaints(mainFrame, false);

    DumpRenderTreeSupportGtk::resetGeolocationClientMock(webView);

    DumpRenderTreeSupportGtk::setCSSGridLayoutEnabled(webView, false);
    DumpRenderTreeSupportGtk::setCSSRegionsEnabled(webView, true);
    DumpRenderTreeSupportGtk::setExperimentalContentSecurityPolicyFeaturesEnabled(true);
    DumpRenderTreeSupportGtk::setSeamlessIFramesEnabled(true);
    DumpRenderTreeSupportGtk::setShadowDOMEnabled(true);

    if (gTestRunner) {
        gTestRunner->setAuthenticationPassword("");
        gTestRunner->setAuthenticationUsername("");
        gTestRunner->setHandlesAuthenticationChallenges(false);
    }

    gtk_widget_set_direction(GTK_WIDGET(webView), GTK_TEXT_DIR_NONE);
}

static bool useLongRunningServerMode(int argc, char *argv[])
{
    // This assumes you've already called getopt_long
    return (argc == optind+1 && !strcmp(argv[optind], "-"));
}

static void runTestingServerLoop()
{
    // When DumpRenderTree runs in server mode, we just wait around for file names
    // to be passed to us and read each in turn, passing the results back to the client
    char filenameBuffer[2048];
    while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
        char* newLineCharacter = strchr(filenameBuffer, '\n');
        if (newLineCharacter)
            *newLineCharacter = '\0';

        if (!strlen(filenameBuffer))
            continue;

        runTest(filenameBuffer);
    }
}

static void initializeGlobalsFromCommandLineOptions(int argc, char *argv[])
{
    struct option options[] = {
        {"notree", no_argument, &dumpTree, false},
        {"pixel-tests", no_argument, &dumpPixelsForAllTests, true},
        {"tree", no_argument, &dumpTree, true},
        {"no-timeout", no_argument, &useTimeoutWatchdog, false},
        {NULL, 0, NULL, 0}
    };
    
    int option;
    while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1) {
        switch (option) {
        case '?': // unknown or ambiguous option
        case ':': // missing argument
            exit(1);
            break;
        }
    }
}


void dump()
{
    invalidateAnyPreviousWaitToDumpWatchdog();

    // Grab widget focus before dumping the contents of a widget, in
    // case it was lost in the course of the test.
    gtk_widget_grab_focus(GTK_WIDGET(webView));

    if (dumpTree) {
        char* result = 0;
        gchar* responseMimeType = webkit_web_frame_get_response_mime_type(mainFrame);

        if (g_str_equal(responseMimeType, "text/plain")) {
            gTestRunner->setDumpAsText(true);
            gTestRunner->setGeneratePixelResults(false);
        }
        g_free(responseMimeType);

        if (gTestRunner->dumpAsText())
            result = dumpFramesAsText(mainFrame);
        else {
            // Widget resizing is done asynchronously in GTK+. We pump the main
            // loop here, to flush any pending resize requests. This prevents
            // timing issues which affect the size of elements in the output.
            // We only enable this workaround for tests that print the render tree
            // because this seems to break some dumpAsText tests: see bug 39988
            // After fixing that test, we should apply this approach to all dumps.
            while (gtk_events_pending())
                gtk_main_iteration();

            result = g_strdup(DumpRenderTreeSupportGtk::dumpRenderTree(mainFrame).data());
        }

        if (!result) {
            const char* errorMessage;
            if (gTestRunner->dumpAsText())
                errorMessage = "[documentElement innerText]";
            else if (gTestRunner->dumpDOMAsWebArchive())
                errorMessage = "[[mainFrame DOMDocument] webArchive]";
            else if (gTestRunner->dumpSourceAsWebArchive())
                errorMessage = "[[mainFrame dataSource] webArchive]";
            else
                errorMessage = "[mainFrame renderTreeAsExternalRepresentation]";
            printf("ERROR: nil result from %s", errorMessage);
        } else {
            printf("%s", result);
            g_free(result);
            if (!gTestRunner->dumpAsText() && !gTestRunner->dumpDOMAsWebArchive() && !gTestRunner->dumpSourceAsWebArchive())
                dumpFrameScrollPosition(mainFrame);

            if (gTestRunner->dumpBackForwardList())
                dumpBackForwardListForAllWebViews();
        }

        if (printSeparators) {
            puts("#EOF"); // terminate the content block
            fputs("#EOF\n", stderr);
            fflush(stdout);
            fflush(stderr);
        }
    }

    if (dumpPixelsForCurrentTest
     && gTestRunner->generatePixelResults()
     && !gTestRunner->dumpDOMAsWebArchive()
     && !gTestRunner->dumpSourceAsWebArchive()) {
        DumpRenderTreeSupportGtk::forceWebViewPaint(webView);
        dumpWebViewAsPixelsAndCompareWithExpected(gTestRunner->expectedPixelHash());
    }

    // FIXME: call displayWebView here when we support --paint

    done = true;
    gtk_main_quit();
}

static CString temporaryDatabaseDirectory()
{
    const char* directoryFromEnvironment = g_getenv("DUMPRENDERTREE_TEMP");
    if (directoryFromEnvironment)
        return directoryFromEnvironment;
    GUniquePtr<char> fallback(g_build_filename(g_get_user_data_dir(), "gtkwebkitdrt", "databases", NULL));
    return fallback.get();
}

static void setDefaultsToConsistentStateValuesForTesting()
{
    resetDefaultsToConsistentValues();

#if PLATFORM(X11)
    webkit_web_settings_add_extra_plugin_directory(webView, TEST_PLUGIN_DIR);
#endif

    webkit_set_web_database_directory_path(temporaryDatabaseDirectory().data());

#if defined(GTK_API_VERSION_2)
    gtk_rc_parse_string("style \"nix_scrollbar_spacing\"                    "
                        "{                                                  "
                        "    GtkScrolledWindow::scrollbar-spacing = 0       "
                        "}                                                  "
                        "class \"GtkWidget\" style \"nix_scrollbar_spacing\"");

#else
    GtkCssProvider* cssProvider = gtk_css_provider_new();
    gtk_css_provider_load_from_data(cssProvider,
                                    "@binding-set NoKeyboardNavigation {        "
                                    "   unbind \"<shift>F10\";                  "
                                    "}                                          "
                                    " * {                                       "
                                    "   -GtkScrolledWindow-scrollbar-spacing: 0;"
                                    "   gtk-key-bindings: NoKeyboardNavigation; "
                                    "}                                          ",
                                    -1, 0);
    gtk_style_context_add_provider_for_screen(gdk_display_get_default_screen(gdk_display_get_default()),
                                              GTK_STYLE_PROVIDER(cssProvider),
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    g_object_unref(cssProvider);
#endif
}

static void sendPixelResultsEOF()
{
    puts("#EOF");

    fflush(stdout);
    fflush(stderr);
}

static void runTest(const string& inputLine)
{
    ASSERT(!inputLine.empty());

    TestCommand command = parseInputLine(inputLine);
    string& testURL = command.pathOrURL;
    dumpPixelsForCurrentTest = command.shouldDumpPixels || dumpPixelsForAllTests;

    // Convert the path into a full file URL if it does not look
    // like an HTTP/S URL (doesn't start with http:// or https://).
    if (testURL.find("http://") && testURL.find("https://")) {
        GFile* testFile = g_file_new_for_path(testURL.c_str());
        gchar* testURLCString = g_file_get_uri(testFile);
        testURL = testURLCString;
        g_free(testURLCString);
        g_object_unref(testFile);
    }

    resetDefaultsToConsistentValues();

    gTestRunner = TestRunner::create(testURL, command.expectedPixelHash);
    topLoadingFrame = 0;
    done = false;

    gTestRunner->setIconDatabaseEnabled(false);

    if (shouldLogFrameLoadDelegates(testURL))
        gTestRunner->setDumpFrameLoadCallbacks(true);

    if (shouldEnableDeveloperExtras(testURL)) {
        gTestRunner->setDeveloperExtrasEnabled(true);
        if (shouldOpenWebInspector(testURL))
            gTestRunner->showWebInspector();
        if (shouldDumpAsText(testURL)) {
            gTestRunner->setDumpAsText(true);
            gTestRunner->setGeneratePixelResults(false);
        }
    }

    WorkQueue::shared()->clear();
    WorkQueue::shared()->setFrozen(false);

    bool isSVGW3CTest = (testURL.find("svg/W3C-SVG-1.1") != string::npos);
    GtkAllocation size;
    size.x = size.y = 0;
    size.width = isSVGW3CTest ? TestRunner::w3cSVGViewWidth : TestRunner::viewWidth;
    size.height = isSVGW3CTest ? TestRunner::w3cSVGViewHeight : TestRunner::viewHeight;
    gtk_window_resize(GTK_WINDOW(window), size.width, size.height);
    gtk_widget_size_allocate(container, &size);

    if (prevTestBFItem)
        g_object_unref(prevTestBFItem);
    WebKitWebBackForwardList* bfList = webkit_web_view_get_back_forward_list(webView);
    prevTestBFItem = webkit_web_back_forward_list_get_current_item(bfList);
    if (prevTestBFItem)
        g_object_ref(prevTestBFItem);

    initializeFonts(testURL.c_str());

    // Focus the web view before loading the test to avoid focusing problems
    gtk_widget_grab_focus(GTK_WIDGET(webView));
    webkit_web_view_open(webView, testURL.c_str());

    gtk_main();

    // If developer extras enabled Web Inspector may have been open by the test.
    if (shouldEnableDeveloperExtras(testURL)) {
        gTestRunner->closeWebInspector();
        gTestRunner->setDeveloperExtrasEnabled(false);
    }

    // Also check if we still have opened webViews and free them.
    if (gTestRunner->closeRemainingWindowsWhenComplete() || webViewList) {
        while (webViewList) {
            g_object_unref(WEBKIT_WEB_VIEW(webViewList->data));
            webViewList = g_slist_next(webViewList);
        }
        g_slist_free(webViewList);
        webViewList = 0;
    }

    WebCoreTestSupport::resetInternalsObject(webkit_web_frame_get_global_context(mainFrame));
    DumpRenderTreeSupportGtk::clearMemoryCache();
    DumpRenderTreeSupportGtk::clearApplicationCache();

    // A blank load seems to be necessary to reset state after certain tests.
    webkit_web_view_open(webView, "about:blank");

    gTestRunner.clear();

    // terminate the (possibly empty) pixels block after all the state reset
    sendPixelResultsEOF();
}

void webViewLoadStarted(WebKitWebView* view, WebKitWebFrame* frame, void*)
{
    // Make sure we only set this once per test.  If it gets cleared, and then set again, we might
    // end up doing two dumps for one test.
    if (!topLoadingFrame && !done)
        topLoadingFrame = frame;
}

static gboolean processWork(void* data)
{
    // if we finish all the commands, we're ready to dump state
    if (WorkQueue::shared()->processWork() && !gTestRunner->waitToDump())
        dump();

    return FALSE;
}

static char* getFrameNameSuitableForTestResult(WebKitWebView* view, WebKitWebFrame* frame)
{
    char* frameName = g_strdup(webkit_web_frame_get_name(frame));

    if (frame == webkit_web_view_get_main_frame(view)) {
        // This is a bit strange. Shouldn't web_frame_get_name return NULL?
        if (frameName && (frameName[0] != '\0')) {
            char* tmp = g_strdup_printf("main frame \"%s\"", frameName);
            g_free(frameName);
            frameName = tmp;
        } else {
            g_free(frameName);
            frameName = g_strdup("main frame");
        }
    } else if (!frameName || (frameName[0] == '\0')) {
        g_free(frameName);
        frameName = g_strdup("frame (anonymous)");
    } else {
        char* tmp = g_strdup_printf("frame \"%s\"", frameName);
        g_free(frameName);
        frameName = tmp;
    }

    return frameName;
}

static void webViewLoadFinished(WebKitWebView* view, WebKitWebFrame* frame, void*)
{
    // The deprecated "load-finished" signal is triggered by postProgressFinishedNotification(),
    // so we can use it here in the DRT to provide the correct dump.
    if (frame != topLoadingFrame)
        return;
    if (gTestRunner->dumpProgressFinishedCallback())
        printf("postProgressFinishedNotification\n");
}

static gboolean webViewLoadError(WebKitWebView*, WebKitWebFrame*, gchar*, gpointer, gpointer)
{
    return TRUE; // Return true here to disable the default error page.
}

static void webViewDocumentLoadFinished(WebKitWebView* view, WebKitWebFrame* frame, void*)
{
    if (!done && gTestRunner->dumpFrameLoadCallbacks()) {
        char* frameName = getFrameNameSuitableForTestResult(view, frame);
        printf("%s - didFinishDocumentLoadForFrame\n", frameName);
        g_free(frameName);
    } else if (!done) {
        guint pendingFrameUnloadEvents = DumpRenderTreeSupportGtk::getPendingUnloadEventCount(frame);
        if (pendingFrameUnloadEvents) {
            char* frameName = getFrameNameSuitableForTestResult(view, frame);
            printf("%s - has %u onunload handler(s)\n", frameName, pendingFrameUnloadEvents);
            g_free(frameName);
        }
    }
}

static void webViewOnloadEvent(WebKitWebView* view, WebKitWebFrame* frame, void*)
{
    if (!done && gTestRunner->dumpFrameLoadCallbacks()) {
        char* frameName = getFrameNameSuitableForTestResult(view, frame);
        printf("%s - didHandleOnloadEventsForFrame\n", frameName);
        g_free(frameName);
    }
}

static void addControllerToWindow(JSContextRef context, JSObjectRef windowObject, const char* controllerName, JSValueRef controller)
{
    JSStringRef controllerNameStr = JSStringCreateWithUTF8CString(controllerName);
    JSObjectSetProperty(context, windowObject, controllerNameStr, controller, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
    JSStringRelease(controllerNameStr); 
}

static void webViewWindowObjectCleared(WebKitWebView* view, WebKitWebFrame* frame, JSGlobalContextRef context, JSObjectRef windowObject, gpointer data)
{
    JSValueRef exception = 0;
    ASSERT(gTestRunner);

    gTestRunner->makeWindowObject(context, windowObject, &exception);
    ASSERT(!exception);

    gcController->makeWindowObject(context, windowObject, &exception);
    ASSERT(!exception);

#if HAVE(ACCESSIBILITY)
    axController->makeWindowObject(context, windowObject, &exception);
    ASSERT(!exception);
#endif

    addControllerToWindow(context, windowObject, "eventSender", makeEventSender(context, !webkit_web_frame_get_parent(frame)));
    addControllerToWindow(context, windowObject, "textInputController", makeTextInputController(context));
    WebCoreTestSupport::injectInternalsObject(context);
}

static gboolean webViewConsoleMessage(WebKitWebView* view, const gchar* message, unsigned int line, const gchar* sourceId, gpointer data)
{
    gchar* testMessage = 0;
    const gchar* uriScheme;

    // Tests expect only the filename part of local URIs
    uriScheme = g_strstr_len(message, -1, "file://");
    if (uriScheme) {
        GString* tempString = g_string_sized_new(strlen(message));
        gchar* filename = g_strrstr(uriScheme, G_DIR_SEPARATOR_S);

        if (filename) {
            // If the path is a lone slash, keep it to avoid empty output.
            if (strlen(filename) > 1)
                filename += strlen(G_DIR_SEPARATOR_S);
            tempString = g_string_append_len(tempString, message, (uriScheme - message));
            tempString = g_string_append_len(tempString, filename, strlen(filename));
            testMessage = g_string_free(tempString, FALSE);
        }
    }

    fprintf(stdout, "CONSOLE MESSAGE: ");
    if (line)
        fprintf(stdout, "line %d: ", line);
    fprintf(stdout, "%s\n", testMessage ? testMessage : message);
    g_free(testMessage);

    return TRUE;
}


static gboolean webViewScriptAlert(WebKitWebView* view, WebKitWebFrame* frame, const gchar* message, gpointer data)
{
    fprintf(stdout, "ALERT: %s\n", message);
    fflush(stdout);
    return TRUE;
}

static gboolean webViewScriptPrompt(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message, const gchar* defaultValue, gchar** value, gpointer data)
{
    fprintf(stdout, "PROMPT: %s, default text: %s\n", message, defaultValue);
    *value = g_strdup(defaultValue);
    return TRUE;
}

static gboolean webViewScriptConfirm(WebKitWebView* view, WebKitWebFrame* frame, const gchar* message, gboolean* didConfirm, gpointer data)
{
    fprintf(stdout, "CONFIRM: %s\n", message);
    *didConfirm = TRUE;
    return TRUE;
}

static void webViewTitleChanged(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, gpointer data)
{
    if (gTestRunner->dumpFrameLoadCallbacks() && !done) {
        GUniquePtr<char> frameName(getFrameNameSuitableForTestResult(view, frame));
        printf("%s - didReceiveTitle: %s\n", frameName.get(), title ? title : "");
    }

    if (gTestRunner->dumpTitleChanges() && !done)
        printf("TITLE CHANGED: '%s'\n", title ? title : "");
}

static bool webViewNavigationPolicyDecisionRequested(WebKitWebView* view, WebKitWebFrame* frame,
                                                     WebKitNetworkRequest* request,
                                                     WebKitWebNavigationAction* navAction,
                                                     WebKitWebPolicyDecision* policyDecision)
{
    // Use the default handler if we're not waiting for policy,
    // i.e., TestRunner::waitForPolicyDelegate
    if (!waitForPolicy)
        return FALSE;

    gchar* typeDescription;
    WebKitWebNavigationReason reason;
    g_object_get(G_OBJECT(navAction), "reason", &reason, NULL);

    switch(reason) {
        case WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED:
            typeDescription = g_strdup("link clicked");
            break;
        case WEBKIT_WEB_NAVIGATION_REASON_FORM_SUBMITTED:
            typeDescription = g_strdup("form submitted");
            break;
        case WEBKIT_WEB_NAVIGATION_REASON_BACK_FORWARD:
            typeDescription = g_strdup("back/forward");
            break;
        case WEBKIT_WEB_NAVIGATION_REASON_RELOAD:
            typeDescription = g_strdup("reload");
            break;
        case WEBKIT_WEB_NAVIGATION_REASON_FORM_RESUBMITTED:
            typeDescription = g_strdup("form resubmitted");
            break;
        case WEBKIT_WEB_NAVIGATION_REASON_OTHER:
            typeDescription = g_strdup("other");
            break;
        default:
            typeDescription = g_strdup("illegal value");
    }

    printf("Policy delegate: attempt to load %s with navigation type '%s'\n", webkit_network_request_get_uri(request), typeDescription);
    g_free(typeDescription);

    webkit_web_policy_decision_ignore(policyDecision);
    gTestRunner->notifyDone();

    return TRUE;
}

static void webViewStatusBarTextChanged(WebKitWebView* view, const gchar* message, gpointer data)
{
    // Are we doing anything wrong? One test that does not call
    // dumpStatusCallbacks gets true here
    if (gTestRunner->dumpStatusCallbacks())
        printf("UI DELEGATE STATUS CALLBACK: setStatusText:%s\n", message);
}

static gboolean webViewClose(WebKitWebView* view)
{
    ASSERT(view);

    webViewList = g_slist_remove(webViewList, view);
    g_object_unref(view);

    return TRUE;
}

static void databaseQuotaExceeded(WebKitWebView* view, WebKitWebFrame* frame, WebKitWebDatabase *database)
{
    ASSERT(view);
    ASSERT(frame);
    ASSERT(database);

    WebKitSecurityOrigin* origin = webkit_web_database_get_security_origin(database);
    if (gTestRunner->dumpDatabaseCallbacks()) {
        printf("UI DELEGATE DATABASE CALLBACK: exceededDatabaseQuotaForSecurityOrigin:{%s, %s, %i} database:%s\n",
            webkit_security_origin_get_protocol(origin),
            webkit_security_origin_get_host(origin),
            webkit_security_origin_get_port(origin),
            webkit_web_database_get_name(database));
    }
    webkit_security_origin_set_web_database_quota(origin, 5 * 1024 * 1024);
}

static bool
geolocationPolicyDecisionRequested(WebKitWebView*, WebKitWebFrame*, WebKitGeolocationPolicyDecision* decision)
{
    if (!gTestRunner->isGeolocationPermissionSet())
        return FALSE;
    if (gTestRunner->geolocationPermission())
        webkit_geolocation_policy_allow(decision);
    else
        webkit_geolocation_policy_deny(decision);

    return TRUE;
}


static WebKitWebView* webViewCreate(WebKitWebView*, WebKitWebFrame*);

static gboolean webInspectorShowWindow(WebKitWebInspector*, gpointer data)
{
    gtk_window_set_default_size(GTK_WINDOW(webInspectorWindow), TestRunner::viewWidth, TestRunner::viewHeight);
    gtk_widget_show_all(webInspectorWindow);
    return TRUE;
}

static gboolean webInspectorCloseWindow(WebKitWebInspector*, gpointer data)
{
    gtk_widget_destroy(webInspectorWindow);
    webInspectorWindow = 0;
    return TRUE;
}

static WebKitWebView* webInspectorInspectWebView(WebKitWebInspector*, gpointer data)
{
    webInspectorWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    GtkWidget* webView = self_scrolling_webkit_web_view_new();
    gtk_container_add(GTK_CONTAINER(webInspectorWindow),
                      webView);

    return WEBKIT_WEB_VIEW(webView);
}

static void topLoadingFrameLoadFinished()
{
    topLoadingFrame = 0;
    WorkQueue::shared()->setFrozen(true); // first complete load freezes the queue for the rest of this test
    if (gTestRunner->waitToDump())
        return;

    if (WorkQueue::shared()->count())
        g_idle_add_full(G_PRIORITY_DEFAULT, processWork, 0, 0);
    else
        dump();
}

static void webFrameLoadStatusNotified(WebKitWebFrame* frame, gpointer user_data)
{
    WebKitLoadStatus loadStatus = webkit_web_frame_get_load_status(frame);

    if (gTestRunner->dumpFrameLoadCallbacks()) {
        GUniquePtr<char> frameName(getFrameNameSuitableForTestResult(webkit_web_frame_get_web_view(frame), frame));

        switch (loadStatus) {
        case WEBKIT_LOAD_PROVISIONAL:
            if (!done)
                printf("%s - didStartProvisionalLoadForFrame\n", frameName.get());
            break;
        case WEBKIT_LOAD_COMMITTED:
            if (!done)
                printf("%s - didCommitLoadForFrame\n", frameName.get());
            break;
        case WEBKIT_LOAD_FINISHED:
            if (!done)
                printf("%s - didFinishLoadForFrame\n", frameName.get());
            break;
        case WEBKIT_LOAD_FAILED:
            if (!done)
                printf("%s - didFailLoadWithError\n", frameName.get());
            break;
        default:
            break;
        }
    }

    if ((loadStatus == WEBKIT_LOAD_FINISHED || loadStatus == WEBKIT_LOAD_FAILED)
        && frame == topLoadingFrame)
        topLoadingFrameLoadFinished();
}

static void frameCreatedCallback(WebKitWebView* webView, WebKitWebFrame* webFrame, gpointer user_data)
{
    g_signal_connect(webFrame, "notify::load-status", G_CALLBACK(webFrameLoadStatusNotified), NULL);
    g_signal_connect(webFrame, "insecure-content-run", G_CALLBACK(didRunInsecureContent), NULL);
}

static String pathFromSoupURI(SoupURI* uri)
{
    if (!uri)
        return "(null)";

    if (!g_str_equal(uri->scheme, "file"))
        return soupURIToStringPreservingPassword(uri).data();

    String pathString = uri->path;
    GUniquePtr<gchar> pathBasename(g_path_get_basename(pathString.utf8().data()));

    WebKitWebFrame* mainFrame = webkit_web_view_get_main_frame(webView);
    GUniquePtr<SoupURI> mainFrameUri(soup_uri_new(webkit_web_frame_get_uri(mainFrame)));

    String mainFrameUriPathString = mainFrameUri.get()->path;
    String basePath = mainFrameUriPathString.substring(0, mainFrameUriPathString.reverseFind('/') + 1);

    if (!basePath.isEmpty() && pathString.startsWith(basePath))
        return pathString.substring(basePath.length());

    return pathBasename.get();
}

static CString convertSoupMessageToURLPath(SoupMessage* soupMessage)
{
    if (!soupMessage)
        return CString("(null)");
    if (SoupURI* requestURI = soup_message_get_uri(soupMessage))
        return pathFromSoupURI(requestURI).utf8();
    return CString("(null)");
}

static CString convertNetworkRequestToURLPath(WebKitNetworkRequest* request)
{
    return convertSoupMessageToURLPath(webkit_network_request_get_message(request));
}

static CString convertWebResourceToURLPath(WebKitWebResource* webResource)
{
    GUniquePtr<SoupURI> uri(soup_uri_new(webkit_web_resource_get_uri(webResource)));
    return pathFromSoupURI(uri.get()).utf8();
}

static CString urlSuitableForTestResult(const char* uriString)
{
    if (!g_str_has_prefix(uriString, "file://"))
        return CString(uriString);

    GUniquePtr<gchar> basename(g_path_get_basename(uriString));
    return CString(basename.get());
}

static CString descriptionSuitableForTestResult(SoupURI* uri)
{
    if (!uri)
        return CString("(null)");

    GUniquePtr<char> uriString(soup_uri_to_string(uri, false));
    return urlSuitableForTestResult(uriString.get());
}

static CString descriptionSuitableForTestResult(GError* error, WebKitWebResource* webResource)
{
    const gchar* errorDomain = g_quark_to_string(error->domain);
    CString resourceURIString(urlSuitableForTestResult(webkit_web_resource_get_uri(webResource)));

    if (g_str_equal(errorDomain, "webkit-network-error-quark") || g_str_equal(errorDomain, "soup_http_error_quark"))
        errorDomain = "NSURLErrorDomain";

    if (g_str_equal(errorDomain, "WebKitPolicyError"))
        errorDomain = "WebKitErrorDomain";

    // TODO: the other ports get the failingURL from the ResourceError
    GUniquePtr<char> errorString(g_strdup_printf("<NSError domain %s, code %d, failing URL \"%s\">",
                                              errorDomain, error->code, resourceURIString.data()));
    return CString(errorString.get());
}

static CString descriptionSuitableForTestResult(WebKitNetworkRequest* request)
{
    SoupMessage* soupMessage = webkit_network_request_get_message(request);

    if (!soupMessage)
        return CString("(null)");

    SoupURI* mainDocumentURI = soup_message_get_first_party(soupMessage);
    CString mainDocumentURIString(descriptionSuitableForTestResult(mainDocumentURI));
    CString path(convertNetworkRequestToURLPath(request));
    GUniquePtr<char> description(g_strdup_printf("<NSURLRequest URL %s, main document URL %s, http method %s>",
        path.data(), mainDocumentURIString.data(), soupMessage->method));
    return CString(description.get());
}

static CString descriptionSuitableForTestResult(WebKitNetworkResponse* response)
{
    if (!response)
        return CString("(null)");

    int statusCode = 0;
    CString responseURIString(urlSuitableForTestResult(webkit_network_response_get_uri(response)));
    SoupMessage* soupMessage = webkit_network_response_get_message(response);
    CString path;

    if (soupMessage) {
        statusCode = soupMessage->status_code;
        path = convertSoupMessageToURLPath(soupMessage);
    } else
        path = CString("(null)");

    GUniquePtr<char> description(g_strdup_printf("<NSURLResponse %s, http status code %d>", path.data(), statusCode));
    return CString(description.get());
}

static void willSendRequestCallback(WebKitWebView* webView, WebKitWebFrame* webFrame, WebKitWebResource* resource, WebKitNetworkRequest* request, WebKitNetworkResponse* response)
{


    if (!done && gTestRunner->willSendRequestReturnsNull()) {
        // As requested by the TestRunner, don't perform the request.
        webkit_network_request_set_uri(request, "about:blank");
        return;
    }

    if (!done && gTestRunner->dumpResourceLoadCallbacks())
        printf("%s - willSendRequest %s redirectResponse %s\n",
               convertNetworkRequestToURLPath(request).data(),
               descriptionSuitableForTestResult(request).data(),
               descriptionSuitableForTestResult(response).data());

    SoupMessage* soupMessage = webkit_network_request_get_message(request);
    SoupURI* uri = soup_uri_new(webkit_network_request_get_uri(request));

    if (SOUP_URI_IS_VALID(uri)) {
        GUniquePtr<char> uriString(soup_uri_to_string(uri, FALSE));

        if (SOUP_URI_VALID_FOR_HTTP(uri) && g_strcmp0(uri->host, "127.0.0.1")
            && g_strcmp0(uri->host, "255.255.255.255")
            && g_ascii_strncasecmp(uri->host, "localhost", 9)) {
            printf("Blocked access to external URL %s\n", uriString.get());
            // Cancel load of blocked resource to avoid potential
            // network-related timeouts in tests.
            webkit_network_request_set_uri(request, "about:blank");
            soup_uri_free(uri);
            return;
        }

        const string& destination = gTestRunner->redirectionDestinationForURL(uriString.get());
        if (!destination.empty())
            webkit_network_request_set_uri(request, destination.c_str());
    }

    if (uri)
        soup_uri_free(uri);

    if (soupMessage) {
        const set<string>& clearHeaders = gTestRunner->willSendRequestClearHeaders();
        for (set<string>::const_iterator header = clearHeaders.begin(); header != clearHeaders.end(); ++header)
            soup_message_headers_remove(soupMessage->request_headers, header->c_str());
    }
}


static void didReceiveResponse(WebKitWebView* webView, WebKitWebFrame*, WebKitWebResource* webResource, WebKitNetworkResponse* response)
{
    if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
        CString responseDescription(descriptionSuitableForTestResult(response));
        CString path(convertWebResourceToURLPath(webResource));
        printf("%s - didReceiveResponse %s\n", path.data(), responseDescription.data());
    }

    // TODO: add "has MIME type" whenever dumpResourceResponseMIMETypes() is supported.
    // See https://bugs.webkit.org/show_bug.cgi?id=58222.
}

static void didFinishLoading(WebKitWebView* webView, WebKitWebFrame* webFrame, WebKitWebResource* webResource)
{
    if (!done && gTestRunner->dumpResourceLoadCallbacks())
        printf("%s - didFinishLoading\n", convertWebResourceToURLPath(webResource).data());
}

static void didFailLoadingWithError(WebKitWebView* webView, WebKitWebFrame* webFrame, WebKitWebResource* webResource, GError* webError)
{
    if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
        CString webErrorString(descriptionSuitableForTestResult(webError, webResource));
        printf("%s - didFailLoadingWithError: %s\n", convertWebResourceToURLPath(webResource).data(),
               webErrorString.data());
    }
}

static void didRunInsecureContent(WebKitWebFrame*, WebKitSecurityOrigin*, const char* url)
{
    if (!done && gTestRunner->dumpFrameLoadCallbacks())
        printf("didRunInsecureContent\n");
}

static gboolean webViewRunFileChooser(WebKitWebView*, WebKitFileChooserRequest*)
{
    // We return TRUE to not propagate the event further so the
    // default file chooser dialog is not shown.
    return TRUE;
}

static void frameLoadEventCallback(WebKitWebFrame* frame, DumpRenderTreeSupportGtk::FrameLoadEvent event, const char* url)
{
    if (done || !gTestRunner->dumpFrameLoadCallbacks())
        return;

    GUniquePtr<char> frameName(getFrameNameSuitableForTestResult(webkit_web_frame_get_web_view(frame), frame));
    switch (event) {
    case DumpRenderTreeSupportGtk::WillPerformClientRedirectToURL:
        ASSERT(url);
        printf("%s - willPerformClientRedirectToURL: %s \n", frameName.get(), url);
        break;
    case DumpRenderTreeSupportGtk::DidCancelClientRedirect:
        printf("%s - didCancelClientRedirectForFrame\n", frameName.get());
        break;
    case DumpRenderTreeSupportGtk::DidReceiveServerRedirectForProvisionalLoad:
        printf("%s - didReceiveServerRedirectForProvisionalLoadForFrame\n", frameName.get());
        break;
    case DumpRenderTreeSupportGtk::DidDisplayInsecureContent:
        printf ("didDisplayInsecureContent\n");
        break;
    case DumpRenderTreeSupportGtk::DidDetectXSS:
        printf ("didDetectXSS\n");
        break;
    default:
        ASSERT_NOT_REACHED();
    }
}

static bool authenticationCallback(CString& username, CString& password, WebKitWebResource* webResource)
{
    CString description(convertWebResourceToURLPath(webResource));

    if (!gTestRunner->handlesAuthenticationChallenges()) {
        printf("%s - didReceiveAuthenticationChallenge - Simulating cancelled authentication sheet\n", description.data());
        return false;
    }

    username = gTestRunner->authenticationUsername().c_str();
    password = gTestRunner->authenticationPassword().c_str();
    printf("%s - didReceiveAuthenticationChallenge - Responding with %s:%s\n", description.data(), username.data(), password.data());
    return true;
}

static WebKitWebView* createWebView()
{
    // It is important to declare DRT is running early so when creating
    // web view mock clients are used instead of proper ones.
    DumpRenderTreeSupportGtk::setDumpRenderTreeModeEnabled(true);

    DumpRenderTreeSupportGtk::setFrameLoadEventCallback(frameLoadEventCallback);
    DumpRenderTreeSupportGtk::setAuthenticationCallback(authenticationCallback);

    WebKitWebView* view = WEBKIT_WEB_VIEW(self_scrolling_webkit_web_view_new());

    g_object_connect(G_OBJECT(view),
                     "signal::load-started", webViewLoadStarted, 0,
                     "signal::load-finished", webViewLoadFinished, 0,
                     "signal::load-error", webViewLoadError, 0,
                     "signal::window-object-cleared", webViewWindowObjectCleared, 0,
                     "signal::console-message", webViewConsoleMessage, 0,
                     "signal::script-alert", webViewScriptAlert, 0,
                     "signal::script-prompt", webViewScriptPrompt, 0,
                     "signal::script-confirm", webViewScriptConfirm, 0,
                     "signal::title-changed", webViewTitleChanged, 0,
                     "signal::navigation-policy-decision-requested", webViewNavigationPolicyDecisionRequested, 0,
                     "signal::status-bar-text-changed", webViewStatusBarTextChanged, 0,
                     "signal::create-web-view", webViewCreate, 0,
                     "signal::close-web-view", webViewClose, 0,
                     "signal::database-quota-exceeded", databaseQuotaExceeded, 0,
                     "signal::document-load-finished", webViewDocumentLoadFinished, 0,
                     "signal::geolocation-policy-decision-requested", geolocationPolicyDecisionRequested, 0,
                     "signal::onload-event", webViewOnloadEvent, 0,
                     "signal::drag-begin", dragBeginCallback, 0,
                     "signal::drag-end", dragEndCallback, 0,
                     "signal::drag-failed", dragFailedCallback, 0,
                     "signal::frame-created", frameCreatedCallback, 0,
                     "signal::resource-request-starting", willSendRequestCallback, 0,
                     "signal::resource-response-received", didReceiveResponse, 0,
                     "signal::resource-load-finished", didFinishLoading, 0,
                     "signal::resource-load-failed", didFailLoadingWithError, 0,
                     "signal::run-file-chooser", webViewRunFileChooser, 0,
                     NULL);
    connectEditingCallbacks(view);

    WebKitWebInspector* inspector = webkit_web_view_get_inspector(view);
    g_object_connect(G_OBJECT(inspector),
                     "signal::inspect-web-view", webInspectorInspectWebView, 0,
                     "signal::show-window", webInspectorShowWindow, 0,
                     "signal::close-window", webInspectorCloseWindow, 0,
                     NULL);

    if (webView) {
        WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
        webkit_web_view_set_settings(view, settings);
    }

    // frame-created is not issued for main frame. That's why we must do this here
    WebKitWebFrame* frame = webkit_web_view_get_main_frame(view);
    g_signal_connect(frame, "notify::load-status", G_CALLBACK(webFrameLoadStatusNotified), NULL);
    g_signal_connect(frame, "insecure-content-run", G_CALLBACK(didRunInsecureContent), NULL);

    return view;
}

static WebKitWebView* webViewCreate(WebKitWebView* view, WebKitWebFrame* frame)
{
    if (!gTestRunner->canOpenWindows())
        return 0;

    // Make sure that waitUntilDone has been called.
    ASSERT(gTestRunner->waitToDump());

    WebKitWebView* newWebView = createWebView();
    g_object_ref_sink(G_OBJECT(newWebView));
    webViewList = g_slist_prepend(webViewList, newWebView);
    return newWebView;
}

static void logHandler(const gchar* domain, GLogLevelFlags level, const gchar* message, gpointer data)
{
    if (level < G_LOG_LEVEL_DEBUG)
        fprintf(stderr, "%s\n", message);
}

int main(int argc, char* argv[])
{
    gtk_init(&argc, &argv);

    // Some plugins might try to use the GLib logger for printing debug
    // messages. This will cause tests to fail because of unexpected output.
    // We squelch all debug messages sent to the logger.
    g_log_set_default_handler(logHandler, 0);

    initializeGlobalsFromCommandLineOptions(argc, argv);
    initializeFonts();

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
#ifdef GTK_API_VERSION_2
    container = gtk_hbox_new(TRUE, 0);
#else
    container = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_box_set_homogeneous(GTK_BOX(container), TRUE);
#endif
    gtk_container_add(GTK_CONTAINER(window), container);
    gtk_widget_show_all(window);

    webView = createWebView();
    gtk_box_pack_start(GTK_BOX(container), GTK_WIDGET(webView), TRUE, TRUE, 0);
    gtk_widget_realize(GTK_WIDGET(webView));
    gtk_widget_show_all(container);
    mainFrame = webkit_web_view_get_main_frame(webView);

    setDefaultsToConsistentStateValuesForTesting();

    gcController = new GCController();
#if HAVE(ACCESSIBILITY)
    axController = new AccessibilityController();
#endif

    if (useLongRunningServerMode(argc, argv)) {
        printSeparators = true;
        runTestingServerLoop();
    } else {
        printSeparators = (optind < argc-1 || (dumpPixelsForCurrentTest && dumpTree));
        for (int i = optind; i != argc; ++i)
            runTest(argv[i]);
    }

    delete gcController;
    gcController = 0;

#if HAVE(ACCESSIBILITY)
    delete axController;
    axController = 0;
#endif

    gtk_widget_destroy(window);

    return 0;
}