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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/ambient/ambient_controller.h"
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include "ash/ambient/ambient_constants.h"
#include "ash/ambient/ambient_managed_photo_controller.h"
#include "ash/ambient/ambient_ui_settings.h"
#include "ash/ambient/managed/screensaver_images_policy_handler.h"
#include "ash/ambient/metrics/ambient_metrics.h"
#include "ash/ambient/metrics/managed_screensaver_metrics.h"
#include "ash/ambient/test/ambient_ash_test_base.h"
#include "ash/ambient/test/ambient_ash_test_helper.h"
#include "ash/ambient/test/test_ambient_client.h"
#include "ash/ambient/ui/ambient_container_view.h"
#include "ash/ambient/ui/ambient_view_ids.h"
#include "ash/ambient/ui/photo_view.h"
#include "ash/ambient/util/ambient_util.h"
#include "ash/ambient/util/time_of_day_utils.h"
#include "ash/assistant/assistant_interaction_controller_impl.h"
#include "ash/constants/ambient_time_of_day_constants.h"
#include "ash/constants/ambient_video.h"
#include "ash/constants/ash_paths.h"
#include "ash/login/login_screen_controller.h"
#include "ash/login/ui/lock_screen.h"
#include "ash/public/cpp/ambient/ambient_prefs.h"
#include "ash/public/cpp/ambient/ambient_ui_model.h"
#include "ash/public/cpp/ambient/fake_ambient_backend_controller_impl.h"
#include "ash/public/cpp/assistant/controller/assistant_interaction_controller.h"
#include "ash/public/cpp/personalization_app/time_of_day_test_utils.h"
#include "ash/public/cpp/test/in_process_data_decoder.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/test/test_ash_web_view.h"
#include "ash/wallpaper/wallpaper_controller_impl.h"
#include "ash/webui/personalization_app/mojom/personalization_app.mojom-shared.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_path_override.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "build/buildflag.h"
#include "chromeos/ash/components/assistant/buildflags.h"
#include "chromeos/ash/components/dbus/dlcservice/dlcservice.pb.h"
#include "chromeos/ash/components/dbus/dlcservice/fake_dlcservice_client.h"
#include "chromeos/ash/services/assistant/public/cpp/features.h"
#include "chromeos/ash/services/libassistant/public/cpp/assistant_interaction_metadata.h"
#include "chromeos/dbus/power_manager/suspend.pb.h"
#include "net/base/url_util.h"
#include "ui/base/user_activity/user_activity_detector.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/events/pointer_details.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/test/test_event_handler.h"
#include "ui/events/types/event_type.h"
namespace ash {
namespace {
using ash::personalization_app::mojom::AmbientTheme;
using assistant::AssistantInteractionMetadata;
constexpr char kUser1[] = "user1@gmail.com";
constexpr char kUser2[] = "user2@gmail.com";
constexpr base::FilePath::CharType kTestDlcRootPath[] =
FILE_PATH_LITERAL("/test/time_of_day");
// Expects argument of type `dlcservice::DlcsWithContent::DlcInfo`.
MATCHER(HasVideoDlcPackageId, "") {
return arg.id() == kTimeOfDayDlcId;
}
std::vector<base::OnceClosure> GetEventGeneratorCallbacks(
ui::test::EventGenerator* event_generator) {
std::vector<base::OnceClosure> event_callbacks;
event_callbacks.push_back(
base::BindOnce(&ui::test::EventGenerator::ClickLeftButton,
base::Unretained(event_generator)));
event_callbacks.push_back(
base::BindOnce(&ui::test::EventGenerator::ClickRightButton,
base::Unretained(event_generator)));
event_callbacks.push_back(
base::BindOnce(&ui::test::EventGenerator::DragMouseBy,
base::Unretained(event_generator), /*dx=*/10,
/*dy=*/10));
event_callbacks.push_back(
base::BindOnce(&ui::test::EventGenerator::GestureScrollSequence,
base::Unretained(event_generator),
/*start=*/gfx::Point(10, 10),
/*end=*/gfx::Point(20, 10),
/*step_delay=*/base::Milliseconds(10),
/*steps=*/1));
event_callbacks.push_back(
base::BindOnce(&ui::test::EventGenerator::PressTouch,
base::Unretained(event_generator), std::nullopt));
return event_callbacks;
}
class AmbientUiVisibilityBarrier : public AmbientUiModelObserver {
public:
explicit AmbientUiVisibilityBarrier(AmbientUiVisibility target_visibility)
: target_visibility_(target_visibility) {
observation_.Observe(AmbientUiModel::Get());
}
AmbientUiVisibilityBarrier(const AmbientUiVisibilityBarrier&) = delete;
AmbientUiVisibilityBarrier& operator=(const AmbientUiVisibilityBarrier&) =
delete;
~AmbientUiVisibilityBarrier() override = default;
void WaitWithTimeout(base::TimeDelta timeout) {
if (AmbientUiModel::Get()->ui_visibility() == target_visibility_)
return;
base::test::ScopedRunLoopTimeout run_loop_timeout(FROM_HERE, timeout);
base::RunLoop run_loop;
run_loop_quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
}
private:
void OnAmbientUiVisibilityChanged(AmbientUiVisibility visibility) override {
if (visibility == target_visibility_ && run_loop_quit_closure_) {
// Post task so that any existing tasks get run before WaitWithTimeout()
// completes.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(run_loop_quit_closure_));
}
}
const AmbientUiVisibility target_visibility_;
base::ScopedObservation<AmbientUiModel, AmbientUiModelObserver> observation_{
this};
base::RepeatingClosure run_loop_quit_closure_;
};
// UpdateDisplay triggers a rogue MouseEvent that cancels Ambient mode when
// testing with Xvfb. A corresponding MouseEvent is not fired on a real device
// when an external display is added. Ignore this MouseEvent for testing.
// Store the old |ShouldIgnoreNativePlatformEvents| value and reset it at the
// end of the test.
class ScopedIgnoreNativePlatformEvents {
public:
ScopedIgnoreNativePlatformEvents()
: old_should_ignore_events_(
ui::PlatformEventSource::ShouldIgnoreNativePlatformEvents()) {
ui::PlatformEventSource::SetIgnoreNativePlatformEvents(true);
}
ScopedIgnoreNativePlatformEvents(const ScopedIgnoreNativePlatformEvents&) =
delete;
ScopedIgnoreNativePlatformEvents& operator=(
const ScopedIgnoreNativePlatformEvents&) = delete;
~ScopedIgnoreNativePlatformEvents() {
ui::PlatformEventSource::SetIgnoreNativePlatformEvents(
old_should_ignore_events_);
}
private:
const bool old_should_ignore_events_;
};
} // namespace
class AmbientControllerTest : public AmbientAshTestBase {
public:
AmbientControllerTest() {
dlcservice_client_.set_install_root_path(kTestDlcRootPath);
}
~AmbientControllerTest() override = default;
// AmbientAshTestBase:
void SetUp() override {
std::vector<base::test::FeatureRef> features_to_enable =
personalization_app::GetTimeOfDayFeatures();
feature_list_.InitWithFeatures(features_to_enable, {});
AmbientAshTestBase::SetUp();
GetSessionControllerClient()->set_show_lock_screen_views(true);
}
bool IsPrefObserved(const std::string& pref_name) {
auto* pref_change_registrar =
ambient_controller()->pref_change_registrar_.get();
DCHECK(pref_change_registrar);
return pref_change_registrar->IsObserved(pref_name);
}
bool CurrentThemeUsesPhotos() {
switch (GetCurrentUiSettings().theme()) {
case AmbientTheme::kSlideshow:
case AmbientTheme::kFeelTheBreeze:
case AmbientTheme::kFloatOnBy:
return true;
case AmbientTheme::kVideo:
return false;
}
}
bool AreSessionSpecificObserversBound() {
auto* ctrl = ambient_controller();
bool ui_model_bound = ctrl->ambient_ui_model_observer_.IsObserving();
// Ideally, we should check whether
// |ambient_ui_launcher()->backend_observer_.IsObserving()|. Check
// |ambient_ui_launcher()| instead because
// |ambient_controller->ambient_ui_launcher_| is not initialized in test.
bool backend_model_bound = ambient_ui_launcher();
bool power_manager_bound =
ctrl->power_manager_client_observer_.IsObserving();
bool fingerprint_bound = ctrl->fingerprint_observer_receiver_.is_bound();
// The backend model is only necessary for themes that use photos from it.
if (CurrentThemeUsesPhotos()) {
EXPECT_EQ(ui_model_bound, backend_model_bound)
<< "observers should all have the same state";
}
EXPECT_EQ(ui_model_bound, power_manager_bound)
<< "observers should all have the same state";
EXPECT_EQ(ui_model_bound, fingerprint_bound)
<< "observers should all have the same state";
return ui_model_bound;
}
base::test::ScopedFeatureList feature_list_;
protected:
base::UserActionTester user_action_tester_;
FakeDlcserviceClient dlcservice_client_;
};
// Tests for behavior that are agnostic to the AmbientUiSettings selected by
// the user should use this test harness.
//
// Currently there are test cases that actually fall under this category but
// do not use this test fixture. This is done purely for time constraint reasons
// (it takes a lot of compute time to repeat every single one of these test
// cases).
class AmbientControllerTestForAnyUiSettings
: public AmbientControllerTest,
public ::testing::WithParamInterface<AmbientUiSettings> {
protected:
void SetUp() override {
AmbientControllerTest::SetUp();
SetAmbientUiSettings(GetParam());
}
};
INSTANTIATE_TEST_SUITE_P(
AllUiSettings,
AmbientControllerTestForAnyUiSettings,
// Only one lottie-animated theme and video is
// sufficient here. The main goal here is to make sure
// that fundamental behavior holds for all themes.
testing::Values(AmbientUiSettings(AmbientTheme::kSlideshow),
AmbientUiSettings(AmbientTheme::kVideo,
AmbientVideo::kNewMexico)
#if BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
,
AmbientUiSettings(AmbientTheme::kFeelTheBreeze)
#endif // BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
),
[](const ::testing::TestParamInfo<AmbientUiSettings>& param_info) {
return std::string(
ambient::util::AmbientThemeToString(param_info.param.theme()));
});
TEST_P(AmbientControllerTestForAnyUiSettings, ShowAmbientScreenUponLock) {
LockScreen();
// Lockscreen will not immediately show Ambient mode.
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Ambient mode will show after inacivity and successfully loading first
// image.
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kShouldShow);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Clean up.
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_P(AmbientControllerTestForAnyUiSettings,
NotShowAmbientWhenPrefNotEnabled) {
SetAmbientModeEnabled(false);
LockScreen();
// Lockscreen will not immediately show Ambient mode.
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Ambient mode will not show after inacivity and successfully loading first
// image.
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Clean up.
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_P(AmbientControllerTestForAnyUiSettings, HideAmbientScreen) {
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kShouldShow);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
HideAmbientScreen();
FastForwardTiny();
EXPECT_TRUE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kHidden);
// Clean up.
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_P(AmbientControllerTestForAnyUiSettings, CloseAmbientScreenUponUnlock) {
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kShouldShow);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
UnlockScreen();
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// The view should be destroyed along the widget.
FastForwardTiny();
EXPECT_TRUE(GetContainerViews().empty());
}
TEST_P(AmbientControllerTestForAnyUiSettings,
CloseAmbientScreenUponUnlockSecondaryUser) {
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kShouldShow);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
SimulateUserLogin({kUser2});
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// The view should be destroyed along the widget.
FastForwardTiny();
EXPECT_TRUE(GetContainerViews().empty());
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// The view should be destroyed along the widget.
FastForwardTiny();
EXPECT_TRUE(GetContainerViews().empty());
}
TEST_F(AmbientControllerTest,
CloseAmbientScreenUponPowerButtonClickInTabletMode) {
ash::TabletModeControllerTestApi().EnterTabletMode();
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerViews().empty());
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
SimulatePowerButtonClick();
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// The view should be destroyed along the widget.
EXPECT_TRUE(GetContainerViews().empty());
}
TEST_F(AmbientControllerTest, ConsumerShouldNotRecordManagedMetrics) {
base::HistogramTester histogram_tester;
SetAmbientModeEnabled(true);
SetAmbientModeEnabled(false);
histogram_tester.ExpectTotalCount(
GetManagedScreensaverHistogram(kManagedScreensaverEnabledUMA),
/*expected_count=*/0);
}
TEST_F(AmbientControllerTest, NotShowAmbientWhenLockSecondaryUser) {
// Simulate the login screen.
ClearLogin();
SimulateUserLogin({kUser1});
SetAmbientModeEnabled(true);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kShouldShow);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
SimulateUserLogin({kUser2});
SetAmbientModeEnabled(true);
// Ambient mode should not show for second user even if that user has the pref
// turned on.
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// The view should be destroyed along the widget.
FastForwardTiny();
EXPECT_TRUE(GetContainerViews().empty());
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// The view should be destroyed along the widget.
EXPECT_TRUE(GetContainerViews().empty());
}
TEST_P(AmbientControllerTestForAnyUiSettings,
ShouldRequestAccessTokenWhenLockingScreen) {
GetAmbientAshTestHelper()->ambient_client().SetAutomaticalyIssueToken(false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Lock the screen will request a token.
LockScreen();
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Should close ambient widget already when unlocking screen.
UnlockScreen();
EXPECT_FALSE(IsAccessTokenRequestPending());
}
TEST_F(AmbientControllerTest, ShouldNotRequestAccessTokenWhenPrefNotEnabled) {
SetAmbientModeEnabled(false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Lock the screen will not request a token.
LockScreen();
EXPECT_FALSE(IsAccessTokenRequestPending());
UnlockScreen();
EXPECT_FALSE(IsAccessTokenRequestPending());
}
TEST_P(AmbientControllerTestForAnyUiSettings, ShouldReturnCachedAccessToken) {
GetAmbientAshTestHelper()->ambient_client().SetAutomaticalyIssueToken(false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Lock the screen will request a token.
LockScreen();
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Another token request will return cached token.
base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
base::RunLoop run_loop;
ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
[&](const GaiaId& gaia_id, const std::string& access_token_fetched) {
EXPECT_EQ(access_token_fetched, TestAmbientClient::kTestAccessToken);
std::move(closure).Run();
run_loop.Quit();
}));
EXPECT_FALSE(IsAccessTokenRequestPending());
run_loop.Run();
// Clean up.
CloseAmbientScreen();
}
// The test body intentionally does not have any actual test expectations. The
// test just has to run without crashing on tear down.
// http://crbug.com/1428481
TEST_P(AmbientControllerTestForAnyUiSettings,
ShutsDownWithoutCrashingWhileAmbientSessionActive) {
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Simulates what happens in a real shutdown scenario. The crash bug above
// cannot be reproduced without this.
ClearLogin();
}
TEST_F(AmbientControllerTest, ShouldReturnEmptyAccessToken) {
GetAmbientAshTestHelper()->ambient_client().SetAutomaticalyIssueToken(false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Lock the screen will request a token.
LockScreen();
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Another token request will return cached token.
base::OnceClosure closure = base::MakeExpectedRunClosure(FROM_HERE);
base::RunLoop run_loop_1;
ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
[&](const GaiaId& gaia_id, const std::string& access_token_fetched) {
EXPECT_EQ(access_token_fetched, TestAmbientClient::kTestAccessToken);
std::move(closure).Run();
run_loop_1.Quit();
}));
EXPECT_FALSE(IsAccessTokenRequestPending());
run_loop_1.Run();
base::RunLoop run_loop_2;
// When token expired, another token request will get empty token.
constexpr base::TimeDelta kTokenRefreshDelay = base::Seconds(60);
task_environment()->FastForwardBy(kTokenRefreshDelay);
closure = base::MakeExpectedRunClosure(FROM_HERE);
ambient_controller()->RequestAccessToken(base::BindLambdaForTesting(
[&](const GaiaId& gaia_id, const std::string& access_token_fetched) {
EXPECT_TRUE(access_token_fetched.empty());
std::move(closure).Run();
run_loop_2.Quit();
}));
EXPECT_FALSE(IsAccessTokenRequestPending());
run_loop_2.Run();
// Clean up.
CloseAmbientScreen();
}
TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenAfterFailure) {
GetAmbientAshTestHelper()->ambient_client().SetAutomaticalyIssueToken(false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Lock the screen will request a token.
LockScreen();
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/true);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Token request automatically retry.
task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
EXPECT_TRUE(IsAccessTokenRequestPending());
// Clean up.
CloseAmbientScreen();
}
TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenWithBackoffPolicy) {
GetAmbientAshTestHelper()->ambient_client().SetAutomaticalyIssueToken(false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Lock the screen will request a token.
LockScreen();
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/true);
EXPECT_FALSE(IsAccessTokenRequestPending());
base::TimeDelta delay1 = GetRefreshTokenDelay();
task_environment()->FastForwardBy(delay1 * 1.1);
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/true);
EXPECT_FALSE(IsAccessTokenRequestPending());
base::TimeDelta delay2 = GetRefreshTokenDelay();
EXPECT_GT(delay2, delay1);
task_environment()->FastForwardBy(delay2 * 1.1);
EXPECT_TRUE(IsAccessTokenRequestPending());
// Clean up.
CloseAmbientScreen();
}
TEST_F(AmbientControllerTest, ShouldRetryRefreshAccessTokenOnlyThreeTimes) {
GetAmbientAshTestHelper()->ambient_client().SetAutomaticalyIssueToken(false);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Lock the screen will request a token.
LockScreen();
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/true);
EXPECT_FALSE(IsAccessTokenRequestPending());
// 1st retry.
task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/true);
EXPECT_FALSE(IsAccessTokenRequestPending());
// 2nd retry.
task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/true);
EXPECT_FALSE(IsAccessTokenRequestPending());
// 3rd retry.
task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
EXPECT_TRUE(IsAccessTokenRequestPending());
IssueAccessToken(/*is_empty=*/true);
EXPECT_FALSE(IsAccessTokenRequestPending());
// Will not retry.
task_environment()->FastForwardBy(GetRefreshTokenDelay() * 1.1);
EXPECT_FALSE(IsAccessTokenRequestPending());
CloseAmbientScreen();
}
TEST_F(AmbientControllerTest,
CheckAcquireAndReleaseWakeLockWhenBatteryIsCharging) {
// Simulate a device being connected to a charger initially.
SetPowerStateCharging();
// Lock screen to start ambient mode, and flush the loop to ensure
// the acquire wake lock request has reached the wake lock provider.
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
HideAmbientScreen();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Ambient screen showup again after inactivity.
FastForwardByLockScreenInactivityTimeout();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Unlock screen to exit ambient mode.
UnlockScreen();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_F(AmbientControllerTest,
CheckAcquireAndReleaseWakeLockWhenBatteryBatteryIsFullAndDischarging) {
SetPowerStateDischarging();
SetBatteryPercent(100.f);
SetExternalPowerConnected();
// Lock screen to start ambient mode, and flush the loop to ensure
// the acquire wake lock request has reached the wake lock provider.
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
HideAmbientScreen();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Ambient screen showup again after inactivity.
FastForwardByLockScreenInactivityTimeout();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Unlock screen to exit ambient mode.
UnlockScreen();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_F(AmbientControllerTest,
CheckAcquireAndReleaseWakeLockWhenBatteryStateChanged) {
// When the battery is not charging
// No power connected, should not acquire wake lock
SetPowerStateDischarging();
SetExternalPowerDisconnected();
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// External official power connected, should acquire wake lock.
SetExternalPowerConnected();
base::RunLoop().RunUntilIdle();
HideAmbientScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// External USB power connected, should not acquire wake lock because the
// power is not strong enough.
SetExternalUsbPowerConnected();
base::RunLoop().RunUntilIdle();
HideAmbientScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// When the battery is charging, should acquire wake lock.
SetPowerStateCharging();
base::RunLoop().RunUntilIdle();
HideAmbientScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_P(AmbientControllerTestForAnyUiSettings, ShouldCloseOnEvent) {
auto* ambient_ui_model = AmbientUiModel::Get();
std::vector<base::OnceClosure> event_callbacks =
GetEventGeneratorCallbacks(GetEventGenerator());
for (auto& event_callback : event_callbacks) {
SetAmbientShownAndWaitForWidgets();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->IsShowing());
std::move(event_callback).Run();
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_EQ(AmbientUiVisibility::kClosed, ambient_ui_model->ui_visibility());
EXPECT_TRUE(GetContainerViews().empty());
}
}
TEST_P(AmbientControllerTestForAnyUiSettings,
ShouldDismissToLockScreenOnEvent) {
auto* ambient_ui_model = AmbientUiModel::Get();
std::vector<base::OnceClosure> event_callbacks =
GetEventGeneratorCallbacks(GetEventGenerator());
for (auto& event_callback : event_callbacks) {
// Lock screen and fast forward a bit to verify entered hidden state.
LockScreen();
FastForwardTiny();
EXPECT_EQ(AmbientUiVisibility::kHidden, ambient_ui_model->ui_visibility());
// Wait for timeout to elapse so ambient is shown.
FastForwardByLockScreenInactivityTimeout();
EXPECT_EQ(AmbientUiVisibility::kShouldShow,
ambient_ui_model->ui_visibility());
EXPECT_TRUE(ambient_controller()->IsShowing());
// Send an event.
std::move(event_callback).Run();
EXPECT_TRUE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiVisibility::kHidden, ambient_ui_model->ui_visibility());
// The lock screen timer should have just restarted, so greater than 99% of
// time remaining on the timer until ambient restarts.
EXPECT_GT(GetRemainingLockScreenTimeoutFraction().value(), 0.99f);
// Wait the timeout duration again.
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
// Ambient has started again due to elapsed timeout.
EXPECT_EQ(AmbientUiVisibility::kShouldShow,
ambient_ui_model->ui_visibility());
EXPECT_TRUE(ambient_controller()->IsShowing());
// Reset for next iteration.
UnlockScreen();
}
}
// Currently only runs for non-video screen saver settings due to needing to set
// photo download delay.
TEST_F(AmbientControllerTest, ShouldResetLockScreenInactivityTimerOnEvent) {
auto* ambient_ui_model = AmbientUiModel::Get();
// Set a long photo download delay so that state is
// `AmbientUiVisibility::kShouldShow` but widget does not exist to receive
// events yet.
SetPhotoDownloadDelay(base::Seconds(1));
SetAmbientTheme(AmbientTheme::kSlideshow);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
// Ambient controller is shown but photos have not yet downloaded, so ambient
// widget and container views do not exist.
EXPECT_EQ(AmbientUiVisibility::kShouldShow,
ambient_ui_model->ui_visibility());
EXPECT_FALSE(ambient_controller()->IsShowing())
<< "Ambient container views should not exist because photos not yet "
"downloaded";
// Inactivity timer has elapsed so nullopt.
EXPECT_FALSE(GetRemainingLockScreenTimeoutFraction().has_value());
// Send a user activity through `UserActivityDetector`. `EventGenerator` is
// not hooked up to `UserActivityDetector` in this test environment, so
// manually trigger `UserActivityDetector` ourselves.
auto* user_activity_detector = ui::UserActivityDetector::Get();
ui::KeyEvent event(ui::EventType::kKeyPressed, ui::VKEY_A, ui::EF_NONE);
user_activity_detector->DidProcessEvent(&event);
EXPECT_EQ(AmbientUiVisibility::kShouldShow, ambient_ui_model->ui_visibility())
<< "Still shown because waiting for `OnKeyEvent` to be called";
// Call `OnKeyEvent` via `EventGenerator`.
GetEventGenerator()->PressAndReleaseKey(ui::VKEY_A);
EXPECT_EQ(AmbientUiVisibility::kHidden, ambient_ui_model->ui_visibility())
<< "Should be kHidden because of recent OnKeyEvent call";
EXPECT_GT(GetRemainingLockScreenTimeoutFraction().value(), 0.99)
<< "Lock screen inactivity timer should have restarted";
FastForwardByLockScreenInactivityTimeout(0.5);
EXPECT_GT(GetRemainingLockScreenTimeoutFraction().value(), 0.4);
FastForwardByLockScreenInactivityTimeout(0.51);
EXPECT_FALSE(GetRemainingLockScreenTimeoutFraction().has_value())
<< "Inactivity timer has stopped";
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_FALSE(ambient_controller()->IsShowing())
<< "Photos still have not yet downloaded";
task_environment()->FastForwardBy(base::Seconds(2));
// Finally visible and running now that images downloaded.
EXPECT_TRUE(ambient_controller()->IsShowing());
}
TEST_P(AmbientControllerTestForAnyUiSettings,
ShouldDismissContainerViewOnKeyEvent) {
// Without user interaction, should show ambient mode.
SetAmbientShownAndWaitForWidgets();
EXPECT_TRUE(ambient_controller()->IsShowing());
CloseAmbientScreen();
// When ambient is shown, OnUserActivity() should ignore key event.
SetAmbientShownAndWaitForWidgets();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// General key press will exit ambient mode.
// Simulate key press to close the widget.
ui::test::TestEventHandler event_handler;
Shell::GetPrimaryRootWindow()->AddPreTargetHandler(&event_handler);
PressAndReleaseKey(ui::VKEY_A);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// First key press event should be consumed by ambient mode when closing the
// UI. Only the key release event gets propagated to the rest of the system.
EXPECT_EQ(event_handler.num_key_events(), 1);
Shell::GetPrimaryRootWindow()->RemovePreTargetHandler(&event_handler);
}
TEST_F(AmbientControllerTest, ShouldPropagateKeyPressIfNotRendering) {
// Force ambient mode to be in a state where it's trying to download photos
// but has not started rendering yet. In this state, the user should hit the
// keyboard and see the effect in the existing UI (probably the lock screen).
// The key stroke should also dismiss ambient mode.
SetAmbientTheme(AmbientTheme::kSlideshow);
DisableBackupCacheDownloads();
backend_controller()->SetFetchScreenUpdateInfoResponseSize(0);
ambient_controller()->SetUiVisibilityShouldShow();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_FALSE(GetContainerView());
ui::test::TestEventHandler event_handler;
Shell::GetPrimaryRootWindow()->AddPreTargetHandler(&event_handler);
PressAndReleaseKey(ui::VKEY_A);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Unlike the `ShouldDismissContainerViewOnKeyEvent` test case, both key
// events (press and release) should be propagated to the `event_handler` in
// the background.
EXPECT_EQ(event_handler.num_key_events(), 2);
Shell::GetPrimaryRootWindow()->RemovePreTargetHandler(&event_handler);
}
TEST_P(AmbientControllerTestForAnyUiSettings, ShowThenImmediatelyClose) {
// Try to launch ambient mode. It may not finish initialization or start
// rendering. Then close it immediately. Wait a while, and make sure no
// pending tasks run that may launch the UI unexpectedly afterwards.
ambient_controller()->SetUiVisibilityShouldShow();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
CloseAmbientScreen();
ASSERT_FALSE(ambient_controller()->ShouldShowAmbientUi());
task_environment()->FastForwardBy(base::Minutes(1));
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_FALSE(GetContainerView());
}
TEST_F(AmbientControllerTest,
ShouldDismissContainerViewOnKeyEventWhenLockScreenInBackground) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
SetPowerStateCharging();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should not lock the device and enter ambient mode when the screen is
// dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByBackgroundLockScreenTimeout();
EXPECT_TRUE(IsLocked());
// Should not disrupt ongoing ambient mode.
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// General key press will exit ambient mode.
// Simulate key press to close the widget.
PressAndReleaseKey(ui::VKEY_A);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest,
ShouldShowAmbientScreenWithLockscreenWhenScreenIsDimmed) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
SetPowerStateCharging();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should enter ambient mode when the screen is dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByBackgroundLockScreenTimeout();
EXPECT_TRUE(IsLocked());
// Should not disrupt ongoing ambient mode.
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Closes ambient for clean-up.
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest,
ShouldShowAmbientScreenWithLockscreenWithNoisyPowerEvents) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
SetPowerStateCharging();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should enter ambient mode when the screen is dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByBackgroundLockScreenTimeout(0.5001);
SetPowerStateCharging();
FastForwardByBackgroundLockScreenTimeout(0.5001);
SetPowerStateCharging();
EXPECT_TRUE(IsLocked());
// Should not disrupt ongoing ambient mode.
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Closes ambient for clean-up.
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest,
ShouldShowAmbientScreenWithoutLockscreenWhenScreenIsDimmed) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
// When power is discharging, we do not lock the screen with ambient
// mode since we do not prevent the device go to sleep which will natually
// lock the device.
SetPowerStateDischarging();
SetExternalPowerDisconnected();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should not lock the device but still enter ambient mode when the screen is
// dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByBackgroundLockScreenTimeout();
EXPECT_FALSE(IsLocked());
// Closes ambient for clean-up.
CloseAmbientScreen();
}
TEST_F(AmbientControllerTest, ShouldShowAmbientScreenWhenScreenIsDimmed) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
SetPowerStateCharging();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should not lock the device but enter ambient mode when the screen is
// dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByBackgroundLockScreenTimeout();
EXPECT_FALSE(IsLocked());
// Closes ambient for clean-up.
CloseAmbientScreen();
}
TEST_F(AmbientControllerTest, HandlesPreviousImageFailuresWithLockScreen) {
SetAmbientTheme(AmbientTheme::kSlideshow);
// Simulate failures to download FIFE urls. Ambient mode should close and
// remember the old failure.
SetDownloadPhotoData("");
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
AmbientUiVisibilityBarrier ambient_closed_barrier(
AmbientUiVisibility::kClosed);
ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
ASSERT_FALSE(ambient_controller()->ShouldShowAmbientUi());
UnlockScreen();
// Now simulate FIFE downloads starting to work again. The device should be
// able to enter ambient mode.
ClearDownloadPhotoData();
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, HandlesPreviousImageFailuresWithDimmedScreen) {
SetAmbientTheme(AmbientTheme::kSlideshow);
GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
SetPowerStateCharging();
// Simulate failures to download FIFE urls. Ambient mode should close and
// remember the old failure.
SetDownloadPhotoData("");
SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
FastForwardTiny();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
AmbientUiVisibilityBarrier ambient_closed_barrier(
AmbientUiVisibility::kClosed);
ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
ASSERT_FALSE(ambient_controller()->ShouldShowAmbientUi());
SetScreenIdleStateAndWait(/*is_screen_dimmed=*/false, /*is_off=*/false);
// Usually would enter ambient mode when the screen is dimmed, but this time
// it shouldn't because of the previous image failures.
SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
FastForwardTiny();
ASSERT_FALSE(ambient_controller()->ShouldShowAmbientUi());
SetScreenIdleStateAndWait(/*is_screen_dimmed=*/false, /*is_off=*/false);
// Now simulate FIFE downloads starting to work again. The device should be
// able to enter ambient mode.
ClearDownloadPhotoData();
SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
FastForwardTiny();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Closes ambient for clean-up.
CloseAmbientScreen();
}
TEST_F(AmbientControllerTest, ShouldHideAmbientScreenWhenDisplayIsOff) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(false);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should not lock the device and enter ambient mode when the screen is
// dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Should dismiss ambient mode screen.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Screen back on again, should not have ambient screen.
SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest,
ShouldHideAmbientScreenWhenDisplayIsOffThenComesBackWithLockScreen) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
SetPowerStateCharging();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should not lock the device and enter ambient mode when the screen is
// dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByBackgroundLockScreenTimeout();
EXPECT_TRUE(IsLocked());
// Should dismiss ambient mode screen.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Screen back on again, should not have ambient screen, but still has lock
// screen.
SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
EXPECT_TRUE(IsLocked());
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest,
ShouldHideAmbientScreenWhenDisplayIsOffAndNotStartWhenLockScreen) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
SetPowerStateDischarging();
SetExternalPowerDisconnected();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Should not lock the device and enter ambient mode when the screen is
// dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Should not lock the device because the device is not charging.
FastForwardByBackgroundLockScreenTimeout();
EXPECT_FALSE(IsLocked());
// Should dismiss ambient mode screen.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Lock screen will not start ambient mode.
LockScreen();
EXPECT_TRUE(IsLocked());
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Screen back on again, should not have ambient screen, but still has lock
// screen.
SetScreenIdleStateAndWait(/*dimmed=*/false, /*off=*/false);
EXPECT_TRUE(IsLocked());
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, HandlesPhotoDownloadOutage) {
SetAmbientTheme(AmbientTheme::kSlideshow);
SetDownloadPhotoData("");
LockScreen();
FastForwardByLockScreenInactivityTimeout();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
AmbientUiVisibilityBarrier ambient_closed_barrier(
AmbientUiVisibility::kClosed);
ambient_closed_barrier.WaitWithTimeout(base::Seconds(15));
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_P(AmbientControllerTestForAnyUiSettings, HideCursor) {
auto* cursor_manager = Shell::Get()->cursor_manager();
LockScreen();
cursor_manager->ShowCursor();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerViews().empty());
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kShouldShow);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_FALSE(cursor_manager->IsCursorVisible());
// Clean up.
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_P(AmbientControllerTestForAnyUiSettings, ShowsOnMultipleDisplays) {
UpdateDisplay("800x600,800x600");
FastForwardTiny();
SetAmbientShownAndWaitForWidgets();
auto* screen = display::Screen::GetScreen();
EXPECT_EQ(screen->GetNumDisplays(), 2);
EXPECT_EQ(GetContainerViews().size(), 2u);
AmbientViewID expected_child_view_id;
switch (GetParam().theme()) {
case AmbientTheme::kVideo:
expected_child_view_id = kAmbientVideoWebView;
break;
case AmbientTheme::kSlideshow:
expected_child_view_id = AmbientViewID::kAmbientPhotoView;
break;
case AmbientTheme::kFeelTheBreeze:
case AmbientTheme::kFloatOnBy:
expected_child_view_id = AmbientViewID::kAmbientAnimationView;
break;
}
EXPECT_TRUE(GetContainerViews().front()->GetViewByID(expected_child_view_id));
EXPECT_TRUE(GetContainerViews().back()->GetViewByID(expected_child_view_id));
// Check that each root controller has an ambient widget.
for (auto* ctrl : RootWindowController::root_window_controllers())
EXPECT_TRUE(ctrl->ambient_widget_for_testing() &&
ctrl->ambient_widget_for_testing()->IsVisible());
}
TEST_P(AmbientControllerTestForAnyUiSettings, RespondsToDisplayAdded) {
ScopedIgnoreNativePlatformEvents ignore_native_platform_events;
UpdateDisplay("800x600");
SetAmbientShownAndWaitForWidgets();
auto* screen = display::Screen::GetScreen();
EXPECT_EQ(screen->GetNumDisplays(), 1);
EXPECT_EQ(GetContainerViews().size(), 1u);
UpdateDisplay("800x600,800x600");
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->IsShowing());
EXPECT_EQ(screen->GetNumDisplays(), 2);
EXPECT_EQ(GetContainerViews().size(), 2u);
for (auto* ctrl : RootWindowController::root_window_controllers())
EXPECT_TRUE(ctrl->ambient_widget_for_testing() &&
ctrl->ambient_widget_for_testing()->IsVisible());
}
TEST_F(AmbientControllerTest, RespondsToDisplayAddedWhileInitializing) {
static constexpr base::TimeDelta kPhotoDownloadDelay = base::Seconds(2);
ScopedIgnoreNativePlatformEvents ignore_native_platform_events;
SetAmbientTheme(AmbientTheme::kSlideshow);
SetPhotoDownloadDelay(kPhotoDownloadDelay);
UpdateDisplay("800x600");
ambient_controller()->SetUiVisibilityShouldShow();
// First photo is downloaded, but the minimum required to start
// `kSlideshow` is two, so `AmbientPhotoController` should attempt to
// download another before starting the ui.
task_environment()->FastForwardBy(kPhotoDownloadDelay);
ASSERT_TRUE(GetContainerViews().empty());
// Now user plugs in second display.
UpdateDisplay("800x600,800x600");
task_environment()->FastForwardBy(kPhotoDownloadDelay);
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->IsShowing());
EXPECT_EQ(display::Screen::GetScreen()->GetNumDisplays(), 2);
EXPECT_EQ(GetContainerViews().size(), 2u);
for (auto* ctrl : RootWindowController::root_window_controllers()) {
EXPECT_TRUE(ctrl->ambient_widget_for_testing() &&
ctrl->ambient_widget_for_testing()->IsVisible());
}
}
TEST_P(AmbientControllerTestForAnyUiSettings, HandlesDisplayRemoved) {
UpdateDisplay("800x600,800x600");
FastForwardTiny();
SetAmbientShownAndWaitForWidgets();
auto* screen = display::Screen::GetScreen();
EXPECT_EQ(screen->GetNumDisplays(), 2);
EXPECT_EQ(GetContainerViews().size(), 2u);
EXPECT_TRUE(ambient_controller()->IsShowing());
// Changing to one screen will destroy the widget on the non-primary screen.
UpdateDisplay("800x600");
FastForwardTiny();
EXPECT_EQ(screen->GetNumDisplays(), 1);
EXPECT_EQ(GetContainerViews().size(), 1u);
EXPECT_TRUE(ambient_controller()->IsShowing());
}
TEST_F(AmbientControllerTest, ClosesAmbientBeforeSuspend) {
LockScreen();
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
SimulateSystemSuspendAndWait(power_manager::SuspendImminent::Reason::
SuspendImminent_Reason_LID_CLOSED);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByLockScreenInactivityTimeout();
// Ambient mode should not resume until SuspendDone is received.
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, RestartsAmbientAfterSuspend) {
LockScreen();
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
SimulateSystemSuspendAndWait(
power_manager::SuspendImminent::Reason::SuspendImminent_Reason_IDLE);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// This call should be blocked by prior |SuspendImminent| until |SuspendDone|.
ambient_controller()->SetUiVisibilityShouldShow();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
SimulateSystemResumeAndWait();
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, ObservesPrefsWhenAmbientEnabled) {
SetAmbientModeEnabled(false);
// This pref is always observed.
EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
std::vector<std::string> other_prefs{
ambient::prefs::kAmbientModeLockScreenInactivityTimeoutSeconds,
ambient::prefs::kAmbientModeLockScreenBackgroundTimeoutSeconds,
ambient::prefs::kAmbientModePhotoRefreshIntervalSeconds};
for (auto& pref_name : other_prefs)
EXPECT_FALSE(IsPrefObserved(pref_name));
SetAmbientModeEnabled(true);
EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
for (auto& pref_name : other_prefs)
EXPECT_TRUE(IsPrefObserved(pref_name));
}
TEST_F(AmbientControllerTest, BindsObserversWhenAmbientEnabled) {
auto* ctrl = ambient_controller();
SetAmbientModeEnabled(false);
// SessionObserver must always be observing to detect when user pref service
// is started.
EXPECT_TRUE(ctrl->session_observer_.IsObserving());
EXPECT_FALSE(AreSessionSpecificObserversBound());
SetAmbientModeEnabled(true);
// Session observer should still be observing.
EXPECT_TRUE(ctrl->session_observer_.IsObserving());
EXPECT_TRUE(AreSessionSpecificObserversBound());
}
TEST_F(AmbientControllerTest, SwitchActiveUsersDoesNotDoubleBindObservers) {
ClearLogin();
SimulateUserLogin({kUser1});
SetAmbientModeEnabled(true);
// Observers are bound for primary user with Ambient mode enabled.
EXPECT_TRUE(AreSessionSpecificObserversBound());
EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
// Observers are still bound when secondary user logs in.
SimulateUserLogin({kUser2});
EXPECT_TRUE(AreSessionSpecificObserversBound());
EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
// Observers are not re-bound for primary user when session is active.
SwitchActiveUser(AccountId::FromUserEmail(kUser1));
EXPECT_TRUE(AreSessionSpecificObserversBound());
EXPECT_TRUE(IsPrefObserved(ambient::prefs::kAmbientModeEnabled));
// Switch back to secondary user.
SwitchActiveUser(AccountId::FromUserEmail(kUser2));
}
TEST_F(AmbientControllerTest, BindsObserversWhenAmbientOn) {
auto* ctrl = ambient_controller();
LockScreen();
// Start monitoring user activity on hidden ui.
EXPECT_TRUE(ctrl->user_activity_observer_.IsObserving());
// Do not monitor power status yet.
EXPECT_FALSE(ctrl->power_status_observer_.IsObserving());
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ctrl->user_activity_observer_.IsObserving());
EXPECT_TRUE(ctrl->power_status_observer_.IsObserving());
UnlockScreen();
EXPECT_FALSE(ctrl->user_activity_observer_.IsObserving());
EXPECT_FALSE(ctrl->power_status_observer_.IsObserving());
}
TEST_P(AmbientControllerTestForAnyUiSettings,
ShowDismissAmbientScreenUponAssistantQuery) {
if (ash::assistant::features::IsNewEntryPointEnabled()) {
GTEST_SKIP() << "Assistant is not available if new entry point is enabled. "
"crbug.com/388361414";
}
// Without user interaction, should show ambient mode.
SetAmbientShownAndWaitForWidgets();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
// Trigger Assistant interaction.
static_cast<AssistantInteractionControllerImpl*>(
AssistantInteractionController::Get())
->OnInteractionStarted(AssistantInteractionMetadata());
base::RunLoop().RunUntilIdle();
// Ambient screen should dismiss.
EXPECT_TRUE(GetContainerViews().empty());
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
// For all test cases that depend on ash ambient resources (lottie files, image
// assets, etc) being present to run.
#if BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
#define ANIMATION_TEST_WITH_RESOURCES(test_case_name) test_case_name
#else
#define ANIMATION_TEST_WITH_RESOURCES(test_case_name) DISABLED_##test_case_name
#endif // BUILDFLAG(HAS_ASH_AMBIENT_ANIMATION_RESOURCES)
TEST_F(AmbientControllerTest,
ANIMATION_TEST_WITH_RESOURCES(RendersCorrectView)) {
SetAmbientTheme(AmbientTheme::kFeelTheBreeze);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(GetContainerView());
EXPECT_FALSE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
UnlockScreen();
SetAmbientTheme(AmbientTheme::kSlideshow);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
EXPECT_FALSE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
UnlockScreen();
SetAmbientTheme(AmbientTheme::kFeelTheBreeze);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(GetContainerView());
EXPECT_FALSE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientAnimationView));
}
TEST_F(AmbientControllerTest,
ANIMATION_TEST_WITH_RESOURCES(ClearsCacheWhenSwitchingThemes)) {
SetAmbientTheme(AmbientTheme::kSlideshow);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(GetContainerView());
ASSERT_FALSE(GetCachedFiles().empty());
UnlockScreen();
SetAmbientTheme(AmbientTheme::kFeelTheBreeze);
// Mimic a network outage where no photos can be downloaded. Since the cache
// should have been cleared when we switched ambient animation themes, the
// UI shouldn't start with a photo cached during slideshow mode.
SetDownloadPhotoData(/*data=*/"");
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(GetContainerView());
EXPECT_TRUE(GetCachedFiles().empty());
}
TEST_F(AmbientControllerTest,
ANIMATION_TEST_WITH_RESOURCES(MetricsStartupTimeSuspendAfterTimeMax)) {
SetAmbientTheme(AmbientTheme::kSlideshow);
base::HistogramTester histogram_tester;
LockScreen();
FastForwardByLockScreenInactivityTimeout();
task_environment()->FastForwardBy(ambient::kMetricsStartupTimeMax);
FastForwardTiny();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
SimulateSystemSuspendAndWait(power_manager::SuspendImminent::Reason::
SuspendImminent_Reason_LID_CLOSED);
ASSERT_FALSE(ambient_controller()->ShouldShowAmbientUi());
histogram_tester.ExpectTotalCount("Ash.AmbientMode.StartupTime.SlideShow", 1);
UnlockScreen();
}
TEST_F(AmbientControllerTest,
ANIMATION_TEST_WITH_RESOURCES(MetricsStartupTimeScreenOffAfterTimeMax)) {
SetAmbientTheme(AmbientTheme::kSlideshow);
base::HistogramTester histogram_tester;
LockScreen();
FastForwardByLockScreenInactivityTimeout();
task_environment()->FastForwardBy(ambient::kMetricsStartupTimeMax);
FastForwardTiny();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/true);
ASSERT_FALSE(ambient_controller()->ShouldShowAmbientUi());
histogram_tester.ExpectTotalCount("Ash.AmbientMode.StartupTime.SlideShow", 1);
UnlockScreen();
}
TEST_F(AmbientControllerTest, ShouldStartScreenSaverPreview) {
ASSERT_EQ(0,
user_action_tester_.GetActionCount(kScreenSaverPreviewUserAction));
ambient_controller()->SetUiVisibilityPreview();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_FALSE(IsLocked());
EXPECT_EQ(1,
user_action_tester_.GetActionCount(kScreenSaverPreviewUserAction));
}
TEST_F(AmbientControllerTest,
ShouldNotDismissScreenSaverPreviewOnUserActivity) {
ambient_controller()->SetUiVisibilityPreview();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ui::MouseEvent mouse_event(ui::EventType::kMouseReleased, gfx::Point(),
gfx::Point(), base::TimeTicks(), ui::EF_NONE,
ui::EF_NONE);
ui::UserActivityDetector::Get()->DidProcessEvent(&mouse_event);
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, ShouldDismissScreenSaverPreviewOnKeyReleased) {
ambient_controller()->SetUiVisibilityPreview();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->ReleaseKey(ui::VKEY_A, ui::EF_NONE);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->PressKey(ui::VKEY_A, ui::EF_NONE);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest,
ShouldNotDismissScreenSaverPreviewOnSomeMouseEvents) {
ambient_controller()->SetUiVisibilityPreview();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->MoveMouseWheel(10, 10);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->SendMouseEnter();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->SendMouseExit();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, ShouldDismissScreenSaverPreviewOnMouseClick) {
ambient_controller()->SetUiVisibilityPreview();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->ClickLeftButton();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
ambient_controller()->SetUiVisibilityPreview();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->ClickRightButton();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, MaybeDismissUIOnMouseMove) {
ambient_controller()->SetUiVisibilityPreview();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
GetEventGenerator()->MoveMouseTo(gfx::Point(5, 5), /*count=*/2);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
task_environment()->FastForwardBy(kDismissPreviewOnMouseMoveDelay);
FastForwardTiny();
GetEventGenerator()->MoveMouseTo(gfx::Point(5, 5), /*count=*/2);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerTest, ShouldDismissScreenSaverPreviewOnTouch) {
SetAmbientTheme(AmbientTheme::kSlideshow);
// Case 1: Launch slide show, but it hasn't started rendering yet because it's
// downloading photos. User hits touchpad, and that should close the ambient
// session even though it never started rendering.
ambient_controller()->SetUiVisibilityPreview();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_FALSE(GetContainerView());
GetEventGenerator()->PressTouch();
GetEventGenerator()->ReleaseTouch();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Case 2: Launch slide show and wait for it to starts rendering. User hits
// touchpad, and that should close the ambient session.
SetAmbientPreviewAndWaitForWidgets();
ASSERT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
GetEventGenerator()->PressTouch();
GetEventGenerator()->ReleaseTouch();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
class AmbientControllerForManagedScreensaverTest : public AmbientAshTestBase {
public:
AmbientControllerForManagedScreensaverTest() {
CreateTestData();
// Required as otherwise the PathService::CheckedGet fails in the
// screensaver images policy handler.
device_policy_screensaver_folder_override_ =
std::make_unique<base::ScopedPathOverride>(
ash::DIR_DEVICE_POLICY_SCREENSAVER_DATA, temp_dir_.GetPath());
}
void SetUp() override {
AmbientAshTestBase::SetUp();
// Disable consumer ambient mode
SetAmbientModeEnabled(false);
GetSessionControllerClient()->set_show_lock_screen_views(true);
}
void TearDown() override {
image_file_paths_.clear();
AmbientAshTestBase::TearDown();
}
protected:
void CreateTestData() {
bool success = temp_dir_.CreateUniqueTempDir();
ASSERT_TRUE(success);
base::FilePath image_1 =
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("IMAGE_1.jpg"));
CreateTestImageJpegFile(image_1, 4, 4, SK_ColorRED);
base::FilePath image_2 =
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("IMAGE_2.jpg"));
CreateTestImageJpegFile(image_2, 8, 8, SK_ColorGREEN);
image_file_paths_.push_back(image_1);
image_file_paths_.push_back(image_2);
}
void SimulateScreensaverStart() {
LockScreen();
FastForwardByLockScreenInactivityTimeout();
EXPECT_EQ(std::nullopt, GetRemainingLockScreenTimeoutFraction());
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
InProcessDataDecoder decoder_;
std::vector<base::FilePath> image_file_paths_;
base::ScopedTempDir temp_dir_;
std::unique_ptr<base::ScopedPathOverride>
device_policy_screensaver_folder_override_;
};
TEST_F(AmbientControllerForManagedScreensaverTest,
VerifyEnabledPolicyHistogram) {
base::HistogramTester histogram_tester;
SetAmbientModeManagedScreensaverEnabled(true);
SetAmbientModeManagedScreensaverEnabled(false);
SetAmbientModeManagedScreensaverEnabled(true);
EXPECT_THAT(histogram_tester.GetAllSamples(GetManagedScreensaverHistogram(
kManagedScreensaverEnabledUMA)),
BucketsAre(base::Bucket(false, 1), base::Bucket(true, 2)));
}
TEST_F(AmbientControllerForManagedScreensaverTest,
ScreensaverIsShownWithEnoughImages) {
SetAmbientModeManagedScreensaverEnabled(true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SimulateScreensaverStart();
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
// Peripheral Ui is always hidden in managed screeensaver mode
EXPECT_FALSE(GetAmbientSlideshowPeripheralUi()->GetVisible())
<< "Peripheral Ui should be hidden in managed mode";
GetEventGenerator()->ClickLeftButton();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_FALSE(GetContainerView());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
ScreensaverIsNotShownWithoutImages) {
SetAmbientModeManagedScreensaverEnabled(true);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_FALSE(GetContainerView());
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
UiLauncherIsNullWhenManagedAmbientModeIsDisabled) {
SetAmbientModeEnabled(false);
SetAmbientModeManagedScreensaverEnabled(false);
ASSERT_FALSE(ambient_controller()->ambient_ui_launcher());
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
DisablingManagedAmbientModeFallsbackToUserAmbientModeIfEnabled) {
SetAmbientModeEnabled(true);
SetAmbientModeManagedScreensaverEnabled(true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SimulateScreensaverStart();
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
SetAmbientModeManagedScreensaverEnabled(false);
SetAmbientTheme(AmbientTheme::kSlideshow);
UnlockScreen();
LockScreen();
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
EXPECT_TRUE(GetAmbientSlideshowPeripheralUi()->GetVisible());
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
LaunchingManagedAmbientModeAfterAmbientModeWorksAsExpected) {
SetAmbientModeEnabled(/*enabled=*/true);
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SimulateScreensaverStart();
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
LaunchingAmbientModeAfterManagedAmbientModeWorksAsExpected) {
SetAmbientModeEnabled(/*enabled=*/false);
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
SetAmbientModeEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SimulateScreensaverStart();
UnlockScreen();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverTest, PrefObserverUpdatesUiModel) {
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
ASSERT_TRUE(ambient_controller()->ambient_ui_launcher());
PrefService* pref_service =
Shell::Get()->session_controller()->GetActivePrefService();
AmbientUiModel* ui_model = ambient_controller()->ambient_ui_model();
constexpr size_t kExpectedIdleTimeout = 55;
constexpr size_t kExpectedPhotoRefreshInterval = 77;
pref_service->SetInteger(
ambient::prefs::kAmbientModeManagedScreensaverIdleTimeoutSeconds,
kExpectedIdleTimeout);
EXPECT_EQ(base::Seconds(kExpectedIdleTimeout),
ui_model->lock_screen_inactivity_timeout());
pref_service->SetInteger(
ambient::prefs::kAmbientModeManagedScreensaverImageDisplayIntervalSeconds,
kExpectedPhotoRefreshInterval);
EXPECT_EQ(base::Seconds(kExpectedPhotoRefreshInterval),
ui_model->photo_refresh_interval());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
WorksWithAmbientManagedPhotoSource) {
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SimulateScreensaverStart();
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
UnlockScreen();
ASSERT_FALSE(GetContainerView());
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SimulateScreensaverStart();
// Will start as there are images present already
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
}
TEST_F(AmbientControllerForManagedScreensaverTest,
ManagedAmbientModeGetsEnabledOnLockScreenAndStartsIt) {
LockScreen();
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
FastForwardByLockScreenInactivityTimeout();
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
}
class AmbientControllerForManagedScreensaverLoginScreenTest
: public AmbientControllerForManagedScreensaverTest {
public:
void SetUp() override {
// For login screen tests we don't want to start a session rather we want to
// start on the login screen.
set_start_session(false);
AmbientControllerForManagedScreensaverTest::SetUp();
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
}
void TriggerScreensaverOnLoginScreen() {
GetSessionControllerClient()->RequestSignOut();
// The login screen can't be shown without a wallpaper.
Shell::Get()->wallpaper_controller()->ShowDefaultWallpaperForTesting();
Shell::Get()->login_screen_controller()->ShowLoginScreen();
GetSessionControllerClient()->FlushForTest();
FastForwardByLockScreenInactivityTimeout();
}
};
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
UMAEngagementTime) {
base::HistogramTester histogram_tester;
constexpr base::TimeDelta kExpectedTimeBucket1 = base::Seconds(5);
constexpr base::TimeDelta kExpectedTimeBucket2 = base::Seconds(10);
TriggerScreensaverOnLoginScreen();
ASSERT_TRUE(GetContainerView());
task_environment()->FastForwardBy(kExpectedTimeBucket1);
// Dismiss Screensaver
GetEventGenerator()->ClickLeftButton();
ASSERT_FALSE(GetContainerView());
FastForwardByLockScreenInactivityTimeout();
ASSERT_TRUE(GetContainerView());
task_environment()->FastForwardBy(kExpectedTimeBucket2);
// Dismiss Screensaver
GetEventGenerator()->ClickLeftButton();
auto histogram_name = GetManagedScreensaverHistogram(
kManagedScreensaverEngagementTimeSlideshowUMA);
histogram_tester.ExpectTimeBucketCount(histogram_name, kExpectedTimeBucket1,
1);
histogram_tester.ExpectTimeBucketCount(histogram_name, kExpectedTimeBucket2,
1);
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest, UMAStartupTime) {
base::HistogramTester histogram_tester;
constexpr base::TimeDelta kExpectedTimeBucket1 = base::Seconds(0);
TriggerScreensaverOnLoginScreen();
ASSERT_TRUE(GetContainerView());
GetEventGenerator()->ClickLeftButton();
ASSERT_FALSE(GetContainerView());
FastForwardByLockScreenInactivityTimeout();
ASSERT_TRUE(GetContainerView());
auto histogram_name = GetManagedScreensaverHistogram(
kManagedScreensaverStartupTimeSlideshowUMA);
histogram_tester.ExpectTimeBucketCount(histogram_name, kExpectedTimeBucket1,
2);
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
ShownOnLoginScreen) {
TriggerScreensaverOnLoginScreen();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
GetEventGenerator()->ClickLeftButton();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
ShownOnLoginWhenPrefUpdatedLater) {
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/false);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Login screen is shown when the managed mode is disabled
TriggerScreensaverOnLoginScreen();
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
NotShownOnLoginScreenWhenDisabled) {
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/false);
FastForwardByLockScreenInactivityTimeout();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
UserLogsInAmbientModeDisabledAndManagedAmbientModeEnabled) {
TriggerScreensaverOnLoginScreen();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
// Simulate user session start (e.g. user login)
SimulateUserLogin(kRegularUserLoginInfo);
// Confirm that ambient mode is not shown if disabled. (disabled by default)
FastForwardByLockScreenInactivityTimeout();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_FALSE(GetContainerView());
ASSERT_FALSE(ambient_controller()->ambient_ui_launcher());
// Enabling and locking screen starts the managed ambient mode
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
UserLogsInAmbientModeEnabled) {
TriggerScreensaverOnLoginScreen();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
// Simulate user session start (e.g. consumer user login)
SimulateNewUserFirstLogin(kUser1);
// Enabling and locking screen starts the consumer ambient mode
SetAmbientModeEnabled(true);
DisableBackupCacheDownloads();
LockScreen();
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
ManagedScreensaverClosedWhenImagesCleared) {
TriggerScreensaverOnLoginScreen();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
// Clear images
managed_policy_handler()->SetImagesForTesting({});
EXPECT_FALSE(ambient_controller()->IsShowing());
FastForwardByLockScreenInactivityTimeout();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Simulate login
SimulateUserLogin(kRegularUserLoginInfo);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
SetAmbientModeManagedScreensaverEnabled(true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
managed_policy_handler()->SetImagesForTesting({});
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
FastForwardByLockScreenInactivityTimeout();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
ManagedScreensaverClosedWhenImageLoadingFails) {
TriggerScreensaverOnLoginScreen();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
// Set invalid images ( i.e. either the paths are invalid or images themselves
// have been deleted).
std::vector<base::FilePath> invalid_image_paths = {
base::FilePath(FILE_PATH_LITERAL("invalid_path_1")),
base::FilePath(FILE_PATH_LITERAL("invalid_path_2"))};
managed_policy_handler()->SetImagesForTesting(invalid_image_paths);
// Fast forward a tiny amount to run any async tasks.
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
// Simulate login
SimulateUserLogin(kRegularUserLoginInfo);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
SetAmbientModeManagedScreensaverEnabled(true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SimulateScreensaverStart();
EXPECT_TRUE(ambient_controller()->IsShowing());
managed_policy_handler()->SetImagesForTesting(invalid_image_paths);
// Fast forward a tiny amount to run any async tasks.
FastForwardTiny();
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
ManagedScreensaverNotShownInKioskSessions) {
// Confirm that the screensaver is still triggered on the login screen
TriggerScreensaverOnLoginScreen();
// New tests are flaky most of the time in the flakiness cluster on CQ due to
// mocked time, fast forward by 20% time to make sure that they work as
// expected.
// TODO(b/305199163) Remove after investigating the root cause and coming
// up with a general solution.
FastForwardByLockScreenInactivityTimeout(/*factor=*/0.2f);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
ASSERT_TRUE(GetContainerView());
SimulateKioskMode(user_manager::UserType::kKioskWebApp);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
SetAmbientModeManagedScreensaverEnabled(true);
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
// There is no lock screen in kiosk sessions so we just try to forward the
// time and try setting screen state to idle.
FastForwardByLockScreenInactivityTimeout();
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
EXPECT_EQ(AmbientUiModel::Get()->ui_visibility(),
AmbientUiVisibility::kClosed);
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
ManagedScreensaverDoesNotShowCursorWhenDisabledOrNotStarted) {
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/false);
TriggerScreensaverOnLoginScreen();
ASSERT_FALSE(GetContainerView());
// Hide the cursor.
Shell::Get()->cursor_manager()->HideCursor();
// Disabling an already disabled screensaver shouldn't show the cursor.
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/false);
EXPECT_FALSE(Shell::Get()->cursor_manager()->IsCursorVisible());
// Just enabling the screensaver and updating the images one by one should not
// change the cursor visibility.
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting({image_file_paths_[0]});
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
EXPECT_FALSE(Shell::Get()->cursor_manager()->IsCursorVisible());
// Waiting for some time without activity should not change the cursor
// visibility.
FastForwardByLockScreenInactivityTimeout(/*factor=*/0.5f);
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_FALSE(Shell::Get()->cursor_manager()->IsCursorVisible());
}
TEST_F(AmbientControllerForManagedScreensaverLoginScreenTest,
ManagedScreensaverInsufficientImagesErrorClearedOnGettingNewData) {
TriggerScreensaverOnLoginScreen();
// TODO(b/305199163) Remove after investigating the flakiness root cause and
// coming up with a general solution.
FastForwardByLockScreenInactivityTimeout(/*factor=*/0.2f);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_FALSE(managed_photo_controller()->HasScreenUpdateErrors());
// Only set one image to trigger insufficient images error.
managed_policy_handler()->SetImagesForTesting({image_file_paths_[0]});
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_TRUE(managed_photo_controller()->HasScreenUpdateErrors());
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
FastForwardByLockScreenInactivityTimeout(/*factor=*/1.2f);
// Confirm that the screensaver is shown and errors are cleared.
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_FALSE(managed_photo_controller()->HasScreenUpdateErrors());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
ManagedScreensaverNotShownOnScreenDim) {
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
managed_policy_handler()->SetImagesForTesting(image_file_paths_);
SetScreenIdleStateAndWait(/*is_screen_dimmed=*/true, /*is_off=*/false);
EXPECT_FALSE(IsLocked());
EXPECT_FALSE(ambient_controller()->ShouldShowAmbientUi());
}
TEST_F(AmbientControllerForManagedScreensaverTest,
ManagedScreensaverAlwaysShowsFullImages) {
const gfx::Rect screen_bounds_landscape(/*width=*/320, /*height=*/180);
UpdateDisplay("320x180");
SetAmbientModeManagedScreensaverEnabled(/*enabled=*/true);
const base::FilePath image_large_1 =
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("IMAGE_L.jpg"));
CreateTestImageJpegFile(image_large_1, 400, 180, SK_ColorRED);
const base::FilePath image_large_2 =
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("IMAGE_L_2.jpg"));
CreateTestImageJpegFile(image_large_2, 400, 180, SK_ColorGREEN);
const std::vector<base::FilePath> images{image_large_1, image_large_2};
managed_policy_handler()->SetImagesForTesting(images);
SimulateScreensaverStart();
ASSERT_TRUE(GetContainerView());
const gfx::Rect image_bounds_landscape =
GetAmbientBackgroundImageView()->GetImageBoundsInScreenForTesting();
EXPECT_TRUE(screen_bounds_landscape.Contains(image_bounds_landscape));
// Top and bottom black bars of 18 pixels due to height scaling.
EXPECT_EQ(image_bounds_landscape,
gfx::Rect(/*x=*/0, /*y=*/18, /*width=*/320, /*height=*/144));
// Rotate screen
const gfx::Rect screen_bounds_portrait(/*width=*/180, /*height=*/320);
UpdateDisplay("180x320");
FastForwardByLockScreenInactivityTimeout();
ASSERT_TRUE(GetContainerView());
const gfx::Rect image_bounds_portrait =
GetAmbientBackgroundImageView()->GetImageBoundsInScreenForTesting();
EXPECT_TRUE(screen_bounds_portrait.Contains(image_bounds_portrait));
// Top and bottom black bars of 119 pixels due to height scaling.
EXPECT_EQ(image_bounds_portrait,
gfx::Rect(/*x=*/0, /*y=*/119, /*width=*/180, /*height=*/81));
}
TEST_F(AmbientControllerTest, RendersCorrectViewForVideo) {
SetAmbientUiSettings(
AmbientUiSettings(AmbientTheme::kVideo, AmbientVideo::kNewMexico));
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(GetContainerView());
const TestAshWebView* web_view = static_cast<const TestAshWebView*>(
GetContainerView()->GetViewByID(kAmbientVideoWebView));
ASSERT_TRUE(web_view);
EXPECT_TRUE(web_view->current_url().SchemeIsFile());
const base::FilePath video_html_full_path =
base::FilePath(kTestDlcRootPath).Append(kTimeOfDayVideoHtmlSubPath);
EXPECT_EQ(web_view->current_url().path(), video_html_full_path.value());
std::string video_file_requested;
ASSERT_TRUE(net::GetValueForKeyInQuery(web_view->current_url(), "video_file",
&video_file_requested));
EXPECT_EQ(video_file_requested, kTimeOfDayNewMexicoVideo);
UnlockScreen();
SetAmbientTheme(AmbientTheme::kSlideshow);
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(GetContainerView());
EXPECT_TRUE(
GetContainerView()->GetViewByID(AmbientViewID::kAmbientPhotoView));
UnlockScreen();
SetAmbientUiSettings(
AmbientUiSettings(AmbientTheme::kVideo, AmbientVideo::kClouds));
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
ASSERT_TRUE(GetContainerView());
web_view = static_cast<const TestAshWebView*>(
GetContainerView()->GetViewByID(kAmbientVideoWebView));
ASSERT_TRUE(web_view);
EXPECT_TRUE(web_view->current_url().SchemeIsFile());
EXPECT_EQ(web_view->current_url().path(), video_html_full_path.value());
ASSERT_TRUE(net::GetValueForKeyInQuery(web_view->current_url(), "video_file",
&video_file_requested));
EXPECT_EQ(video_file_requested, kTimeOfDayCloudsVideo);
}
class AmbientControllerDurationTest : public AmbientAshTestBase {
public:
AmbientControllerDurationTest() = default;
~AmbientControllerDurationTest() override = default;
void SetUp() override {
AmbientAshTestBase::SetUp();
GetSessionControllerClient()->set_show_lock_screen_views(true);
}
};
TEST_F(AmbientControllerDurationTest, SetScreenSaverDuration) {
// Duration is default to forever.
SetAmbientModeEnabled(true);
EXPECT_EQ(0, GetScreenSaverDuration());
// Set screen saver duration.
SetScreenSaverDuration(5);
EXPECT_EQ(5, GetScreenSaverDuration());
SetScreenSaverDuration(10);
EXPECT_EQ(10, GetScreenSaverDuration());
SetScreenSaverDuration(0);
EXPECT_EQ(0, GetScreenSaverDuration());
}
TEST_F(AmbientControllerDurationTest, AcquireWakeLockAfterScreenSaverStarts) {
// Simulate User logged in.
ClearLogin();
SimulateUserLogin({kUser1});
// Set screen saver duration to forever.
SetAmbientModeEnabled(true);
SetScreenSaverDuration(0);
EXPECT_EQ(0, GetScreenSaverDuration());
// Simulate a device being connected to a charger initially.
SetPowerStateCharging();
// Lock screen to start ambient mode.
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
HideAmbientScreen();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Ambient screen showup again after inactivity.
FastForwardByLockScreenInactivityTimeout();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Unlock screen to exit ambient mode.
UnlockScreen();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_F(AmbientControllerDurationTest, ReleaseWakeLockWhenDurationIsReached) {
// Simulate User logged in.
ClearLogin();
SimulateUserLogin({kUser1});
// Simulate a device being connected to a charger initially.
SetPowerStateCharging();
// Set screen saver duration to any option that is not kForever.
const int duration_minutes = 5;
SetAmbientModeEnabled(true);
SetScreenSaverDuration(duration_minutes);
EXPECT_EQ(duration_minutes, GetScreenSaverDuration());
// Lock screen to start ambient mode.
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Fast forward to when duration is reached. Verify that the wake lock has
// been released.
FastForwardByDurationInMinutes(duration_minutes);
FastForwardTiny();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_F(AmbientControllerDurationTest, HoldWakeLockIfDurationIsSetToForever) {
// Simulate User logged in.
ClearLogin();
SimulateUserLogin({kUser1});
// Simulate a device being connected to a charger initially.
SetPowerStateCharging();
// Set screen saver duration to kForever.
constexpr int kForever = 0;
SetAmbientModeEnabled(true);
SetScreenSaverDuration(kForever);
EXPECT_EQ(kForever, GetScreenSaverDuration());
// Lock screen to start ambient mode.
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Fast forward to a time very long afterwards. Verify that screen saver is
// still running.
// Use 61 minutes because it is longer than any duration options but not too
// long so that this test could complete within a few seconds.
const int kLongTimeInMinutes = 61;
FastForwardByDurationInMinutes(kLongTimeInMinutes);
EXPECT_TRUE(ambient_controller()->ShouldShowAmbientUi());
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_F(AmbientControllerDurationTest, DoNotAcquireWakeLockOnBatteryMode) {
ClearLogin();
SimulateUserLogin({kUser1});
// Set power to battery mode.
SetPowerStateDischarging();
SetExternalPowerDisconnected();
SetAmbientModeEnabled(true);
SetScreenSaverDuration(0);
EXPECT_EQ(0, GetScreenSaverDuration());
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_F(AmbientControllerDurationTest, AcquireWakeLockWhileOnAcMode) {
ClearLogin();
SimulateUserLogin({kUser1});
// Set power to AC mode, charging.
SetPowerStateCharging();
SetExternalPowerConnected();
SetAmbientModeEnabled(true);
SetScreenSaverDuration(0);
EXPECT_EQ(0, GetScreenSaverDuration());
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
TEST_F(AmbientControllerDurationTest, ReleaseWakeLockWhenUnplugged) {
ClearLogin();
SimulateUserLogin({kUser1});
// Set power to AC mode. Verify that wake lock is acquired.
SetPowerStateCharging();
SetAmbientModeEnabled(true);
SetScreenSaverDuration(0);
EXPECT_EQ(0, GetScreenSaverDuration());
LockScreen();
FastForwardByLockScreenInactivityTimeout();
FastForwardTiny();
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Set power to battery mode. Verify that wake lock is released.
SetPowerStateDischarging();
SetExternalPowerDisconnected();
FastForwardTiny();
EXPECT_EQ(0, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
}
} // namespace ash
|