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 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
|
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.ui.ControlTest');
goog.setTestOnly('goog.ui.ControlTest');
goog.require('goog.a11y.aria');
goog.require('goog.a11y.aria.State');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.dom.classlist');
goog.require('goog.events');
goog.require('goog.events.BrowserEvent');
goog.require('goog.events.KeyCodes');
goog.require('goog.html.testing');
goog.require('goog.object');
goog.require('goog.string');
goog.require('goog.style');
goog.require('goog.testing.ExpectedFailures');
goog.require('goog.testing.events');
goog.require('goog.testing.events.Event');
goog.require('goog.testing.jsunit');
goog.require('goog.ui.Component');
goog.require('goog.ui.Control');
goog.require('goog.ui.ControlRenderer');
goog.require('goog.ui.registry');
goog.require('goog.userAgent');
// Disabled due to problems on farm.
var testFocus = false;
var control;
var ALL_EVENTS = goog.object.getValues(goog.ui.Component.EventType);
var events = {};
var expectedFailures;
var sandbox;
var aria = goog.a11y.aria;
var State = goog.a11y.aria.State;
function setUpPage() {
expectedFailures = new goog.testing.ExpectedFailures();
sandbox = document.getElementById('sandbox');
}
/**
* A dummy renderer, for testing purposes.
* @constructor
* @extends {goog.ui.ControlRenderer}
*/
function TestRenderer() {
goog.ui.ControlRenderer.call(this);
}
goog.inherits(TestRenderer, goog.ui.ControlRenderer);
/**
* Initializes the testcase prior to execution.
*/
function setUp() {
control = new goog.ui.Control('Hello');
control.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
goog.events.listen(control, ALL_EVENTS, countEvent);
}
/**
* Cleans up after executing the testcase.
*/
function tearDown() {
control.dispose();
expectedFailures.handleTearDown();
goog.dom.removeChildren(sandbox);
resetEventCount();
}
/**
* Resets the global counter for events dispatched by test objects.
*/
function resetEventCount() {
goog.object.clear(events);
}
/**
* Increments the global counter for events of this type.
* @param {goog.events.Event} e Event to count.
*/
function countEvent(e) {
var type = e.type;
var target = e.target;
if (!events[target]) {
events[target] = {};
}
if (events[target][type]) {
events[target][type]++;
} else {
events[target][type] = 1;
}
}
/**
* Returns the number of times test objects dispatched events of the given
* type since the global counter was last reset.
* @param {goog.ui.Control} target Event target.
* @param {string} type Event type.
* @return {number} Number of events of this type.
*/
function getEventCount(target, type) {
return events[target] && events[target][type] || 0;
}
/**
* Returns true if no events were dispatched since the last reset.
* @return {boolean} Whether no events have been dispatched since the last
* reset.
*/
function noEventsDispatched() {
return !events || goog.object.isEmpty(events);
}
/**
* Returns the number of event listeners created by the control.
* @param {goog.ui.Control} control Control whose event listers are to be
* counted.
* @return {number} Number of event listeners.
*/
function getListenerCount(control) {
return control.googUiComponentHandler_ ?
goog.object.getCount(control.getHandler().keys_) :
0;
}
/**
* Simulates a mousedown event on the given element, including focusing it.
* @param {Element} element Element on which to simulate mousedown.
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
* @return {boolean} Whether the event was allowed to proceed.
*/
function fireMouseDownAndFocus(element, opt_button) {
var result = goog.testing.events.fireMouseDownEvent(element, opt_button);
if (result) {
// Browsers move focus for all buttons, not just the left button.
element.focus();
}
return result;
}
/**
* @return {boolean} Whether we're on Mac Safari 3.x.
*/
function isMacSafari3() {
return goog.userAgent.WEBKIT && goog.userAgent.MAC &&
!goog.userAgent.isVersionOrHigher('527');
}
/**
* Tests the {@link goog.ui.Control} constructor.
*/
function testConstructor() {
assertNotNull('Constructed control must not be null', control);
assertEquals(
'Content must have expected value', 'Hello', control.getContent());
assertEquals(
'Renderer must default to the registered renderer',
goog.ui.registry.getDefaultRenderer(goog.ui.Control),
control.getRenderer());
var content = goog.dom.createDom(
goog.dom.TagName.DIV, null, 'Hello',
goog.dom.createDom(goog.dom.TagName.B, null, 'World'));
var testRenderer = new TestRenderer();
var fakeDomHelper = {};
var foo = new goog.ui.Control(content, testRenderer, fakeDomHelper);
assertNotNull('Constructed object must not be null', foo);
assertEquals('Content must have expected value', content, foo.getContent());
assertEquals(
'Renderer must have expected value', testRenderer, foo.getRenderer());
assertEquals(
'DOM helper must have expected value', fakeDomHelper, foo.getDomHelper());
foo.dispose();
}
/**
* Tests {@link goog.ui.Control#getHandler}.
*/
function testGetHandler() {
assertUndefined(
'Event handler must be undefined before getHandler() ' +
'is called',
control.googUiComponentHandler_);
var handler = control.getHandler();
assertNotNull('Event handler must not be null', handler);
assertEquals(
'getHandler() must return the same instance if called again', handler,
control.getHandler());
}
/**
* Tests {@link goog.ui.Control#isHandleMouseEvents}.
*/
function testIsHandleMouseEvents() {
assertTrue(
'Controls must handle their own mouse events by default',
control.isHandleMouseEvents());
}
/**
* Tests {@link goog.ui.Control#setHandleMouseEvents}.
*/
function testSetHandleMouseEvents() {
assertTrue(
'Control must handle its own mouse events by default',
control.isHandleMouseEvents());
control.setHandleMouseEvents(false);
assertFalse(
'Control must no longer handle its own mouse events',
control.isHandleMouseEvents());
control.setHandleMouseEvents(true);
assertTrue(
'Control must once again handle its own mouse events',
control.isHandleMouseEvents());
control.render(sandbox);
assertTrue(
'Rendered control must handle its own mouse events',
control.isHandleMouseEvents());
control.setHandleMouseEvents(false);
assertFalse(
'Rendered control must no longer handle its own mouse events',
control.isHandleMouseEvents());
control.setHandleMouseEvents(true);
assertTrue(
'Rendered control must once again handle its own mouse events',
control.isHandleMouseEvents());
}
/**
* Tests {@link goog.ui.Control#getKeyEventTarget}.
*/
function testGetKeyEventTarget() {
assertNull(
'Key event target of control without DOM must be null',
control.getKeyEventTarget());
control.createDom();
assertEquals(
'Key event target of control with DOM must be its element',
control.getElement(), control.getKeyEventTarget());
}
/**
* Tests {@link goog.ui.Control#getKeyHandler}.
*/
function testGetKeyHandler() {
assertUndefined(
'Key handler must be undefined before getKeyHandler() ' +
'is called',
control.keyHandler_);
var keyHandler = control.getKeyHandler();
assertNotNull('Key handler must not be null', keyHandler);
assertEquals(
'getKeyHandler() must return the same instance if called ' +
'again',
keyHandler, control.getKeyHandler());
}
/**
* Tests {@link goog.ui.Control#getRenderer}.
*/
function testGetRenderer() {
assertEquals(
'Renderer must be the default registered renderer',
goog.ui.registry.getDefaultRenderer(goog.ui.Control),
control.getRenderer());
}
/**
* Tests {@link goog.ui.Control#setRenderer}.
*/
function testSetRenderer() {
control.createDom();
assertNotNull('Control must have a DOM', control.getElement());
assertFalse('Control must not be in the document', control.isInDocument());
assertEquals(
'Renderer must be the default registered renderer',
goog.ui.registry.getDefaultRenderer(goog.ui.Control),
control.getRenderer());
var testRenderer = new TestRenderer();
control.setRenderer(testRenderer);
assertNull(
'Control must not have a DOM after its renderer is reset',
control.getElement());
assertFalse(
'Control still must not be in the document', control.isInDocument());
assertEquals(
'Renderer must have expected value', testRenderer, control.getRenderer());
control.render(sandbox);
assertTrue('Control must be in the document', control.isInDocument());
assertThrows(
'Resetting the renderer after the control has entered ' +
'the document must throw error',
function() { control.setRenderer({}); });
}
/**
* Tests {@link goog.ui.Control#getExtraClassNames}.
*/
function testGetExtraClassNames() {
assertNull(
'Control must not have any extra class names by default',
control.getExtraClassNames());
}
/**
* Tests {@link goog.ui.Control#addExtraClassName} and
* {@link goog.ui.Control#removeExtraClassName}.
*/
function testAddRemoveClassName() {
assertNull(
'Control must not have any extra class names by default',
control.getExtraClassNames());
control.addClassName('foo');
assertArrayEquals(
'Control must have expected extra class names', ['foo'],
control.getExtraClassNames());
assertNull('Control must not have a DOM', control.getElement());
control.createDom();
assertSameElements(
'Control\'s element must have expected class names',
['goog-control', 'foo'], goog.dom.classlist.get(control.getElement()));
control.addClassName('bar');
assertArrayEquals(
'Control must have expected extra class names', ['foo', 'bar'],
control.getExtraClassNames());
assertSameElements(
'Control\'s element must have expected class names',
['goog-control', 'foo', 'bar'],
goog.dom.classlist.get(control.getElement()));
control.addClassName('bar');
assertArrayEquals(
'Adding the same class name again must be a no-op', ['foo', 'bar'],
control.getExtraClassNames());
assertSameElements(
'Adding the same class name again must be a no-op',
['goog-control', 'foo', 'bar'],
goog.dom.classlist.get(control.getElement()));
control.addClassName(null);
assertArrayEquals(
'Adding null class name must be a no-op', ['foo', 'bar'],
control.getExtraClassNames());
assertSameElements(
'Adding null class name must be a no-op', ['goog-control', 'foo', 'bar'],
goog.dom.classlist.get(control.getElement()));
control.removeClassName(null);
assertArrayEquals(
'Removing null class name must be a no-op', ['foo', 'bar'],
control.getExtraClassNames());
assertSameElements(
'Removing null class name must be a no-op',
['goog-control', 'foo', 'bar'],
goog.dom.classlist.get(control.getElement()));
control.removeClassName('foo');
assertArrayEquals(
'Control must have expected extra class names', ['bar'],
control.getExtraClassNames());
assertSameElements(
'Control\'s element must have expected class names',
['goog-control', 'bar'], goog.dom.classlist.get(control.getElement()));
control.removeClassName('bar');
assertNull(
'Control must not have any extra class names',
control.getExtraClassNames());
assertSameElements(
'Control\'s element must have expected class names', ['goog-control'],
goog.dom.classlist.get(control.getElement()));
}
/**
* Tests {@link goog.ui.Control#enableClassName}.
*/
function testEnableClassName() {
assertNull(
'Control must not have any extra class names by default',
control.getExtraClassNames());
control.enableClassName('foo', true);
assertArrayEquals(
'Control must have expected extra class names', ['foo'],
control.getExtraClassNames());
control.enableClassName('bar', true);
assertArrayEquals(
'Control must have expected extra class names', ['foo', 'bar'],
control.getExtraClassNames());
control.enableClassName('bar', true);
assertArrayEquals(
'Enabling the same class name again must be a no-op', ['foo', 'bar'],
control.getExtraClassNames());
control.enableClassName(null);
assertArrayEquals(
'Enabling null class name must be a no-op', ['foo', 'bar'],
control.getExtraClassNames());
control.enableClassName('foo', false);
assertArrayEquals(
'Control must have expected extra class names', ['bar'],
control.getExtraClassNames());
control.enableClassName('bar', false);
assertNull(
'Control must not have any extra class names',
control.getExtraClassNames());
}
/**
* Tests {@link goog.ui.Control#createDom}.
*/
function testCreateDom() {
assertNull('Control must not have a DOM by default', control.getElement());
assertFalse(
'Control must not allow text selection by default',
control.isAllowTextSelection());
assertTrue('Control must be visible by default', control.isVisible());
control.createDom();
assertNotNull('Control must have a DOM', control.getElement());
assertTrue(
'Control\'s element must be unselectable',
goog.style.isUnselectable(control.getElement()));
assertTrue(
'Control\'s element must be visible',
control.getElement().style.display != 'none');
control.setAllowTextSelection(true);
control.createDom();
assertFalse(
'Control\'s element must be selectable',
goog.style.isUnselectable(control.getElement()));
control.setVisible(false);
control.createDom();
assertTrue(
'Control\'s element must be hidden',
control.getElement().style.display == 'none');
}
/**
* Tests {@link goog.ui.Control#getContentElement}.
*/
function testGetContentElement() {
assertNull(
'Unrendered control must not have a content element',
control.getContentElement());
control.createDom();
assertEquals(
'Control\'s content element must equal its root element',
control.getElement(), control.getContentElement());
}
/**
* Tests {@link goog.ui.Control#canDecorate}.
*/
function testCanDecorate() {
assertTrue(control.canDecorate(goog.dom.createElement(goog.dom.TagName.DIV)));
}
/**
* Tests {@link goog.ui.Control#decorateInternal}.
*/
function testDecorateInternal() {
sandbox.innerHTML = '<div id="foo">Hello, <b>World</b>!</div>';
var foo = goog.dom.getElement('foo');
control.decorate(foo);
assertEquals(
'Decorated control\'s element must have expected value', foo,
control.getElement());
assertTrue(
'Element must be unselectable',
goog.style.isUnselectable(control.getElement()));
assertTrue(
'Element must be visible', control.getElement().style.display != 'none');
}
/**
* Tests {@link goog.ui.Control#decorateInternal} with a control that
* allows text selection.
*/
function testDecorateInternalForSelectableControl() {
sandbox.innerHTML = '<div id="foo">Hello, <b>World</b>!</div>';
var foo = goog.dom.getElement('foo');
control.setAllowTextSelection(true);
control.decorate(foo);
assertEquals(
'Decorated control\'s element must have expected value', foo,
control.getElement());
assertFalse(
'Element must be selectable',
goog.style.isUnselectable(control.getElement()));
assertTrue('Control must be visible', control.isVisible());
}
/**
* Tests {@link goog.ui.Control#decorateInternal} with a hidden element.
*/
function testDecorateInternalForHiddenElement() {
sandbox.innerHTML = '<div id="foo" style="display:none">Hello!</div>';
var foo = goog.dom.getElement('foo');
control.decorate(foo);
assertEquals(
'Decorated control\'s element must have expected value', foo,
control.getElement());
assertTrue(
'Element must be unselectable',
goog.style.isUnselectable(control.getElement()));
assertFalse('Control must be hidden', control.isVisible());
}
/**
* Tests {@link goog.ui.Control#enterDocument}.
*/
function testEnterDocument() {
control.render(sandbox);
assertTrue('Control must be in the document', control.isInDocument());
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9)) {
assertEquals(
'Control must have 5 mouse & 3 key event listeners on IE8', 8,
getListenerCount(control));
} else {
assertEquals(
'Control must have 4 mouse and 3 key event listeners', 7,
getListenerCount(control));
}
assertEquals(
'Control\'s key event handler must be attached to its ' +
'key event target',
control.getKeyEventTarget(), control.getKeyHandler().element_);
}
/**
* Tests {@link goog.ui.Control#enterDocument} for a control that doesn't
* handle mouse events.
*/
function testEnterDocumentForControlWithoutMouseHandling() {
control.setHandleMouseEvents(false);
control.render(sandbox);
assertTrue('Control must be in the document', control.isInDocument());
assertEquals(
'Control must have 3 key event listeners', 3, getListenerCount(control));
assertEquals(
'Control\'s key event handler must be attached to its ' +
'key event target',
control.getKeyEventTarget(), control.getKeyHandler().element_);
}
/**
* Tests {@link goog.ui.Control#enterDocument} for a control that isn't
* focusable.
*/
function testEnterDocumentForNonFocusableControl() {
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
control.render(sandbox);
assertTrue('Control must be in the document', control.isInDocument());
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9)) {
assertEquals(
'Control must have 5 mouse event listeners on IE8', 5,
getListenerCount(control));
} else {
assertEquals(
'Control must have 4 mouse event listeners', 4,
getListenerCount(control));
}
assertUndefined(
'Control must not have a key event handler', control.keyHandler_);
}
/**
* Tests {@link goog.ui.Control#enterDocument} for a control that doesn't
* need to do any event handling.
*/
function testEnterDocumentForControlWithoutEventHandlers() {
control.setHandleMouseEvents(false);
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
control.render(sandbox);
assertTrue('Control must be in the document', control.isInDocument());
assertEquals(
'Control must have 0 event listeners', 0, getListenerCount(control));
assertUndefined(
'Control must not have an event handler',
control.googUiComponentHandler_);
assertUndefined(
'Control must not have a key event handler', control.keyHandler_);
}
/**
* Tests {@link goog.ui.Control#exitDocument}.
*/
function testExitDocument() {
control.render(sandbox);
assertTrue('Control must be in the document', control.isInDocument());
if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9)) {
assertEquals(
'Control must have 5 mouse & 3 key event listeners on IE8', 8,
getListenerCount(control));
} else {
assertEquals(
'Control must have 4 mouse and 3 key event listeners', 7,
getListenerCount(control));
}
assertEquals(
'Control\'s key event handler must be attached to its ' +
'key event target',
control.getKeyEventTarget(), control.getKeyHandler().element_);
// Expected to fail on Mac Safari prior to version 527.
expectedFailures.expectFailureFor(isMacSafari3());
try {
assertTrue(
'Control\'s element must support keyboard focus',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
} catch (e) {
expectedFailures.handleException(e);
}
control.exitDocument();
assertFalse(
'Control must no longer be in the document', control.isInDocument());
assertEquals(
'Control must have no event listeners', 0, getListenerCount(control));
assertNull(
'Control\'s key event handler must be unattached',
control.getKeyHandler().element_);
assertFalse(
'Control\'s element must no longer support keyboard focus',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
}
/**
* Tests {@link goog.ui.Control#dispose}.
*/
function testDispose() {
control.render(sandbox);
var handler = control.getHandler();
var keyHandler = control.getKeyHandler();
control.dispose();
assertFalse(
'Control must no longer be in the document', control.isInDocument());
assertTrue('Control must have been disposed of', control.isDisposed());
assertUndefined('Renderer must have been deleted', control.getRenderer());
assertNull('Content must be nulled out', control.getContent());
assertTrue('Event handler must have been disposed of', handler.isDisposed());
assertUndefined(
'Event handler must have been deleted', control.googUiComponentHandler_);
assertTrue('Key handler must have been disposed of', keyHandler.isDisposed());
assertUndefined('Key handler must have been deleted', control.keyHandler_);
assertNull(
'Extra class names must have been nulled out',
control.getExtraClassNames());
}
/**
* Tests {@link goog.ui.Control#getContent}.
*/
function testGetContent() {
assertNull(
'Empty control must have null content',
(new goog.ui.Control(null)).getContent());
assertEquals(
'Control must have expected content', 'Hello', control.getContent());
control.render(sandbox);
assertEquals(
'Control must have expected content after rendering', 'Hello',
control.getContent());
}
/**
* Tests {@link goog.ui.Control#getContent}.
*/
function testGetContentForDecoratedControl() {
sandbox.innerHTML = '<div id="empty"></div>\n' +
'<div id="text">Hello, world!</div>\n' +
'<div id="element"><span>Foo</span></div>\n' +
'<div id="nodelist">Hello, <b>world</b>!</div>\n';
var empty = new goog.ui.Control(null);
empty.decorate(goog.dom.getElement('empty'));
assertNull(
'Content of control decorating empty DIV must be null',
empty.getContent());
empty.dispose();
var text = new goog.ui.Control(null);
text.decorate(goog.dom.getElement('text'));
assertEquals(
'Content of control decorating DIV with text contents ' +
'must be as expected',
'Hello, world!', text.getContent().nodeValue);
text.dispose();
var element = new goog.ui.Control(null);
element.decorate(goog.dom.getElement('element'));
assertEquals(
'Content of control decorating DIV with element child ' +
'must be as expected',
goog.dom.getElement('element').firstChild, element.getContent());
element.dispose();
var nodelist = new goog.ui.Control(null);
nodelist.decorate(goog.dom.getElement('nodelist'));
assertSameElements(
'Content of control decorating DIV with mixed ' +
'contents must be as expected',
goog.dom.getElement('nodelist').childNodes, nodelist.getContent());
nodelist.dispose();
}
/**
* Tests {@link goog.ui.Control#setAriaLabel}.
*/
function testSetAriaLabel_render() {
assertNull(
'Controls must not have any aria label by default',
control.getAriaLabel());
control.setAriaLabel('label');
assertEquals('Control must have aria label', 'label', control.getAriaLabel());
control.render(sandbox);
var elem = control.getElementStrict();
assertEquals(
'Element must have control\'s aria label after rendering', 'label',
goog.a11y.aria.getLabel(elem));
control.setAriaLabel('new label');
assertEquals(
'Element must have the new aria label', 'new label',
goog.a11y.aria.getLabel(elem));
}
/**
* Tests {@link goog.ui.Control#setAriaLabel}.
*/
function testSetAriaLabel_decorate() {
assertNull(
'Controls must not have any aria label by default',
control.getAriaLabel());
control.setAriaLabel('label');
assertEquals('Control must have aria label', 'label', control.getAriaLabel());
sandbox.innerHTML = '<div id="nodelist" role="button">' +
'Hello, <b>world</b>!</div>';
control.decorate(goog.dom.getElement('nodelist'));
var elem = control.getElementStrict();
assertEquals(
'Element must have control\'s aria label after rendering', 'label',
goog.a11y.aria.getLabel(elem));
assertEquals(
'Element must have the correct role', 'button',
elem.getAttribute('role'));
control.setAriaLabel('new label');
assertEquals(
'Element must have the new aria label', 'new label',
goog.a11y.aria.getLabel(elem));
}
/**
* Tests {@link goog.ui.Control#setContent}.
*/
function testSetContent() {
control.setContent('Bye');
assertEquals(
'Unrendered control control must have expected contents', 'Bye',
control.getContent());
assertNull('No DOM must be created by setContent', control.getElement());
control.createDom();
assertEquals(
'Rendered control\'s DOM must have expected contents', 'Bye',
control.getElement().innerHTML);
control.setContent(null);
assertNull(
'Rendered control must have expected contents', control.getContent());
assertEquals(
'Rendered control\'s DOM must have expected contents', '',
control.getElement().innerHTML);
control.setContent([
goog.dom.createDom(
goog.dom.TagName.DIV, null,
goog.dom.createDom(goog.dom.TagName.SPAN, null, 'Hello')),
'World'
]);
assertHTMLEquals(
'Control\'s DOM must be updated', '<div><span>Hello</span></div>World',
control.getElement().innerHTML);
}
/**
* Tests {@link goog.ui.Control#setContentInternal}.
*/
function testSetContentInternal() {
control.render(sandbox);
assertEquals(
'Control must have expected content after rendering', 'Hello',
control.getContent());
control.setContentInternal('Bye');
assertEquals(
'Control must have expected contents', 'Bye', control.getContent());
assertEquals(
'Control\'s DOM must be unchanged', 'Hello',
control.getElement().innerHTML);
}
/**
* Tests {@link goog.ui.Control#getCaption}.
*/
function testGetCaption() {
assertEquals(
'Empty control\'s caption must be empty string', '',
(new goog.ui.Control(null)).getCaption());
assertEquals(
'Caption must have expected value', 'Hello', control.getCaption());
sandbox.innerHTML = '<div id="nodelist">Hello, <b>world</b>!</div>';
control.decorate(goog.dom.getElement('nodelist'));
assertEquals(
'Caption must have expected value', 'Hello, world!',
control.getCaption());
var arrayContent = goog.array.clone(
goog.dom.safeHtmlToNode(
goog.html.testing.newSafeHtmlForTest(
' <b> foo</b><i> bar</i> ')).childNodes);
control.setContent(arrayContent);
assertEquals(
'whitespaces must be normalized in the caption', 'foo bar',
control.getCaption());
control.setContent('\xa0foo');
assertEquals(
'indenting spaces must be kept', '\xa0foo', control.getCaption());
}
/**
* Tests {@link goog.ui.Control#setCaption}.
*/
function testSetCaption() {
control.setCaption('Hello, world!');
assertEquals(
'Control must have a string caption "Hello, world!"', 'Hello, world!',
control.getCaption());
}
/**
* Tests {@link goog.ui.Control#setRightToLeft}.
*/
function testSetRightToLeft() {
control.createDom();
assertFalse(
'Control\'s element must not have right-to-left class',
goog.dom.classlist.contains(control.getElement(), 'goog-control-rtl'));
control.setRightToLeft(true);
assertTrue(
'Control\'s element must have right-to-left class',
goog.dom.classlist.contains(control.getElement(), 'goog-control-rtl'));
control.render(sandbox);
assertThrows(
'Changing the render direction of a control already in ' +
'the document is an error',
function() { control.setRightToLeft(false); });
}
/**
* Tests {@link goog.ui.Control#isAllowTextSelection}.
*/
function testIsAllowTextSelection() {
assertFalse(
'Controls must not allow text selection by default',
control.isAllowTextSelection());
}
/**
* Tests {@link goog.ui.Control#setAllowTextSelection}.
*/
function testSetAllowTextSelection() {
assertFalse(
'Controls must not allow text selection by default',
control.isAllowTextSelection());
control.setAllowTextSelection(true);
assertTrue(
'Control must allow text selection', control.isAllowTextSelection());
control.setAllowTextSelection(false);
assertFalse(
'Control must no longer allow text selection',
control.isAllowTextSelection());
control.render(sandbox);
assertFalse(
'Control must not allow text selection even after rendered',
control.isAllowTextSelection());
control.setAllowTextSelection(true);
assertTrue(
'Control must once again allow text selection',
control.isAllowTextSelection());
}
/**
* Tests {@link goog.ui.Control#isVisible}.
*/
function testIsVisible() {
assertTrue('Controls must be visible by default', control.isVisible());
}
/**
* Tests {@link goog.ui.Control#setVisible} before it is rendered.
*/
function testSetVisible() {
assertFalse(
'setVisible(true) must return false if already visible',
control.setVisible(true));
assertTrue('No events must have been dispatched', noEventsDispatched());
assertTrue(
'setVisible(false) must return true if previously visible',
control.setVisible(false));
assertEquals(
'One HIDE event must have been dispatched', 1,
getEventCount(control, goog.ui.Component.EventType.HIDE));
assertFalse('Control must no longer be visible', control.isVisible());
assertTrue(
'setVisible(true) must return true if previously hidden',
control.setVisible(true));
assertEquals(
'One SHOW event must have been dispatched', 1,
getEventCount(control, goog.ui.Component.EventType.SHOW));
assertTrue('Control must be visible', control.isVisible());
}
/**
* Tests {@link goog.ui.Control#setVisible} after it is rendered.
*/
function testSetVisibleForRenderedControl() {
control.render(sandbox);
assertTrue(
'No events must have been dispatched during rendering',
noEventsDispatched());
assertFalse(
'setVisible(true) must return false if already visible',
control.setVisible(true));
assertTrue('No events must have been dispatched', noEventsDispatched());
assertTrue(
'Control\'s element must be visible',
control.getElement().style.display != 'none');
assertTrue(
'setVisible(false) must return true if previously visible',
control.setVisible(false));
assertEquals(
'One HIDE event must have been dispatched', 1,
getEventCount(control, goog.ui.Component.EventType.HIDE));
assertFalse('Control must no longer be visible', control.isVisible());
assertTrue(
'Control\'s element must be hidden',
control.getElement().style.display == 'none');
assertTrue(
'setVisible(true) must return true if previously hidden',
control.setVisible(true));
assertEquals(
'One SHOW event must have been dispatched', 1,
getEventCount(control, goog.ui.Component.EventType.SHOW));
assertTrue('Control must be visible', control.isVisible());
assertTrue(
'Control\'s element must be visible',
control.getElement().style.display != 'none');
}
/**
* Tests {@link goog.ui.Control#setVisible} for disabled non-focusable
* controls.
*/
function testSetVisibleForDisabledNonFocusableControl() {
// Hidden, disabled, non-focusable control becoming visible.
control.setEnabled(false);
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
control.render(sandbox);
assertTrue('Control must be visible', control.isVisible());
assertFalse(
'Control must not have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
// Visible, disabled, non-focusable control becoming hidden.
control.getKeyEventTarget().focus();
assertEquals(
'Control must not have dispatched FOCUS', 0,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
assertFalse('Control must not have keyboard focus', control.isFocused());
control.setVisible(false);
assertFalse('Control must be hidden', control.isVisible());
assertFalse(
'Control must not have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
assertEquals(
'Control must have dispatched HIDE', 1,
getEventCount(control, goog.ui.Component.EventType.HIDE));
assertEquals(
'Control must not have dispatched BLUR', 0,
getEventCount(control, goog.ui.Component.EventType.BLUR));
}
/**
* Tests {@link goog.ui.Control#setVisible} for disabled focusable controls.
*/
function testSetVisibleForDisabledFocusableControl() {
// Hidden, disabled, focusable control becoming visible.
control.setEnabled(false);
control.setSupportedState(goog.ui.Component.State.FOCUSED, true);
control.render(sandbox);
assertTrue('Control must be visible', control.isVisible());
assertFalse(
'Control must not have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
// Visible, disabled, focusable control becoming hidden.
control.getKeyEventTarget().focus();
assertEquals(
'Control must not have dispatched FOCUS', 0,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
assertFalse('Control must not have keyboard focus', control.isFocused());
control.setVisible(false);
assertFalse('Control must be hidden', control.isVisible());
assertFalse(
'Control must not have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
assertEquals(
'Control must have dispatched HIDE', 1,
getEventCount(control, goog.ui.Component.EventType.HIDE));
assertEquals(
'Control must not have dispatched BLUR', 0,
getEventCount(control, goog.ui.Component.EventType.BLUR));
}
/**
* Tests {@link goog.ui.Control#setVisible} for enabled non-focusable
* controls.
*/
function testSetVisibleForEnabledNonFocusableControl() {
// Hidden, enabled, non-focusable control becoming visible.
control.setEnabled(true);
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
control.render(sandbox);
assertTrue('Control must be visible', control.isVisible());
assertFalse(
'Control must not have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
if (testFocus) {
// Visible, enabled, non-focusable control becoming hidden.
control.getKeyEventTarget().focus();
assertEquals(
'Control must not have dispatched FOCUS', 0,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
assertFalse('Control must not have keyboard focus', control.isFocused());
control.setVisible(false);
assertFalse('Control must be hidden', control.isVisible());
assertFalse(
'Control must not have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
assertEquals(
'Control must have dispatched HIDE', 1,
getEventCount(control, goog.ui.Component.EventType.HIDE));
assertEquals(
'Control must not have dispatched BLUR', 0,
getEventCount(control, goog.ui.Component.EventType.BLUR));
}
}
/**
* Tests {@link goog.ui.Control#setVisible} for enabled focusable controls.
*/
function testSetVisibleForEnabledFocusableControl() {
// Hidden, enabled, focusable control becoming visible.
control.setEnabled(true);
control.setSupportedState(goog.ui.Component.State.FOCUSED, true);
control.render(sandbox);
assertTrue('Control must be visible', control.isVisible());
if (testFocus) {
// Expected to fail on Mac Safari prior to version 527.
expectedFailures.expectFailureFor(isMacSafari3());
try {
// Mac Safari currently doesn't support tabIndex on arbitrary
// elements.
assertTrue(
'Control must have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
} catch (e) {
expectedFailures.handleException(e);
}
// Visible, enabled, focusable control becoming hidden.
control.getKeyEventTarget().focus();
// Expected to fail on IE.
expectedFailures.expectFailureFor(goog.userAgent.IE);
try {
// IE dispatches focus and blur events asynchronously!
assertEquals(
'Control must have dispatched FOCUS', 1,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
assertTrue('Control must have keyboard focus', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
control.setVisible(false);
assertFalse('Control must be hidden', control.isVisible());
assertFalse(
'Control must not have a tab index',
goog.dom.isFocusableTabIndex(control.getKeyEventTarget()));
assertEquals(
'Control must have dispatched HIDE', 1,
getEventCount(control, goog.ui.Component.EventType.HIDE));
// Expected to fail on IE.
expectedFailures.expectFailureFor(goog.userAgent.IE);
try {
// IE dispatches focus and blur events asynchronously!
assertEquals(
'Control must have dispatched BLUR', 1,
getEventCount(control, goog.ui.Component.EventType.BLUR));
assertFalse(
'Control must no longer have keyboard focus', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
}
}
/**
* Tests {@link goog.ui.Control#isEnabled}.
*/
function testIsEnabled() {
assertTrue('Controls must be enabled by default', control.isEnabled());
}
/**
* Tests {@link goog.ui.Control#setEnabled}.
*/
function testSetEnabled() {
control.render(sandbox);
control.setHighlighted(true);
control.setActive(true);
control.getKeyEventTarget().focus();
resetEventCount();
control.setEnabled(true);
assertTrue('No events must have been dispatched', noEventsDispatched());
assertTrue('Control must be enabled', control.isEnabled());
assertTrue('Control must be highlighted', control.isHighlighted());
assertTrue('Control must be active', control.isActive());
var elem = control.getElementStrict();
assertTrue(
'Control element must not have aria-disabled',
goog.string.isEmptyOrWhitespace(aria.getState(elem, State.DISABLED)));
assertEquals(
'Control element must have a tabIndex of 0', 0,
goog.string.toNumber(elem.getAttribute('tabIndex') || ''));
if (testFocus) {
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers
// asynchronously, and Mac Safari 3 doesn't support keyboard focus.
expectedFailures.expectFailureFor(goog.userAgent.IE);
expectedFailures.expectFailureFor(isMacSafari3());
try {
assertTrue('Control must be focused', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
}
resetEventCount();
control.setEnabled(false);
assertEquals(
'One DISABLE event must have been dispatched', 1,
getEventCount(control, goog.ui.Component.EventType.DISABLE));
assertFalse('Control must be disabled', control.isEnabled());
assertFalse('Control must not be highlighted', control.isHighlighted());
assertFalse('Control must not be active', control.isActive());
assertFalse('Control must not be focused', control.isFocused());
assertEquals(
'Control element must have aria-disabled true', 'true',
aria.getState(control.getElementStrict(), State.DISABLED));
assertNull(
'Control element must not have a tabIndex',
control.getElement().getAttribute('tabIndex'));
control.setEnabled(true);
control.exitDocument();
var cssClass = goog.getCssName(goog.ui.ControlRenderer.CSS_CLASS, 'disabled');
var element = goog.dom.createDom(goog.dom.TagName.DIV, {tabIndex: 0});
element.className = cssClass;
goog.dom.appendChild(sandbox, element);
control.decorate(element);
assertEquals(
'Control element must have aria-disabled true', 'true',
aria.getState(control.getElementStrict(), State.DISABLED));
assertNull(
'Control element must not have a tabIndex',
control.getElement().getAttribute('tabIndex'));
control.setEnabled(true);
elem = control.getElementStrict();
assertEquals(
'Control element must have aria-disabled false', 'false',
aria.getState(elem, State.DISABLED));
assertEquals(
'Control element must have tabIndex 0', 0,
goog.string.toNumber(elem.getAttribute('tabIndex') || ''));
}
/**
* Tests {@link goog.ui.Control#setState} when using
* goog.ui.Component.State.DISABLED.
*/
function testSetStateWithDisabled() {
control.render(sandbox);
control.setHighlighted(true);
control.setActive(true);
control.getKeyEventTarget().focus();
resetEventCount();
control.setState(goog.ui.Component.State.DISABLED, false);
assertTrue('No events must have been dispatched', noEventsDispatched());
assertTrue('Control must be enabled', control.isEnabled());
assertTrue('Control must be highlighted', control.isHighlighted());
assertTrue('Control must be active', control.isActive());
assertTrue(
'Control element must not have aria-disabled',
goog.string.isEmptyOrWhitespace(
aria.getState(control.getElementStrict(), State.DISABLED)));
assertEquals(
'Control element must have a tabIndex of 0', 0,
goog.string.toNumber(
control.getElement().getAttribute('tabIndex') || ''));
if (testFocus) {
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers
// asynchronously, and Mac Safari 3 doesn't support keyboard focus.
expectedFailures.expectFailureFor(goog.userAgent.IE);
expectedFailures.expectFailureFor(isMacSafari3());
try {
assertTrue('Control must be focused', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
}
resetEventCount();
control.setState(goog.ui.Component.State.DISABLED, true);
assertEquals(
'One DISABLE event must have been dispatched', 1,
getEventCount(control, goog.ui.Component.EventType.DISABLE));
assertFalse('Control must be disabled', control.isEnabled());
assertFalse('Control must not be highlighted', control.isHighlighted());
assertFalse('Control must not be active', control.isActive());
assertFalse('Control must not be focused', control.isFocused());
assertEquals(
'Control element must have aria-disabled true', 'true',
aria.getState(control.getElementStrict(), State.DISABLED));
assertNull(
'Control element must not have a tabIndex',
control.getElement().getAttribute('tabIndex'));
control.setState(goog.ui.Component.State.DISABLED, false);
control.exitDocument();
var cssClass = goog.getCssName(goog.ui.ControlRenderer.CSS_CLASS, 'disabled');
var element = goog.dom.createDom(goog.dom.TagName.DIV, {tabIndex: 0});
element.className = cssClass;
goog.dom.appendChild(sandbox, element);
control.decorate(element);
assertEquals(
'Control element must have aria-disabled true', 'true',
aria.getState(control.getElementStrict(), State.DISABLED));
assertNull(
'Control element must not have a tabIndex',
control.getElement().getAttribute('tabIndex'));
control.setState(goog.ui.Component.State.DISABLED, false);
elem = control.getElementStrict();
assertEquals(
'Control element must have aria-disabled false', 'false',
aria.getState(elem, State.DISABLED));
assertEquals(
'Control element must have tabIndex 0', 0,
goog.string.toNumber(elem.getAttribute('tabIndex') || ''));
}
/**
* Tests {@link goog.ui.Control#setEnabled} when the control has a parent.
*/
function testSetEnabledWithParent() {
var child = new goog.ui.Control(null);
child.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
control.addChild(child, true /* opt_render */);
control.setEnabled(false);
resetEventCount();
assertFalse('Parent must be disabled', control.isEnabled());
assertTrue('Child must be enabled', child.isEnabled());
child.setEnabled(false);
assertTrue(
'No events must have been dispatched when child is disabled',
noEventsDispatched());
assertTrue('Child must still be enabled', child.isEnabled());
resetEventCount();
control.setEnabled(true);
assertEquals(
'One ENABLE event must have been dispatched by the parent', 1,
getEventCount(control, goog.ui.Component.EventType.ENABLE));
assertTrue('Parent must be enabled', control.isEnabled());
assertTrue('Child must still be enabled', child.isEnabled());
resetEventCount();
child.setEnabled(false);
assertEquals(
'One DISABLE event must have been dispatched by the child', 1,
getEventCount(child, goog.ui.Component.EventType.DISABLE));
assertTrue('Parent must still be enabled', control.isEnabled());
assertFalse('Child must now be disabled', child.isEnabled());
resetEventCount();
control.setEnabled(false);
assertEquals(
'One DISABLE event must have been dispatched by the parent', 1,
getEventCount(control, goog.ui.Component.EventType.DISABLE));
assertFalse('Parent must now be disabled', control.isEnabled());
assertFalse('Child must still be disabled', child.isEnabled());
child.dispose();
}
/**
* Tests {@link goog.ui.Control#isHighlighted}.
*/
function testIsHighlighted() {
assertFalse(
'Controls must not be highlighted by default', control.isHighlighted());
}
/**
* Tests {@link goog.ui.Control#setHighlighted}.
*/
function testSetHighlighted() {
control.setSupportedState(goog.ui.Component.State.HOVER, false);
control.setHighlighted(true);
assertFalse(
'Control must not be highlighted, because it isn\'t ' +
'highlightable',
control.isHighlighted());
assertTrue(
'Control must not have dispatched any events', noEventsDispatched());
control.setSupportedState(goog.ui.Component.State.HOVER, true);
control.setHighlighted(true);
assertTrue('Control must be highlighted', control.isHighlighted());
assertEquals(
'Control must have dispatched a HIGHLIGHT event', 1,
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT));
control.setHighlighted(true);
assertTrue('Control must still be highlighted', control.isHighlighted());
assertEquals(
'Control must not dispatch more HIGHLIGHT events', 1,
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT));
control.setHighlighted(false);
assertFalse('Control must not be highlighted', control.isHighlighted());
assertEquals(
'Control must have dispatched an UNHIGHLIGHT event', 1,
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT));
control.setEnabled(false);
assertFalse('Control must be disabled', control.isEnabled());
control.setHighlighted(true);
assertTrue(
'Control must be highlighted, even when disabled',
control.isHighlighted());
assertEquals(
'Control must have dispatched another HIGHLIGHT event', 2,
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT));
}
/**
* Tests {@link goog.ui.Control#isActive}.
*/
function testIsActive() {
assertFalse('Controls must not be active by default', control.isActive());
}
/**
* Tests {@link goog.ui.Control#setActive}.
*/
function testSetActive() {
control.setSupportedState(goog.ui.Component.State.ACTIVE, false);
control.setActive(true);
assertFalse(
'Control must not be active, because it isn\'t activateable',
control.isActive());
assertTrue(
'Control must not have dispatched any events', noEventsDispatched());
control.setSupportedState(goog.ui.Component.State.ACTIVE, true);
control.setActive(true);
assertTrue('Control must be active', control.isActive());
assertEquals(
'Control must have dispatched an ACTIVATE event', 1,
getEventCount(control, goog.ui.Component.EventType.ACTIVATE));
control.setActive(true);
assertTrue('Control must still be active', control.isActive());
assertEquals(
'Control must not dispatch more ACTIVATE events', 1,
getEventCount(control, goog.ui.Component.EventType.ACTIVATE));
control.setEnabled(false);
assertFalse('Control must be disabled', control.isEnabled());
assertFalse('Control must not be active', control.isActive());
assertEquals(
'Control must have dispatched a DEACTIVATE event', 1,
getEventCount(control, goog.ui.Component.EventType.DEACTIVATE));
}
/**
* Tests disposing the control from an action event handler.
*/
function testDisposeOnAction() {
goog.events.listen(control, goog.ui.Component.EventType.ACTION, function(e) {
control.dispose();
});
// Control must not throw an exception if disposed of in an ACTION event
// handler.
control.performActionInternal();
control.setActive(true);
assertTrue('Control should have been disposed of', control.isDisposed());
}
/**
* Tests {@link goog.ui.Control#isSelected}.
*/
function testIsSelected() {
assertFalse('Controls must not be selected by default', control.isSelected());
}
/**
* Tests {@link goog.ui.Control#setSelected}.
*/
function testSetSelected() {
control.setSupportedState(goog.ui.Component.State.SELECTED, false);
control.setSelected(true);
assertFalse(
'Control must not be selected, because it isn\'t selectable',
control.isSelected());
assertTrue(
'Control must not have dispatched any events', noEventsDispatched());
control.setSupportedState(goog.ui.Component.State.SELECTED, true);
control.setSelected(true);
assertTrue('Control must be selected', control.isSelected());
assertEquals(
'Control must have dispatched a SELECT event', 1,
getEventCount(control, goog.ui.Component.EventType.SELECT));
control.setSelected(true);
assertTrue('Control must still be selected', control.isSelected());
assertEquals(
'Control must not dispatch more SELECT events', 1,
getEventCount(control, goog.ui.Component.EventType.SELECT));
control.setSelected(false);
assertFalse('Control must not be selected', control.isSelected());
assertEquals(
'Control must have dispatched an UNSELECT event', 1,
getEventCount(control, goog.ui.Component.EventType.UNSELECT));
control.setEnabled(false);
assertFalse('Control must be disabled', control.isEnabled());
control.setSelected(true);
assertTrue(
'Control must be selected, even when disabled', control.isSelected());
assertEquals(
'Control must have dispatched another SELECT event', 2,
getEventCount(control, goog.ui.Component.EventType.SELECT));
}
/**
* Tests {@link goog.ui.Control#isChecked}.
*/
function testIsChecked() {
assertFalse('Controls must not be checked by default', control.isChecked());
}
/**
* Tests {@link goog.ui.Control#setChecked}.
*/
function testSetChecked() {
control.setSupportedState(goog.ui.Component.State.CHECKED, false);
control.setChecked(true);
assertFalse(
'Control must not be checked, because it isn\'t checkable',
control.isChecked());
assertTrue(
'Control must not have dispatched any events', noEventsDispatched());
control.setSupportedState(goog.ui.Component.State.CHECKED, true);
control.setChecked(true);
assertTrue('Control must be checked', control.isChecked());
assertEquals(
'Control must have dispatched a CHECK event', 1,
getEventCount(control, goog.ui.Component.EventType.CHECK));
control.setChecked(true);
assertTrue('Control must still be checked', control.isChecked());
assertEquals(
'Control must not dispatch more CHECK events', 1,
getEventCount(control, goog.ui.Component.EventType.CHECK));
control.setChecked(false);
assertFalse('Control must not be checked', control.isChecked());
assertEquals(
'Control must have dispatched an UNCHECK event', 1,
getEventCount(control, goog.ui.Component.EventType.UNCHECK));
control.setEnabled(false);
assertFalse('Control must be disabled', control.isEnabled());
control.setChecked(true);
assertTrue(
'Control must be checked, even when disabled', control.isChecked());
assertEquals(
'Control must have dispatched another CHECK event', 2,
getEventCount(control, goog.ui.Component.EventType.CHECK));
}
/**
* Tests {@link goog.ui.Control#isFocused}.
*/
function testIsFocused() {
assertFalse('Controls must not be focused by default', control.isFocused());
}
/**
* Tests {@link goog.ui.Control#setFocused}.
*/
function testSetFocused() {
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
control.setFocused(true);
assertFalse(
'Control must not be focused, because it isn\'t focusable',
control.isFocused());
assertTrue(
'Control must not have dispatched any events', noEventsDispatched());
control.setSupportedState(goog.ui.Component.State.FOCUSED, true);
control.setFocused(true);
assertTrue('Control must be focused', control.isFocused());
assertEquals(
'Control must have dispatched a FOCUS event', 1,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
control.setFocused(true);
assertTrue('Control must still be focused', control.isFocused());
assertEquals(
'Control must not dispatch more FOCUS events', 1,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
control.setFocused(false);
assertFalse('Control must not be focused', control.isFocused());
assertEquals(
'Control must have dispatched an BLUR event', 1,
getEventCount(control, goog.ui.Component.EventType.BLUR));
control.setEnabled(false);
assertFalse('Control must be disabled', control.isEnabled());
control.setFocused(true);
assertTrue(
'Control must be focused, even when disabled', control.isFocused());
assertEquals(
'Control must have dispatched another FOCUS event', 2,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
}
/**
* Tests {@link goog.ui.Control#isOpen}.
*/
function testIsOpen() {
assertFalse('Controls must not be open by default', control.isOpen());
}
/**
* Tests {@link goog.ui.Control#setOpen}.
*/
function testSetOpen() {
control.setSupportedState(goog.ui.Component.State.OPENED, false);
control.setOpen(true);
assertFalse(
'Control must not be opened, because it isn\'t openable',
control.isOpen());
assertTrue(
'Control must not have dispatched any events', noEventsDispatched());
control.setSupportedState(goog.ui.Component.State.OPENED, true);
control.setOpen(true);
assertTrue('Control must be opened', control.isOpen());
assertEquals(
'Control must have dispatched a OPEN event', 1,
getEventCount(control, goog.ui.Component.EventType.OPEN));
control.setOpen(true);
assertTrue('Control must still be opened', control.isOpen());
assertEquals(
'Control must not dispatch more OPEN events', 1,
getEventCount(control, goog.ui.Component.EventType.OPEN));
control.setOpen(false);
assertFalse('Control must not be opened', control.isOpen());
assertEquals(
'Control must have dispatched an CLOSE event', 1,
getEventCount(control, goog.ui.Component.EventType.CLOSE));
control.setEnabled(false);
assertFalse('Control must be disabled', control.isEnabled());
control.setOpen(true);
assertTrue('Control must be opened, even when disabled', control.isOpen());
assertEquals(
'Control must have dispatched another OPEN event', 2,
getEventCount(control, goog.ui.Component.EventType.OPEN));
}
/**
* Tests {@link goog.ui.Control#getState}.
*/
function testGetState() {
assertEquals(
'Controls must be in the default state', 0x00, control.getState());
}
/**
* Tests {@link goog.ui.Control#hasState}.
*/
function testHasState() {
assertFalse(
'Control must not be disabled',
control.hasState(goog.ui.Component.State.DISABLED));
assertFalse(
'Control must not be in the HOVER state',
control.hasState(goog.ui.Component.State.HOVER));
assertFalse(
'Control must not be active',
control.hasState(goog.ui.Component.State.ACTIVE));
assertFalse(
'Control must not be selected',
control.hasState(goog.ui.Component.State.SELECTED));
assertFalse(
'Control must not be checked',
control.hasState(goog.ui.Component.State.CHECKED));
assertFalse(
'Control must not be focused',
control.hasState(goog.ui.Component.State.FOCUSED));
assertFalse(
'Control must not be open',
control.hasState(goog.ui.Component.State.OPEN));
}
/**
* Tests {@link goog.ui.Control#setState}.
*/
function testSetState() {
control.createDom();
control.setSupportedState(goog.ui.Component.State.ACTIVE, false);
assertFalse(
'Control must not be active',
control.hasState(goog.ui.Component.State.ACTIVE));
control.setState(goog.ui.Component.State.ACTIVE, true);
assertFalse(
'Control must still be inactive (because it doesn\'t ' +
'support the ACTIVE state)',
control.hasState(goog.ui.Component.State.ACTIVE));
control.setSupportedState(goog.ui.Component.State.ACTIVE, true);
control.setState(goog.ui.Component.State.ACTIVE, true);
assertTrue(
'Control must be active',
control.hasState(goog.ui.Component.State.ACTIVE));
assertTrue(
'Control must have the active CSS style',
goog.dom.classlist.contains(control.getElement(), 'goog-control-active'));
control.setState(goog.ui.Component.State.ACTIVE, true);
assertTrue(
'Control must still be active',
control.hasState(goog.ui.Component.State.ACTIVE));
assertTrue(
'Control must still have the active CSS style',
goog.dom.classlist.contains(control.getElement(), 'goog-control-active'));
assertTrue('No events must have been dispatched', noEventsDispatched());
}
/**
* Tests {@link goog.ui.Control#setStateInternal}.
*/
function testSetStateInternal() {
control.setStateInternal(0x00);
assertEquals('State should be 0x00', 0x00, control.getState());
control.setStateInternal(0x17);
assertEquals('State should be 0x17', 0x17, control.getState());
}
/**
* Tests {@link goog.ui.Control#isSupportedState}.
*/
function testIsSupportedState() {
assertTrue(
'Control must support DISABLED',
control.isSupportedState(goog.ui.Component.State.DISABLED));
assertTrue(
'Control must support HOVER',
control.isSupportedState(goog.ui.Component.State.HOVER));
assertTrue(
'Control must support ACTIVE',
control.isSupportedState(goog.ui.Component.State.ACTIVE));
assertTrue(
'Control must support FOCUSED',
control.isSupportedState(goog.ui.Component.State.FOCUSED));
assertFalse(
'Control must no support SELECTED',
control.isSupportedState(goog.ui.Component.State.SELECTED));
assertFalse(
'Control must no support CHECKED',
control.isSupportedState(goog.ui.Component.State.CHECKED));
assertFalse(
'Control must no support OPENED',
control.isSupportedState(goog.ui.Component.State.OPENED));
}
/**
* Tests {@link goog.ui.Control#setSupportedState}.
*/
function testSetSupportedState() {
control.setSupportedState(goog.ui.Component.State.HOVER, true);
assertTrue(
'Control must still support HOVER',
control.isSupportedState(goog.ui.Component.State.HOVER));
control.setSupportedState(goog.ui.Component.State.HOVER, false);
assertFalse(
'Control must no longer support HOVER',
control.isSupportedState(goog.ui.Component.State.HOVER));
control.setState(goog.ui.Component.State.ACTIVE, true);
control.setSupportedState(goog.ui.Component.State.ACTIVE, false);
assertFalse(
'Control must no longer support ACTIVE',
control.isSupportedState(goog.ui.Component.State.ACTIVE));
assertFalse(
'Control must no longer be in the ACTIVE state',
control.hasState(goog.ui.Component.State.ACTIVE));
control.render(sandbox);
control.setSupportedState(goog.ui.Component.State.FOCUSED, true);
control.setState(goog.ui.Component.State.FOCUSED, true);
assertThrows(
'Must not be able to disable support for the FOCUSED ' +
"state for a control that's already in the document and focused",
function() {
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
});
assertTrue('No events must have been dispatched', noEventsDispatched());
}
/**
* Tests {@link goog.ui.Control#isAutoState}.
*/
function testIsAutoState() {
assertTrue(
'Control must have DISABLED as an auto-state',
control.isAutoState(goog.ui.Component.State.DISABLED));
assertTrue(
'Control must have HOVER as an auto-state',
control.isAutoState(goog.ui.Component.State.HOVER));
assertTrue(
'Control must have ACTIVE as an auto-state',
control.isAutoState(goog.ui.Component.State.ACTIVE));
assertTrue(
'Control must have FOCUSED as an auto-state',
control.isAutoState(goog.ui.Component.State.FOCUSED));
assertFalse(
'Control must not have SELECTED as an auto-state',
control.isAutoState(goog.ui.Component.State.SELECTED));
assertFalse(
'Control must not have CHECKED as an auto-state',
control.isAutoState(goog.ui.Component.State.CHECKED));
assertFalse(
'Control must not have OPENED as an auto-state',
control.isAutoState(goog.ui.Component.State.OPENED));
assertTrue('No events must have been dispatched', noEventsDispatched());
}
/**
* Tests {@link goog.ui.Control#setAutoStates}.
*/
function testSetAutoStates() {
control.setAutoStates(goog.ui.Component.State.HOVER, false);
assertFalse(
'Control must not have HOVER as an auto-state',
control.isAutoState(goog.ui.Component.State.HOVER));
control.setAutoStates(
goog.ui.Component.State.ACTIVE | goog.ui.Component.State.FOCUSED, false);
assertFalse(
'Control must not have ACTIVE as an auto-state',
control.isAutoState(goog.ui.Component.State.ACTIVE));
assertFalse(
'Control must not have FOCUSED as an auto-state',
control.isAutoState(goog.ui.Component.State.FOCUSED));
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
control.setAutoStates(goog.ui.Component.State.FOCUSED, true);
assertFalse(
'Control must not have FOCUSED as an auto-state if it no ' +
'longer supports FOCUSED',
control.isAutoState(goog.ui.Component.State.FOCUSED));
assertTrue('No events must have been dispatched', noEventsDispatched());
}
/**
* Tests {@link goog.ui.Control#isDispatchTransitionEvents}.
*/
function testIsDispatchTransitionEvents() {
assertTrue(
'Control must dispatch DISABLED transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.DISABLED));
assertTrue(
'Control must dispatch HOVER transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.HOVER));
assertTrue(
'Control must dispatch ACTIVE transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.ACTIVE));
assertTrue(
'Control must dispatch FOCUSED transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.FOCUSED));
assertFalse(
'Control must not dispatch SELECTED transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.SELECTED));
assertFalse(
'Control must not dispatch CHECKED transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.CHECKED));
assertFalse(
'Control must not dispatch OPENED transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.OPENED));
assertTrue('No events must have been dispatched', noEventsDispatched());
}
/**
* Tests {@link goog.ui.Control#setDispatchTransitionEvents}.
*/
function testSetDispatchTransitionEvents() {
control.setDispatchTransitionEvents(goog.ui.Component.State.HOVER, false);
assertFalse(
'Control must not dispatch HOVER transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.HOVER));
control.setSupportedState(goog.ui.Component.State.SELECTED, true);
control.setDispatchTransitionEvents(goog.ui.Component.State.SELECTED, true);
assertTrue(
'Control must dispatch SELECTED transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.SELECTED));
assertTrue('No events must have been dispatched', noEventsDispatched());
}
/**
* Tests {@link goog.ui.Control#isTransitionAllowed}.
*/
function testIsTransitionAllowed() {
assertTrue(
'Control must support the HOVER state',
control.isSupportedState(goog.ui.Component.State.HOVER));
assertFalse(
'Control must not be in the HOVER state',
control.hasState(goog.ui.Component.State.HOVER));
assertTrue(
'Control must dispatch HOVER transition events',
control.isDispatchTransitionEvents(goog.ui.Component.State.HOVER));
assertTrue(
'Control must be allowed to transition to the HOVER state',
control.isTransitionAllowed(goog.ui.Component.State.HOVER, true));
assertEquals(
'Control must have dispatched one HIGHLIGHT event', 1,
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT));
assertFalse(
'Control must not be highlighted',
control.hasState(goog.ui.Component.State.HOVER));
control.setState(goog.ui.Component.State.HOVER, true);
control.setDispatchTransitionEvents(goog.ui.Component.State.HOVER, false);
assertTrue(
'Control must be allowed to transition from the HOVER state',
control.isTransitionAllowed(goog.ui.Component.State.HOVER, false));
assertEquals(
'Control must not have dispatched any UNHIGHLIGHT events', 0,
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT));
assertTrue(
'Control must still be highlighted',
control.hasState(goog.ui.Component.State.HOVER));
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
resetEventCount();
assertFalse(
'Control doesn\'t support the FOCUSED state',
control.isSupportedState(goog.ui.Component.State.FOCUSED));
assertFalse(
'Control must not be FOCUSED',
control.hasState(goog.ui.Component.State.FOCUSED));
assertFalse(
'Control must not be allowed to transition to the FOCUSED ' +
'state',
control.isTransitionAllowed(goog.ui.Component.State.FOCUSED, true));
assertEquals(
'Control must not have dispatched any FOCUS events', 0,
getEventCount(control, goog.ui.Component.EventType.FOCUS));
control.setEnabled(false);
resetEventCount();
assertTrue(
'Control must support the DISABLED state',
control.isSupportedState(goog.ui.Component.State.DISABLED));
assertTrue(
'Control must be DISABLED',
control.hasState(goog.ui.Component.State.DISABLED));
assertFalse(
'Control must not be allowed to transition to the DISABLED ' +
'state, because it is already there',
control.isTransitionAllowed(goog.ui.Component.State.DISABLED, true));
assertEquals(
'Control must not have dispatched any ENABLE events', 0,
getEventCount(control, goog.ui.Component.EventType.ENABLE));
}
/**
* Tests {@link goog.ui.Control#handleKeyEvent}.
*/
function testHandleKeyEvent() {
control.render();
control.isVisible = control.isEnabled = function() { return true; };
goog.testing.events.fireKeySequence(
control.getKeyEventTarget(), goog.events.KeyCodes.A);
assertEquals(
'Control must not have dispatched an ACTION event', 0,
getEventCount(control, goog.ui.Component.EventType.ACTION));
goog.testing.events.fireKeySequence(
control.getKeyEventTarget(), goog.events.KeyCodes.ENTER);
assertEquals(
'Control must have dispatched an ACTION event', 1,
getEventCount(control, goog.ui.Component.EventType.ACTION));
}
/**
* Tests {@link goog.ui.Control#performActionInternal}.
*/
function testPerformActionInternal() {
assertFalse('Control must not be checked', control.isChecked());
assertFalse('Control must not be selected', control.isSelected());
assertFalse('Control must not be open', control.isOpen());
control.performActionInternal();
assertFalse('Control must not be checked', control.isChecked());
assertFalse('Control must not be selected', control.isSelected());
assertFalse('Control must not be open', control.isOpen());
assertEquals(
'Control must have dispatched an ACTION event', 1,
getEventCount(control, goog.ui.Component.EventType.ACTION));
control.setSupportedState(goog.ui.Component.State.CHECKED, true);
control.setSupportedState(goog.ui.Component.State.SELECTED, true);
control.setSupportedState(goog.ui.Component.State.OPENED, true);
control.performActionInternal();
assertTrue('Control must be checked', control.isChecked());
assertTrue('Control must be selected', control.isSelected());
assertTrue('Control must be open', control.isOpen());
assertEquals(
'Control must have dispatched a CHECK event', 1,
getEventCount(control, goog.ui.Component.EventType.CHECK));
assertEquals(
'Control must have dispatched a SELECT event', 1,
getEventCount(control, goog.ui.Component.EventType.SELECT));
assertEquals(
'Control must have dispatched a OPEN event', 1,
getEventCount(control, goog.ui.Component.EventType.OPEN));
assertEquals(
'Control must have dispatched another ACTION event', 2,
getEventCount(control, goog.ui.Component.EventType.ACTION));
control.performActionInternal();
assertFalse('Control must not be checked', control.isChecked());
assertTrue('Control must be selected', control.isSelected());
assertFalse('Control must not be open', control.isOpen());
assertEquals(
'Control must have dispatched an UNCHECK event', 1,
getEventCount(control, goog.ui.Component.EventType.UNCHECK));
assertEquals(
'Control must not have dispatched an UNSELECT event', 0,
getEventCount(control, goog.ui.Component.EventType.UNSELECT));
assertEquals(
'Control must have dispatched a CLOSE event', 1,
getEventCount(control, goog.ui.Component.EventType.CLOSE));
assertEquals(
'Control must have dispatched another ACTION event', 3,
getEventCount(control, goog.ui.Component.EventType.ACTION));
}
/**
* Tests {@link goog.ui.Control#handleMouseOver}.
*/
function testHandleMouseOver() {
control.setContent(
goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'caption'}, 'Hello'));
control.render(sandbox);
var element = control.getElement();
var caption = goog.dom.getElement('caption');
// Verify baseline assumptions.
assertTrue(
'Caption must be contained within the control',
goog.dom.contains(element, caption));
assertTrue('Control must be enabled', control.isEnabled());
assertTrue(
'HOVER must be an auto-state',
control.isAutoState(goog.ui.Component.State.HOVER));
assertFalse(
'Control must not start out highlighted', control.isHighlighted());
// Scenario 1: relatedTarget is contained within the control's DOM.
goog.testing.events.fireMouseOverEvent(element, caption);
assertTrue(
'No events must have been dispatched for internal mouse move',
noEventsDispatched());
assertFalse(
'Control must not be highlighted for internal mouse move',
control.isHighlighted());
resetEventCount();
// Scenario 2: preventDefault() is called on the ENTER event.
var key = goog.events.listen(
control, goog.ui.Component.EventType.ENTER,
function(e) { e.preventDefault(); });
goog.testing.events.fireMouseOverEvent(element, sandbox);
assertEquals(
'Control must have dispatched 1 ENTER event', 1,
getEventCount(control, goog.ui.Component.EventType.ENTER));
assertFalse(
'Control must not be highlighted if ENTER is canceled',
control.isHighlighted());
goog.events.unlistenByKey(key);
resetEventCount();
// Scenario 3: Control is disabled.
control.setEnabled(false);
goog.testing.events.fireMouseOverEvent(element, sandbox);
assertEquals(
'Control must dispatch ENTER event on mouseover even if ' +
'disabled',
1, getEventCount(control, goog.ui.Component.EventType.ENTER));
assertFalse(
'Control must not be highlighted if it is disabled',
control.isHighlighted());
control.setEnabled(true);
resetEventCount();
// Scenario 4: HOVER is not an auto-state.
control.setAutoStates(goog.ui.Component.State.HOVER, false);
goog.testing.events.fireMouseOverEvent(element, sandbox);
assertEquals(
'Control must dispatch ENTER event on mouseover even if ' +
'HOVER is not an auto-state',
1, getEventCount(control, goog.ui.Component.EventType.ENTER));
assertFalse(
'Control must not be highlighted if HOVER isn\'t an auto-' +
'state',
control.isHighlighted());
control.setAutoStates(goog.ui.Component.State.HOVER, true);
resetEventCount();
// Scenario 5: All is well.
goog.testing.events.fireMouseOverEvent(element, sandbox);
assertEquals(
'Control must dispatch ENTER event on mouseover', 1,
getEventCount(control, goog.ui.Component.EventType.ENTER));
assertEquals(
'Control must dispatch HIGHLIGHT event on mouseover', 1,
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT));
assertTrue('Control must be highlighted', control.isHighlighted());
resetEventCount();
// Scenario 6: relatedTarget is null
control.setHighlighted(false);
goog.testing.events.fireMouseOverEvent(element, null);
assertEquals(
'Control must dispatch ENTER event on mouseover', 1,
getEventCount(control, goog.ui.Component.EventType.ENTER));
assertEquals(
'Control must dispatch HIGHLIGHT event on mouseover', 1,
getEventCount(control, goog.ui.Component.EventType.HIGHLIGHT));
assertTrue('Control must be highlighted', control.isHighlighted());
resetEventCount();
}
/**
* Tests {@link goog.ui.Control#handleMouseOut}.
*/
function testHandleMouseOut() {
control.setContent(
goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'caption'}, 'Hello'));
control.setHighlighted(true);
control.setActive(true);
resetEventCount();
control.render(sandbox);
var element = control.getElement();
var caption = goog.dom.getElement('caption');
// Verify baseline assumptions.
assertTrue(
'Caption must be contained within the control',
goog.dom.contains(element, caption));
assertTrue('Control must be enabled', control.isEnabled());
assertTrue(
'HOVER must be an auto-state',
control.isAutoState(goog.ui.Component.State.HOVER));
assertTrue(
'ACTIVE must be an auto-state',
control.isAutoState(goog.ui.Component.State.ACTIVE));
assertTrue('Control must start out highlighted', control.isHighlighted());
assertTrue('Control must start out active', control.isActive());
// Scenario 1: relatedTarget is contained within the control's DOM.
goog.testing.events.fireMouseOutEvent(element, caption);
assertTrue(
'No events must have been dispatched for internal mouse move',
noEventsDispatched());
assertTrue(
'Control must not be un-highlighted for internal mouse move',
control.isHighlighted());
assertTrue(
'Control must not be deactivated for internal mouse move',
control.isActive());
resetEventCount();
// Scenario 2: preventDefault() is called on the LEAVE event.
var key = goog.events.listen(
control, goog.ui.Component.EventType.LEAVE,
function(e) { e.preventDefault(); });
goog.testing.events.fireMouseOutEvent(element, sandbox);
assertEquals(
'Control must have dispatched 1 LEAVE event', 1,
getEventCount(control, goog.ui.Component.EventType.LEAVE));
assertTrue(
'Control must not be un-highlighted if LEAVE is canceled',
control.isHighlighted());
assertTrue(
'Control must not be deactivated if LEAVE is canceled',
control.isActive());
goog.events.unlistenByKey(key);
resetEventCount();
// Scenario 3: ACTIVE is not an auto-state.
control.setAutoStates(goog.ui.Component.State.ACTIVE, false);
goog.testing.events.fireMouseOutEvent(element, sandbox);
assertEquals(
'Control must dispatch LEAVE event on mouseout even if ' +
'ACTIVE is not an auto-state',
1, getEventCount(control, goog.ui.Component.EventType.LEAVE));
assertTrue(
'Control must not be deactivated if ACTIVE isn\'t an auto-' +
'state',
control.isActive());
assertFalse(
'Control must be un-highlighted even if ACTIVE isn\'t an ' +
'auto-state',
control.isHighlighted());
control.setAutoStates(goog.ui.Component.State.ACTIVE, true);
control.setHighlighted(true);
resetEventCount();
// Scenario 4: HOVER is not an auto-state.
control.setAutoStates(goog.ui.Component.State.HOVER, false);
goog.testing.events.fireMouseOutEvent(element, sandbox);
assertEquals(
'Control must dispatch LEAVE event on mouseout even if ' +
'HOVER is not an auto-state',
1, getEventCount(control, goog.ui.Component.EventType.LEAVE));
assertFalse(
'Control must be deactivated even if HOVER isn\'t an auto-' +
'state',
control.isActive());
assertTrue(
'Control must not be un-highlighted if HOVER isn\'t an auto-' +
'state',
control.isHighlighted());
control.setAutoStates(goog.ui.Component.State.HOVER, true);
control.setActive(true);
resetEventCount();
// Scenario 5: All is well.
goog.testing.events.fireMouseOutEvent(element, sandbox);
assertEquals(
'Control must dispatch LEAVE event on mouseout', 1,
getEventCount(control, goog.ui.Component.EventType.LEAVE));
assertEquals(
'Control must dispatch DEACTIVATE event on mouseout', 1,
getEventCount(control, goog.ui.Component.EventType.DEACTIVATE));
assertEquals(
'Control must dispatch UNHIGHLIGHT event on mouseout', 1,
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT));
assertFalse('Control must be deactivated', control.isActive());
assertFalse('Control must be unhighlighted', control.isHighlighted());
resetEventCount();
// Scenario 6: relatedTarget is null
control.setActive(true);
control.setHighlighted(true);
goog.testing.events.fireMouseOutEvent(element, null);
assertEquals(
'Control must dispatch LEAVE event on mouseout', 1,
getEventCount(control, goog.ui.Component.EventType.LEAVE));
assertEquals(
'Control must dispatch DEACTIVATE event on mouseout', 1,
getEventCount(control, goog.ui.Component.EventType.DEACTIVATE));
assertEquals(
'Control must dispatch UNHIGHLIGHT event on mouseout', 1,
getEventCount(control, goog.ui.Component.EventType.UNHIGHLIGHT));
assertFalse('Control must be deactivated', control.isActive());
assertFalse('Control must be unhighlighted', control.isHighlighted());
resetEventCount();
}
function testIsMouseEventWithinElement() {
var child = goog.dom.createElement(goog.dom.TagName.DIV);
var parent = goog.dom.createDom(goog.dom.TagName.DIV, null, child);
var notChild = goog.dom.createElement(goog.dom.TagName.DIV);
var event = new goog.testing.events.Event('mouseout');
event.relatedTarget = child;
assertTrue(
'Event is within element',
goog.ui.Control.isMouseEventWithinElement_(event, parent));
var event = new goog.testing.events.Event('mouseout');
event.relatedTarget = notChild;
assertFalse(
'Event is not within element',
goog.ui.Control.isMouseEventWithinElement_(event, parent));
}
function testHandleMouseDown() {
control.render(sandbox);
assertFalse(
'preventDefault() must have been called for control that ' +
'doesn\'t support text selection',
fireMouseDownAndFocus(control.getElement()));
assertTrue('Control must be highlighted', control.isHighlighted());
assertTrue('Control must be active', control.isActive());
if (testFocus) {
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers
// asynchronously, and Mac Safari 3 doesn't support keyboard focus.
expectedFailures.expectFailureFor(goog.userAgent.IE);
expectedFailures.expectFailureFor(isMacSafari3());
try {
assertTrue('Control must be focused', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
}
}
function testHandleMouseDownForDisabledControl() {
control.setEnabled(false);
control.render(sandbox);
assertFalse(
'preventDefault() must have been called for control that ' +
'doesn\'t support text selection',
fireMouseDownAndFocus(control.getElement()));
assertFalse('Control must not be highlighted', control.isHighlighted());
assertFalse('Control must not be active', control.isActive());
if (testFocus) {
assertFalse('Control must not be focused', control.isFocused());
}
}
function testHandleMouseDownForNoHoverAutoState() {
control.setAutoStates(goog.ui.Component.State.HOVER, false);
control.render(sandbox);
assertFalse(
'preventDefault() must have been called for control that ' +
'doesn\'t support text selection',
fireMouseDownAndFocus(control.getElement()));
assertFalse('Control must not be highlighted', control.isHighlighted());
assertTrue('Control must be active', control.isActive());
if (testFocus) {
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers
// asynchronously, and Mac Safari 3 doesn't support keyboard focus.
expectedFailures.expectFailureFor(goog.userAgent.IE);
expectedFailures.expectFailureFor(isMacSafari3());
try {
assertTrue('Control must be focused', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
}
}
function testHandleMouseDownForRightMouseButton() {
control.render(sandbox);
assertTrue(
'preventDefault() must not have been called for right ' +
'mouse button',
fireMouseDownAndFocus(
control.getElement(), goog.events.BrowserEvent.MouseButton.RIGHT));
assertTrue('Control must be highlighted', control.isHighlighted());
assertFalse('Control must not be active', control.isActive());
if (testFocus) {
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers
// asynchronously, and Mac Safari 3 doesn't support keyboard focus.
expectedFailures.expectFailureFor(goog.userAgent.IE);
expectedFailures.expectFailureFor(isMacSafari3());
try {
assertTrue('Control must be focused', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
}
}
function testHandleMouseDownForNoActiveAutoState() {
control.setAutoStates(goog.ui.Component.State.ACTIVE, false);
control.render(sandbox);
assertFalse(
'preventDefault() must have been called for control that ' +
'doesn\'t support text selection',
fireMouseDownAndFocus(control.getElement()));
assertTrue('Control must be highlighted', control.isHighlighted());
assertFalse('Control must not be active', control.isActive());
if (testFocus) {
// Expected to fail on IE and Mac Safari 3. IE calls focus handlers
// asynchronously, and Mac Safari 3 doesn't support keyboard focus.
expectedFailures.expectFailureFor(goog.userAgent.IE);
expectedFailures.expectFailureFor(isMacSafari3());
try {
assertTrue('Control must be focused', control.isFocused());
} catch (e) {
expectedFailures.handleException(e);
}
}
}
function testHandleMouseDownForNonFocusableControl() {
control.setSupportedState(goog.ui.Component.State.FOCUSED, false);
control.render(sandbox);
assertFalse(
'preventDefault() must have been called for control that ' +
'doesn\'t support text selection',
fireMouseDownAndFocus(control.getElement()));
assertTrue('Control must be highlighted', control.isHighlighted());
assertTrue('Control must be active', control.isActive());
assertFalse('Control must not be focused', control.isFocused());
}
// TODO(attila): Find out why this is flaky on FF2/Linux and FF1.5/Win.
// function testHandleMouseDownForSelectableControl() {
// control.setAllowTextSelection(true);
// control.render(sandbox);
// assertTrue('preventDefault() must not have been called for control ' +
// 'that supports text selection',
// fireMouseDownAndFocus(control.getElement()));
// assertTrue('Control must be highlighted', control.isHighlighted());
// assertTrue('Control must be active', control.isActive());
// // Expected to fail on IE and Mac Safari 3. IE calls focus handlers
// // asynchronously, and Mac Safari 3 doesn't support keyboard focus.
// expectedFailures.expectFailureFor(goog.userAgent.IE);
// expectedFailures.expectFailureFor(isMacSafari3());
// try {
// assertTrue('Control must be focused', control.isFocused());
// } catch (e) {
// expectedFailures.handleException(e);
// }
//}
/**
* Tests {@link goog.ui.Control#handleMouseUp}.
*/
function testHandleMouseUp() {
control.setActive(true);
// Override performActionInternal() for testing purposes.
var actionPerformed = false;
control.performActionInternal = function() {
actionPerformed = true;
return true;
};
resetEventCount();
control.render(sandbox);
var element = control.getElement();
// Verify baseline assumptions.
assertTrue('Control must be enabled', control.isEnabled());
assertTrue(
'HOVER must be an auto-state',
control.isAutoState(goog.ui.Component.State.HOVER));
assertTrue(
'ACTIVE must be an auto-state',
control.isAutoState(goog.ui.Component.State.ACTIVE));
assertFalse(
'Control must not start out highlighted', control.isHighlighted());
assertTrue('Control must start out active', control.isActive());
// Scenario 1: Control is disabled.
control.setEnabled(false);
goog.testing.events.fireMouseUpEvent(element);
assertFalse(
'Disabled control must not highlight on mouseup',
control.isHighlighted());
assertFalse('No action must have been performed', actionPerformed);
control.setActive(true);
control.setEnabled(true);
// Scenario 2: HOVER is not an auto-state.
control.setAutoStates(goog.ui.Component.State.HOVER, false);
goog.testing.events.fireMouseUpEvent(element);
assertFalse(
'Control must not highlight on mouseup if HOVER isn\'t an ' +
'auto-state',
control.isHighlighted());
assertTrue(
'Action must have been performed even if HOVER isn\'t an ' +
'auto-state',
actionPerformed);
assertFalse(
'Control must have been deactivated on mouseup even if ' +
'HOVER isn\'t an auto-state',
control.isActive());
actionPerformed = false;
control.setActive(true);
control.setAutoStates(goog.ui.Component.State.HOVER, true);
// Scenario 3: Control is not active.
control.setActive(false);
goog.testing.events.fireMouseUpEvent(element);
assertTrue(
'Control must highlight on mouseup, even if inactive',
control.isHighlighted());
assertFalse(
'No action must have been performed if control is inactive',
actionPerformed);
assertFalse(
'Inactive control must remain inactive after mouseup',
control.isActive());
control.setHighlighted(false);
control.setActive(true);
// Scenario 4: performActionInternal() returns false.
control.performActionInternal = function() {
actionPerformed = true;
return false;
};
goog.testing.events.fireMouseUpEvent(element);
assertTrue(
'Control must highlight on mouseup, even if no action is ' +
'performed',
control.isHighlighted());
assertTrue('performActionInternal must have been called', actionPerformed);
assertTrue(
'Control must not deactivate if performActionInternal ' +
'returns false',
control.isActive());
control.setHighlighted(false);
actionPerformed = false;
control.performActionInternal = function() {
actionPerformed = true;
return true;
};
// Scenario 5: ACTIVE is not an auto-state.
control.setAutoStates(goog.ui.Component.State.ACTIVE, false);
goog.testing.events.fireMouseUpEvent(element);
assertTrue(
'Control must highlight on mouseup even if ACTIVE isn\'t an ' +
'auto-state',
control.isHighlighted());
assertTrue(
'Action must have been performed even if ACTIVE isn\'t an ' +
'auto-state',
actionPerformed);
assertTrue(
'Control must not have been deactivated on mouseup if ' +
'ACTIVE isn\'t an auto-state',
control.isActive());
actionPerformed = false;
control.setHighlighted(false);
control.setAutoStates(goog.ui.Component.State.ACTIVE, true);
// Scenario 6: All is well.
goog.testing.events.fireMouseUpEvent(element);
assertTrue('Control must highlight on mouseup', control.isHighlighted());
assertTrue('Action must have been performed', actionPerformed);
assertFalse('Control must have been deactivated', control.isActive());
}
function testDefaultConstructor() {
var control = new goog.ui.Control();
assertNull(control.getContent());
}
function assertClickSequenceFires(msg) {
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION);
goog.testing.events.fireClickSequence(control.getKeyEventTarget());
assertEquals(
msg, actionCount + 1,
getEventCount(control, goog.ui.Component.EventType.ACTION));
}
function assertIsolatedClickFires(msg) {
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION);
goog.testing.events.fireClickEvent(control.getKeyEventTarget());
assertEquals(
msg, actionCount + 1,
getEventCount(control, goog.ui.Component.EventType.ACTION));
}
function assertIsolatedClickDoesNotFire(msg) {
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION);
goog.testing.events.fireClickEvent(control.getKeyEventTarget());
assertEquals(
msg, actionCount,
getEventCount(control, goog.ui.Component.EventType.ACTION));
}
function testIeMouseEventSequenceSimulator() {
control.render(sandbox);
// Click sequences and isolated clicks must be handled correctly in any order.
assertClickSequenceFires('ACTION event expected after a click sequence');
assertClickSequenceFires(
'ACTION event expected after a second consecutive click sequence');
if (goog.userAgent.IE) {
// For some reason in IE8 and perhaps earlier, isolated clicks do not result
// a detectable dispatch of an ACTION event, so we'll only assert the
// desired handling of isolated clicks in IE9 and higher.
if (goog.userAgent.isVersionOrHigher(9)) {
assertIsolatedClickFires(
'ACTION event expected after an isolated click immediately ' +
'following a click sequence');
assertIsolatedClickFires(
'ACTION event expected after second consecutive isolated click');
} else {
// For IE8-and-lower, fire an isolated click event in preparation for our
// final assertion.
goog.testing.events.fireClickEvent(control.getKeyEventTarget());
}
} else {
assertIsolatedClickDoesNotFire(
'No ACTION event expected after an isolated click immediately ' +
'following a click sequence');
assertIsolatedClickDoesNotFire(
'No ACTION event expected after second consecutive isolated click');
}
assertClickSequenceFires(
'ACTION event expected after click sequence immediately following ' +
'an isolated click ');
}
function testIeMouseEventSequenceSimulatorStrictMode() {
if (!document.createEvent) {
return;
}
control.render(sandbox);
var actionCount = getEventCount(control, goog.ui.Component.EventType.ACTION);
var e = document.createEvent('MouseEvents');
e.initMouseEvent(
'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,
null);
control.getElementStrict().dispatchEvent(e);
if (goog.userAgent.IE) {
assertEquals(
'ACTION event expected after an isolated click', actionCount + 1,
getEventCount(control, goog.ui.Component.EventType.ACTION));
} else {
assertEquals(
'No ACTION event expected after an isolated click', actionCount,
getEventCount(control, goog.ui.Component.EventType.ACTION));
}
}
|