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 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <tuple>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "cc/trees/render_frame_metadata.h"
#include "components/viz/common/features.h"
#include "components/viz/common/surfaces/local_surface_id.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "components/viz/test/begin_frame_args_test.h"
#include "components/viz/test/compositor_frame_helpers.h"
#include "components/viz/test/mock_compositor_frame_sink_client.h"
#include "content/browser/gpu/compositor_util.h"
#include "content/browser/renderer_host/frame_token_message_queue.h"
#include "content/browser/renderer_host/input/touch_emulator.h"
#include "content/browser/renderer_host/render_view_host_delegate_view.h"
#include "content/browser/renderer_host/render_widget_host_delegate.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "content/common/edit_command.h"
#include "content/common/input/synthetic_web_input_event_builders.h"
#include "content/common/input_messages.h"
#include "content/common/render_frame_metadata.mojom.h"
#include "content/common/view_messages.h"
#include "content/common/visual_properties.h"
#include "content/public/browser/keyboard_event_processing_result.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/test/fake_renderer_compositor_frame_sink.h"
#include "content/test/mock_widget_impl.h"
#include "content/test/mock_widget_input_handler.h"
#include "content/test/test_render_view_host.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/screen.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/blink/blink_features.h"
#include "ui/events/blink/web_input_event_traits.h"
#include "ui/events/gesture_detection/gesture_provider_config_helper.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/canvas.h"
#if defined(OS_ANDROID)
#include "content/browser/renderer_host/render_widget_host_view_android.h"
#include "ui/android/screen_android.h"
#endif
#if defined(USE_AURA) || defined(OS_MACOSX)
#include "content/browser/compositor/test/test_image_transport_factory.h"
#endif
#if defined(USE_AURA)
#include "content/browser/renderer_host/render_widget_host_view_aura.h"
#include "content/browser/renderer_host/ui_events_helper.h"
#include "ui/aura/test/test_screen.h"
#include "ui/events/event.h"
#endif
using base::TimeDelta;
using blink::WebGestureDevice;
using blink::WebGestureEvent;
using blink::WebInputEvent;
using blink::WebKeyboardEvent;
using blink::WebMouseEvent;
using blink::WebMouseWheelEvent;
using blink::WebTouchEvent;
using blink::WebTouchPoint;
namespace content {
// MockInputRouter -------------------------------------------------------------
class MockInputRouter : public InputRouter {
public:
explicit MockInputRouter(InputRouterClient* client)
: sent_mouse_event_(false),
sent_wheel_event_(false),
sent_keyboard_event_(false),
sent_gesture_event_(false),
send_touch_event_not_cancelled_(false),
message_received_(false),
client_(client) {}
~MockInputRouter() override {}
// InputRouter
void SendMouseEvent(const MouseEventWithLatencyInfo& mouse_event) override {
sent_mouse_event_ = true;
}
void SendWheelEvent(
const MouseWheelEventWithLatencyInfo& wheel_event) override {
sent_wheel_event_ = true;
}
void SendKeyboardEvent(
const NativeWebKeyboardEventWithLatencyInfo& key_event) override {
sent_keyboard_event_ = true;
}
void SendGestureEvent(
const GestureEventWithLatencyInfo& gesture_event) override {
sent_gesture_event_ = true;
}
void SendTouchEvent(const TouchEventWithLatencyInfo& touch_event) override {
send_touch_event_not_cancelled_ =
client_->FilterInputEvent(touch_event.event, touch_event.latency) ==
INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
}
void NotifySiteIsMobileOptimized(bool is_mobile_optimized) override {}
bool HasPendingEvents() const override { return false; }
void SetDeviceScaleFactor(float device_scale_factor) override {}
void SetFrameTreeNodeId(int frameTreeNodeId) override {}
base::Optional<cc::TouchAction> AllowedTouchAction() override {
return cc::kTouchActionAuto;
}
void SetForceEnableZoom(bool enabled) override {}
void BindHost(mojom::WidgetInputHandlerHostRequest request,
bool frame_handler) override {}
void StopFling() override {}
bool FlingCancellationIsDeferred() override { return false; }
void OnSetTouchAction(cc::TouchAction touch_action) override {}
void ForceSetTouchActionAuto() override {}
// IPC::Listener
bool OnMessageReceived(const IPC::Message& message) override {
message_received_ = true;
return false;
}
bool sent_mouse_event_;
bool sent_wheel_event_;
bool sent_keyboard_event_;
bool sent_gesture_event_;
bool send_touch_event_not_cancelled_;
bool message_received_;
private:
InputRouterClient* client_;
DISALLOW_COPY_AND_ASSIGN(MockInputRouter);
};
// TestFrameTokenMessageQueue ----------------------------------------------
class TestFrameTokenMessageQueue : public FrameTokenMessageQueue {
public:
explicit TestFrameTokenMessageQueue(FrameTokenMessageQueue::Client* client)
: FrameTokenMessageQueue(client) {}
~TestFrameTokenMessageQueue() override {}
uint32_t processed_frame_messages_count() {
return processed_frame_messages_count_;
}
protected:
void ProcessSwapMessages(std::vector<IPC::Message> messages) override {
processed_frame_messages_count_++;
}
private:
uint32_t processed_frame_messages_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(TestFrameTokenMessageQueue);
};
// MockRenderWidgetHost ----------------------------------------------------
class MockRenderWidgetHost : public RenderWidgetHostImpl {
public:
// Allow poking at a few private members.
using RenderWidgetHostImpl::GetVisualProperties;
using RenderWidgetHostImpl::RendererExited;
using RenderWidgetHostImpl::SetInitialVisualProperties;
using RenderWidgetHostImpl::old_visual_properties_;
using RenderWidgetHostImpl::is_hidden_;
using RenderWidgetHostImpl::visual_properties_ack_pending_;
using RenderWidgetHostImpl::input_router_;
using RenderWidgetHostImpl::frame_token_message_queue_;
void OnTouchEventAck(const TouchEventWithLatencyInfo& event,
InputEventAckSource ack_source,
InputEventAckState ack_result) override {
// Sniff touch acks.
acked_touch_event_type_ = event.event.GetType();
RenderWidgetHostImpl::OnTouchEventAck(event, ack_source, ack_result);
}
void reset_new_content_rendering_timeout_fired() {
new_content_rendering_timeout_fired_ = false;
}
bool new_content_rendering_timeout_fired() const {
return new_content_rendering_timeout_fired_;
}
void DisableGestureDebounce() {
input_router_.reset(new InputRouterImpl(this, this, fling_scheduler_.get(),
InputRouter::Config()));
}
void ExpectForceEnableZoom(bool enable) {
EXPECT_EQ(enable, force_enable_zoom_);
InputRouterImpl* input_router =
static_cast<InputRouterImpl*>(input_router_.get());
EXPECT_EQ(enable, input_router->touch_action_filter_.force_enable_zoom_);
}
WebInputEvent::Type acked_touch_event_type() const {
return acked_touch_event_type_;
}
// Mocks out |renderer_compositor_frame_sink_| with a
// CompositorFrameSinkClientPtr bound to
// |mock_renderer_compositor_frame_sink|.
void SetMockRendererCompositorFrameSink(
viz::MockCompositorFrameSinkClient* mock_renderer_compositor_frame_sink) {
renderer_compositor_frame_sink_ =
mock_renderer_compositor_frame_sink->BindInterfacePtr();
}
void SetupForInputRouterTest() {
input_router_.reset(new MockInputRouter(this));
}
MockInputRouter* mock_input_router() {
return static_cast<MockInputRouter*>(input_router_.get());
}
InputRouter* input_router() { return input_router_.get(); }
uint32_t processed_frame_messages_count() {
CHECK(frame_token_message_queue_);
return static_cast<TestFrameTokenMessageQueue*>(
frame_token_message_queue_.get())
->processed_frame_messages_count();
}
static MockRenderWidgetHost* Create(RenderWidgetHostDelegate* delegate,
RenderProcessHost* process,
int32_t routing_id) {
mojom::WidgetPtr widget;
std::unique_ptr<MockWidgetImpl> widget_impl =
std::make_unique<MockWidgetImpl>(mojo::MakeRequest(&widget));
return new MockRenderWidgetHost(delegate, process, routing_id,
std::move(widget_impl), std::move(widget));
}
mojom::WidgetInputHandler* GetWidgetInputHandler() override {
return &mock_widget_input_handler_;
}
MockWidgetInputHandler mock_widget_input_handler_;
protected:
void NotifyNewContentRenderingTimeoutForTesting() override {
new_content_rendering_timeout_fired_ = true;
}
bool new_content_rendering_timeout_fired_;
WebInputEvent::Type acked_touch_event_type_;
private:
MockRenderWidgetHost(RenderWidgetHostDelegate* delegate,
RenderProcessHost* process,
int routing_id,
std::unique_ptr<MockWidgetImpl> widget_impl,
mojom::WidgetPtr widget)
: RenderWidgetHostImpl(delegate,
process,
routing_id,
std::move(widget),
false),
new_content_rendering_timeout_fired_(false),
widget_impl_(std::move(widget_impl)),
fling_scheduler_(std::make_unique<FlingScheduler>(this)) {
acked_touch_event_type_ = blink::WebInputEvent::kUndefined;
frame_token_message_queue_.reset(new TestFrameTokenMessageQueue(this));
}
std::unique_ptr<MockWidgetImpl> widget_impl_;
std::unique_ptr<FlingScheduler> fling_scheduler_;
DISALLOW_COPY_AND_ASSIGN(MockRenderWidgetHost);
};
namespace {
// RenderWidgetHostProcess -----------------------------------------------------
class RenderWidgetHostProcess : public MockRenderProcessHost {
public:
explicit RenderWidgetHostProcess(BrowserContext* browser_context)
: MockRenderProcessHost(browser_context) {
}
~RenderWidgetHostProcess() override {}
bool IsInitializedAndNotDead() const override { return true; }
private:
DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostProcess);
};
// TestView --------------------------------------------------------------------
// This test view allows us to specify the size, and keep track of acked
// touch-events.
class TestView : public TestRenderWidgetHostView {
public:
explicit TestView(RenderWidgetHostImpl* rwh)
: TestRenderWidgetHostView(rwh),
unhandled_wheel_event_count_(0),
acked_event_count_(0),
gesture_event_type_(-1),
use_fake_compositor_viewport_pixel_size_(false),
ack_result_(INPUT_EVENT_ACK_STATE_UNKNOWN),
top_controls_height_(0.f),
bottom_controls_height_(0.f) {}
// Sets the bounds returned by GetViewBounds.
void SetBounds(const gfx::Rect& bounds) override {
if (bounds_ == bounds)
return;
bounds_ = bounds;
local_surface_id_allocator_.GenerateId();
}
void SetScreenInfo(const ScreenInfo& screen_info) {
if (screen_info_ == screen_info)
return;
screen_info_ = screen_info;
local_surface_id_allocator_.GenerateId();
}
void InvalidateLocalSurfaceId() { local_surface_id_allocator_.Invalidate(); }
void GetScreenInfo(ScreenInfo* screen_info) const override {
*screen_info = screen_info_;
}
void set_top_controls_height(float top_controls_height) {
top_controls_height_ = top_controls_height;
}
void set_bottom_controls_height(float bottom_controls_height) {
bottom_controls_height_ = bottom_controls_height;
}
const WebTouchEvent& acked_event() const { return acked_event_; }
int acked_event_count() const { return acked_event_count_; }
void ClearAckedEvent() {
acked_event_.SetType(blink::WebInputEvent::kUndefined);
acked_event_count_ = 0;
}
const WebMouseWheelEvent& unhandled_wheel_event() const {
return unhandled_wheel_event_;
}
int unhandled_wheel_event_count() const {
return unhandled_wheel_event_count_;
}
int gesture_event_type() const { return gesture_event_type_; }
InputEventAckState ack_result() const { return ack_result_; }
void SetMockCompositorViewportPixelSize(
const gfx::Size& mock_compositor_viewport_pixel_size) {
if (use_fake_compositor_viewport_pixel_size_ &&
mock_compositor_viewport_pixel_size_ ==
mock_compositor_viewport_pixel_size) {
return;
}
use_fake_compositor_viewport_pixel_size_ = true;
mock_compositor_viewport_pixel_size_ = mock_compositor_viewport_pixel_size;
local_surface_id_allocator_.GenerateId();
}
void ClearMockCompositorViewportPixelSize() {
if (!use_fake_compositor_viewport_pixel_size_)
return;
use_fake_compositor_viewport_pixel_size_ = false;
local_surface_id_allocator_.GenerateId();
}
const viz::BeginFrameAck& last_did_not_produce_frame_ack() {
return last_did_not_produce_frame_ack_;
}
// RenderWidgetHostView override.
gfx::Rect GetViewBounds() const override { return bounds_; }
float GetTopControlsHeight() const override { return top_controls_height_; }
float GetBottomControlsHeight() const override {
return bottom_controls_height_;
}
const viz::LocalSurfaceId& GetLocalSurfaceId() const override {
return local_surface_id_allocator_.GetCurrentLocalSurfaceId();
}
void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
InputEventAckState ack_result) override {
acked_event_ = touch.event;
++acked_event_count_;
}
void WheelEventAck(const WebMouseWheelEvent& event,
InputEventAckState ack_result) override {
if (ack_result == INPUT_EVENT_ACK_STATE_CONSUMED)
return;
unhandled_wheel_event_count_++;
unhandled_wheel_event_ = event;
}
void GestureEventAck(const WebGestureEvent& event,
InputEventAckState ack_result) override {
gesture_event_type_ = event.GetType();
ack_result_ = ack_result;
}
gfx::Size GetCompositorViewportPixelSize() const override {
if (use_fake_compositor_viewport_pixel_size_)
return mock_compositor_viewport_pixel_size_;
return TestRenderWidgetHostView::GetCompositorViewportPixelSize();
}
void OnDidNotProduceFrame(const viz::BeginFrameAck& ack) override {
last_did_not_produce_frame_ack_ = ack;
}
protected:
WebMouseWheelEvent unhandled_wheel_event_;
int unhandled_wheel_event_count_;
WebTouchEvent acked_event_;
int acked_event_count_;
int gesture_event_type_;
gfx::Rect bounds_;
bool use_fake_compositor_viewport_pixel_size_;
gfx::Size mock_compositor_viewport_pixel_size_;
InputEventAckState ack_result_;
float top_controls_height_;
float bottom_controls_height_;
viz::BeginFrameAck last_did_not_produce_frame_ack_;
viz::ParentLocalSurfaceIdAllocator local_surface_id_allocator_;
ScreenInfo screen_info_;
private:
DISALLOW_COPY_AND_ASSIGN(TestView);
};
// MockRenderViewHostDelegateView ------------------------------------------
class MockRenderViewHostDelegateView : public RenderViewHostDelegateView {
public:
MockRenderViewHostDelegateView() = default;
~MockRenderViewHostDelegateView() override = default;
int start_dragging_count() const { return start_dragging_count_; }
// RenderViewHostDelegateView:
void StartDragging(const DropData& drop_data,
blink::WebDragOperationsMask allowed_ops,
const gfx::ImageSkia& image,
const gfx::Vector2d& image_offset,
const DragEventSourceInfo& event_info,
RenderWidgetHostImpl* source_rwh) override {
++start_dragging_count_;
}
private:
int start_dragging_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(MockRenderViewHostDelegateView);
};
// FakeRenderFrameMetadataObserver -----------------------------------------
// Fake out the renderer side of mojom::RenderFrameMetadataObserver, allowing
// for RenderWidgetHostImpl to be created.
//
// All methods are no-opts, the provided mojo request and info are held, but
// never bound.
class FakeRenderFrameMetadataObserver
: public mojom::RenderFrameMetadataObserver {
public:
FakeRenderFrameMetadataObserver(
mojom::RenderFrameMetadataObserverRequest request,
mojom::RenderFrameMetadataObserverClientPtrInfo client_info);
~FakeRenderFrameMetadataObserver() override {}
void ReportAllFrameSubmissionsForTesting(bool enabled) override {}
private:
mojom::RenderFrameMetadataObserverRequest request_;
mojom::RenderFrameMetadataObserverClientPtrInfo client_info_;
DISALLOW_COPY_AND_ASSIGN(FakeRenderFrameMetadataObserver);
};
FakeRenderFrameMetadataObserver::FakeRenderFrameMetadataObserver(
mojom::RenderFrameMetadataObserverRequest request,
mojom::RenderFrameMetadataObserverClientPtrInfo client_info)
: request_(std::move(request)), client_info_(std::move(client_info)) {}
// MockRenderWidgetHostDelegate --------------------------------------------
class MockRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
public:
MockRenderWidgetHostDelegate()
: prehandle_keyboard_event_(false),
prehandle_keyboard_event_is_shortcut_(false),
prehandle_keyboard_event_called_(false),
prehandle_keyboard_event_type_(WebInputEvent::kUndefined),
unhandled_keyboard_event_called_(false),
unhandled_keyboard_event_type_(WebInputEvent::kUndefined),
handle_wheel_event_(false),
handle_wheel_event_called_(false),
unresponsive_timer_fired_(false),
ignore_input_events_(false),
render_view_host_delegate_view_(new MockRenderViewHostDelegateView()) {}
~MockRenderWidgetHostDelegate() override {}
// Tests that make sure we ignore keyboard event acknowledgments to events we
// didn't send work by making sure we didn't call UnhandledKeyboardEvent().
bool unhandled_keyboard_event_called() const {
return unhandled_keyboard_event_called_;
}
WebInputEvent::Type unhandled_keyboard_event_type() const {
return unhandled_keyboard_event_type_;
}
bool prehandle_keyboard_event_called() const {
return prehandle_keyboard_event_called_;
}
WebInputEvent::Type prehandle_keyboard_event_type() const {
return prehandle_keyboard_event_type_;
}
void set_prehandle_keyboard_event(bool handle) {
prehandle_keyboard_event_ = handle;
}
void set_handle_wheel_event(bool handle) {
handle_wheel_event_ = handle;
}
void set_prehandle_keyboard_event_is_shortcut(bool is_shortcut) {
prehandle_keyboard_event_is_shortcut_ = is_shortcut;
}
bool handle_wheel_event_called() const { return handle_wheel_event_called_; }
bool unresponsive_timer_fired() const { return unresponsive_timer_fired_; }
MockRenderViewHostDelegateView* mock_delegate_view() {
return render_view_host_delegate_view_.get();
}
void SetZoomLevel(double zoom_level) { zoom_level_ = zoom_level; }
double GetPendingPageZoomLevel() const override { return zoom_level_; }
void FocusOwningWebContents(
RenderWidgetHostImpl* render_widget_host) override {
focus_owning_web_contents_call_count++;
}
int GetFocusOwningWebContentsCallCount() const {
return focus_owning_web_contents_call_count;
}
RenderViewHostDelegateView* GetDelegateView() override {
return mock_delegate_view();
}
void SetIgnoreInputEvents(bool ignore_input_events) {
ignore_input_events_ = ignore_input_events;
}
protected:
KeyboardEventProcessingResult PreHandleKeyboardEvent(
const NativeWebKeyboardEvent& event) override {
prehandle_keyboard_event_type_ = event.GetType();
prehandle_keyboard_event_called_ = true;
if (prehandle_keyboard_event_)
return KeyboardEventProcessingResult::HANDLED;
return prehandle_keyboard_event_is_shortcut_
? KeyboardEventProcessingResult::NOT_HANDLED_IS_SHORTCUT
: KeyboardEventProcessingResult::NOT_HANDLED;
}
void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) override {
unhandled_keyboard_event_type_ = event.GetType();
unhandled_keyboard_event_called_ = true;
}
bool HandleWheelEvent(const blink::WebMouseWheelEvent& event) override {
handle_wheel_event_called_ = true;
return handle_wheel_event_;
}
void RendererUnresponsive(
RenderWidgetHostImpl* render_widget_host,
base::RepeatingClosure hang_monitor_restarter) override {
unresponsive_timer_fired_ = true;
}
bool ShouldIgnoreInputEvents() override { return ignore_input_events_; }
void ExecuteEditCommand(
const std::string& command,
const base::Optional<base::string16>& value) override {}
void Cut() override {}
void Copy() override {}
void Paste() override {}
void SelectAll() override {}
private:
bool prehandle_keyboard_event_;
bool prehandle_keyboard_event_is_shortcut_;
bool prehandle_keyboard_event_called_;
WebInputEvent::Type prehandle_keyboard_event_type_;
bool unhandled_keyboard_event_called_;
WebInputEvent::Type unhandled_keyboard_event_type_;
bool handle_wheel_event_;
bool handle_wheel_event_called_;
bool unresponsive_timer_fired_;
bool ignore_input_events_;
std::unique_ptr<MockRenderViewHostDelegateView>
render_view_host_delegate_view_;
double zoom_level_ = 0;
int focus_owning_web_contents_call_count = 0;
};
// RenderWidgetHostTest --------------------------------------------------------
class RenderWidgetHostTest : public testing::Test {
public:
RenderWidgetHostTest()
: process_(nullptr),
handle_key_press_event_(false),
handle_mouse_event_(false),
last_simulated_event_time_(ui::EventTimeForNow()) {
std::vector<base::StringPiece> features;
std::vector<base::StringPiece> disabled_features;
features.push_back(features::kVsyncAlignedInputEvents.name);
feature_list_.InitFromCommandLine(base::JoinString(features, ","),
base::JoinString(disabled_features, ","));
}
~RenderWidgetHostTest() override {}
bool KeyPressEventCallback(const NativeWebKeyboardEvent& /* event */) {
return handle_key_press_event_;
}
bool MouseEventCallback(const blink::WebMouseEvent& /* event */) {
return handle_mouse_event_;
}
void RunLoopFor(base::TimeDelta duration) {
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), duration);
run_loop.Run();
}
protected:
// testing::Test
void SetUp() override {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kValidateInputEventStream);
browser_context_.reset(new TestBrowserContext());
delegate_.reset(new MockRenderWidgetHostDelegate());
process_ = new RenderWidgetHostProcess(browser_context_.get());
sink_ = &process_->sink();
#if defined(USE_AURA) || defined(OS_MACOSX)
ImageTransportFactory::SetFactory(
std::make_unique<TestImageTransportFactory>());
#endif
#if defined(OS_ANDROID)
ui::SetScreenAndroid(); // calls display::Screen::SetScreenInstance().
#endif
#if defined(USE_AURA)
screen_.reset(aura::TestScreen::Create(gfx::Size()));
display::Screen::SetScreenInstance(screen_.get());
#endif
host_.reset(MockRenderWidgetHost::Create(delegate_.get(), process_,
process_->GetNextRoutingID()));
view_.reset(new TestView(host_.get()));
ConfigureView(view_.get());
host_->SetView(view_.get());
SetInitialVisualProperties();
host_->Init();
host_->DisableGestureDebounce();
viz::mojom::CompositorFrameSinkPtr sink;
viz::mojom::CompositorFrameSinkRequest sink_request =
mojo::MakeRequest(&sink);
viz::mojom::CompositorFrameSinkClientRequest client_request =
mojo::MakeRequest(&renderer_compositor_frame_sink_ptr_);
renderer_compositor_frame_sink_ =
std::make_unique<FakeRendererCompositorFrameSink>(
std::move(sink), std::move(client_request));
mojom::RenderFrameMetadataObserverPtr
renderer_render_frame_metadata_observer_ptr;
mojom::RenderFrameMetadataObserverRequest
render_frame_metadata_observer_request =
mojo::MakeRequest(&renderer_render_frame_metadata_observer_ptr);
mojom::RenderFrameMetadataObserverClientPtrInfo
render_frame_metadata_observer_client_info;
mojom::RenderFrameMetadataObserverClientRequest
render_frame_metadata_observer_client_request =
mojo::MakeRequest(&render_frame_metadata_observer_client_info);
renderer_render_frame_metadata_observer_ =
std::make_unique<FakeRenderFrameMetadataObserver>(
std::move(render_frame_metadata_observer_request),
std::move(render_frame_metadata_observer_client_info));
host_->RequestCompositorFrameSink(
std::move(sink_request),
std::move(renderer_compositor_frame_sink_ptr_));
host_->RegisterRenderFrameMetadataObserver(
std::move(render_frame_metadata_observer_client_request),
std::move(renderer_render_frame_metadata_observer_ptr));
}
void TearDown() override {
view_.reset();
host_.reset();
delegate_.reset();
process_ = nullptr;
browser_context_.reset();
#if defined(USE_AURA)
display::Screen::SetScreenInstance(nullptr);
screen_.reset();
#endif
#if defined(USE_AURA) || defined(OS_MACOSX)
ImageTransportFactory::Terminate();
#endif
#if defined(OS_ANDROID)
display::Screen::SetScreenInstance(nullptr);
#endif
// Process all pending tasks to avoid leaks.
base::RunLoop().RunUntilIdle();
}
void SetInitialVisualProperties() {
VisualProperties visual_properties;
bool needs_ack = false;
host_->GetVisualProperties(&visual_properties, &needs_ack);
host_->SetInitialVisualProperties(visual_properties, needs_ack);
}
virtual void ConfigureView(TestView* view) {
}
base::TimeTicks GetNextSimulatedEventTime() {
last_simulated_event_time_ += simulated_event_time_delta_;
return last_simulated_event_time_;
}
void SimulateKeyboardEvent(WebInputEvent::Type type) {
SimulateKeyboardEvent(type, 0);
}
void SimulateKeyboardEvent(WebInputEvent::Type type, int modifiers) {
NativeWebKeyboardEvent native_event(type, modifiers,
GetNextSimulatedEventTime());
host_->ForwardKeyboardEvent(native_event);
}
void SimulateKeyboardEventWithCommands(WebInputEvent::Type type) {
NativeWebKeyboardEvent native_event(type, 0, GetNextSimulatedEventTime());
EditCommands commands;
commands.emplace_back("name", "value");
host_->ForwardKeyboardEventWithCommands(native_event, ui::LatencyInfo(),
&commands, nullptr);
}
void SimulateMouseEvent(WebInputEvent::Type type) {
host_->ForwardMouseEvent(SyntheticWebMouseEventBuilder::Build(type));
}
void SimulateMouseEventWithLatencyInfo(WebInputEvent::Type type,
const ui::LatencyInfo& ui_latency) {
host_->ForwardMouseEventWithLatencyInfo(
SyntheticWebMouseEventBuilder::Build(type),
ui_latency);
}
void SimulateWheelEvent(float dX, float dY, int modifiers, bool precise) {
host_->ForwardWheelEvent(SyntheticWebMouseWheelEventBuilder::Build(
0, 0, dX, dY, modifiers, precise));
}
void SimulateWheelEvent(float dX,
float dY,
int modifiers,
bool precise,
WebMouseWheelEvent::Phase phase) {
WebMouseWheelEvent wheel_event = SyntheticWebMouseWheelEventBuilder::Build(
0, 0, dX, dY, modifiers, precise);
wheel_event.phase = phase;
host_->ForwardWheelEvent(wheel_event);
}
void SimulateWheelEventWithLatencyInfo(float dX,
float dY,
int modifiers,
bool precise,
const ui::LatencyInfo& ui_latency) {
host_->ForwardWheelEventWithLatencyInfo(
SyntheticWebMouseWheelEventBuilder::Build(0, 0, dX, dY, modifiers,
precise),
ui_latency);
}
void SimulateWheelEventWithLatencyInfo(float dX,
float dY,
int modifiers,
bool precise,
const ui::LatencyInfo& ui_latency,
WebMouseWheelEvent::Phase phase) {
WebMouseWheelEvent wheel_event = SyntheticWebMouseWheelEventBuilder::Build(
0, 0, dX, dY, modifiers, precise);
wheel_event.phase = phase;
host_->ForwardWheelEventWithLatencyInfo(wheel_event, ui_latency);
}
void SimulateMouseMove(int x, int y, int modifiers) {
SimulateMouseEvent(WebInputEvent::kMouseMove, x, y, modifiers, false);
}
void SimulateMouseEvent(
WebInputEvent::Type type, int x, int y, int modifiers, bool pressed) {
WebMouseEvent event =
SyntheticWebMouseEventBuilder::Build(type, x, y, modifiers);
if (pressed)
event.button = WebMouseEvent::Button::kLeft;
event.SetTimeStamp(GetNextSimulatedEventTime());
host_->ForwardMouseEvent(event);
}
// Inject simple synthetic WebGestureEvent instances.
void SimulateGestureEvent(WebInputEvent::Type type,
WebGestureDevice sourceDevice) {
host_->ForwardGestureEvent(
SyntheticWebGestureEventBuilder::Build(type, sourceDevice));
}
void SimulateGestureEventWithLatencyInfo(WebInputEvent::Type type,
WebGestureDevice sourceDevice,
const ui::LatencyInfo& ui_latency) {
host_->ForwardGestureEventWithLatencyInfo(
SyntheticWebGestureEventBuilder::Build(type, sourceDevice), ui_latency);
}
// Set the timestamp for the touch-event.
void SetTouchTimestamp(base::TimeTicks timestamp) {
touch_event_.SetTimestamp(timestamp);
}
// Sends a touch event (irrespective of whether the page has a touch-event
// handler or not).
uint32_t SendTouchEvent() {
uint32_t touch_event_id = touch_event_.unique_touch_event_id;
host_->ForwardTouchEventWithLatencyInfo(touch_event_, ui::LatencyInfo());
touch_event_.ResetPoints();
return touch_event_id;
}
int PressTouchPoint(int x, int y) {
return touch_event_.PressPoint(x, y);
}
void MoveTouchPoint(int index, int x, int y) {
touch_event_.MovePoint(index, x, y);
}
void ReleaseTouchPoint(int index) {
touch_event_.ReleasePoint(index);
}
const WebInputEvent* GetInputEventFromMessage(const IPC::Message& message) {
base::PickleIterator iter(message);
const char* data;
int data_length;
if (!iter.ReadData(&data, &data_length))
return nullptr;
return reinterpret_cast<const WebInputEvent*>(data);
}
std::unique_ptr<TestBrowserContext> browser_context_;
RenderWidgetHostProcess* process_; // Deleted automatically by the widget.
std::unique_ptr<MockRenderWidgetHostDelegate> delegate_;
std::unique_ptr<MockRenderWidgetHost> host_;
std::unique_ptr<TestView> view_;
std::unique_ptr<display::Screen> screen_;
bool handle_key_press_event_;
bool handle_mouse_event_;
base::TimeTicks last_simulated_event_time_;
base::TimeDelta simulated_event_time_delta_;
IPC::TestSink* sink_;
std::unique_ptr<FakeRendererCompositorFrameSink>
renderer_compositor_frame_sink_;
std::unique_ptr<FakeRenderFrameMetadataObserver>
renderer_render_frame_metadata_observer_;
private:
SyntheticWebTouchEvent touch_event_;
TestBrowserThreadBundle thread_bundle_;
base::test::ScopedFeatureList feature_list_;
viz::mojom::CompositorFrameSinkClientPtr renderer_compositor_frame_sink_ptr_;
DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostTest);
};
// RenderWidgetHostWithSourceTest ----------------------------------------------
// This is for tests that are to be run for all source devices.
class RenderWidgetHostWithSourceTest
: public RenderWidgetHostTest,
public testing::WithParamInterface<WebGestureDevice> {};
} // namespace
// -----------------------------------------------------------------------------
TEST_F(RenderWidgetHostTest, SynchronizeVisualProperties) {
// The initial zoom is 0 so host should not send a sync message
delegate_->SetZoomLevel(0);
EXPECT_FALSE(host_->SynchronizeVisualProperties());
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_FALSE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// The zoom has changed so host should send out a sync message
process_->sink().ClearMessages();
double new_zoom_level = content::ZoomFactorToZoomLevel(0.25);
delegate_->SetZoomLevel(new_zoom_level);
EXPECT_TRUE(host_->SynchronizeVisualProperties());
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_NEAR(new_zoom_level, host_->old_visual_properties_->zoom_level, 0.01);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// The initial bounds is the empty rect, so setting it to the same thing
// shouldn't send the resize message.
process_->sink().ClearMessages();
view_->SetBounds(gfx::Rect());
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_FALSE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// No visual properties ACK if the physical backing gets set, but the view
// bounds are zero.
view_->SetMockCompositorViewportPixelSize(gfx::Size(200, 200));
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
// Setting the view bounds to nonzero should send out the notification.
// but should not expect ack for empty physical backing size.
gfx::Rect original_size(0, 0, 100, 100);
process_->sink().ClearMessages();
view_->SetBounds(original_size);
view_->SetMockCompositorViewportPixelSize(gfx::Size());
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(original_size.size(), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// Setting the bounds and physical backing size to nonzero should send out
// the notification and expect an ack.
process_->sink().ClearMessages();
view_->ClearMockCompositorViewportPixelSize();
host_->SynchronizeVisualProperties();
EXPECT_TRUE(host_->visual_properties_ack_pending_);
EXPECT_EQ(original_size.size(), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
cc::RenderFrameMetadata metadata;
metadata.viewport_size_in_pixels = original_size.size();
metadata.local_surface_id = base::nullopt;
host_->DidUpdateVisualProperties(metadata);
EXPECT_FALSE(host_->visual_properties_ack_pending_);
process_->sink().ClearMessages();
gfx::Rect second_size(0, 0, 110, 110);
EXPECT_FALSE(host_->visual_properties_ack_pending_);
view_->SetBounds(second_size);
EXPECT_TRUE(host_->SynchronizeVisualProperties());
EXPECT_TRUE(host_->visual_properties_ack_pending_);
// Sending out a new notification should NOT send out a new IPC message since
// a visual properties ACK is pending.
gfx::Rect third_size(0, 0, 120, 120);
process_->sink().ClearMessages();
view_->SetBounds(third_size);
EXPECT_FALSE(host_->SynchronizeVisualProperties());
EXPECT_TRUE(host_->visual_properties_ack_pending_);
EXPECT_FALSE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// Send a update that's a visual properties ACK, but for the original_size we
// sent. Since this isn't the second_size, the message handler should
// immediately send a new resize message for the new size to the renderer.
process_->sink().ClearMessages();
metadata.viewport_size_in_pixels = original_size.size();
metadata.local_surface_id = base::nullopt;
host_->DidUpdateVisualProperties(metadata);
EXPECT_TRUE(host_->visual_properties_ack_pending_);
EXPECT_EQ(third_size.size(), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// Send the visual properties ACK for the latest size.
process_->sink().ClearMessages();
metadata.viewport_size_in_pixels = third_size.size();
metadata.local_surface_id = base::nullopt;
host_->DidUpdateVisualProperties(metadata);
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(third_size.size(), host_->old_visual_properties_->new_size);
EXPECT_FALSE(process_->sink().GetFirstMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// Now clearing the bounds should send out a notification but we shouldn't
// expect a visual properties ACK (since the renderer won't ack empty sizes).
// The message should contain the new size (0x0) and not the previous one that
// we skipped.
process_->sink().ClearMessages();
view_->SetBounds(gfx::Rect());
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(gfx::Size(), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// Send a rect that has no area but has either width or height set.
process_->sink().ClearMessages();
view_->SetBounds(gfx::Rect(0, 0, 0, 30));
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(gfx::Size(0, 30), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// Set the same size again. It should not be sent again.
process_->sink().ClearMessages();
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(gfx::Size(0, 30), host_->old_visual_properties_->new_size);
EXPECT_FALSE(process_->sink().GetFirstMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// A different size should be sent again, however.
view_->SetBounds(gfx::Rect(0, 0, 0, 31));
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(gfx::Size(0, 31), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// An invalid LocalSurfaceId should result in no change to the
// |visual_properties_ack_pending_| bit.
process_->sink().ClearMessages();
view_->SetBounds(gfx::Rect(25, 25));
view_->InvalidateLocalSurfaceId();
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(gfx::Size(25, 25), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
}
// Test that a resize event is sent if SynchronizeVisualProperties() is called
// after a ScreenInfo change.
TEST_F(RenderWidgetHostTest, ResizeScreenInfo) {
ScreenInfo screen_info;
screen_info.device_scale_factor = 1.f;
screen_info.rect = blink::WebRect(0, 0, 800, 600);
screen_info.available_rect = blink::WebRect(0, 0, 800, 600);
screen_info.orientation_angle = 0;
screen_info.orientation_type = SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY;
view_->SetScreenInfo(screen_info);
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
process_->sink().ClearMessages();
screen_info.orientation_angle = 180;
screen_info.orientation_type = SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY;
view_->SetScreenInfo(screen_info);
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
process_->sink().ClearMessages();
screen_info.device_scale_factor = 2.f;
view_->SetScreenInfo(screen_info);
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
process_->sink().ClearMessages();
// No screen change.
view_->SetScreenInfo(screen_info);
host_->SynchronizeVisualProperties();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_FALSE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
}
// Test for crbug.com/25097. If a renderer crashes between a resize and the
// corresponding update message, we must be sure to clear the visual properties
// ACK logic.
TEST_F(RenderWidgetHostTest, ResizeThenCrash) {
// Clear the first Resize message that carried screen info.
process_->sink().ClearMessages();
// Setting the bounds to a "real" rect should send out the notification.
gfx::Rect original_size(0, 0, 100, 100);
view_->SetBounds(original_size);
host_->SynchronizeVisualProperties();
EXPECT_TRUE(host_->visual_properties_ack_pending_);
EXPECT_EQ(original_size.size(), host_->old_visual_properties_->new_size);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
// Simulate a renderer crash before the update message. Ensure all the
// visual properties ACK logic is cleared. Must clear the view first so it
// doesn't get deleted.
host_->SetView(nullptr);
host_->RendererExited(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
EXPECT_FALSE(host_->visual_properties_ack_pending_);
EXPECT_EQ(nullptr, host_->old_visual_properties_);
// Reset the view so we can exit the test cleanly.
host_->SetView(view_.get());
}
// Unable to include render_widget_host_view_mac.h and compile.
#if !defined(OS_MACOSX)
// Tests setting background transparency.
TEST_F(RenderWidgetHostTest, Background) {
std::unique_ptr<RenderWidgetHostViewBase> view;
#if defined(USE_AURA)
view.reset(new RenderWidgetHostViewAura(
host_.get(), false, false /* is_mus_browser_plugin_guest */));
// TODO(derat): Call this on all platforms: http://crbug.com/102450.
view->InitAsChild(nullptr);
#elif defined(OS_ANDROID)
view.reset(new RenderWidgetHostViewAndroid(host_.get(), NULL));
#endif
host_->SetView(view.get());
ASSERT_FALSE(view->GetBackgroundColor());
view->SetBackgroundColor(SK_ColorTRANSPARENT);
ASSERT_TRUE(view->GetBackgroundColor());
EXPECT_EQ(static_cast<unsigned>(SK_ColorTRANSPARENT),
*view->GetBackgroundColor());
const IPC::Message* set_background =
process_->sink().GetUniqueMessageMatching(
ViewMsg_SetBackgroundOpaque::ID);
ASSERT_TRUE(set_background);
std::tuple<bool> sent_background;
ViewMsg_SetBackgroundOpaque::Read(set_background, &sent_background);
EXPECT_FALSE(std::get<0>(sent_background));
host_->SetView(nullptr);
static_cast<RenderWidgetHostViewBase*>(view.release())->Destroy();
}
#endif
// Test that we don't paint when we're hidden, but we still send the ACK. Most
// of the rest of the painting is tested in the GetBackingStore* ones.
TEST_F(RenderWidgetHostTest, HiddenPaint) {
// Hide the widget, it should have sent out a message to the renderer.
EXPECT_FALSE(host_->is_hidden_);
host_->WasHidden();
EXPECT_TRUE(host_->is_hidden_);
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_WasHidden::ID));
// Send it an update as from the renderer.
process_->sink().ClearMessages();
cc::RenderFrameMetadata metadata;
metadata.viewport_size_in_pixels = gfx::Size(100, 100);
metadata.local_surface_id = base::nullopt;
host_->DidUpdateVisualProperties(metadata);
// Now unhide.
process_->sink().ClearMessages();
host_->WasShown(false /* record_presentation_time */);
EXPECT_FALSE(host_->is_hidden_);
// It should have sent out a restored message with a request to paint.
const IPC::Message* restored = process_->sink().GetUniqueMessageMatching(
ViewMsg_WasShown::ID);
ASSERT_TRUE(restored);
std::tuple<bool, base::TimeTicks> needs_repaint;
ViewMsg_WasShown::Read(restored, &needs_repaint);
EXPECT_TRUE(std::get<0>(needs_repaint));
}
TEST_F(RenderWidgetHostTest, IgnoreKeyEventsHandledByRenderer) {
// Simulate a keyboard event.
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
// Make sure we sent the input event to the renderer.
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(INPUT_EVENT_ACK_STATE_CONSUMED);
EXPECT_FALSE(delegate_->unhandled_keyboard_event_called());
}
TEST_F(RenderWidgetHostTest, SendEditCommandsBeforeKeyEvent) {
// Simulate a keyboard event.
SimulateKeyboardEventWithCommands(WebInputEvent::kRawKeyDown);
// Make sure we sent commands and key event to the renderer.
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(2u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEditCommand());
ASSERT_TRUE(dispatched_events[1]->ToEvent());
// Send the simulated response from the renderer back.
dispatched_events[1]->ToEvent()->CallCallback(INPUT_EVENT_ACK_STATE_CONSUMED);
}
TEST_F(RenderWidgetHostTest, PreHandleRawKeyDownEvent) {
// Simulate the situation that the browser handled the key down event during
// pre-handle phrase.
delegate_->set_prehandle_keyboard_event(true);
// Simulate a keyboard event.
SimulateKeyboardEventWithCommands(WebInputEvent::kRawKeyDown);
EXPECT_TRUE(delegate_->prehandle_keyboard_event_called());
EXPECT_EQ(WebInputEvent::kRawKeyDown,
delegate_->prehandle_keyboard_event_type());
// Make sure the commands and key event are not sent to the renderer.
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
EXPECT_EQ(0u, dispatched_events.size());
// The browser won't pre-handle a Char event.
delegate_->set_prehandle_keyboard_event(false);
// Forward the Char event.
SimulateKeyboardEvent(WebInputEvent::kChar);
// Make sure the Char event is suppressed.
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
EXPECT_EQ(0u, dispatched_events.size());
// Forward the KeyUp event.
SimulateKeyboardEvent(WebInputEvent::kKeyUp);
// Make sure the KeyUp event is suppressed.
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
EXPECT_EQ(0u, dispatched_events.size());
// Simulate a new RawKeyDown event.
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_EQ(WebInputEvent::kRawKeyDown,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(
INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
EXPECT_TRUE(delegate_->unhandled_keyboard_event_called());
EXPECT_EQ(WebInputEvent::kRawKeyDown,
delegate_->unhandled_keyboard_event_type());
}
TEST_F(RenderWidgetHostTest, RawKeyDownShortcutEvent) {
// Simulate the situation that the browser marks the key down as a keyboard
// shortcut, but doesn't consume it in the pre-handle phase.
delegate_->set_prehandle_keyboard_event_is_shortcut(true);
// Simulate a keyboard event.
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
EXPECT_TRUE(delegate_->prehandle_keyboard_event_called());
EXPECT_EQ(WebInputEvent::kRawKeyDown,
delegate_->prehandle_keyboard_event_type());
// Make sure the RawKeyDown event is sent to the renderer.
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_EQ(WebInputEvent::kRawKeyDown,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(
INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
EXPECT_EQ(WebInputEvent::kRawKeyDown,
delegate_->unhandled_keyboard_event_type());
// The browser won't pre-handle a Char event.
delegate_->set_prehandle_keyboard_event_is_shortcut(false);
// Forward the Char event.
SimulateKeyboardEvent(WebInputEvent::kChar);
// The Char event is not suppressed; the renderer will ignore it
// if the preceding RawKeyDown shortcut goes unhandled.
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_EQ(WebInputEvent::kChar,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(
INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
EXPECT_EQ(WebInputEvent::kChar, delegate_->unhandled_keyboard_event_type());
// Forward the KeyUp event.
SimulateKeyboardEvent(WebInputEvent::kKeyUp);
// Make sure only KeyUp was sent to the renderer.
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_EQ(WebInputEvent::kKeyUp,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(
INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
EXPECT_EQ(WebInputEvent::kKeyUp, delegate_->unhandled_keyboard_event_type());
}
TEST_F(RenderWidgetHostTest, UnhandledWheelEvent) {
SimulateWheelEvent(-5, 0, 0, true, WebMouseWheelEvent::kPhaseBegan);
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_EQ(WebInputEvent::kMouseWheel,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(
INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
EXPECT_TRUE(delegate_->handle_wheel_event_called());
EXPECT_EQ(1, view_->unhandled_wheel_event_count());
EXPECT_EQ(-5, view_->unhandled_wheel_event().delta_x);
}
TEST_F(RenderWidgetHostTest, HandleWheelEvent) {
// Indicate that we're going to handle this wheel event
delegate_->set_handle_wheel_event(true);
SimulateWheelEvent(-5, 0, 0, true, WebMouseWheelEvent::kPhaseBegan);
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_EQ(WebInputEvent::kMouseWheel,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(
INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
// ensure the wheel event handler was invoked
EXPECT_TRUE(delegate_->handle_wheel_event_called());
// and that it suppressed the unhandled wheel event handler.
EXPECT_EQ(0, view_->unhandled_wheel_event_count());
}
TEST_F(RenderWidgetHostTest, EventsCausingFocus) {
SimulateMouseEvent(WebInputEvent::kMouseDown);
EXPECT_EQ(1, delegate_->GetFocusOwningWebContentsCallCount());
PressTouchPoint(0, 1);
SendTouchEvent();
EXPECT_EQ(2, delegate_->GetFocusOwningWebContentsCallCount());
ReleaseTouchPoint(0);
SendTouchEvent();
EXPECT_EQ(2, delegate_->GetFocusOwningWebContentsCallCount());
SimulateGestureEvent(WebInputEvent::kGestureTapDown,
blink::kWebGestureDeviceTouchscreen);
EXPECT_EQ(2, delegate_->GetFocusOwningWebContentsCallCount());
SimulateGestureEvent(WebInputEvent::kGestureTap,
blink::kWebGestureDeviceTouchscreen);
EXPECT_EQ(3, delegate_->GetFocusOwningWebContentsCallCount());
}
TEST_F(RenderWidgetHostTest, UnhandledGestureEvent) {
SimulateGestureEvent(WebInputEvent::kGestureTwoFingerTap,
blink::kWebGestureDeviceTouchscreen);
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_EQ(WebInputEvent::kGestureTwoFingerTap,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(
INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
EXPECT_EQ(WebInputEvent::kGestureTwoFingerTap, view_->gesture_event_type());
EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, view_->ack_result());
}
// Test that the hang monitor timer expires properly if a new timer is started
// while one is in progress (see crbug.com/11007).
TEST_F(RenderWidgetHostTest, DontPostponeInputEventAckTimeout) {
// Start with a short timeout.
host_->StartInputEventAckTimeout(TimeDelta::FromMilliseconds(10));
// Immediately try to add a long 30 second timeout.
EXPECT_FALSE(delegate_->unresponsive_timer_fired());
host_->StartInputEventAckTimeout(TimeDelta::FromSeconds(30));
// Wait long enough for first timeout and see if it fired.
RunLoopFor(TimeDelta::FromMilliseconds(10));
EXPECT_TRUE(delegate_->unresponsive_timer_fired());
}
// Test that the hang monitor timer expires properly if it is started, stopped,
// and then started again.
TEST_F(RenderWidgetHostTest, StopAndStartInputEventAckTimeout) {
// Start with a short timeout, then stop it.
host_->StartInputEventAckTimeout(TimeDelta::FromMilliseconds(10));
host_->StopInputEventAckTimeout();
// Start it again to ensure it still works.
EXPECT_FALSE(delegate_->unresponsive_timer_fired());
host_->StartInputEventAckTimeout(TimeDelta::FromMilliseconds(10));
// Wait long enough for first timeout and see if it fired.
RunLoopFor(TimeDelta::FromMilliseconds(40));
EXPECT_TRUE(delegate_->unresponsive_timer_fired());
}
// Test that the hang monitor timer expires properly if it is started, then
// updated to a shorter duration.
TEST_F(RenderWidgetHostTest, ShorterDelayInputEventAckTimeout) {
// Start with a timeout.
host_->StartInputEventAckTimeout(TimeDelta::FromMilliseconds(100));
// Start it again with shorter delay.
EXPECT_FALSE(delegate_->unresponsive_timer_fired());
host_->StartInputEventAckTimeout(TimeDelta::FromMilliseconds(20));
// Wait long enough for the second timeout and see if it fired.
RunLoopFor(TimeDelta::FromMilliseconds(25));
EXPECT_TRUE(delegate_->unresponsive_timer_fired());
}
// Test that the hang monitor timer is effectively disabled when the widget is
// hidden.
TEST_F(RenderWidgetHostTest, InputEventAckTimeoutDisabledForInputWhenHidden) {
host_->set_hung_renderer_delay(base::TimeDelta::FromMicroseconds(1));
SimulateMouseEvent(WebInputEvent::kMouseMove, 10, 10, 0, false);
// Hiding the widget should deactivate the timeout.
host_->WasHidden();
// The timeout should not fire.
EXPECT_FALSE(delegate_->unresponsive_timer_fired());
RunLoopFor(TimeDelta::FromMicroseconds(2));
EXPECT_FALSE(delegate_->unresponsive_timer_fired());
// The timeout should never reactivate while hidden.
SimulateMouseEvent(WebInputEvent::kMouseMove, 10, 10, 0, false);
RunLoopFor(TimeDelta::FromMicroseconds(2));
EXPECT_FALSE(delegate_->unresponsive_timer_fired());
// Showing the widget should restore the timeout, as the events have
// not yet been ack'ed.
host_->WasShown(false /* record_presentation_time */);
RunLoopFor(TimeDelta::FromMicroseconds(2));
EXPECT_TRUE(delegate_->unresponsive_timer_fired());
}
// Test that the hang monitor catches two input events but only one ack.
// This can happen if the second input event causes the renderer to hang.
// This test will catch a regression of crbug.com/111185.
TEST_F(RenderWidgetHostTest, MultipleInputEvents) {
// Configure the host to wait 10ms before considering
// the renderer hung.
host_->set_hung_renderer_delay(base::TimeDelta::FromMicroseconds(10));
// Send two events but only one ack.
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
ASSERT_EQ(2u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
// Send the simulated response from the renderer back.
dispatched_events[0]->ToEvent()->CallCallback(INPUT_EVENT_ACK_STATE_CONSUMED);
// Wait long enough for first timeout and see if it fired.
RunLoopFor(TimeDelta::FromMicroseconds(20));
EXPECT_TRUE(delegate_->unresponsive_timer_fired());
}
// Test that the rendering timeout for newly loaded content fires when enough
// time passes without receiving a new compositor frame. This test assumes
// Surface Synchronization is off.
TEST_F(RenderWidgetHostTest, NewContentRenderingTimeoutWithoutSurfaceSync) {
// If Surface Synchronization is on, we have a separate code path for
// cancelling new content rendering timeout that is tested separately.
if (features::IsSurfaceSynchronizationEnabled())
return;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
// Mocking |renderer_compositor_frame_sink_| to prevent crashes in
// renderer_compositor_frame_sink_->DidReceiveCompositorFrameAck(resources).
std::unique_ptr<viz::MockCompositorFrameSinkClient>
mock_compositor_frame_sink_client =
std::make_unique<viz::MockCompositorFrameSinkClient>();
host_->SetMockRendererCompositorFrameSink(
mock_compositor_frame_sink_client.get());
host_->set_new_content_rendering_delay_for_testing(
base::TimeDelta::FromMicroseconds(10));
// Start the timer and immediately send a CompositorFrame with the
// content_source_id of the new page. The timeout shouldn't fire.
host_->DidNavigate(5);
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetContentSourceId(5)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
RunLoopFor(TimeDelta::FromMicroseconds(20));
EXPECT_FALSE(host_->new_content_rendering_timeout_fired());
host_->reset_new_content_rendering_timeout_fired();
// Start the timer but receive frames only from the old page. The timer
// should fire.
host_->DidNavigate(10);
frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetContentSourceId(9)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
RunLoopFor(TimeDelta::FromMicroseconds(20));
EXPECT_TRUE(host_->new_content_rendering_timeout_fired());
host_->reset_new_content_rendering_timeout_fired();
// Send a CompositorFrame with content_source_id of the new page before we
// attempt to start the timer. The timer shouldn't fire.
frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetContentSourceId(7)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
host_->DidNavigate(7);
RunLoopFor(TimeDelta::FromMicroseconds(20));
EXPECT_FALSE(host_->new_content_rendering_timeout_fired());
host_->reset_new_content_rendering_timeout_fired();
// Don't send any frames after the timer starts. The timer should fire.
host_->DidNavigate(20);
RunLoopFor(TimeDelta::FromMicroseconds(20));
EXPECT_TRUE(host_->new_content_rendering_timeout_fired());
host_->reset_new_content_rendering_timeout_fired();
}
// This tests that a compositor frame received with a stale content source ID
// in its metadata is properly discarded.
TEST_F(RenderWidgetHostTest, SwapCompositorFrameWithBadSourceId) {
// If Surface Synchronization is on, we don't keep track of content_source_id
// in CompositorFrameMetadata.
if (features::IsSurfaceSynchronizationEnabled())
return;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
host_->DidNavigate(100);
host_->set_new_content_rendering_delay_for_testing(
base::TimeDelta::FromMicroseconds(9999));
{
// First swap a frame with an invalid ID.
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetBeginFrameAck(viz::BeginFrameAck(0, 1, true))
.SetContentSourceId(99)
.Build();
// Mocking |renderer_compositor_frame_sink_| to prevent crashes in
// renderer_compositor_frame_sink_->DidReceiveCompositorFrameAck(resources).
std::unique_ptr<viz::MockCompositorFrameSinkClient>
mock_compositor_frame_sink_client =
std::make_unique<viz::MockCompositorFrameSinkClient>();
host_->SetMockRendererCompositorFrameSink(
mock_compositor_frame_sink_client.get());
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_FALSE(
static_cast<TestView*>(host_->GetView())->did_swap_compositor_frame());
EXPECT_EQ(viz::BeginFrameAck(0, 1, false),
static_cast<TestView*>(host_->GetView())
->last_did_not_produce_frame_ack());
static_cast<TestView*>(host_->GetView())->reset_did_swap_compositor_frame();
}
{
// Test with a valid content ID as a control.
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetContentSourceId(100)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_TRUE(
static_cast<TestView*>(host_->GetView())->did_swap_compositor_frame());
static_cast<TestView*>(host_->GetView())->reset_did_swap_compositor_frame();
}
{
// We also accept frames with higher content IDs, to cover the case where
// the browser process receives a compositor frame for a new page before
// the corresponding DidCommitProvisionalLoad (it's a race).
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetContentSourceId(101)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_TRUE(
static_cast<TestView*>(host_->GetView())->did_swap_compositor_frame());
}
}
TEST_F(RenderWidgetHostTest, IgnoreInputEvent) {
host_->SetupForInputRouterTest();
delegate_->SetIgnoreInputEvents(true);
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
SimulateMouseEvent(WebInputEvent::kMouseMove);
EXPECT_FALSE(host_->mock_input_router()->sent_mouse_event_);
SimulateWheelEvent(0, 100, 0, true);
EXPECT_FALSE(host_->mock_input_router()->sent_wheel_event_);
SimulateGestureEvent(WebInputEvent::kGestureScrollBegin,
blink::kWebGestureDeviceTouchpad);
EXPECT_FALSE(host_->mock_input_router()->sent_gesture_event_);
PressTouchPoint(100, 100);
SendTouchEvent();
EXPECT_FALSE(host_->mock_input_router()->send_touch_event_not_cancelled_);
}
TEST_F(RenderWidgetHostTest, KeyboardListenerIgnoresEvent) {
host_->SetupForInputRouterTest();
host_->AddKeyPressEventCallback(
base::Bind(&RenderWidgetHostTest::KeyPressEventCallback,
base::Unretained(this)));
handle_key_press_event_ = false;
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
EXPECT_TRUE(host_->mock_input_router()->sent_keyboard_event_);
}
TEST_F(RenderWidgetHostTest, KeyboardListenerSuppressFollowingEvents) {
host_->SetupForInputRouterTest();
host_->AddKeyPressEventCallback(
base::Bind(&RenderWidgetHostTest::KeyPressEventCallback,
base::Unretained(this)));
// The callback handles the first event
handle_key_press_event_ = true;
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
// Following Char events should be suppressed
handle_key_press_event_ = false;
SimulateKeyboardEvent(WebInputEvent::kChar);
EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
SimulateKeyboardEvent(WebInputEvent::kChar);
EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
// Sending RawKeyDown event should stop suppression
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
EXPECT_TRUE(host_->mock_input_router()->sent_keyboard_event_);
host_->mock_input_router()->sent_keyboard_event_ = false;
SimulateKeyboardEvent(WebInputEvent::kChar);
EXPECT_TRUE(host_->mock_input_router()->sent_keyboard_event_);
}
TEST_F(RenderWidgetHostTest, MouseEventCallbackCanHandleEvent) {
host_->SetupForInputRouterTest();
host_->AddMouseEventCallback(
base::Bind(&RenderWidgetHostTest::MouseEventCallback,
base::Unretained(this)));
handle_mouse_event_ = true;
SimulateMouseEvent(WebInputEvent::kMouseDown);
EXPECT_FALSE(host_->mock_input_router()->sent_mouse_event_);
handle_mouse_event_ = false;
SimulateMouseEvent(WebInputEvent::kMouseDown);
EXPECT_TRUE(host_->mock_input_router()->sent_mouse_event_);
}
TEST_F(RenderWidgetHostTest, InputRouterReceivesHasTouchEventHandlers) {
host_->SetupForInputRouterTest();
host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
EXPECT_TRUE(host_->mock_input_router()->message_received_);
}
void CheckLatencyInfoComponentInMessage(
MockWidgetInputHandler::MessageVector& dispatched_events,
WebInputEvent::Type expected_type) {
ASSERT_EQ(1u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
EXPECT_TRUE(dispatched_events[0]->ToEvent()->Event()->web_event->GetType() ==
expected_type);
EXPECT_TRUE(
dispatched_events[0]->ToEvent()->Event()->latency_info.FindLatency(
ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, nullptr));
dispatched_events[0]->ToEvent()->CallCallback(INPUT_EVENT_ACK_STATE_CONSUMED);
}
void CheckLatencyInfoComponentInGestureScrollUpdate(
MockWidgetInputHandler::MessageVector& dispatched_events) {
ASSERT_EQ(2u, dispatched_events.size());
ASSERT_TRUE(dispatched_events[0]->ToEvent());
ASSERT_TRUE(dispatched_events[1]->ToEvent());
EXPECT_EQ(WebInputEvent::kTouchScrollStarted,
dispatched_events[0]->ToEvent()->Event()->web_event->GetType());
EXPECT_EQ(WebInputEvent::kGestureScrollUpdate,
dispatched_events[1]->ToEvent()->Event()->web_event->GetType());
EXPECT_TRUE(
dispatched_events[1]->ToEvent()->Event()->latency_info.FindLatency(
ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT, nullptr));
dispatched_events[1]->ToEvent()->CallCallback(INPUT_EVENT_ACK_STATE_CONSUMED);
}
// Tests that after input event passes through RWHI through ForwardXXXEvent()
// or ForwardXXXEventWithLatencyInfo(), LatencyInfo component
// ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT will always present in the
// event's LatencyInfo.
TEST_F(RenderWidgetHostTest, InputEventRWHLatencyComponent) {
host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
// Tests RWHI::ForwardWheelEvent().
SimulateWheelEvent(-5, 0, 0, true, WebMouseWheelEvent::kPhaseBegan);
MockWidgetInputHandler::MessageVector dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInMessage(dispatched_events,
WebInputEvent::kMouseWheel);
// Tests RWHI::ForwardWheelEventWithLatencyInfo().
SimulateWheelEventWithLatencyInfo(-5, 0, 0, true, ui::LatencyInfo(),
WebMouseWheelEvent::kPhaseChanged);
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInMessage(dispatched_events,
WebInputEvent::kMouseWheel);
// Tests RWHI::ForwardMouseEvent().
SimulateMouseEvent(WebInputEvent::kMouseMove);
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInMessage(dispatched_events,
WebInputEvent::kMouseMove);
// Tests RWHI::ForwardMouseEventWithLatencyInfo().
SimulateMouseEventWithLatencyInfo(WebInputEvent::kMouseMove,
ui::LatencyInfo());
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInMessage(dispatched_events,
WebInputEvent::kMouseMove);
// Tests RWHI::ForwardGestureEvent().
PressTouchPoint(0, 1);
SendTouchEvent();
host_->input_router()->OnSetTouchAction(cc::kTouchActionAuto);
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInMessage(dispatched_events,
WebInputEvent::kTouchStart);
SimulateGestureEvent(WebInputEvent::kGestureScrollBegin,
blink::kWebGestureDeviceTouchscreen);
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInMessage(dispatched_events,
WebInputEvent::kGestureScrollBegin);
// Tests RWHI::ForwardGestureEventWithLatencyInfo().
SimulateGestureEventWithLatencyInfo(WebInputEvent::kGestureScrollUpdate,
blink::kWebGestureDeviceTouchscreen,
ui::LatencyInfo());
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInGestureScrollUpdate(dispatched_events);
ReleaseTouchPoint(0);
SendTouchEvent();
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
// Tests RWHI::ForwardTouchEventWithLatencyInfo().
PressTouchPoint(0, 1);
SendTouchEvent();
dispatched_events =
host_->mock_widget_input_handler_.GetAndResetDispatchedMessages();
CheckLatencyInfoComponentInMessage(dispatched_events,
WebInputEvent::kTouchStart);
}
TEST_F(RenderWidgetHostTest, RendererExitedResetsInputRouter) {
// RendererExited will delete the view.
host_->SetView(new TestView(host_.get()));
host_->RendererExited(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
// Make sure the input router is in a fresh state.
ASSERT_FALSE(host_->input_router()->HasPendingEvents());
}
// Regression test for http://crbug.com/401859 and http://crbug.com/522795.
TEST_F(RenderWidgetHostTest, RendererExitedResetsIsHidden) {
// RendererExited will delete the view.
host_->SetView(new TestView(host_.get()));
host_->WasShown(false /* record_presentation_time */);
ASSERT_FALSE(host_->is_hidden());
host_->RendererExited(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
ASSERT_TRUE(host_->is_hidden());
// Make sure the input router is in a fresh state.
ASSERT_FALSE(host_->input_router()->HasPendingEvents());
}
TEST_F(RenderWidgetHostTest, VisualProperties) {
gfx::Rect bounds(0, 0, 100, 100);
gfx::Size compositor_viewport_pixel_size(40, 50);
view_->SetBounds(bounds);
view_->SetMockCompositorViewportPixelSize(compositor_viewport_pixel_size);
VisualProperties visual_properties;
bool needs_ack = false;
host_->GetVisualProperties(&visual_properties, &needs_ack);
EXPECT_EQ(bounds.size(), visual_properties.new_size);
EXPECT_EQ(compositor_viewport_pixel_size,
visual_properties.compositor_viewport_pixel_size);
}
TEST_F(RenderWidgetHostTest, VisualPropertiesDeviceScale) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitchASCII(switches::kEnableUseZoomForDSF, "true");
float device_scale = 3.5f;
ScreenInfo screen_info;
screen_info.device_scale_factor = device_scale;
view_->SetScreenInfo(screen_info);
host_->SynchronizeVisualProperties();
float top_controls_height = 10.0f;
float bottom_controls_height = 20.0f;
view_->set_top_controls_height(top_controls_height);
view_->set_bottom_controls_height(bottom_controls_height);
VisualProperties visual_properties;
bool needs_ack = false;
host_->GetVisualProperties(&visual_properties, &needs_ack);
EXPECT_EQ(top_controls_height * device_scale,
visual_properties.top_controls_height);
EXPECT_EQ(bottom_controls_height * device_scale,
visual_properties.bottom_controls_height);
}
// Make sure no dragging occurs after renderer exited. See crbug.com/704832.
TEST_F(RenderWidgetHostTest, RendererExitedNoDrag) {
host_->SetView(new TestView(host_.get()));
EXPECT_EQ(delegate_->mock_delegate_view()->start_dragging_count(), 0);
GURL http_url = GURL("http://www.domain.com/index.html");
DropData drop_data;
drop_data.url = http_url;
drop_data.html_base_url = http_url;
blink::WebDragOperationsMask drag_operation = blink::kWebDragOperationEvery;
DragEventSourceInfo event_info;
host_->OnStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Vector2d(),
event_info);
EXPECT_EQ(delegate_->mock_delegate_view()->start_dragging_count(), 1);
// Simulate that renderer exited due navigation to the next page.
host_->RendererExited(base::TERMINATION_STATUS_NORMAL_TERMINATION, 0);
EXPECT_FALSE(host_->GetView());
host_->OnStartDragging(drop_data, drag_operation, SkBitmap(), gfx::Vector2d(),
event_info);
EXPECT_EQ(delegate_->mock_delegate_view()->start_dragging_count(), 1);
}
class RenderWidgetHostInitialSizeTest : public RenderWidgetHostTest {
public:
RenderWidgetHostInitialSizeTest()
: RenderWidgetHostTest(), initial_size_(200, 100) {}
void ConfigureView(TestView* view) override {
view->SetBounds(gfx::Rect(initial_size_));
}
protected:
gfx::Size initial_size_;
};
TEST_F(RenderWidgetHostInitialSizeTest, InitialSize) {
// Having an initial size set means that the size information had been sent
// with the reqiest to new up the RenderView and so subsequent
// SynchronizeVisualProperties calls should not result in new IPC (unless the
// size has actually changed).
EXPECT_FALSE(host_->SynchronizeVisualProperties());
EXPECT_FALSE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
EXPECT_EQ(initial_size_, host_->old_visual_properties_->new_size);
EXPECT_TRUE(host_->visual_properties_ack_pending_);
}
TEST_F(RenderWidgetHostTest, HideUnthrottlesResize) {
gfx::Size original_size(100, 100);
view_->SetBounds(gfx::Rect(original_size));
process_->sink().ClearMessages();
EXPECT_TRUE(host_->SynchronizeVisualProperties());
EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
ViewMsg_SynchronizeVisualProperties::ID));
EXPECT_EQ(original_size, host_->old_visual_properties_->new_size);
EXPECT_TRUE(host_->visual_properties_ack_pending_);
// Hiding the widget should unthrottle resize.
host_->WasHidden();
EXPECT_FALSE(host_->visual_properties_ack_pending_);
}
// Tests that event dispatch after the delegate has been detached doesn't cause
// a crash. See crbug.com/563237.
TEST_F(RenderWidgetHostTest, EventDispatchPostDetach) {
host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
process_->sink().ClearMessages();
host_->DetachDelegate();
// Tests RWHI::ForwardGestureEventWithLatencyInfo().
SimulateGestureEventWithLatencyInfo(WebInputEvent::kGestureScrollUpdate,
blink::kWebGestureDeviceTouchscreen,
ui::LatencyInfo());
// Tests RWHI::ForwardWheelEventWithLatencyInfo().
SimulateWheelEventWithLatencyInfo(-5, 0, 0, true, ui::LatencyInfo());
ASSERT_FALSE(host_->input_router()->HasPendingEvents());
}
// Check that if messages of a frame arrive earlier than the frame itself, we
// queue the messages until the frame arrives and then process them.
TEST_F(RenderWidgetHostTest, FrameToken_MessageThenFrame) {
const uint32_t frame_token = 99;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
std::vector<IPC::Message> messages;
messages.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(5));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token, messages));
EXPECT_EQ(1u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(1u, host_->processed_frame_messages_count());
}
// Check that if a frame arrives earlier than its messages, we process the
// messages immedtiately.
TEST_F(RenderWidgetHostTest, FrameToken_FrameThenMessage) {
const uint32_t frame_token = 99;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
std::vector<IPC::Message> messages;
messages.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(5));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token, messages));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(1u, host_->processed_frame_messages_count());
}
// Check that if messages of multiple frames arrive before the frames, we
// process each message once it frame arrives.
TEST_F(RenderWidgetHostTest, FrameToken_MultipleMessagesThenTokens) {
const uint32_t frame_token1 = 99;
const uint32_t frame_token2 = 100;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
std::vector<IPC::Message> messages1;
std::vector<IPC::Message> messages2;
messages1.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(5));
messages2.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(6));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token1, messages1));
EXPECT_EQ(1u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token2, messages2));
EXPECT_EQ(2u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token1)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(1u, host_->frame_token_message_queue_->size());
EXPECT_EQ(1u, host_->processed_frame_messages_count());
frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token2)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(2u, host_->processed_frame_messages_count());
}
// Check that if multiple frames arrive before their messages, each message is
// processed immediately as soon as it arrives.
TEST_F(RenderWidgetHostTest, FrameToken_MultipleTokensThenMessages) {
const uint32_t frame_token1 = 99;
const uint32_t frame_token2 = 100;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
std::vector<IPC::Message> messages1;
std::vector<IPC::Message> messages2;
messages1.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(5));
messages2.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(6));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token1)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token2)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token1, messages1));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(1u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token2, messages2));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(2u, host_->processed_frame_messages_count());
}
// Check that if one frame is lost but its messages arrive, we process the
// messages on the arrival of the next frame.
TEST_F(RenderWidgetHostTest, FrameToken_DroppedFrame) {
const uint32_t frame_token1 = 99;
const uint32_t frame_token2 = 100;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
std::vector<IPC::Message> messages1;
std::vector<IPC::Message> messages2;
messages1.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(5));
messages2.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(6));
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token1, messages1));
EXPECT_EQ(1u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token2, messages2));
EXPECT_EQ(2u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token2)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(2u, host_->processed_frame_messages_count());
}
// Check that if the renderer crashes, we drop all queued messages and allow
// smaller frame tokens to be sent by the renderer.
TEST_F(RenderWidgetHostTest, FrameToken_RendererCrash) {
const uint32_t frame_token1 = 99;
const uint32_t frame_token2 = 50;
const uint32_t frame_token3 = 30;
const viz::LocalSurfaceId local_surface_id(1,
base::UnguessableToken::Create());
std::vector<IPC::Message> messages1;
std::vector<IPC::Message> messages3;
messages1.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(5));
messages3.push_back(ViewHostMsg_DidFirstVisuallyNonEmptyPaint(6));
// Mocking |renderer_compositor_frame_sink_| to prevent crashes in
// renderer_compositor_frame_sink_->DidReceiveCompositorFrameAck(resources).
std::unique_ptr<viz::MockCompositorFrameSinkClient>
mock_compositor_frame_sink_client =
std::make_unique<viz::MockCompositorFrameSinkClient>();
host_->SetMockRendererCompositorFrameSink(
mock_compositor_frame_sink_client.get());
// If we don't do this, then RWHI destroys the view in RendererExited and
// then a crash occurs when we attempt to destroy it again in TearDown().
host_->SetView(nullptr);
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token1, messages1));
EXPECT_EQ(1u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->RendererExited(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->Init();
auto frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token2)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->RendererExited(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
host_->SetView(view_.get());
host_->Init();
host_->OnMessageReceived(
ViewHostMsg_FrameSwapMessages(0, frame_token3, messages3));
EXPECT_EQ(1u, host_->frame_token_message_queue_->size());
EXPECT_EQ(0u, host_->processed_frame_messages_count());
frame = viz::CompositorFrameBuilder()
.AddDefaultRenderPass()
.SetFrameToken(frame_token3)
.SetSendFrameTokenToEmbedder(true)
.Build();
host_->SubmitCompositorFrame(local_surface_id, std::move(frame),
base::nullopt, 0);
EXPECT_EQ(0u, host_->frame_token_message_queue_->size());
EXPECT_EQ(1u, host_->processed_frame_messages_count());
}
TEST_F(RenderWidgetHostTest, InflightEventCountResetsAfterRebind) {
// Simulate a keyboard event.
SimulateKeyboardEvent(WebInputEvent::kRawKeyDown);
EXPECT_EQ(1u, host_->in_flight_event_count());
mojom::WidgetPtr widget;
std::unique_ptr<MockWidgetImpl> widget_impl =
std::make_unique<MockWidgetImpl>(mojo::MakeRequest(&widget));
host_->SetWidget(std::move(widget));
EXPECT_EQ(0u, host_->in_flight_event_count());
}
TEST_F(RenderWidgetHostTest, ForceEnableZoomShouldUpdateAfterRebind) {
SCOPED_TRACE("force_enable_zoom is false at start.");
host_->ExpectForceEnableZoom(false);
// Set force_enable_zoom true.
host_->SetForceEnableZoom(true);
SCOPED_TRACE("force_enable_zoom is true after set.");
host_->ExpectForceEnableZoom(true);
// Rebind should also update to the latest force_enable_zoom state.
mojom::WidgetPtr widget;
std::unique_ptr<MockWidgetImpl> widget_impl =
std::make_unique<MockWidgetImpl>(mojo::MakeRequest(&widget));
host_->SetWidget(std::move(widget));
SCOPED_TRACE("force_enable_zoom is true after rebind.");
host_->ExpectForceEnableZoom(true);
}
TEST_F(RenderWidgetHostTest, RenderWidgetSurfaceProperties) {
RenderWidgetSurfaceProperties prop1;
prop1.size = gfx::Size(200, 200);
prop1.device_scale_factor = 1.f;
RenderWidgetSurfaceProperties prop2;
prop2.size = gfx::Size(300, 300);
prop2.device_scale_factor = 2.f;
EXPECT_EQ(
"RenderWidgetSurfaceProperties(size(this: 200x200, other: 300x300), "
"device_scale_factor(this: 1, other: 2))",
prop1.ToDiffString(prop2));
EXPECT_EQ(
"RenderWidgetSurfaceProperties(size(this: 300x300, other: 200x200), "
"device_scale_factor(this: 2, other: 1))",
prop2.ToDiffString(prop1));
EXPECT_EQ("", prop1.ToDiffString(prop1));
EXPECT_EQ("", prop2.ToDiffString(prop2));
}
// If a navigation happens while the widget is hidden, we shouldn't show
// contents of the previous page when we become visible.
TEST_F(RenderWidgetHostTest, NavigateInBackgroundShowsBlank) {
// When visible, navigation does not immediately call into
// ClearDisplayedGraphics.
host_->WasShown(false /* record_presentation_time */);
host_->DidNavigate(5);
EXPECT_FALSE(host_->new_content_rendering_timeout_fired());
// Hide then show. ClearDisplayedGraphics must be called.
host_->WasHidden();
host_->WasShown(false /* record_presentation_time */);
EXPECT_TRUE(host_->new_content_rendering_timeout_fired());
host_->reset_new_content_rendering_timeout_fired();
// Hide, navigate, then show. ClearDisplayedGraphics must be called.
host_->WasHidden();
host_->DidNavigate(6);
host_->WasShown(false /* record_presentation_time */);
EXPECT_TRUE(host_->new_content_rendering_timeout_fired());
}
TEST_F(RenderWidgetHostTest, RendererHangRecordsMetrics) {
base::SimpleTestTickClock clock;
host_->set_clock_for_testing(&clock);
base::HistogramTester tester;
// RenderWidgetHost makes private the methods it overrides from
// InputRouterClient. Call them through the base class.
InputRouterClient* input_router_client = host_.get();
// Do a 3s hang. This shouldn't affect metrics.
input_router_client->IncrementInFlightEventCount();
clock.Advance(base::TimeDelta::FromSeconds(3));
input_router_client->DecrementInFlightEventCount(
InputEventAckSource::UNKNOWN);
tester.ExpectTotalCount("Renderer.Hung.Duration", 0u);
// Do a 17s hang. This should affect metrics.
input_router_client->IncrementInFlightEventCount();
clock.Advance(base::TimeDelta::FromSeconds(17));
input_router_client->DecrementInFlightEventCount(
InputEventAckSource::UNKNOWN);
tester.ExpectTotalCount("Renderer.Hung.Duration", 1u);
tester.ExpectUniqueSample("Renderer.Hung.Duration", 17000, 1);
}
} // namespace content
|