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 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
|
/*
* Copyright (C) 2011 Igalia S.L.
* Copyright (C) 2014 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2,1 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitTestServer.h"
#include "WebViewTest.h"
#include <WebCore/SoupVersioning.h>
#include <glib/gstdio.h>
#include <wtf/glib/GRefPtr.h>
#if PLATFORM(WPE) && USE(WPEBACKEND_FDO_AUDIO_EXTENSION)
#include <wpe/extensions/audio.h>
#endif
class IsPlayingAudioWebViewTest : public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(IsPlayingAudioWebViewTest);
static void isPlayingAudioChanged(GObject*, GParamSpec*, IsPlayingAudioWebViewTest* test)
{
g_signal_handlers_disconnect_by_func(test->m_webView, reinterpret_cast<void*>(isPlayingAudioChanged), test);
g_main_loop_quit(test->m_mainLoop);
}
void waitUntilIsPlayingAudioChanged()
{
g_signal_connect(m_webView, "notify::is-playing-audio", G_CALLBACK(isPlayingAudioChanged), this);
g_main_loop_run(m_mainLoop);
}
void periodicallyCheckIsPlayingForAWhile()
{
m_tickCount = 0;
g_timeout_add(50, [](gpointer userData) -> gboolean {
auto* test = static_cast<IsPlayingAudioWebViewTest*>(userData);
g_assert_true(webkit_web_view_is_playing_audio(test->m_webView));
test->m_tickCount++;
if (test->m_tickCount >= 10) {
test->quitMainLoop();
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}, this);
g_main_loop_run(m_mainLoop);
}
private:
uint32_t m_tickCount { 0 };
};
static WebKitTestServer* gServer;
static void testWebViewWebContext(WebViewTest* test, gconstpointer)
{
g_assert_true(webkit_web_view_get_context(test->m_webView) == test->m_webContext.get());
g_assert_true(webkit_web_context_get_default() != test->m_webContext.get());
// Check that a web view created with g_object_new has the default context.
auto webView = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
nullptr));
g_assert_true(webkit_web_view_get_context(webView.get()) == webkit_web_context_get_default());
// Check that a web view created with a related view has the related view context.
webView = Test::adoptView(Test::createWebView(test->m_webView));
g_assert_true(webkit_web_view_get_context(webView.get()) == test->m_webContext.get());
// Check that a web context given as construct parameter is ignored if a related view is also provided.
webView = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
"web-context", webkit_web_context_get_default(),
"related-view", test->m_webView,
nullptr));
g_assert_true(webkit_web_view_get_context(webView.get()) == test->m_webContext.get());
}
static void testWebViewWebContextLifetime(WebViewTest* test, gconstpointer)
{
WebKitWebContext* webContext = webkit_web_context_new();
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webContext));
auto* webView = Test::createWebView(webContext);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView));
#if PLATFORM(GTK)
g_object_ref_sink(webView);
#endif
g_object_unref(webContext);
// Check that the web view still has a valid context.
WebKitWebContext* tmpContext = webkit_web_view_get_context(WEBKIT_WEB_VIEW(webView));
g_assert_true(WEBKIT_IS_WEB_CONTEXT(tmpContext));
g_object_unref(webView);
WebKitWebContext* webContext2 = webkit_web_context_new();
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webContext2));
auto* webView2 = Test::createWebView(webContext2);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView2));
#if PLATFORM(GTK)
g_object_ref_sink(webView2);
#endif
g_object_unref(webView2);
// Check that the context is still valid.
g_assert_true(WEBKIT_IS_WEB_CONTEXT(webContext2));
g_object_unref(webContext2);
}
static void testWebViewCloseQuickly(WebViewTest* test, gconstpointer)
{
auto webView = Test::adoptView(Test::createWebView());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView.get()));
g_idle_add([](gpointer userData) -> gboolean {
static_cast<WebViewTest*>(userData)->quitMainLoop();
return G_SOURCE_REMOVE;
}, test);
g_main_loop_run(test->m_mainLoop);
webView = nullptr;
}
#if PLATFORM(WPE)
static void testWebViewWebBackend(Test* test, gconstpointer)
{
static struct wpe_view_backend_interface s_testingInterface = {
// create
[](void*, struct wpe_view_backend*) -> void* { return nullptr; },
// destroy
[](void*) { },
// initialize
[](void*) { },
// get_renderer_host_fd
[](void*) -> int { return -1; },
// padding
nullptr,
nullptr,
nullptr,
nullptr
};
// User provided backend with default deleter (we don't have a way to check the backend will be actually freed).
GRefPtr<WebKitWebView> webView = adoptGRef(webkit_web_view_new(webkit_web_view_backend_new(wpe_view_backend_create_with_backend_interface(&s_testingInterface, nullptr), nullptr, nullptr)));
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView.get()));
auto* viewBackend = webkit_web_view_get_backend(webView.get());
g_assert_nonnull(viewBackend);
auto* wpeBackend = webkit_web_view_backend_get_wpe_backend(viewBackend);
g_assert_nonnull(wpeBackend);
webView = nullptr;
// User provided backend with destroy notify.
wpeBackend = wpe_view_backend_create_with_backend_interface(&s_testingInterface, nullptr);
webView = adoptGRef(webkit_web_view_new(webkit_web_view_backend_new(wpeBackend, [](gpointer userData) {
auto* backend = *static_cast<struct wpe_view_backend**>(userData);
wpe_view_backend_destroy(backend);
*static_cast<struct wpe_view_backend**>(userData) = nullptr;
}, &wpeBackend)));
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView.get()));
webView = nullptr;
g_assert_null(wpeBackend);
// User provided backend owned by another object with destroy notify.
static bool hasInstance = false;
struct BackendOwner {
BackendOwner(struct wpe_view_backend* backend)
: backend(backend)
{
hasInstance = true;
}
~BackendOwner()
{
wpe_view_backend_destroy(backend);
hasInstance = false;
}
struct wpe_view_backend* backend;
};
auto* owner = new BackendOwner(wpe_view_backend_create_with_backend_interface(&s_testingInterface, nullptr));
g_assert_true(hasInstance);
webView = adoptGRef(webkit_web_view_new(webkit_web_view_backend_new(owner->backend, [](gpointer userData) {
delete static_cast<BackendOwner*>(userData);
}, owner)));
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView.get()));
g_assert_true(hasInstance);
webView = nullptr;
g_assert_false(hasInstance);
}
#endif // PLATFORM(WPE)
static void ephemeralViewloadChanged(WebKitWebView* webView, WebKitLoadEvent loadEvent, WebViewTest* test)
{
if (loadEvent != WEBKIT_LOAD_FINISHED)
return;
g_signal_handlers_disconnect_by_func(webView, reinterpret_cast<void*>(ephemeralViewloadChanged), test);
test->quitMainLoop();
}
static void testWebViewEphemeral(WebViewTest* test, gconstpointer)
{
g_assert_false(webkit_web_view_is_ephemeral(test->m_webView));
g_assert_false(webkit_web_context_is_ephemeral(webkit_web_view_get_context(test->m_webView)));
auto* manager = webkit_web_context_get_website_data_manager(test->m_webContext.get());
g_assert_false(webkit_website_data_manager_is_ephemeral(manager));
g_assert_true(webkit_web_view_get_website_data_manager(test->m_webView) == manager);
webkit_website_data_manager_clear(manager, WEBKIT_WEBSITE_DATA_DISK_CACHE, 0, nullptr, [](GObject* manager, GAsyncResult* result, gpointer userData) {
webkit_website_data_manager_clear_finish(WEBKIT_WEBSITE_DATA_MANAGER(manager), result, nullptr);
static_cast<WebViewTest*>(userData)->quitMainLoop();
}, test);
g_main_loop_run(test->m_mainLoop);
// A WebView on a non ephemeral context can be ephemeral.
auto webView = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
"web-context", webkit_web_view_get_context(test->m_webView),
"is-ephemeral", TRUE,
nullptr));
g_assert_true(webkit_web_view_is_ephemeral(webView.get()));
g_assert_false(webkit_web_context_is_ephemeral(webkit_web_view_get_context(webView.get())));
g_assert_true(webkit_web_view_get_website_data_manager(webView.get()) != manager);
g_signal_connect(webView.get(), "load-changed", G_CALLBACK(ephemeralViewloadChanged), test);
webkit_web_view_load_uri(webView.get(), gServer->getURIForPath("/").data());
g_main_loop_run(test->m_mainLoop);
// Disk cache delays the storing of initial resources for 1 second to avoid
// affecting early page load. So, wait 1 second here to make sure resources
// have already been stored.
test->wait(1);
webkit_website_data_manager_fetch(manager, WEBKIT_WEBSITE_DATA_DISK_CACHE, nullptr, [](GObject* manager, GAsyncResult* result, gpointer userData) {
auto* test = static_cast<WebViewTest*>(userData);
g_assert_null(webkit_website_data_manager_fetch_finish(WEBKIT_WEBSITE_DATA_MANAGER(manager), result, nullptr));
test->quitMainLoop();
}, test);
g_main_loop_run(test->m_mainLoop);
}
static void testWebViewCustomCharset(WebViewTest* test, gconstpointer)
{
test->loadURI(gServer->getURIForPath("/").data());
test->waitUntilLoadFinished();
g_assert_null(webkit_web_view_get_custom_charset(test->m_webView));
webkit_web_view_set_custom_charset(test->m_webView, "utf8");
// Changing the charset reloads the page, so wait until reloaded.
test->waitUntilLoadFinished();
g_assert_cmpstr(webkit_web_view_get_custom_charset(test->m_webView), ==, "utf8");
// Go back to the default charset and wait until reloaded.
webkit_web_view_set_custom_charset(test->m_webView, nullptr);
test->waitUntilLoadFinished();
g_assert_null(webkit_web_view_get_custom_charset(test->m_webView));
}
static void testWebViewSettings(WebViewTest* test, gconstpointer)
{
WebKitSettings* defaultSettings = webkit_web_view_get_settings(test->m_webView);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(defaultSettings));
g_assert_nonnull(defaultSettings);
g_assert_true(webkit_settings_get_enable_javascript(defaultSettings));
GRefPtr<WebKitSettings> newSettings = adoptGRef(webkit_settings_new());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(newSettings.get()));
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript", FALSE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
WebKitSettings* settings = webkit_web_view_get_settings(test->m_webView);
g_assert_true(settings != defaultSettings);
g_assert_false(webkit_settings_get_enable_javascript(settings));
auto webView2 = Test::adoptView(Test::createWebView());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView2.get()));
webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView2.get()), settings);
g_assert_true(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webView2.get())) == settings);
GRefPtr<WebKitSettings> newSettings2 = adoptGRef(webkit_settings_new());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(newSettings2.get()));
webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView2.get()), newSettings2.get());
settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webView2.get()));
g_assert_true(settings == newSettings2.get());
g_assert_true(webkit_settings_get_enable_javascript(settings));
auto webView3 = Test::adoptView(Test::createWebView(newSettings2.get()));
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webView3.get()));
g_assert_true(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webView3.get())) == newSettings2.get());
}
static void testWebViewZoomLevel(WebViewTest* test, gconstpointer)
{
g_assert_cmpfloat(webkit_web_view_get_zoom_level(test->m_webView), ==, 1);
webkit_web_view_set_zoom_level(test->m_webView, 2.5);
g_assert_cmpfloat(webkit_web_view_get_zoom_level(test->m_webView), ==, 2.5);
webkit_settings_set_zoom_text_only(webkit_web_view_get_settings(test->m_webView), TRUE);
// The zoom level shouldn't change when zoom-text-only setting changes.
g_assert_cmpfloat(webkit_web_view_get_zoom_level(test->m_webView), ==, 2.5);
}
static void testWebViewRunAsyncFunctions(WebViewTest* test, gconstpointer)
{
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return new Promise((resolve) => { resolve(42); });", nullptr, nullptr, &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 42);
GVariantDict dict;
g_variant_dict_init(&dict, nullptr);
g_variant_dict_insert(&dict, "count", "u", 42);
auto* args = g_variant_dict_end(&dict);
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return new Promise((resolve) => { resolve(count); });", args, nullptr, &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 42);
g_variant_dict_init(&dict, nullptr);
g_variant_dict_insert(&dict, "motto", "s", "Never gonna give you up");
args = g_variant_dict_end(&dict);
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return new Promise((resolve) => { resolve(motto); });", args, nullptr, &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
GUniquePtr<char> valueString(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "Never gonna give you up");
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return new Promise(function(resolve, reject) { setTimeout(function(){ reject('Rejected!') }, 0); })", nullptr, nullptr, &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
g_assert_true(g_strstr_len(error->message, strlen(error->message), "Rejected!") != nullptr);
g_variant_dict_init(&dict, nullptr);
g_variant_dict_insert(&dict, "countt", "u", 42);
args = g_variant_dict_end(&dict);
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return new Promise((resolve) => { resolve(count); });", args, nullptr, &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
g_variant_dict_init(&dict, nullptr);
g_variant_dict_insert(&dict, "count", "u", 42);
args = g_variant_dict_end(&dict);
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return count", args, nullptr, &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 42);
{
// Set a value in main world.
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("a = 25;", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 25);
// Read back value from main world.
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return a", nullptr, nullptr, &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 25);
// Values of the main world are not available in the isolated one.
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return a", nullptr, "WebExtensionTestScriptWorld", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
// An empty string for world name is a distinct isolated world.
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return a", nullptr, "", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
// Running a script in a world that doesn't exist should fail.
javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return a", nullptr, "InvalidScriptWorld", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
}
{
// Disable JS support and expect an error when attempting to evaluate JS code.
WebKitSettings* defaultSettings = webkit_web_view_get_settings(test->m_webView);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(defaultSettings));
g_assert_nonnull(defaultSettings);
g_assert_true(webkit_settings_get_enable_javascript(defaultSettings));
GRefPtr<WebKitSettings> newSettings = adoptGRef(webkit_settings_new());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(newSettings.get()));
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript", FALSE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
WebKitJavascriptResult* javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return new Promise((resolve) => { resolve(42); });", nullptr, nullptr, &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript", TRUE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
}
{
// Disable JS markup support and expect no error when attempting to evaluate JS code.
WebKitSettings* defaultSettings = webkit_web_view_get_settings(test->m_webView);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(defaultSettings));
g_assert_nonnull(defaultSettings);
g_assert_true(webkit_settings_get_enable_javascript_markup(defaultSettings));
GRefPtr<WebKitSettings> newSettings = adoptGRef(webkit_settings_new());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(newSettings.get()));
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript-markup", FALSE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
WebKitJavascriptResult* javascriptResult = test->runAsyncJavaScriptFunctionInWorldAndWaitUntilFinished("return new Promise((resolve) => { resolve(42); });", nullptr, nullptr, &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript-markup", TRUE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
}
}
static void testWebViewRunJavaScript(WebViewTest* test, gconstpointer)
{
static const char* html = "<html><body><a id='WebKitLink' href='http://www.webkitgtk.org/' title='WebKitGTK Title'>WebKitGTK Website</a></body></html>";
test->loadHtml(html, 0);
test->waitUntilLoadFinished();
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("window.document.getElementById('WebKitLink').title;", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
GUniquePtr<char> valueString(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "WebKitGTK Title");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("window.document.getElementById('WebKitLink').href;", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
valueString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "http://www.webkitgtk.org/");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("window.document.getElementById('WebKitLink').textContent", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
valueString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "WebKitGTK Website");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("a = 25;", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 25);
javascriptResult = test->runJavaScriptAndWaitUntilFinished("a = 2.5;", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 2.5);
javascriptResult = test->runJavaScriptAndWaitUntilFinished("a = true", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultToBoolean(javascriptResult));
javascriptResult = test->runJavaScriptAndWaitUntilFinished("a = false", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_false(WebViewTest::javascriptResultToBoolean(javascriptResult));
javascriptResult = test->runJavaScriptAndWaitUntilFinished("a = null", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultIsNull(javascriptResult));
javascriptResult = test->runJavaScriptAndWaitUntilFinished("function Foo() { a = 25; } Foo();", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultIsUndefined(javascriptResult));
javascriptResult = test->runJavaScriptFromGResourceAndWaitUntilFinished("/org/webkit/glib/tests/link-title.js", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
valueString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "WebKitGTK Title");
javascriptResult = test->runJavaScriptFromGResourceAndWaitUntilFinished("/wrong/path/to/resource.js", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
javascriptResult = test->runJavaScriptAndWaitUntilFinished("foo();", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
// Values of the main world are not available in the isolated one.
javascriptResult = test->runJavaScriptInWorldAndWaitUntilFinished("a", "WebExtensionTestScriptWorld", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
javascriptResult = test->runJavaScriptInWorldAndWaitUntilFinished("a = 50", "WebExtensionTestScriptWorld", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 50);
// Values of the isolated world are not available in the normal one.
javascriptResult = test->runJavaScriptAndWaitUntilFinished("a", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_assert_cmpfloat(WebViewTest::javascriptResultToNumber(javascriptResult), ==, 25);
// Running a script in a world that doesn't exist should fail.
javascriptResult = test->runJavaScriptInWorldAndWaitUntilFinished("a", "InvalidScriptWorld", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
{
// Disable JS support and expect an error when attempting to evaluate JS code.
WebKitSettings* defaultSettings = webkit_web_view_get_settings(test->m_webView);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(defaultSettings));
g_assert_nonnull(defaultSettings);
g_assert_true(webkit_settings_get_enable_javascript(defaultSettings));
GRefPtr<WebKitSettings> newSettings = adoptGRef(webkit_settings_new());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(newSettings.get()));
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript", FALSE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("console.log(\"Hi\");", &error.outPtr());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript", TRUE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
}
{
// Disable JS markup support and expect no error when attempting to evaluate JS code.
WebKitSettings* defaultSettings = webkit_web_view_get_settings(test->m_webView);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(defaultSettings));
g_assert_nonnull(defaultSettings);
g_assert_true(webkit_settings_get_enable_javascript_markup(defaultSettings));
GRefPtr<WebKitSettings> newSettings = adoptGRef(webkit_settings_new());
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(newSettings.get()));
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript-markup", FALSE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("console.log(\"Hi\");", &error.outPtr());
g_assert_nonnull(javascriptResult);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(webkit_javascript_result_get_js_value(javascriptResult)));
g_assert_no_error(error.get());
g_object_set(G_OBJECT(newSettings.get()), "enable-javascript-markup", TRUE, NULL);
webkit_web_view_set_settings(test->m_webView, newSettings.get());
}
}
class FullScreenClientTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(FullScreenClientTest);
enum FullScreenEvent {
None,
Enter,
Leave
};
static gboolean viewEnterFullScreenCallback(WebKitWebView*, FullScreenClientTest* test)
{
test->m_event = Enter;
g_main_loop_quit(test->m_mainLoop);
return FALSE;
}
static gboolean viewLeaveFullScreenCallback(WebKitWebView*, FullScreenClientTest* test)
{
test->m_event = Leave;
g_main_loop_quit(test->m_mainLoop);
return FALSE;
}
FullScreenClientTest()
: m_event(None)
{
webkit_settings_set_enable_fullscreen(webkit_web_view_get_settings(m_webView), TRUE);
g_signal_connect(m_webView, "enter-fullscreen", G_CALLBACK(viewEnterFullScreenCallback), this);
g_signal_connect(m_webView, "leave-fullscreen", G_CALLBACK(viewLeaveFullScreenCallback), this);
}
~FullScreenClientTest()
{
g_signal_handlers_disconnect_matched(m_webView, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
}
void requestFullScreenAndWaitUntilEnteredFullScreen()
{
m_event = None;
webkit_web_view_run_javascript(m_webView, "document.documentElement.webkitRequestFullScreen();", 0, 0, 0);
g_main_loop_run(m_mainLoop);
}
static gboolean leaveFullScreenIdle(FullScreenClientTest* test)
{
#if PLATFORM(GTK)
test->keyStroke(GDK_KEY_Escape);
#else
test->keyStroke(WPE_KEY_Escape);
#endif
return FALSE;
}
void leaveFullScreenAndWaitUntilLeftFullScreen()
{
m_event = None;
g_idle_add(reinterpret_cast<GSourceFunc>(leaveFullScreenIdle), this);
g_main_loop_run(m_mainLoop);
}
FullScreenEvent m_event;
};
#if ENABLE(FULLSCREEN_API)
static void testWebViewFullScreen(FullScreenClientTest* test, gconstpointer)
{
test->showInWindow();
test->loadHtml("<html><body>FullScreen test</body></html>", 0);
test->waitUntilLoadFinished();
test->requestFullScreenAndWaitUntilEnteredFullScreen();
g_assert_cmpint(test->m_event, ==, FullScreenClientTest::Enter);
test->leaveFullScreenAndWaitUntilLeftFullScreen();
g_assert_cmpint(test->m_event, ==, FullScreenClientTest::Leave);
}
#endif
static void testWebViewCanShowMIMEType(WebViewTest* test, gconstpointer)
{
// Supported MIME types.
g_assert_true(webkit_web_view_can_show_mime_type(test->m_webView, "text/html"));
g_assert_true(webkit_web_view_can_show_mime_type(test->m_webView, "text/plain"));
g_assert_true(webkit_web_view_can_show_mime_type(test->m_webView, "image/jpeg"));
// Unsupported MIME types.
g_assert_false(webkit_web_view_can_show_mime_type(test->m_webView, "text/vcard"));
g_assert_false(webkit_web_view_can_show_mime_type(test->m_webView, "application/zip"));
g_assert_false(webkit_web_view_can_show_mime_type(test->m_webView, "application/octet-stream"));
#if ENABLE(NETSCAPE_PLUGIN_API)
// Plugins are only supported when enabled.
webkit_web_context_set_additional_plugins_directory(webkit_web_view_get_context(test->m_webView), WEBKIT_TEST_PLUGIN_DIR);
g_assert_true(webkit_web_view_can_show_mime_type(test->m_webView, "application/x-webkit-test-netscape"));
webkit_settings_set_enable_plugins(webkit_web_view_get_settings(test->m_webView), FALSE);
g_assert_false(webkit_web_view_can_show_mime_type(test->m_webView, "application/x-webkit-test-netscape"));
#endif
}
#if PLATFORM(GTK)
class FormClientTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(FormClientTest);
static void submitFormCallback(WebKitWebView*, WebKitFormSubmissionRequest* request, FormClientTest* test)
{
test->submitForm(request);
}
FormClientTest()
: m_submitPositionX(0)
, m_submitPositionY(0)
{
g_signal_connect(m_webView, "submit-form", G_CALLBACK(submitFormCallback), this);
}
~FormClientTest()
{
g_signal_handlers_disconnect_matched(m_webView, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
}
void submitForm(WebKitFormSubmissionRequest* request)
{
assertObjectIsDeletedWhenTestFinishes(G_OBJECT(request));
m_request = request;
webkit_form_submission_request_submit(request);
quitMainLoop();
}
GHashTable* getTextFieldsAsHashTable()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return webkit_form_submission_request_get_text_fields(m_request.get());
#pragma GCC diagnostic pop
}
GPtrArray* getTextFieldNames()
{
GPtrArray* names;
webkit_form_submission_request_list_text_fields(m_request.get(), &names, nullptr);
return names;
}
GPtrArray* getTextFieldValues()
{
GPtrArray* values;
webkit_form_submission_request_list_text_fields(m_request.get(), nullptr, &values);
return values;
}
static gboolean doClickIdleCallback(FormClientTest* test)
{
test->clickMouseButton(test->m_submitPositionX, test->m_submitPositionY, 1);
return FALSE;
}
void submitFormAtPosition(int x, int y)
{
m_submitPositionX = x;
m_submitPositionY = y;
g_idle_add(reinterpret_cast<GSourceFunc>(doClickIdleCallback), this);
g_main_loop_run(m_mainLoop);
}
int m_submitPositionX;
int m_submitPositionY;
GRefPtr<WebKitFormSubmissionRequest> m_request;
};
static void testWebViewSubmitForm(FormClientTest* test, gconstpointer)
{
test->showInWindow();
const char* formHTML =
"<html><body>"
" <form action='#'>"
" <input type='text' name='text1' value='value1'>"
" <input type='text' name='text2' value='value2'>"
" <input type='text' value='value3'>"
" <input type='text' name='text2'>"
" <input type='password' name='password' value='secret'>"
" <textarea cols='5' rows='5' name='textarea'>Text</textarea>"
" <input type='hidden' name='hidden1' value='hidden1'>"
" <input type='submit' value='Submit' style='position:absolute; left:1; top:1' size='10'>"
" </form>"
"</body></html>";
test->loadHtml(formHTML, "file:///");
test->waitUntilLoadFinished();
test->submitFormAtPosition(5, 5);
GHashTable* tableValues = test->getTextFieldsAsHashTable();
g_assert_nonnull(tableValues);
g_assert_cmpuint(g_hash_table_size(tableValues), ==, 4);
g_assert_cmpstr(static_cast<char*>(g_hash_table_lookup(tableValues, "text1")), ==, "value1");
g_assert_cmpstr(static_cast<char*>(g_hash_table_lookup(tableValues, "")), ==, "value3");
g_assert_cmpstr(static_cast<char*>(g_hash_table_lookup(tableValues, "text2")), ==, "");
g_assert_cmpstr(static_cast<char*>(g_hash_table_lookup(tableValues, "password")), ==, "secret");
GPtrArray* names = test->getTextFieldNames();
g_assert_nonnull(names);
g_assert_cmpuint(names->len, ==, 5);
g_assert_cmpstr(static_cast<char*>(names->pdata[0]), ==, "text1");
g_assert_cmpstr(static_cast<char*>(names->pdata[1]), ==, "text2");
g_assert_cmpstr(static_cast<char*>(names->pdata[2]), ==, "");
g_assert_cmpstr(static_cast<char*>(names->pdata[3]), ==, "text2");
g_assert_cmpstr(static_cast<char*>(names->pdata[4]), ==, "password");
GPtrArray* values = test->getTextFieldValues();
g_assert_nonnull(values);
g_assert_cmpuint(values->len, ==, 5);
g_assert_cmpstr(static_cast<char*>(values->pdata[0]), ==, "value1");
g_assert_cmpstr(static_cast<char*>(values->pdata[1]), ==, "value2");
g_assert_cmpstr(static_cast<char*>(values->pdata[2]), ==, "value3");
g_assert_cmpstr(static_cast<char*>(values->pdata[3]), ==, "");
g_assert_cmpstr(static_cast<char*>(values->pdata[4]), ==, "secret");
}
#endif // PLATFORM(GTK)
class SaveWebViewTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(SaveWebViewTest);
SaveWebViewTest()
: m_tempDirectory(g_dir_make_tmp("WebKit2SaveViewTest-XXXXXX", 0))
{
}
~SaveWebViewTest()
{
if (G_IS_FILE(m_file.get()))
g_file_delete(m_file.get(), 0, 0);
if (G_IS_INPUT_STREAM(m_inputStream.get()))
g_input_stream_close(m_inputStream.get(), 0, 0);
if (m_tempDirectory)
g_rmdir(m_tempDirectory.get());
}
static void webViewSavedToStreamCallback(GObject* object, GAsyncResult* result, SaveWebViewTest* test)
{
GUniqueOutPtr<GError> error;
test->m_inputStream = adoptGRef(webkit_web_view_save_finish(test->m_webView, result, &error.outPtr()));
g_assert_true(G_IS_INPUT_STREAM(test->m_inputStream.get()));
g_assert_no_error(error.get());
test->quitMainLoop();
}
static void webViewSavedToFileCallback(GObject* object, GAsyncResult* result, SaveWebViewTest* test)
{
GUniqueOutPtr<GError> error;
g_assert_true(webkit_web_view_save_to_file_finish(test->m_webView, result, &error.outPtr()));
g_assert_no_error(error.get());
test->quitMainLoop();
}
void saveAndWaitForStream()
{
webkit_web_view_save(m_webView, WEBKIT_SAVE_MODE_MHTML, 0, reinterpret_cast<GAsyncReadyCallback>(webViewSavedToStreamCallback), this);
g_main_loop_run(m_mainLoop);
}
void saveAndWaitForFile()
{
m_saveDestinationFilePath.reset(g_build_filename(m_tempDirectory.get(), "testWebViewSaveResult.mht", NULL));
m_file = adoptGRef(g_file_new_for_path(m_saveDestinationFilePath.get()));
webkit_web_view_save_to_file(m_webView, m_file.get(), WEBKIT_SAVE_MODE_MHTML, 0, reinterpret_cast<GAsyncReadyCallback>(webViewSavedToFileCallback), this);
g_main_loop_run(m_mainLoop);
}
GUniquePtr<char> m_tempDirectory;
GUniquePtr<char> m_saveDestinationFilePath;
GRefPtr<GInputStream> m_inputStream;
GRefPtr<GFile> m_file;
};
static void testWebViewSave(SaveWebViewTest* test, gconstpointer)
{
test->loadHtml("<html>"
"<body>"
" <p>A paragraph with plain text</p>"
" <p>"
" A red box: <img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3AYWDTMVwnSZnwAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFklEQVQI12P8z8DAwMDAxMDAwMDAAAANHQEDK+mmyAAAAABJRU5ErkJggg=='></br>"
" A blue box: <img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3AYWDTMvBHhALQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFklEQVQI12Nk4PnPwMDAxMDAwMDAAAALrwEPPIs1pgAAAABJRU5ErkJggg=='>"
" </p>"
"</body>"
"</html>", 0);
test->waitUntilLoadFinished();
// Write to a file and to an input stream.
test->saveAndWaitForFile();
test->saveAndWaitForStream();
// We should have exactly the same amount of bytes in the file
// than those coming from the GInputStream. We don't compare the
// strings read since the 'Date' field and the boundaries will be
// different on each case. MHTML functionality will be tested by
// Layout tests, so checking the amount of bytes is enough.
GUniqueOutPtr<GError> error;
gchar buffer[512] = { 0 };
gssize readBytes = 0;
gssize totalBytesFromStream = 0;
while ((readBytes = g_input_stream_read(test->m_inputStream.get(), &buffer, 512, 0, &error.outPtr()))) {
g_assert_no_error(error.get());
totalBytesFromStream += readBytes;
}
// Check that the file exists and that it contains the same amount of bytes.
GRefPtr<GFileInfo> fileInfo = adoptGRef(g_file_query_info(test->m_file.get(), G_FILE_ATTRIBUTE_STANDARD_SIZE, static_cast<GFileQueryInfoFlags>(0), 0, 0));
g_assert_cmpint(g_file_info_get_size(fileInfo.get()), ==, totalBytesFromStream);
}
// To test page visibility API. Currently only 'visible', 'hidden' and 'prerender' states are implemented fully in WebCore.
// See also http://www.w3.org/TR/2011/WD-page-visibility-20110602/ and https://developers.google.com/chrome/whitepapers/pagevisibility
static void testWebViewPageVisibility(WebViewTest* test, gconstpointer)
{
test->loadHtml("<html><title></title>"
"<body><p>Test Web Page Visibility</p>"
"<script>"
"document.addEventListener(\"visibilitychange\", onVisibilityChange, false);"
"function onVisibilityChange() {"
" document.title = document.visibilityState;"
"}"
"</script>"
"</body></html>",
0);
// Wait until the page is loaded. Initial visibility should be 'prerender'.
test->waitUntilLoadFinished();
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.visibilityState;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
GUniquePtr<char> valueString(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "hidden");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.hidden;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultToBoolean(javascriptResult));
// Show the page. The visibility should be updated to 'visible'.
test->showInWindow();
test->waitUntilTitleChangedTo("visible");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.visibilityState;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
valueString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "visible");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.hidden;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
g_assert_false(WebViewTest::javascriptResultToBoolean(javascriptResult));
// Hide the page. The visibility should be updated to 'hidden'.
test->hideView();
test->waitUntilTitleChangedTo("hidden");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.visibilityState;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
valueString.reset(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(valueString.get(), ==, "hidden");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.hidden;", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultToBoolean(javascriptResult));
}
static void testWebViewDocumentFocus(WebViewTest* test, gconstpointer)
{
if (!g_strcmp0(g_getenv("UNDER_XVFB"), "yes")) {
g_test_skip("This tests doesn't work under Xvfb");
return;
}
test->showInWindow();
test->loadHtml("<html><title></title>"
"<body onload='document.getElementById(\"editable\").focus()'>"
"<input id='editable'></input>"
"<script>"
"document.addEventListener(\"visibilitychange\", onVisibilityChange, false);"
"function onVisibilityChange() {"
" document.title = document.visibilityState;"
"}"
"</script>"
"</body></html>",
nullptr);
test->waitUntilLoadFinished();
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.hasFocus();", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultToBoolean(javascriptResult));
// Hide the view to make it lose the focus, the window is still the active one though.
test->hideView();
test->waitUntilTitleChangedTo("hidden");
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.hasFocus();", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
g_assert_false(WebViewTest::javascriptResultToBoolean(javascriptResult));
}
#if PLATFORM(GTK)
class SnapshotWebViewTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(SnapshotWebViewTest);
static void onSnapshotCancelledReady(WebKitWebView* web_view, GAsyncResult* res, SnapshotWebViewTest* test)
{
GUniqueOutPtr<GError> error;
test->m_surface = webkit_web_view_get_snapshot_finish(web_view, res, &error.outPtr());
g_assert_null(test->m_surface);
g_assert_error(error.get(), G_IO_ERROR, G_IO_ERROR_CANCELLED);
test->quitMainLoop();
}
gboolean getSnapshotAndCancel()
{
if (m_surface)
cairo_surface_destroy(m_surface);
m_surface = 0;
GRefPtr<GCancellable> cancellable = adoptGRef(g_cancellable_new());
webkit_web_view_get_snapshot(m_webView, WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE, cancellable.get(), reinterpret_cast<GAsyncReadyCallback>(onSnapshotCancelledReady), this);
g_cancellable_cancel(cancellable.get());
g_main_loop_run(m_mainLoop);
return true;
}
};
static void testWebViewSnapshot(SnapshotWebViewTest* test, gconstpointer)
{
test->loadHtml("<html><head><style>html { width: 200px; height: 100px; } ::-webkit-scrollbar { display: none; }</style></head><body><p>Whatever</p></body></html>", nullptr);
test->waitUntilLoadFinished();
// WEBKIT_SNAPSHOT_REGION_VISIBLE returns a null surface when the view is not visible.
cairo_surface_t* surface1 = test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
g_assert_null(surface1);
// WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT works even if the window is not visible.
surface1 = test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT, WEBKIT_SNAPSHOT_OPTIONS_NONE);
g_assert_nonnull(surface1);
g_assert_cmpuint(cairo_surface_get_type(surface1), ==, CAIRO_SURFACE_TYPE_IMAGE);
g_assert_cmpint(cairo_image_surface_get_width(surface1), ==, 200);
g_assert_cmpint(cairo_image_surface_get_height(surface1), ==, 100);
// Show the WebView in a popup widow of 50x50 and try again with WEBKIT_SNAPSHOT_REGION_VISIBLE.
test->showInWindow(50, 50);
surface1 = cairo_surface_reference(test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE));
g_assert_nonnull(surface1);
g_assert_cmpuint(cairo_surface_get_type(surface1), ==, CAIRO_SURFACE_TYPE_IMAGE);
g_assert_cmpint(cairo_image_surface_get_width(surface1), ==, 50);
g_assert_cmpint(cairo_image_surface_get_height(surface1), ==, 50);
// Select all text in the WebView, request a snapshot ignoring selection.
test->selectAll();
cairo_surface_t* surface2 = test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_NONE);
g_assert_nonnull(surface2);
g_assert_true(Test::cairoSurfacesEqual(surface1, surface2));
// Request a new snapshot, including the selection this time. The size should be the same but the result
// must be different to the one previously obtained.
surface2 = test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_INCLUDE_SELECTION_HIGHLIGHTING);
g_assert_cmpuint(cairo_surface_get_type(surface2), ==, CAIRO_SURFACE_TYPE_IMAGE);
g_assert_cmpint(cairo_image_surface_get_width(surface1), ==, cairo_image_surface_get_width(surface2));
g_assert_cmpint(cairo_image_surface_get_height(surface1), ==, cairo_image_surface_get_height(surface2));
g_assert_false(Test::cairoSurfacesEqual(surface1, surface2));
// Get a snpashot with a transparent background, the result must be different.
surface2 = test->getSnapshotAndWaitUntilReady(WEBKIT_SNAPSHOT_REGION_VISIBLE, WEBKIT_SNAPSHOT_OPTIONS_TRANSPARENT_BACKGROUND);
g_assert_cmpuint(cairo_surface_get_type(surface2), ==, CAIRO_SURFACE_TYPE_IMAGE);
g_assert_cmpint(cairo_image_surface_get_width(surface1), ==, cairo_image_surface_get_width(surface2));
g_assert_cmpint(cairo_image_surface_get_height(surface1), ==, cairo_image_surface_get_height(surface2));
g_assert_false(Test::cairoSurfacesEqual(surface1, surface2));
cairo_surface_destroy(surface1);
// Test that cancellation works.
g_assert_true(test->getSnapshotAndCancel());
}
#endif // PLATFORM(GTK)
#if ENABLE(NOTIFICATIONS)
class NotificationWebViewTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE_WITH_SETUP_TEARDOWN(NotificationWebViewTest, setup, teardown);
static void setup()
{
WebViewTest::shouldInitializeWebViewInConstructor = false;
}
static void teardown()
{
WebViewTest::shouldInitializeWebViewInConstructor = true;
}
enum NotificationEvent {
None,
Permission,
Shown,
Clicked,
OnClicked,
Closed,
OnClosed,
};
static gboolean permissionRequestCallback(WebKitWebView*, WebKitPermissionRequest *request, NotificationWebViewTest* test)
{
g_assert_true(WEBKIT_IS_NOTIFICATION_PERMISSION_REQUEST(request));
g_assert_true(test->m_isExpectingPermissionRequest);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(request));
test->m_event = Permission;
webkit_permission_request_allow(request);
g_main_loop_quit(test->m_mainLoop);
return TRUE;
}
static gboolean notificationClosedCallback(WebKitNotification* notification, NotificationWebViewTest* test)
{
g_assert_true(test->m_notification == notification);
test->m_notification = nullptr;
test->m_event = Closed;
if (g_main_loop_is_running(test->m_mainLoop))
g_main_loop_quit(test->m_mainLoop);
return TRUE;
}
static gboolean notificationClickedCallback(WebKitNotification* notification, NotificationWebViewTest* test)
{
g_assert_true(test->m_notification == notification);
test->m_event = Clicked;
return TRUE;
}
static gboolean showNotificationCallback(WebKitWebView*, WebKitNotification* notification, NotificationWebViewTest* test)
{
g_assert_null(test->m_notification);
test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(notification));
test->m_notification = notification;
g_signal_connect(notification, "closed", G_CALLBACK(notificationClosedCallback), test);
g_signal_connect(notification, "clicked", G_CALLBACK(notificationClickedCallback), test);
test->m_event = Shown;
g_main_loop_quit(test->m_mainLoop);
return TRUE;
}
static void notificationsMessageReceivedCallback(WebKitUserContentManager* userContentManager, WebKitJavascriptResult* javascriptResult, NotificationWebViewTest* test)
{
GUniquePtr<char> valueString(WebViewTest::javascriptResultToCString(javascriptResult));
if (g_str_equal(valueString.get(), "clicked"))
test->m_event = OnClicked;
else if (g_str_equal(valueString.get(), "closed"))
test->m_event = OnClosed;
g_main_loop_quit(test->m_mainLoop);
}
void initialize()
{
initializeWebView();
g_signal_connect(m_webView, "permission-request", G_CALLBACK(permissionRequestCallback), this);
g_signal_connect(m_webView, "show-notification", G_CALLBACK(showNotificationCallback), this);
webkit_user_content_manager_register_script_message_handler(m_userContentManager.get(), "notifications");
g_signal_connect(m_userContentManager.get(), "script-message-received::notifications", G_CALLBACK(notificationsMessageReceivedCallback), this);
}
~NotificationWebViewTest()
{
g_signal_handlers_disconnect_matched(m_webView, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
g_signal_handlers_disconnect_matched(m_userContentManager.get(), G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
webkit_user_content_manager_unregister_script_message_handler(m_userContentManager.get(), "notifications");
}
bool hasPermission()
{
auto* result = runJavaScriptAndWaitUntilFinished("Notification.permission;", nullptr);
g_assert_nonnull(result);
GUniquePtr<char> value(javascriptResultToCString(result));
return !g_strcmp0(value.get(), "granted");
}
void requestPermissionAndWaitUntilGiven()
{
m_event = None;
m_isExpectingPermissionRequest = true;
webkit_web_view_run_javascript(m_webView, "Notification.requestPermission();", nullptr, nullptr, nullptr);
g_main_loop_run(m_mainLoop);
}
void requestNotificationAndWaitUntilShown(const char* title, const char* body)
{
m_event = None;
GUniquePtr<char> jscode(g_strdup_printf("n = new Notification('%s', { body: '%s'});", title, body));
webkit_web_view_run_javascript(m_webView, jscode.get(), nullptr, nullptr, nullptr);
g_main_loop_run(m_mainLoop);
}
void requestNotificationAndWaitUntilShown(const char* title, const char* body, const char* tag)
{
m_event = None;
GUniquePtr<char> jscode(g_strdup_printf("n = new Notification('%s', { body: '%s', tag: '%s'});", title, body, tag));
webkit_web_view_run_javascript(m_webView, jscode.get(), nullptr, nullptr, nullptr);
g_main_loop_run(m_mainLoop);
}
void clickNotificationAndWaitUntilClicked()
{
m_event = None;
runJavaScriptAndWaitUntilFinished("n.onclick = function() { window.webkit.messageHandlers.notifications.postMessage('clicked'); }", nullptr);
webkit_notification_clicked(m_notification);
g_assert_cmpint(m_event, ==, Clicked);
g_main_loop_run(m_mainLoop);
}
void closeNotificationAndWaitUntilClosed()
{
m_event = None;
webkit_web_view_run_javascript(m_webView, "n.close()", nullptr, nullptr, nullptr);
g_main_loop_run(m_mainLoop);
}
void closeNotificationAndWaitUntilOnClosed()
{
g_assert_nonnull(m_notification);
m_event = None;
runJavaScriptAndWaitUntilFinished("n.onclose = function() { window.webkit.messageHandlers.notifications.postMessage('closed'); }", nullptr);
webkit_notification_close(m_notification);
g_assert_cmpint(m_event, ==, Closed);
g_main_loop_run(m_mainLoop);
}
NotificationEvent m_event { None };
WebKitNotification* m_notification { nullptr };
bool m_isExpectingPermissionRequest { false };
bool m_hasPermission { false };
};
static void testWebViewNotification(NotificationWebViewTest* test, gconstpointer)
{
test->initialize();
// Notifications don't work with local or special schemes.
test->loadURI(gServer->getURIForPath("/").data());
test->waitUntilLoadFinished();
g_assert_false(test->hasPermission());
test->requestPermissionAndWaitUntilGiven();
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::Permission);
g_assert_true(test->hasPermission());
static const char* title = "This is a notification";
static const char* body = "This is the body.";
static const char* tag = "This is the tag.";
test->requestNotificationAndWaitUntilShown(title, body, tag);
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::Shown);
g_assert_nonnull(test->m_notification);
g_assert_cmpstr(webkit_notification_get_title(test->m_notification), ==, title);
g_assert_cmpstr(webkit_notification_get_body(test->m_notification), ==, body);
g_assert_cmpstr(webkit_notification_get_tag(test->m_notification), ==, tag);
test->clickNotificationAndWaitUntilClicked();
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::OnClicked);
test->closeNotificationAndWaitUntilClosed();
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::Closed);
test->requestNotificationAndWaitUntilShown(title, body);
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::Shown);
g_assert_cmpstr(webkit_notification_get_tag(test->m_notification), ==, nullptr);
test->closeNotificationAndWaitUntilOnClosed();
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::OnClosed);
// The first notification should be closed automatically because the tag is
// the same. It will crash in showNotificationCallback on failure.
test->requestNotificationAndWaitUntilShown(title, body, tag);
test->requestNotificationAndWaitUntilShown(title, body, tag);
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::Shown);
}
static void setInitialNotificationPermissionsAllowedCallback(WebKitWebContext* context, NotificationWebViewTest* test)
{
GList* allowedOrigins = g_list_prepend(nullptr, webkit_security_origin_new_for_uri(gServer->baseURL().string().utf8().data()));
webkit_web_context_initialize_notification_permissions(test->m_webContext.get(), allowedOrigins, nullptr);
g_list_free_full(allowedOrigins, reinterpret_cast<GDestroyNotify>(webkit_security_origin_unref));
}
static void setInitialNotificationPermissionsDisallowedCallback(WebKitWebContext* context, NotificationWebViewTest* test)
{
GList* disallowedOrigins = g_list_prepend(nullptr, webkit_security_origin_new_for_uri(gServer->baseURL().string().utf8().data()));
webkit_web_context_initialize_notification_permissions(test->m_webContext.get(), nullptr, disallowedOrigins);
g_list_free_full(disallowedOrigins, reinterpret_cast<GDestroyNotify>(webkit_security_origin_unref));
}
static void testWebViewNotificationInitialPermissionAllowed(NotificationWebViewTest* test, gconstpointer)
{
g_signal_connect(test->m_webContext.get(), "initialize-notification-permissions", G_CALLBACK(setInitialNotificationPermissionsAllowedCallback), test);
test->initialize();
test->loadURI(gServer->getURIForPath("/").data());
test->waitUntilLoadFinished();
g_assert_true(test->hasPermission());
test->requestNotificationAndWaitUntilShown("This is a notification", "This is the body.");
g_assert_cmpint(test->m_event, ==, NotificationWebViewTest::Shown);
}
static void testWebViewNotificationInitialPermissionDisallowed(NotificationWebViewTest* test, gconstpointer)
{
g_signal_connect(test->m_webContext.get(), "initialize-notification-permissions", G_CALLBACK(setInitialNotificationPermissionsDisallowedCallback), test);
test->initialize();
test->loadURI(gServer->getURIForPath("/").data());
test->waitUntilLoadFinished();
g_assert_false(test->hasPermission());
}
#endif // ENABLE(NOTIFICATIONS)
static void testWebViewIsPlayingAudio(IsPlayingAudioWebViewTest* test, gconstpointer)
{
// The web view must be realized for the video to start playback and
// trigger changes in WebKitWebView::is-playing-audio.
test->showInWindow();
// Initially, web views should always report no audio being played.
g_assert_false(webkit_web_view_is_playing_audio(test->m_webView));
g_assert_false(webkit_web_view_get_is_muted(test->m_webView));
GUniquePtr<char> resourcePath(g_build_filename(Test::getResourcesDir(Test::WebKit2Resources).data(), "file-with-video.html", nullptr));
GUniquePtr<char> resourceURL(g_filename_to_uri(resourcePath.get(), nullptr, nullptr));
webkit_web_view_load_uri(test->m_webView, resourceURL.get());
test->waitUntilLoadFinished();
g_assert_false(webkit_web_view_is_playing_audio(test->m_webView));
test->runJavaScriptAndWaitUntilFinished("playVideo();", nullptr);
if (!webkit_web_view_is_playing_audio(test->m_webView))
test->waitUntilIsPlayingAudioChanged();
g_assert_true(webkit_web_view_is_playing_audio(test->m_webView));
// Mute the page, webkit_web_view_is_playing_audio() should still return TRUE.
webkit_web_view_set_is_muted(test->m_webView, TRUE);
g_assert_true(webkit_web_view_get_is_muted(test->m_webView));
test->periodicallyCheckIsPlayingForAWhile();
g_assert_true(webkit_web_view_is_playing_audio(test->m_webView));
webkit_web_view_set_is_muted(test->m_webView, FALSE);
g_assert_false(webkit_web_view_get_is_muted(test->m_webView));
g_assert_true(webkit_web_view_is_playing_audio(test->m_webView));
// Pause the video, and check again.
test->runJavaScriptAndWaitUntilFinished("document.getElementById('test-video').pause();", nullptr);
if (webkit_web_view_is_playing_audio(test->m_webView))
test->waitUntilIsPlayingAudioChanged();
g_assert_false(webkit_web_view_is_playing_audio(test->m_webView));
}
static void testWebViewIsAudioMuted(WebViewTest* test, gconstpointer)
{
g_assert_false(webkit_web_view_get_is_muted(test->m_webView));
webkit_web_view_set_is_muted(test->m_webView, TRUE);
g_assert_true(webkit_web_view_get_is_muted(test->m_webView));
webkit_web_view_set_is_muted(test->m_webView, FALSE);
g_assert_false(webkit_web_view_get_is_muted(test->m_webView));
}
static void testWebViewAutoplayPolicy(WebViewTest* test, gconstpointer)
{
WebKitWebsitePolicies* policies = webkit_web_view_get_website_policies(test->m_webView);
g_assert_cmpint(webkit_website_policies_get_autoplay_policy(policies), ==, WEBKIT_AUTOPLAY_ALLOW_WITHOUT_SOUND);
}
static void testWebViewIsWebProcessResponsive(WebViewTest* test, gconstpointer)
{
static const char* hangHTML =
"<html>"
" <body>"
" <script>"
" setTimeout(function() {"
" var start = new Date().getTime();"
" var end = start;"
" while(end < start + 4000) {"
" end = new Date().getTime();"
" }"
" }, 500);"
" </script>"
" </body>"
"</html>";
g_assert_true(webkit_web_view_get_is_web_process_responsive(test->m_webView));
test->loadHtml(hangHTML, nullptr);
test->waitUntilLoadFinished();
// Wait 1 second, so the js while loop kicks in and blocks the web process. Then try to load a new
// page. As the web process is busy this won't work, and after 3 seconds the web process will be marked
// as unresponsive.
test->wait(1);
test->loadHtml("<html></html>", nullptr);
test->waitUntilIsWebProcessResponsiveChanged();
g_assert_false(webkit_web_view_get_is_web_process_responsive(test->m_webView));
// 500ms after the web process is marked as unresponsive, the js while loop will finish and the process
// will be responsive again, finishing the pending load.
test->waitUntilLoadFinished();
g_assert_true(webkit_web_view_get_is_web_process_responsive(test->m_webView));
}
static void testWebViewBackgroundColor(WebViewTest* test, gconstpointer)
{
#if PLATFORM(GTK)
#define ColorType GdkRGBA
#elif PLATFORM(WPE)
#define ColorType WebKitColor
#endif
// White is the default background.
ColorType rgba;
webkit_web_view_get_background_color(test->m_webView, &rgba);
g_assert_cmpfloat(rgba.red, ==, 1);
g_assert_cmpfloat(rgba.green, ==, 1);
g_assert_cmpfloat(rgba.blue, ==, 1);
g_assert_cmpfloat(rgba.alpha, ==, 1);
// Set a different (semi-transparent red).
rgba.red = 1;
rgba.green = 0;
rgba.blue = 0;
rgba.alpha = 0.5;
webkit_web_view_set_background_color(test->m_webView, &rgba);
g_assert_cmpfloat(rgba.red, ==, 1);
g_assert_cmpfloat(rgba.green, ==, 0);
g_assert_cmpfloat(rgba.blue, ==, 0);
g_assert_cmpfloat(rgba.alpha, ==, 0.5);
#if PLATFORM(WPE)
ColorType color;
g_assert(webkit_color_parse(&color, "red"));
g_assert_cmpfloat(color.red, ==, 1);
webkit_web_view_set_background_color(test->m_webView, &color);
webkit_web_view_get_background_color(test->m_webView, &rgba);
g_assert_cmpfloat(rgba.red, ==, 1);
g_assert_cmpfloat(rgba.green, ==, 0);
g_assert_cmpfloat(rgba.blue, ==, 0);
g_assert_cmpfloat(rgba.alpha, ==, 1);
#endif
// The actual rendering can't be tested using unit tests, use
// MiniBrowser --bg-color="<color-value>" for manually testing this API.
}
#if PLATFORM(GTK)
static void testWebViewPreferredSize(WebViewTest* test, gconstpointer)
{
test->loadHtml("<html style='width: 325px; height: 615px'></html>", nullptr);
test->waitUntilLoadFinished();
test->showInWindow();
GtkRequisition minimunSize, naturalSize;
gtk_widget_get_preferred_size(GTK_WIDGET(test->m_webView), &minimunSize, &naturalSize);
g_assert_cmpint(minimunSize.width, ==, 0);
g_assert_cmpint(minimunSize.height, ==, 0);
g_assert_cmpint(naturalSize.width, ==, 325);
g_assert_cmpint(naturalSize.height, ==, 615);
}
#endif
class WebViewTitleTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(WebViewTitleTest);
static void titleChangedCallback(WebKitWebView* view, GParamSpec*, WebViewTitleTest* test)
{
test->m_webViewTitles.append(webkit_web_view_get_title(view));
}
WebViewTitleTest()
{
g_signal_connect(m_webView, "notify::title", G_CALLBACK(titleChangedCallback), this);
}
Vector<CString> m_webViewTitles;
};
static void testWebViewTitleChange(WebViewTitleTest* test, gconstpointer)
{
g_assert_cmpint(test->m_webViewTitles.size(), ==, 0);
test->loadHtml("<head><title>Page Title</title></head>", nullptr);
test->waitUntilTitleChanged();
g_assert_cmpint(test->m_webViewTitles.size(), ==, 1);
g_assert_cmpstr(test->m_webViewTitles[0].data(), ==, "Page Title");
test->loadHtml("<head><title>Another Page Title</title></head>", nullptr);
test->waitUntilTitleChanged();
g_assert_cmpint(test->m_webViewTitles.size(), ==, 2);
g_assert_cmpstr(test->m_webViewTitles[1].data(), ==, "");
test->waitUntilTitleChanged();
g_assert_cmpint(test->m_webViewTitles.size(), ==, 3);
/* Page title should be immediately unset when loading a new page. */
g_assert_cmpstr(test->m_webViewTitles[2].data(), ==, "Another Page Title");
test->loadHtml("<p>This page has no title!</p>", nullptr);
test->waitUntilLoadFinished();
g_assert_cmpint(test->m_webViewTitles.size(), ==, 4);
g_assert_cmpstr(test->m_webViewTitles[3].data(), ==, "");
test->loadHtml("<script>document.title = 'one'; document.title = 'two'; document.title = 'three';</script>", nullptr);
test->waitUntilTitleChanged();
g_assert_cmpint(test->m_webViewTitles.size(), ==, 5);
g_assert_cmpstr(test->m_webViewTitles[4].data(), ==, "three");
}
#if PLATFORM(WPE)
class FrameDisplayedTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(FrameDisplayedTest);
static void titleChangedCallback(WebKitWebView* view, GParamSpec*, WebViewTitleTest* test)
{
test->m_webViewTitles.append(webkit_web_view_get_title(view));
}
FrameDisplayedTest()
: m_id(webkit_web_view_add_frame_displayed_callback(m_webView, [](WebKitWebView*, gpointer userData) {
auto* test = static_cast<FrameDisplayedTest*>(userData);
if (!test->m_maxFrames)
return;
if (++test->m_frameCounter == test->m_maxFrames)
RunLoop::main().dispatch([test] { test->quitMainLoop(); });
}, this, nullptr))
{
g_assert_cmpuint(m_id, >, 0);
}
~FrameDisplayedTest()
{
webkit_web_view_remove_frame_displayed_callback(m_webView, m_id);
}
void waitUntilFramesDisplayed(unsigned framesCount = 1)
{
m_maxFrames = framesCount;
m_frameCounter = 0;
g_main_loop_run(m_mainLoop);
}
unsigned m_id { 0 };
unsigned m_frameCounter { 0 };
unsigned m_maxFrames { 0 };
};
static void testWebViewFrameDisplayed(FrameDisplayedTest* test, gconstpointer)
{
test->showInWindow();
test->loadHtml("<html></html>", nullptr);
test->waitUntilFramesDisplayed();
test->loadHtml("<html><head><style>@keyframes fadeIn { from { opacity: 0; } }</style></head><p style='animation: fadeIn 1s infinite alternate;'>Foo</p></html>", nullptr);
test->waitUntilFramesDisplayed(10);
bool secondCallbackCalled = false;
auto id = webkit_web_view_add_frame_displayed_callback(test->m_webView, [](WebKitWebView*, gpointer userData) {
auto* secondCallbackCalled = static_cast<bool*>(userData);
*secondCallbackCalled = true;
}, &secondCallbackCalled, nullptr);
test->waitUntilFramesDisplayed();
g_assert_true(secondCallbackCalled);
secondCallbackCalled = false;
webkit_web_view_remove_frame_displayed_callback(test->m_webView, id);
test->waitUntilFramesDisplayed();
g_assert_false(secondCallbackCalled);
id = webkit_web_view_add_frame_displayed_callback(test->m_webView, [](WebKitWebView* webView, gpointer userData) {
auto* id = static_cast<unsigned*>(userData);
webkit_web_view_remove_frame_displayed_callback(webView, *id);
}, &id, [](gpointer userData) {
auto* id = static_cast<unsigned*>(userData);
*id = 0;
});
test->waitUntilFramesDisplayed();
g_assert_cmpuint(id, ==, 0);
auto id2 = webkit_web_view_add_frame_displayed_callback(test->m_webView, [](WebKitWebView* webView, gpointer userData) {
auto* id = static_cast<unsigned*>(userData);
if (*id) {
webkit_web_view_remove_frame_displayed_callback(webView, *id);
*id = 0;
}
}, &id, nullptr);
secondCallbackCalled = false;
id = webkit_web_view_add_frame_displayed_callback(test->m_webView, [](WebKitWebView* webView, gpointer userData) {
auto* secondCallbackCalled = static_cast<bool*>(userData);
*secondCallbackCalled = true;
}, &secondCallbackCalled, nullptr);
test->waitUntilFramesDisplayed();
g_assert_cmpuint(id, ==, 0);
g_assert_false(secondCallbackCalled);
webkit_web_view_remove_frame_displayed_callback(test->m_webView, id2);
}
#endif
#if PLATFORM(WPE) && USE(WPEBACKEND_FDO_AUDIO_EXTENSION)
enum class RenderingState {
Unknown,
Started,
Paused,
Stopped
};
class AudioRenderingWebViewTest : public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE_WITH_SETUP_TEARDOWN(AudioRenderingWebViewTest, setup, teardown);
static void setup()
{
}
static void teardown()
{
wpe_audio_register_receiver(nullptr, nullptr);
}
AudioRenderingWebViewTest()
{
wpe_audio_register_receiver(&m_audioReceiver, this);
}
void handleStart(uint32_t id, int32_t channels, const char* layout, int32_t sampleRate)
{
g_assert(m_state == RenderingState::Unknown);
g_assert_false(m_streamId.has_value());
g_assert_cmpuint(id, ==, 0);
m_streamId = id;
m_state = RenderingState::Started;
g_assert_cmpint(channels, ==, 2);
g_assert_cmpstr(layout, ==, "S16LE");
g_assert_cmpint(sampleRate, ==, 44100);
}
void handleStop(uint32_t id)
{
g_assert_cmpuint(*m_streamId, ==, id);
g_assert(m_state != RenderingState::Unknown);
m_state = RenderingState::Stopped;
g_main_loop_quit(m_mainLoop);
m_streamId.reset();
}
void handlePause(uint32_t id)
{
g_assert_cmpuint(*m_streamId, ==, id);
g_assert(m_state != RenderingState::Unknown);
m_state = RenderingState::Paused;
}
void handleResume(uint32_t id)
{
g_assert_cmpuint(*m_streamId, ==, id);
g_assert(m_state == RenderingState::Paused);
m_state = RenderingState::Started;
}
void handlePacket(struct wpe_audio_packet_export* packet_export, uint32_t id, int32_t fd, uint32_t size)
{
g_assert_cmpuint(*m_streamId, ==, id);
g_assert(m_state == RenderingState::Started || m_state == RenderingState::Paused);
g_assert_cmpuint(size, >, 0);
wpe_audio_packet_export_release(packet_export);
}
void waitUntilStarted()
{
g_timeout_add(200, [](gpointer userData) -> gboolean {
auto* test = static_cast<AudioRenderingWebViewTest*>(userData);
if (test->state() == RenderingState::Started) {
test->quitMainLoop();
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}, this);
g_main_loop_run(m_mainLoop);
}
void waitUntilPaused()
{
g_timeout_add(200, [](gpointer userData) -> gboolean {
auto* test = static_cast<AudioRenderingWebViewTest*>(userData);
if (test->state() == RenderingState::Paused) {
test->quitMainLoop();
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}, this);
g_main_loop_run(m_mainLoop);
}
void waitUntilEOS()
{
g_main_loop_run(m_mainLoop);
}
RenderingState state() const { return m_state; }
private:
static const struct wpe_audio_receiver m_audioReceiver;
RenderingState m_state { RenderingState::Unknown };
std::optional<uint32_t> m_streamId;
};
const struct wpe_audio_receiver AudioRenderingWebViewTest::m_audioReceiver = {
[](void* data, uint32_t id, int32_t channels, const char* layout, int32_t sampleRate) { static_cast<AudioRenderingWebViewTest*>(data)->handleStart(id, channels, layout, sampleRate); },
[](void* data, struct wpe_audio_packet_export* packet_export, uint32_t id, int32_t fd, uint32_t size) { static_cast<AudioRenderingWebViewTest*>(data)->handlePacket(packet_export, id, fd, size); },
[](void* data, uint32_t id) { static_cast<AudioRenderingWebViewTest*>(data)->handleStop(id); },
[](void* data, uint32_t id) { static_cast<AudioRenderingWebViewTest*>(data)->handlePause(id); },
[](void* data, uint32_t id) { static_cast<AudioRenderingWebViewTest*>(data)->handleResume(id); }
};
static void testWebViewExternalAudioRendering(AudioRenderingWebViewTest* test, gconstpointer)
{
GUniquePtr<char> resourcePath(g_build_filename(Test::getResourcesDir(Test::WebKit2Resources).data(), "file-with-video.html", nullptr));
GUniquePtr<char> resourceURL(g_filename_to_uri(resourcePath.get(), nullptr, nullptr));
webkit_web_view_load_uri(test->m_webView, resourceURL.get());
test->waitUntilLoadFinished();
test->runJavaScriptAndWaitUntilFinished("playVideo();", nullptr);
test->waitUntilStarted();
g_assert(test->state() == RenderingState::Started);
test->runJavaScriptAndWaitUntilFinished("pauseVideo();", nullptr);
test->waitUntilPaused();
g_assert(test->state() == RenderingState::Paused);
test->runJavaScriptAndWaitUntilFinished("playVideo(); seekNearTheEnd();", nullptr);
test->waitUntilEOS();
g_assert(test->state() == RenderingState::Stopped);
}
#endif
class WebViewTerminateWebProcessTest: public WebViewTest {
public:
MAKE_GLIB_TEST_FIXTURE(WebViewTerminateWebProcessTest);
static void webProcessTerminatedCallback(WebKitWebView* webView, WebKitWebProcessTerminationReason reason, WebViewTerminateWebProcessTest* test)
{
test->m_terminationReason = reason;
}
WebViewTerminateWebProcessTest()
{
g_signal_connect_after(m_webView, "web-process-terminated", G_CALLBACK(WebViewTerminateWebProcessTest::webProcessTerminatedCallback), this);
}
~WebViewTerminateWebProcessTest()
{
g_signal_handlers_disconnect_by_data(m_webView, this);
}
WebKitWebProcessTerminationReason m_terminationReason { WEBKIT_WEB_PROCESS_CRASHED };
};
static void testWebViewTerminateWebProcess(WebViewTerminateWebProcessTest* test, gconstpointer)
{
test->loadHtml("<html></html>", nullptr);
test->waitUntilLoadFinished();
test->m_expectedWebProcessCrash = true;
webkit_web_view_terminate_web_process(test->m_webView);
g_assert_cmpuint(test->m_terminationReason, ==, WEBKIT_WEB_PROCESS_TERMINATED_BY_API);
g_assert_true(webkit_web_view_get_is_web_process_responsive(test->m_webView));
}
static void testWebViewTerminateUnresponsiveWebProcess(WebViewTerminateWebProcessTest* test, gconstpointer)
{
static const char* hangHTML =
"<html>"
" <body>"
" <script>"
" setTimeout(function() {"
" while(true) { }"
" }, 500);"
" </script>"
" </body>"
"</html>";
test->loadHtml(hangHTML, nullptr);
test->waitUntilLoadFinished();
g_assert_true(webkit_web_view_get_is_web_process_responsive(test->m_webView));
// Wait 1 second, so the js while loop kicks in and blocks the web process, and try to load a new page.
// As the web process is busy this won't work, and after 3 seconds the web process will be marked
// as unresponsive.
test->wait(1);
test->loadHtml("<html></html>", nullptr);
test->waitUntilIsWebProcessResponsiveChanged();
g_assert_false(webkit_web_view_get_is_web_process_responsive(test->m_webView));
// Now that the process is unresponsive, terminate it.
test->m_expectedWebProcessCrash = true;
test->m_terminationReason = WEBKIT_WEB_PROCESS_CRASHED;
webkit_web_view_terminate_web_process(test->m_webView);
g_assert_cmpuint(test->m_terminationReason, ==, WEBKIT_WEB_PROCESS_TERMINATED_BY_API);
g_assert_true(webkit_web_view_get_is_web_process_responsive(test->m_webView));
}
static void testWebViewCORSAllowlist(WebViewTest* test, gconstpointer)
{
webkit_web_context_register_uri_scheme(test->m_webContext.get(), "foo",
[](WebKitURISchemeRequest* request, gpointer userData) {
GRefPtr<GInputStream> inputStream = adoptGRef(g_memory_input_stream_new());
const char* data = "<p>foobar!</p>";
g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(inputStream.get()), data, strlen(data), nullptr);
webkit_uri_scheme_request_finish(request, inputStream.get(), strlen(data), "text/html");
}, nullptr, nullptr);
char html[] = "<html><script>let foo = 0; fetch('foo://bar/baz').then(response => { foo = response.status; }).catch(err => { foo = -1; });</script></html>";
auto waitForFooChanged = [&test]() {
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* result;
JSCValue* jscvalue;
int value;
do {
result = test->runJavaScriptAndWaitUntilFinished("foo;", &error.outPtr());
g_assert_no_error(error.get());
jscvalue = webkit_javascript_result_get_js_value(result);
value = jsc_value_to_int32(jscvalue);
webkit_javascript_result_unref(result);
} while (!value);
return value;
};
// Request is not allowed, foo should be 0.
webkit_web_view_load_html(test->m_webView, html, "http://example.com");
test->waitUntilLoadFinished();
g_assert_cmpint(waitForFooChanged(), ==, -1);
// Allowlisting host alone does not work. Path is also required. foo should remain 0.
GUniquePtr<char*> allowlist(g_new(char*, 2));
allowlist.get()[0] = g_strdup("foo://*");
allowlist.get()[1] = nullptr;
webkit_web_view_set_cors_allowlist(test->m_webView, allowlist.get());
webkit_web_view_load_html(test->m_webView, html, "http://example.com");
test->waitUntilLoadFinished();
g_assert_cmpint(waitForFooChanged(), ==, -1);
// Finally let's properly allow our scheme. foo should now change to 42 when the request succeeds.
allowlist.reset(g_new(char*, 2));
allowlist.get()[0] = g_strdup("foo://*/*");
allowlist.get()[1] = nullptr;
webkit_web_view_set_cors_allowlist(test->m_webView, allowlist.get());
webkit_web_view_load_html(test->m_webView, html, "http://example.com");
test->waitUntilLoadFinished();
g_assert_cmpint(waitForFooChanged(), ==, 200);
}
static void testWebViewDefaultContentSecurityPolicy(WebViewTest* test, gconstpointer)
{
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult;
// Sanity check that eval works normally.
javascriptResult = test->runJavaScriptAndWaitUntilFinished("eval('\"allowed\"')", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
GUniquePtr<char> evalValue(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(evalValue.get(), ==, "allowed");
webkit_javascript_result_unref(javascriptResult);
// Create a new web view with a policy that blocks eval().
auto webView = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
"default-content-security-policy", "script-src 'self'",
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
nullptr));
// Ensure JavaScript still functions.
javascriptResult = test->runJavaScriptAndWaitUntilFinished("'allowed'", &error.outPtr(), webView.get());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
GUniquePtr<char> value(WebViewTest::javascriptResultToCString(javascriptResult));
g_assert_cmpstr(value.get(), ==, "allowed");
webkit_javascript_result_unref(javascriptResult);
// Then ensure eval is blocked.
javascriptResult = test->runJavaScriptAndWaitUntilFinished("eval('\"allowed\"')", &error.outPtr(), webView.get());
g_assert_null(javascriptResult);
g_assert_error(error.get(), WEBKIT_JAVASCRIPT_ERROR, WEBKIT_JAVASCRIPT_ERROR_SCRIPT_FAILED);
}
static void testWebViewWebExtensionMode(WebViewTest* test, gconstpointer)
{
GUniqueOutPtr<GError> error;
WebKitJavascriptResult* javascriptResult;
static const char* html =
"<html>"
" <head>"
" <title>unset</title>"
" <meta http-equiv=\"Content-Security-Policy\" content=\"script-src 'unsafe-inline';\">"
" <script>document.title = 'set';</script>"
" </head>"
"</html>";
// Sanity check that this HTML works as expected.
test->loadHtml(html, nullptr);
test->waitUntilLoadFinished();
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.title == 'set';", &error.outPtr());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultToBoolean(javascriptResult));
webkit_javascript_result_unref(javascriptResult);
// Create a new web view with an extension mode that blocks the unsafe-inline keyword.
auto webView = Test::adoptView(g_object_new(WEBKIT_TYPE_WEB_VIEW,
"web-extension-mode", WEBKIT_WEB_EXTENSION_MODE_MANIFESTV3,
#if PLATFORM(WPE)
"backend", Test::createWebViewBackend(),
#endif
nullptr));
test->loadHtml(html, nullptr, webView.get());
test->waitUntilLoadFinished(webView.get());
javascriptResult = test->runJavaScriptAndWaitUntilFinished("document.title == 'unset';", &error.outPtr(), webView.get());
g_assert_nonnull(javascriptResult);
g_assert_no_error(error.get());
g_assert_true(WebViewTest::javascriptResultToBoolean(javascriptResult));
}
#if USE(SOUP2)
static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
#else
static void serverCallback(SoupServer* server, SoupServerMessage* message, const char* path, GHashTable*, gpointer)
#endif
{
if (soup_server_message_get_method(message) != SOUP_METHOD_GET) {
soup_server_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED, nullptr);
return;
}
if (g_str_equal(path, "/")) {
soup_server_message_set_status(message, SOUP_STATUS_OK, nullptr);
soup_message_body_complete(soup_server_message_get_response_body(message));
} else
soup_server_message_set_status(message, SOUP_STATUS_NOT_FOUND, nullptr);
}
void beforeAll()
{
gServer = new WebKitTestServer();
gServer->run(serverCallback);
WebViewTest::add("WebKitWebView", "web-context", testWebViewWebContext);
WebViewTest::add("WebKitWebView", "web-context-lifetime", testWebViewWebContextLifetime);
WebViewTest::add("WebKitWebView", "close-quickly", testWebViewCloseQuickly);
#if PLATFORM(WPE)
Test::add("WebKitWebView", "backend", testWebViewWebBackend);
#endif
WebViewTest::add("WebKitWebView", "ephemeral", testWebViewEphemeral);
WebViewTest::add("WebKitWebView", "custom-charset", testWebViewCustomCharset);
WebViewTest::add("WebKitWebView", "settings", testWebViewSettings);
WebViewTest::add("WebKitWebView", "zoom-level", testWebViewZoomLevel);
WebViewTest::add("WebKitWebView", "run-javascript", testWebViewRunJavaScript);
WebViewTest::add("WebKitWebView", "run-async-js-functions", testWebViewRunAsyncFunctions);
#if ENABLE(FULLSCREEN_API)
FullScreenClientTest::add("WebKitWebView", "fullscreen", testWebViewFullScreen);
#endif
WebViewTest::add("WebKitWebView", "can-show-mime-type", testWebViewCanShowMIMEType);
// FIXME: implement mouse clicks in WPE.
#if PLATFORM(GTK)
FormClientTest::add("WebKitWebView", "submit-form", testWebViewSubmitForm);
#endif
SaveWebViewTest::add("WebKitWebView", "save", testWebViewSave);
// FIXME: View is initially visible in WPE and has a fixed hardcoded size.
#if PLATFORM(GTK)
SnapshotWebViewTest::add("WebKitWebView", "snapshot", testWebViewSnapshot);
#endif
WebViewTest::add("WebKitWebView", "page-visibility", testWebViewPageVisibility);
WebViewTest::add("WebKitWebView", "document-focus", testWebViewDocumentFocus);
#if ENABLE(NOTIFICATIONS)
NotificationWebViewTest::add("WebKitWebView", "notification", testWebViewNotification);
NotificationWebViewTest::add("WebKitWebView", "notification-initial-permission-allowed", testWebViewNotificationInitialPermissionAllowed);
NotificationWebViewTest::add("WebKitWebView", "notification-initial-permission-disallowed", testWebViewNotificationInitialPermissionDisallowed);
#endif
IsPlayingAudioWebViewTest::add("WebKitWebView", "is-playing-audio", testWebViewIsPlayingAudio);
WebViewTest::add("WebKitWebView", "background-color", testWebViewBackgroundColor);
#if PLATFORM(GTK)
WebViewTest::add("WebKitWebView", "preferred-size", testWebViewPreferredSize);
#endif
WebViewTitleTest::add("WebKitWebView", "title-change", testWebViewTitleChange);
#if PLATFORM(WPE)
FrameDisplayedTest::add("WebKitWebView", "frame-displayed", testWebViewFrameDisplayed);
#endif
WebViewTest::add("WebKitWebView", "is-audio-muted", testWebViewIsAudioMuted);
WebViewTest::add("WebKitWebView", "autoplay-policy", testWebViewAutoplayPolicy);
#if PLATFORM(WPE) && USE(WPEBACKEND_FDO_AUDIO_EXTENSION)
AudioRenderingWebViewTest::add("WebKitWebView", "external-audio-rendering", testWebViewExternalAudioRendering);
#endif
WebViewTest::add("WebKitWebView", "is-web-process-responsive", testWebViewIsWebProcessResponsive);
WebViewTerminateWebProcessTest::add("WebKitWebView", "terminate-web-process", testWebViewTerminateWebProcess);
WebViewTerminateWebProcessTest::add("WebKitWebView", "terminate-unresponsive-web-process", testWebViewTerminateUnresponsiveWebProcess);
WebViewTest::add("WebKitWebView", "cors-allowlist", testWebViewCORSAllowlist);
WebViewTest::add("WebKitWebView", "default-content-security-policy", testWebViewDefaultContentSecurityPolicy);
WebViewTest::add("WebKitWebView", "web-extension-mode", testWebViewWebExtensionMode);
}
void afterAll()
{
}
|