1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include <iterator>
#include <memory>
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/run_until.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/data_sharing/public/features.h"
#include "components/data_sharing/public/logger.h"
#include "components/optimization_guide/core/hints/mock_optimization_guide_decider.h"
#include "components/optimization_guide/core/optimization_guide_proto_util.h"
#include "components/optimization_guide/proto/page_entities_metadata.pb.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/testing_pref_service.h"
#include "components/saved_tab_groups/internal/saved_tab_group_model.h"
#include "components/saved_tab_groups/internal/sync_data_type_configuration.h"
#include "components/saved_tab_groups/internal/tab_group_sync_coordinator.h"
#include "components/saved_tab_groups/internal/tab_group_sync_metrics_logger_impl.h"
#include "components/saved_tab_groups/internal/tab_group_sync_service_impl.h"
#include "components/saved_tab_groups/public/collaboration_finder.h"
#include "components/saved_tab_groups/public/features.h"
#include "components/saved_tab_groups/public/pref_names.h"
#include "components/saved_tab_groups/public/saved_tab_group.h"
#include "components/saved_tab_groups/public/types.h"
#include "components/saved_tab_groups/test_support/saved_tab_group_test_utils.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/sync/base/collaboration_id.h"
#include "components/sync/base/data_type.h"
#include "components/sync/model/data_type_controller_delegate.h"
#include "components/sync/test/data_type_store_test_util.h"
#include "components/sync/test/fake_data_type_controller.h"
#include "components/sync/test/mock_data_type_local_change_processor.h"
#include "components/sync/test/test_matchers.h"
#include "components/sync_device_info/device_info_tracker.h"
#include "components/sync_device_info/fake_device_info_tracker.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tab_groups/tab_group_visual_data.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::An;
using testing::Between;
using testing::ByRef;
using testing::ContainerEq;
using testing::Contains;
using testing::DoAll;
using testing::Each;
using testing::ElementsAre;
using testing::Eq;
using testing::Invoke;
using testing::IsEmpty;
using testing::Matcher;
using testing::Not;
using testing::NotNull;
using testing::Pointee;
using testing::Return;
using testing::Sequence;
using testing::SetArgPointee;
using testing::SizeIs;
using testing::WithArgs;
namespace tab_groups {
namespace {
constexpr char kTestCacheGuid[] = "test_cache_guid";
constexpr GaiaId::Literal kDefaultGaiaId("default_gaia_id");
MATCHER_P(HasGuid, guid, "") {
return arg.saved_guid() == guid;
}
MATCHER(IsSharedGroup, "") {
return arg.is_shared_tab_group();
}
MATCHER_P2(HasSharedAttribution, created_by, updated_by, "") {
return arg.shared_attribution().created_by == GaiaId(created_by) &&
arg.shared_attribution().updated_by == GaiaId(updated_by);
}
optimization_guide::OptimizationMetadata GetPageEntitiesMetadata(
const std::string& title) {
optimization_guide::proto::PageEntitiesMetadata page_entities_metadata;
page_entities_metadata.set_alternative_title(title);
optimization_guide::proto::Any any;
any.set_type_url(page_entities_metadata.GetTypeName());
page_entities_metadata.SerializeToString(any.mutable_value());
optimization_guide::OptimizationMetadata metadata;
metadata.set_any_metadata(any);
return metadata;
}
class MockTabGroupSyncServiceObserver : public TabGroupSyncService::Observer {
public:
MockTabGroupSyncServiceObserver() = default;
~MockTabGroupSyncServiceObserver() override = default;
MOCK_METHOD(void, OnInitialized, ());
MOCK_METHOD(void, OnTabGroupAdded, (const SavedTabGroup&, TriggerSource));
MOCK_METHOD(void, OnTabGroupUpdated, (const SavedTabGroup&, TriggerSource));
MOCK_METHOD(void, BeforeTabGroupUpdateFromRemote, (const base::Uuid&));
MOCK_METHOD(void, AfterTabGroupUpdateFromRemote, (const base::Uuid&));
MOCK_METHOD(void, OnTabGroupRemoved, (const LocalTabGroupID&, TriggerSource));
MOCK_METHOD(void, OnTabGroupRemoved, (const base::Uuid&, TriggerSource));
MOCK_METHOD(void, OnTabSelected, (const std::set<LocalTabID>&));
MOCK_METHOD(void,
OnTabGroupMigrated,
(const SavedTabGroup&, const base::Uuid&, TriggerSource));
MOCK_METHOD(void,
OnTabGroupLocalIdChanged,
(const base::Uuid&, const std::optional<LocalTabGroupID>&));
MOCK_METHOD(void, OnTabGroupsReordered, (TriggerSource));
MOCK_METHOD(void, OnSyncBridgeUpdateTypeChanged, (SyncBridgeUpdateType));
};
class MockTabGroupSyncCoordinator : public TabGroupSyncCoordinator {
public:
MockTabGroupSyncCoordinator() = default;
~MockTabGroupSyncCoordinator() override = default;
MOCK_METHOD(std::optional<LocalTabGroupID>,
HandleOpenTabGroupRequest,
(const base::Uuid&, std::unique_ptr<TabGroupActionContext>));
MOCK_METHOD(void,
ConnectLocalTabGroup,
(const base::Uuid&, const LocalTabGroupID&));
MOCK_METHOD(void, DisconnectLocalTabGroup, (const LocalTabGroupID&));
MOCK_METHOD(std::unique_ptr<ScopedLocalObservationPauser>,
CreateScopedLocalObserverPauser,
());
MOCK_METHOD(std::set<LocalTabID>, GetSelectedTabs, ());
MOCK_METHOD(std::u16string, GetTabTitle, (const LocalTabID&));
MOCK_METHOD(void, OnInitialized, ());
MOCK_METHOD(void, OnTabGroupAdded, (const SavedTabGroup&, TriggerSource));
MOCK_METHOD(void, OnTabGroupUpdated, (const SavedTabGroup&, TriggerSource));
MOCK_METHOD(void, OnTabGroupRemoved, (const LocalTabGroupID&, TriggerSource));
MOCK_METHOD(void, OnTabGroupRemoved, (const base::Uuid&, TriggerSource));
MOCK_METHOD(void,
OnTabGroupMigrated,
(const SavedTabGroup&, const base::Uuid&, TriggerSource));
};
class MockCollaborationFinder : public CollaborationFinder {
public:
MockCollaborationFinder() = default;
~MockCollaborationFinder() override = default;
MOCK_METHOD(bool, IsCollaborationAvailable, (const syncer::CollaborationId&));
MOCK_METHOD(void, SetClient, (Client*));
};
MATCHER_P(UuidEq, uuid, "") {
return arg.saved_guid() == uuid;
}
} // namespace
class TabGroupSyncServiceTest : public testing::Test {
public:
TabGroupSyncServiceTest()
: task_environment_(
base::test::SingleThreadTaskEnvironment::MainThreadType::UI,
base::test::TaskEnvironment::TimeSource::MOCK_TIME),
saved_store_(
syncer::DataTypeStoreTestUtil::CreateInMemoryStoreForTest()),
shared_store_(
syncer::DataTypeStoreTestUtil::CreateInMemoryStoreForTest()),
decider_(std::make_unique<
optimization_guide::MockOptimizationGuideDecider>()),
fake_controller_delegate_(syncer::SAVED_TAB_GROUP),
group_1_(test::CreateTestSavedTabGroup()),
group_2_(test::CreateTestSavedTabGroup()),
group_3_(test::CreateTestSavedTabGroup()),
group_4_(test::CreateTestSavedTabGroup()),
local_group_id_1_(test::GenerateRandomTabGroupID()),
local_tab_id_1_(test::GenerateRandomTabID()) {}
~TabGroupSyncServiceTest() override = default;
void SetUp() override {
auto model = std::make_unique<SavedTabGroupModel>();
model_ = model.get();
pref_service_.registry()->RegisterBooleanPref(
prefs::kSavedTabGroupSpecificsToDataMigration, false);
pref_service_.registry()->RegisterBooleanPref(
prefs::kDidSyncTabGroupsInLastSession, true);
pref_service_.registry()->RegisterBooleanPref(
prefs::kDidEnableSharedTabGroupsInLastSession, true);
pref_service_.registry()->RegisterDictionaryPref(prefs::kDeletedTabGroupIds,
base::Value::Dict());
pref_service_.registry()->RegisterDictionaryPref(
prefs::kLocallyClosedRemoteTabGroupIds, base::Value::Dict());
pref_service_.registry()->RegisterBooleanPref(
prefs::kEligibleForVersionUpdatedMessage, false);
pref_service_.registry()->RegisterBooleanPref(
prefs::kEligibleForVersionOutOfDateInstantMessage, false);
pref_service_.registry()->RegisterBooleanPref(
prefs::kEligibleForVersionOutOfDatePersistentMessage, false);
pref_service_.registry()->RegisterBooleanPref(
prefs::kHasShownAnyVersionOutOfDateMessage, false);
auto metrics_logger =
std::make_unique<TabGroupSyncMetricsLoggerImpl>(&device_info_tracker_);
auto collaboration_finder =
std::make_unique<testing::NiceMock<MockCollaborationFinder>>();
collaboration_finder_ = collaboration_finder.get();
EXPECT_CALL(*decider_, RegisterOptimizationTypes(ElementsAre(
optimization_guide::proto::SAVED_TAB_GROUP)))
.Times(1);
EXPECT_CALL(*decider_, RegisterOptimizationTypes(ElementsAre(
optimization_guide::proto::PAGE_ENTITIES)))
.Times(Between(0, 1));
tab_group_sync_service_ = std::make_unique<TabGroupSyncServiceImpl>(
std::move(model),
std::make_unique<SyncDataTypeConfiguration>(
saved_processor_.CreateForwardingProcessor(),
syncer::DataTypeStoreTestUtil::FactoryForForwardingStore(
saved_store_.get())),
std::make_unique<SyncDataTypeConfiguration>(
shared_processor_.CreateForwardingProcessor(),
syncer::DataTypeStoreTestUtil::FactoryForForwardingStore(
shared_store_.get())),
nullptr, &pref_service_, std::move(metrics_logger), decider_.get(),
identity_test_environment_.identity_manager(),
std::move(collaboration_finder), /*logger=*/nullptr);
ON_CALL(saved_processor_, IsTrackingMetadata())
.WillByDefault(testing::Return(true));
ON_CALL(saved_processor_, TrackedCacheGuid())
.WillByDefault(testing::Return(kTestCacheGuid));
ON_CALL(saved_processor_, GetControllerDelegate())
.WillByDefault(testing::Return(fake_controller_delegate_.GetWeakPtr()));
ON_CALL(saved_processor_, GetPossiblyTrimmedRemoteSpecifics(_))
.WillByDefault(
testing::ReturnRef(sync_pb::EntitySpecifics::default_instance()));
ON_CALL(shared_processor_, IsTrackingMetadata())
.WillByDefault(testing::Return(true));
ON_CALL(shared_processor_, TrackedGaiaId())
.WillByDefault(testing::Return(kDefaultGaiaId));
ON_CALL(shared_processor_, GetPossiblyTrimmedRemoteSpecifics(_))
.WillByDefault(
testing::ReturnRef(sync_pb::EntitySpecifics::default_instance()));
ON_CALL(*collaboration_finder_, IsCollaborationAvailable(_))
.WillByDefault(testing::Return(true));
ON_CALL(*decider_,
CanApplyOptimization(
_, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillByDefault(Invoke(
[](const GURL& url,
optimization_guide::proto::OptimizationType optimization_type,
optimization_guide::OptimizationGuideDecisionCallback callback) {
std::move(callback).Run(
optimization_guide::OptimizationGuideDecision::kUnknown,
optimization_guide::OptimizationMetadata());
}));
auto coordinator =
std::make_unique<testing::NiceMock<MockTabGroupSyncCoordinator>>();
coordinator_ = coordinator.get();
tab_group_sync_service_->SetCoordinator(std::move(coordinator));
observer_ =
std::make_unique<testing::NiceMock<MockTabGroupSyncServiceObserver>>();
tab_group_sync_service_->AddObserver(observer_.get());
task_environment_.RunUntilIdle();
MaybeInitializeTestGroups();
task_environment_.RunUntilIdle();
}
testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor>*
mock_saved_processor() {
return &saved_processor_;
}
testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor>*
mock_shared_processor() {
return &shared_processor_;
}
void TearDown() override {
tab_group_sync_service_->RemoveObserver(observer_.get());
model_ = nullptr;
coordinator_ = nullptr;
collaboration_finder_ = nullptr;
}
// Enable sub-classes to not load initial test groups.
virtual void MaybeInitializeTestGroups() { InitializeTestGroups(); }
void InitializeTestGroups() {
base::Uuid id_1 = base::Uuid::GenerateRandomV4();
base::Uuid id_2 = base::Uuid::GenerateRandomV4();
base::Uuid id_3 = base::Uuid::GenerateRandomV4();
const std::u16string title_1 = u"Group One";
const std::u16string title_2 = u"Another Group";
const std::u16string title_3 = u"The Three Musketeers";
const tab_groups::TabGroupColorId& color_1 =
tab_groups::TabGroupColorId::kGrey;
const tab_groups::TabGroupColorId& color_2 =
tab_groups::TabGroupColorId::kRed;
const tab_groups::TabGroupColorId& color_3 =
tab_groups::TabGroupColorId::kGreen;
SavedTabGroupTab group_1_tab_1 = test::CreateSavedTabGroupTab(
"A_Link", u"Only Tab", id_1, /*position=*/0);
group_1_tab_1.SetLocalTabID(local_tab_id_1_);
std::vector<SavedTabGroupTab> group_1_tabs = {group_1_tab_1};
std::vector<SavedTabGroupTab> group_2_tabs = {
test::CreateSavedTabGroupTab("One_Link", u"One Of Two", id_2,
/*position=*/0),
test::CreateSavedTabGroupTab("Two_Link", u"Second", id_2,
/*position=*/1)};
std::vector<SavedTabGroupTab> group_3_tabs = {
test::CreateSavedTabGroupTab("Athos", u"All For One", id_3,
/*position=*/0),
test::CreateSavedTabGroupTab("Porthos", u"And", id_3, /*position=*/1),
test::CreateSavedTabGroupTab("Aramis", u"One For All", id_3,
/*position=*/2)};
group_1_ = SavedTabGroup(title_1, color_1, group_1_tabs, 0, id_1,
local_group_id_1_);
group_2_ = SavedTabGroup(title_2, color_2, group_2_tabs, 1, id_2);
group_3_ = SavedTabGroup(title_3, color_3, group_3_tabs, 2, id_3);
model_->AddedLocally(group_1_);
model_->AddedLocally(group_2_);
model_->AddedLocally(group_3_);
model_->UpdateLocalCacheGuid(/*old_cache_guid=*/std::nullopt,
kTestCacheGuid);
}
void VerifyCacheGuids(const SavedTabGroup& group,
const SavedTabGroupTab* tab,
std::optional<std::string> group_creator_cache_guid,
std::optional<std::string> group_updater_cache_guid,
std::optional<std::string> tab_creator_cache_guid,
std::optional<std::string> tab_updater_cache_guid) {
EXPECT_EQ(group_creator_cache_guid, group.creator_cache_guid());
EXPECT_EQ(group_updater_cache_guid, group.last_updater_cache_guid());
if (!tab) {
return;
}
EXPECT_EQ(tab_creator_cache_guid, tab->creator_cache_guid());
EXPECT_EQ(tab_updater_cache_guid, tab->last_updater_cache_guid());
}
void WaitForPostedTasks() {
// Post a dummy task in the current thread and wait for its completion so
// that any already posted tasks are completed.
base::RunLoop run_loop;
task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, run_loop.QuitClosure());
run_loop.Run();
}
void MakeTabGroupShared(const LocalTabGroupID& local_group_id,
const syncer::CollaborationId& collaboration_id) {
tab_group_sync_service_->MakeTabGroupShared(
local_group_id, collaboration_id, base::DoNothing());
// Simulate all shared tab groups as committed to the server.
for (const SavedTabGroup* group : model_->GetSharedTabGroupsOnly()) {
model_->MarkTransitionedToShared(group->saved_guid());
}
// Sharing a tab group is asynchronous, wait for it to complete.
WaitForPostedTasks();
}
protected:
const syncer::CollaborationId kCollaborationId =
syncer::CollaborationId("collaboration");
base::test::SingleThreadTaskEnvironment task_environment_;
base::test::ScopedFeatureList feature_list_;
signin::IdentityTestEnvironment identity_test_environment_;
TestingPrefServiceSimple pref_service_;
raw_ptr<SavedTabGroupModel> model_;
testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor> saved_processor_;
testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor> shared_processor_;
testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor>
shared_account_processor_;
std::unique_ptr<syncer::DataTypeStore> saved_store_;
std::unique_ptr<syncer::DataTypeStore> shared_store_;
std::unique_ptr<syncer::DataTypeStore> shared_account_store_;
std::unique_ptr<testing::NiceMock<MockTabGroupSyncServiceObserver>> observer_;
raw_ptr<testing::NiceMock<MockCollaborationFinder>> collaboration_finder_;
syncer::FakeDeviceInfoTracker device_info_tracker_;
raw_ptr<testing::NiceMock<MockTabGroupSyncCoordinator>> coordinator_;
std::unique_ptr<optimization_guide::MockOptimizationGuideDecider> decider_;
std::unique_ptr<TabGroupSyncServiceImpl> tab_group_sync_service_;
syncer::FakeDataTypeControllerDelegate fake_controller_delegate_;
SavedTabGroup group_1_;
SavedTabGroup group_2_;
SavedTabGroup group_3_;
SavedTabGroup group_4_;
LocalTabGroupID local_group_id_1_;
LocalTabID local_tab_id_1_;
};
TEST_F(TabGroupSyncServiceTest, ServiceConstruction) {
EXPECT_TRUE(tab_group_sync_service_->GetSavedTabGroupControllerDelegate());
EXPECT_TRUE(tab_group_sync_service_->GetVersioningMessageController());
}
TEST_F(TabGroupSyncServiceTest, GetAllGroups) {
auto all_groups = tab_group_sync_service_->GetAllGroups();
EXPECT_EQ(all_groups.size(), 3u);
EXPECT_EQ(all_groups[0].saved_guid(), group_1_.saved_guid());
EXPECT_EQ(all_groups[1].saved_guid(), group_2_.saved_guid());
EXPECT_EQ(all_groups[2].saved_guid(), group_3_.saved_guid());
SavedTabGroup group_4(test::CreateTestSavedTabGroupWithNoTabs());
LocalTabGroupID tab_group_id = test::GenerateRandomTabGroupID();
group_4.SetLocalGroupId(tab_group_id);
tab_group_sync_service_->AddGroup(group_4);
EXPECT_EQ(model_->Count(), 4);
all_groups = tab_group_sync_service_->GetAllGroups();
EXPECT_EQ(all_groups.size(), 3u);
}
TEST_F(TabGroupSyncServiceTest, GetGroup) {
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
EXPECT_EQ(group->saved_guid(), group_1_.saved_guid());
EXPECT_EQ(group->title(), group_1_.title());
EXPECT_EQ(group->color(), group_1_.color());
test::CompareSavedTabGroupTabs(group->saved_tabs(), group_1_.saved_tabs());
}
TEST_F(TabGroupSyncServiceTest, GetGroupEitherId) {
EitherGroupID either_id;
// When holding a sync group id.
either_id = group_1_.saved_guid();
auto group = tab_group_sync_service_->GetGroup(either_id);
EXPECT_TRUE(group.has_value());
EXPECT_EQ(group->saved_guid(), group_1_.saved_guid());
EXPECT_EQ(group->title(), group_1_.title());
EXPECT_EQ(group->color(), group_1_.color());
test::CompareSavedTabGroupTabs(group->saved_tabs(), group_1_.saved_tabs());
// When holding a local group id.
either_id = local_group_id_1_;
group = tab_group_sync_service_->GetGroup(either_id);
EXPECT_TRUE(group.has_value());
EXPECT_EQ(group->saved_guid(), group_1_.saved_guid());
EXPECT_EQ(group->title(), group_1_.title());
EXPECT_EQ(group->color(), group_1_.color());
test::CompareSavedTabGroupTabs(group->saved_tabs(), group_1_.saved_tabs());
}
TEST_F(TabGroupSyncServiceTest, GetDeletedGroupIdsUsingPrefs) {
// Delete a group from sync. It should add the deleted ID to the pref.
model_->RemovedFromSync(group_1_.saved_guid());
WaitForPostedTasks();
auto deleted_ids = tab_group_sync_service_->GetDeletedGroupIds();
EXPECT_EQ(1u, deleted_ids.size());
EXPECT_TRUE(base::Contains(deleted_ids, local_group_id_1_));
// Now close out the group from tab model and notify service.
// The entry should be cleaned up from prefs.
tab_group_sync_service_->RemoveLocalTabGroupMapping(local_group_id_1_,
ClosingSource::kUnknown);
deleted_ids = tab_group_sync_service_->GetDeletedGroupIds();
EXPECT_EQ(0u, deleted_ids.size());
}
TEST_F(TabGroupSyncServiceTest, GetTitleForPreviouslyExistingSharedTabGroup) {
syncer::CollaborationId collaboration_id("collaboration_id");
// First ensure our test group is shared.
MakeTabGroupShared(local_group_id_1_, collaboration_id);
// Making a tab group shared changes its GUID, so we find the new GUID.
std::optional<SavedTabGroup> shared_group_1 =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group_1.has_value());
// Delete a group from sync. It should save the current title.
model_->RemovedFromSync(shared_group_1->saved_guid());
WaitForPostedTasks();
// We should also have saved the last known title of the shared tab group.
std::optional<std::u16string> title =
tab_group_sync_service_->GetTitleForPreviouslyExistingSharedTabGroup(
collaboration_id);
ASSERT_TRUE(title.has_value());
EXPECT_EQ(group_1_.title(), title);
}
TEST_F(TabGroupSyncServiceTest,
GetDeletedGroupIdsUsingPrefsWhileRemovedFromLocal) {
// Delete a group from local. It should not add the entry to the prefs.
model_->RemovedLocally(group_1_.saved_guid());
WaitForPostedTasks();
auto deleted_ids = tab_group_sync_service_->GetDeletedGroupIds();
EXPECT_EQ(0u, deleted_ids.size());
}
TEST_F(TabGroupSyncServiceTest, AddGroup) {
base::HistogramTester histogram_tester;
// Add a new group.
SavedTabGroup group_4(test::CreateTestSavedTabGroup());
LocalTabGroupID tab_group_id = test::GenerateRandomTabGroupID();
group_4.SetLocalGroupId(tab_group_id);
tab_group_sync_service_->AddGroup(group_4);
// Verify model internals.
EXPECT_TRUE(model_->Contains(group_4.saved_guid()));
EXPECT_EQ(model_->GetIndexOf(group_4.saved_guid()), 3);
EXPECT_EQ(model_->Count(), 4);
// Query the group via service and verify members.
auto group = tab_group_sync_service_->GetGroup(group_4.saved_guid());
EXPECT_TRUE(group.has_value());
EXPECT_EQ(group->saved_guid(), group_4.saved_guid());
EXPECT_EQ(group->title(), group_4.title());
EXPECT_EQ(group->color(), group_4.color());
EXPECT_FALSE(group->created_before_syncing_tab_groups());
VerifyCacheGuids(*group, nullptr, kTestCacheGuid, std::nullopt, std::nullopt,
std::nullopt);
test::CompareSavedTabGroupTabs(group->saved_tabs(), group_4.saved_tabs());
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.Created.GroupCreateOrigin", 1u);
}
TEST_F(TabGroupSyncServiceTest, AddGroup_BeforeInit) {
// Add a new group.
SavedTabGroup group_4(test::CreateTestSavedTabGroup());
LocalTabGroupID tab_group_id = test::GenerateRandomTabGroupID();
group_4.SetLocalGroupId(tab_group_id);
EXPECT_FALSE(model_->Contains(group_4.saved_guid()));
EXPECT_EQ(model_->Count(), 3);
tab_group_sync_service_->SetIsInitializedForTesting(false);
tab_group_sync_service_->AddGroup(group_4);
EXPECT_FALSE(model_->Contains(group_4.saved_guid()));
// Initialize model and add group 4.
model_->LoadStoredEntries(/*groups=*/{}, /*tabs=*/{});
WaitForPostedTasks();
// Verify model internals.
EXPECT_TRUE(model_->Contains(group_4.saved_guid()));
EXPECT_EQ(model_->Count(), 4);
}
TEST_F(TabGroupSyncServiceTest, AddGroupWhenSignedOut) {
// Add a new group while signed out.
ON_CALL(saved_processor_, IsTrackingMetadata())
.WillByDefault(testing::Return(false));
SavedTabGroup group_4(test::CreateTestSavedTabGroup());
LocalTabGroupID tab_group_id = test::GenerateRandomTabGroupID();
group_4.SetLocalGroupId(tab_group_id);
tab_group_sync_service_->AddGroup(group_4);
// Query the group via service and verify members.
auto group = tab_group_sync_service_->GetGroup(group_4.saved_guid());
EXPECT_EQ(group->saved_guid(), group_4.saved_guid());
EXPECT_TRUE(group->created_before_syncing_tab_groups());
}
TEST_F(TabGroupSyncServiceTest, RemoveGroupByLocalId) {
base::HistogramTester histogram_tester;
// Add a group.
SavedTabGroup group_4(test::CreateTestSavedTabGroup());
LocalTabGroupID tab_group_id = test::GenerateRandomTabGroupID();
group_4.SetLocalGroupId(tab_group_id);
tab_group_sync_service_->AddGroup(group_4);
EXPECT_TRUE(
tab_group_sync_service_->GetGroup(group_4.saved_guid()).has_value());
// Remove the group and verify.
tab_group_sync_service_->RemoveGroup(tab_group_id);
EXPECT_EQ(tab_group_sync_service_->GetGroup(group_4.saved_guid()),
std::nullopt);
// Verify model internals.
EXPECT_FALSE(model_->Contains(group_4.saved_guid()));
EXPECT_EQ(model_->Count(), 3);
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.Removed.GroupCreateOrigin", 1u);
}
TEST_F(TabGroupSyncServiceTest, RemoveGroupBySyncId) {
// Remove the group and verify.
tab_group_sync_service_->RemoveGroup(group_1_.saved_guid());
EXPECT_EQ(tab_group_sync_service_->GetGroup(group_1_.saved_guid()),
std::nullopt);
// Verify model internals.
EXPECT_FALSE(model_->Contains(group_1_.saved_guid()));
EXPECT_EQ(model_->Count(), 2);
}
TEST_F(TabGroupSyncServiceTest, UpdateVisualData) {
base::HistogramTester histogram_tester;
tab_groups::TabGroupVisualData visual_data = test::CreateTabGroupVisualData();
tab_group_sync_service_->UpdateVisualData(local_group_id_1_, &visual_data);
auto group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_TRUE(group.has_value());
EXPECT_EQ(group->saved_guid(), group_1_.saved_guid());
EXPECT_EQ(group->title(), visual_data.title());
EXPECT_EQ(group->color(), visual_data.color());
VerifyCacheGuids(*group, nullptr, kTestCacheGuid, kTestCacheGuid,
std::nullopt, std::nullopt);
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.VisualsChanged.GroupCreateOrigin", 1u);
}
TEST_F(TabGroupSyncServiceTest, UpdateSharedAttributionsOnUpdateVisualData) {
MakeTabGroupShared(local_group_id_1_,
syncer::CollaborationId("collaboration"));
EXPECT_CALL(*mock_shared_processor(), TrackedGaiaId())
.WillOnce(Return(GaiaId("new_gaia_id")));
tab_groups::TabGroupVisualData visual_data = test::CreateTabGroupVisualData();
tab_group_sync_service_->UpdateVisualData(local_group_id_1_, &visual_data);
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(group.has_value());
EXPECT_THAT(*group, HasSharedAttribution(kDefaultGaiaId, "new_gaia_id"));
}
TEST_F(TabGroupSyncServiceTest, OpenTabGroup) {
EXPECT_CALL(*coordinator_,
HandleOpenTabGroupRequest(group_2_.saved_guid(), testing::_))
.Times(1);
tab_group_sync_service_->OpenTabGroup(
group_2_.saved_guid(), std::make_unique<TabGroupActionContext>());
}
TEST_F(TabGroupSyncServiceTest, ConnectLocalTabGroup) {
LocalTabGroupID local_id = test::GenerateRandomTabGroupID();
EXPECT_CALL(*coordinator_,
ConnectLocalTabGroup(group_2_.saved_guid(), local_id))
.Times(1);
tab_group_sync_service_->ConnectLocalTabGroup(
group_2_.saved_guid(), local_id, OpeningSource::kOpenedFromRevisitUi);
}
TEST_F(TabGroupSyncServiceTest, ConnectLocalTabGroup_BeforeInit) {
LocalTabGroupID local_id = test::GenerateRandomTabGroupID();
tab_group_sync_service_->SetIsInitializedForTesting(false);
// Expect ConnectLocalTabGroup to not be called before init.
EXPECT_CALL(*coordinator_, ConnectLocalTabGroup(_, _)).Times(0);
tab_group_sync_service_->ConnectLocalTabGroup(
group_2_.saved_guid(), local_id, OpeningSource::kAutoOpenedFromSync);
// Initialize model and connect the group.
EXPECT_CALL(*coordinator_,
ConnectLocalTabGroup(group_2_.saved_guid(), local_id))
.Times(1);
model_->LoadStoredEntries(/*groups=*/{}, /*tabs=*/{});
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, UpdateLocalTabGroupMapping_BeforeInit) {
tab_group_sync_service_->SetIsInitializedForTesting(false);
LocalTabGroupID local_id_4 = test::GenerateRandomTabGroupID();
ASSERT_FALSE(group_4_.local_group_id().has_value());
tab_group_sync_service_->UpdateLocalTabGroupMapping(
group_4_.saved_guid(), local_id_4, OpeningSource::kUnknown);
auto retrieved_group =
tab_group_sync_service_->GetGroup(group_4_.saved_guid());
EXPECT_FALSE(retrieved_group.has_value());
// Initialize model and add group 4.
model_->LoadStoredEntries(/*groups=*/{group_4_}, /*tabs=*/{});
WaitForPostedTasks();
retrieved_group = tab_group_sync_service_->GetGroup(group_4_.saved_guid());
EXPECT_TRUE(retrieved_group.has_value());
EXPECT_EQ(retrieved_group->local_group_id().value(), local_id_4);
EXPECT_EQ(retrieved_group->saved_guid(), group_4_.saved_guid());
test::CompareSavedTabGroupTabs(retrieved_group->saved_tabs(),
group_4_.saved_tabs());
}
TEST_F(TabGroupSyncServiceTest, UpdateLocalTabGroupMapping_AfterInit) {
LocalTabGroupID local_id_2 = test::GenerateRandomTabGroupID();
tab_group_sync_service_->UpdateLocalTabGroupMapping(
group_1_.saved_guid(), local_id_2, OpeningSource::kUnknown);
auto retrieved_group = tab_group_sync_service_->GetGroup(local_id_2);
EXPECT_TRUE(retrieved_group.has_value());
EXPECT_EQ(retrieved_group->local_group_id().value(), local_id_2);
EXPECT_EQ(retrieved_group->saved_guid(), group_1_.saved_guid());
EXPECT_EQ(retrieved_group->title(), group_1_.title());
EXPECT_EQ(retrieved_group->color(), group_1_.color());
test::CompareSavedTabGroupTabs(retrieved_group->saved_tabs(),
group_1_.saved_tabs());
}
TEST_F(TabGroupSyncServiceTest, RemoveLocalTabGroupMapping) {
auto retrieved_group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_TRUE(retrieved_group.has_value());
tab_group_sync_service_->RemoveLocalTabGroupMapping(local_group_id_1_,
ClosingSource::kUnknown);
retrieved_group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_FALSE(retrieved_group.has_value());
// TODO
}
TEST_F(TabGroupSyncServiceTest, AddTab) {
base::HistogramTester histogram_tester;
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
auto local_tab_id_2 = test::GenerateRandomTabID();
VerifyCacheGuids(*group, nullptr, kTestCacheGuid, std::nullopt, std::nullopt,
std::nullopt);
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_2,
u"random tab title", GURL("www.google.com"),
std::nullopt);
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
EXPECT_EQ(2u, group->saved_tabs().size());
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabAdded.GroupCreateOrigin", 1u);
VerifyCacheGuids(*group, nullptr, kTestCacheGuid, kTestCacheGuid,
std::nullopt, std::nullopt);
}
// Tests that adding a tab to a shared group.
TEST_F(TabGroupSyncServiceTest, AddTabToSharedGroup) {
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_EQ(group->saved_tabs().size(), 1u);
MakeTabGroupShared(local_group_id_1_,
syncer::CollaborationId("collaboration"));
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_EQ(shared_group->saved_tabs().size(), 1u);
LocalTabID local_tab_id_2 = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_2, u"foo",
GURL("https://www.google.com"), std::nullopt);
LocalTabID local_tab_id_3 = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_3, u"foo2",
GURL("www.google.com"), std::nullopt);
shared_group = tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group.has_value());
ASSERT_EQ(shared_group->saved_tabs().size(), 3u);
// Only tab 2 has title sanitized as it is an HTTPS url.
EXPECT_EQ(shared_group->saved_tabs()[0].title(), u"Only Tab");
EXPECT_EQ(shared_group->saved_tabs()[1].title(), u"google.com");
EXPECT_EQ(shared_group->saved_tabs()[2].title(), u"foo2");
// All tabs should have the same attribution metadata.
EXPECT_THAT(shared_group->saved_tabs(),
Each(HasSharedAttribution(kDefaultGaiaId, kDefaultGaiaId)));
EXPECT_THAT(*shared_group,
HasSharedAttribution(kDefaultGaiaId, kDefaultGaiaId));
}
TEST_F(TabGroupSyncServiceTest, AddUpdateRemoveTabWithUnknownGroupId) {
base::HistogramTester histogram_tester;
auto unknown_group_id = test::GenerateRandomTabGroupID();
auto local_tab_id = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(unknown_group_id, local_tab_id,
u"random tab title", GURL("www.google.com"),
std::nullopt);
auto group = tab_group_sync_service_->GetGroup(unknown_group_id);
EXPECT_FALSE(group.has_value());
const std::u16string title = u"random tab title";
GURL url = GURL("https://www.google.com");
tab_group_sync_service_->NavigateTab(unknown_group_id, local_tab_id, url,
title);
group = tab_group_sync_service_->GetGroup(unknown_group_id);
EXPECT_FALSE(group.has_value());
tab_group_sync_service_->RemoveTab(unknown_group_id, local_tab_id);
// No histograms should be recorded.
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabAdded.GroupCreateOrigin", 0u);
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabRemoved.GroupCreateOrigin", 0u);
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabNavigated.GroupCreateOrigin", 0u);
}
TEST_F(TabGroupSyncServiceTest, RemoveTab) {
base::HistogramTester histogram_tester;
// Add a new tab.
auto local_tab_id_2 = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_2,
u"random tab title", GURL("www.google.com"),
std::nullopt);
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
EXPECT_EQ(2u, group->saved_tabs().size());
// Remove tab.
tab_group_sync_service_->RemoveTab(local_group_id_1_, local_tab_id_2);
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
EXPECT_EQ(1u, group->saved_tabs().size());
VerifyCacheGuids(*group, nullptr, kTestCacheGuid, kTestCacheGuid,
std::nullopt, std::nullopt);
// Remove the last tab. The group should be removed from the model.
tab_group_sync_service_->RemoveTab(local_group_id_1_, local_tab_id_1_);
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_FALSE(group.has_value());
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabRemoved.GroupCreateOrigin", 2u);
}
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
TEST_F(TabGroupSyncServiceTest, ForceRemoveClosedTabGroupsOnStartup) {
feature_list_.InitWithFeatures(
{tab_groups::kForceRemoveClosedTabGroupsOnStartup}, {});
EXPECT_CALL(*observer_, OnInitialized()).Times(1);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_2_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_3_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
model_->LoadStoredEntries(/*groups=*/{}, /*tabs=*/{});
WaitForPostedTasks();
// Wait again as the posted task will post again.
WaitForPostedTasks();
}
#endif
TEST_F(TabGroupSyncServiceTest, CleanUpHiddenSavedTabGroupsOnStartup) {
SavedTabGroup saved_tab_group_1(test::CreateTestSavedTabGroup());
saved_tab_group_1.SetIsHidden(true);
SavedTabGroup saved_tab_group_2(test::CreateTestSavedTabGroup());
SavedTabGroup shared_group(test::CreateTestSavedTabGroup());
CollaborationId collaboration_id("foo");
shared_group.SetCollaborationId(collaboration_id);
shared_group.SetIsHidden(true);
EXPECT_CALL(*observer_, OnInitialized()).Times(1);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
saved_tab_group_1.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
model_->LoadStoredEntries(
/*groups=*/{saved_tab_group_1, saved_tab_group_2, shared_group},
/*tabs=*/{});
task_environment_.AdvanceClock(GetOriginatingSavedGroupCleanUpTimeInterval());
task_environment_.FastForwardBy(base::Seconds(10));
WaitForPostedTasks();
// Verify model internals.
ASSERT_FALSE(model_->Contains(saved_tab_group_1.saved_guid()));
ASSERT_TRUE(model_->Contains(saved_tab_group_2.saved_guid()));
ASSERT_TRUE(model_->Contains(shared_group.saved_guid()));
}
TEST_F(TabGroupSyncServiceTest,
RestoreHiddenOriginatingSavedGroupOnRemoteSharingFailure) {
// Simulate a remote transition of `group_1_` to a shared tab group.
ASSERT_THAT(tab_group_sync_service_->GetAllGroups(),
Contains(HasGuid(group_1_.saved_guid())));
SavedTabGroup shared_group = group_1_.CloneAsSharedTabGroup(kCollaborationId);
shared_group.MarkTransitionedToShared();
ASSERT_FALSE(shared_group.saved_tabs().empty());
model_->AddedFromSync(shared_group);
WaitForPostedTasks();
// Only `shared_group` should be available in the service.
ASSERT_THAT(tab_group_sync_service_->GetAllGroups(),
Contains(HasGuid(shared_group.saved_guid())));
ASSERT_THAT(tab_group_sync_service_->GetAllGroups(),
Not(Contains(HasGuid(group_1_.saved_guid()))));
// Simulate a remote deletion of `shared_group`.
model_->RemovedFromSync(shared_group.saved_guid());
WaitForPostedTasks();
// The originating saved tab group should be restored and available.
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Contains(HasGuid(group_1_.saved_guid())));
}
TEST_F(TabGroupSyncServiceTest, NavigateTab) {
base::HistogramTester histogram_tester;
auto local_tab_id_2 = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_2,
u"random tab title",
GURL("http://www.google.com"), std::nullopt);
WaitForPostedTasks();
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
auto* tab = group->GetTab(local_tab_id_2);
EXPECT_TRUE(group.has_value());
EXPECT_TRUE(tab);
VerifyCacheGuids(*group, tab, kTestCacheGuid, kTestCacheGuid, kTestCacheGuid,
std::nullopt);
// Update tab and verify observers.
std::u16string new_title = u"tab title 2";
GURL new_url = GURL("http://www.example.com");
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_2,
new_url, new_title);
WaitForPostedTasks();
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
EXPECT_EQ(2u, group->saved_tabs().size());
// Verify updated tab.
tab = group->GetTab(local_tab_id_2);
EXPECT_TRUE(tab);
EXPECT_EQ(new_title, tab->title());
EXPECT_EQ(new_url, tab->url());
VerifyCacheGuids(*group, tab, kTestCacheGuid, kTestCacheGuid, kTestCacheGuid,
kTestCacheGuid);
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabNavigated.GroupCreateOrigin", 1u);
// Update redirect chain. This should not notify observers.
SavedTabGroupTabBuilder tab_builder2;
std::vector<GURL> redirect_url_chain({new_url, GURL("www.example.com")});
tab_builder2.SetRedirectURLChain(redirect_url_chain);
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
tab_group_sync_service_->UpdateTabProperties(local_group_id_1_,
local_tab_id_2, tab_builder2);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, NavigateTabIgnoresSameUrl) {
auto local_tab_id_2 = test::GenerateRandomTabID();
std::u16string new_title = u"tab title 2";
GURL new_url = GURL("https://www.example.com");
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_2, new_title,
new_url, std::nullopt);
WaitForPostedTasks();
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
auto* tab = group->GetTab(local_tab_id_2);
EXPECT_TRUE(group.has_value());
EXPECT_TRUE(tab);
VerifyCacheGuids(*group, tab, kTestCacheGuid, kTestCacheGuid, kTestCacheGuid,
std::nullopt);
// Update tab and verify observers.
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_2,
new_url, new_title);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, NavigateTabWithEmptyUrlRestriction) {
feature_list_.InitWithFeatures({data_sharing::features::kDataSharingFeature},
{});
optimization_guide::OptimizationMetadata metadata;
// Update tab and verify observers.
std::u16string new_title = u"tab title";
GURL new_url = GURL("http://www.example.com");
// False was returned by optimization guide.
EXPECT_CALL(*decider_,
CanApplyOptimization(
new_url, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kFalse,
ByRef(metadata)));
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
new_url, new_title);
WaitForPostedTasks();
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
SavedTabGroupTab* tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(new_title, tab->title());
EXPECT_EQ(new_url, tab->url());
VerifyCacheGuids(*group, tab, kTestCacheGuid, kTestCacheGuid, kTestCacheGuid,
kTestCacheGuid);
}
TEST_F(TabGroupSyncServiceTest, NavigateTabNotBlockedByUrlRestriction) {
feature_list_.InitWithFeatures({data_sharing::features::kDataSharingFeature},
{});
optimization_guide::OptimizationMetadata metadata;
std::u16string title_1 = u"tab title";
GURL url_1 = GURL("http://www.example.com#1");
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_1, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kFalse,
ByRef(metadata)));
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_1, title_1);
WaitForPostedTasks();
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
SavedTabGroupTab* tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_1, tab->title());
EXPECT_EQ(url_1, tab->url());
proto::UrlRestriction url_restriction;
url_restriction.set_block_for_sync(true);
url_restriction.set_block_for_share(true);
url_restriction.set_block_if_only_fragment_differs(true);
metadata.set_any_metadata(optimization_guide::AnyWrapProto(url_restriction));
// Update tab and verify observers.
std::u16string title_2 = u"tab title";
GURL url_2 = GURL("http://www.foo.com");
// True was returned by optimization guide.
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_2, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kTrue,
ByRef(metadata)));
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_2, title_2);
WaitForPostedTasks();
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_2, tab->title());
EXPECT_EQ(url_2, tab->url());
}
TEST_F(TabGroupSyncServiceTest, NavigateTabBlockedDueToSameFragment) {
feature_list_.InitWithFeatures({data_sharing::features::kDataSharingFeature},
{});
optimization_guide::OptimizationMetadata metadata;
std::u16string title_1 = u"tab title";
GURL url_1 = GURL("http://www.example.com#1");
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_1, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kFalse,
ByRef(metadata)));
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_1, title_1);
WaitForPostedTasks();
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
SavedTabGroupTab* tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_1, tab->title());
EXPECT_EQ(url_1, tab->url());
proto::UrlRestriction url_restriction;
url_restriction.set_block_for_sync(true);
url_restriction.set_block_for_share(true);
url_restriction.set_block_if_only_fragment_differs(true);
metadata.set_any_metadata(optimization_guide::AnyWrapProto(url_restriction));
// Update tab and verify observers.
std::u16string title_2 = u"tab title 2";
GURL url_2 = GURL("http://www.example.com#2");
// True was returned by optimization guide.
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_2, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kTrue,
ByRef(metadata)));
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_2, title_2);
WaitForPostedTasks();
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_1, tab->title());
EXPECT_EQ(url_1, tab->url());
}
TEST_F(TabGroupSyncServiceTest, NavigateTabUpdatesAttributionForSharedGroup) {
MakeTabGroupShared(local_group_id_1_, syncer::CollaborationId("collab"));
LocalTabID local_tab_id = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id, u"title",
GURL("http://www.google.com"), std::nullopt);
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(group.has_value());
ASSERT_THAT(group->GetTab(local_tab_id),
Pointee(HasSharedAttribution(kDefaultGaiaId, kDefaultGaiaId)));
EXPECT_CALL(*mock_shared_processor(), TrackedGaiaId())
.WillOnce(Return(GaiaId("other_gaia_id")));
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id,
GURL("http://www.example.com"),
u"title 2");
group = tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(group.has_value());
EXPECT_THAT(group->GetTab(local_tab_id),
Pointee(HasSharedAttribution(kDefaultGaiaId, "other_gaia_id")));
}
TEST_F(TabGroupSyncServiceTest, NavigateTabBlockedDueToSamePath) {
feature_list_.InitWithFeatures({data_sharing::features::kDataSharingFeature},
{});
optimization_guide::OptimizationMetadata metadata;
std::u16string title_1 = u"tab title";
GURL url_1 = GURL("http://www.example.com/xyz#1");
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_1, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kFalse,
ByRef(metadata)));
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_1, title_1);
WaitForPostedTasks();
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
SavedTabGroupTab* tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_1, tab->title());
EXPECT_EQ(url_1, tab->url());
proto::UrlRestriction url_restriction;
url_restriction.set_block_for_sync(true);
url_restriction.set_block_for_share(true);
url_restriction.set_block_if_path_is_same(true);
metadata.set_any_metadata(optimization_guide::AnyWrapProto(url_restriction));
// Update tab and verify observers.
std::u16string title_2 = u"tab title 2";
GURL url_2 = GURL("http://www.example.com/xyz#2");
// True was returned by optimization guide.
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_2, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kTrue,
ByRef(metadata)));
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_2, title_2);
WaitForPostedTasks();
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_1, tab->title());
EXPECT_EQ(url_1, tab->url());
}
TEST_F(TabGroupSyncServiceTest, NavigateTabBlockedDueToSameDomain) {
feature_list_.InitWithFeatures({data_sharing::features::kDataSharingFeature},
{});
optimization_guide::OptimizationMetadata metadata;
std::u16string title_1 = u"tab title";
GURL url_1 = GURL("http://www.example.com/abc#1");
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_1, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kFalse,
ByRef(metadata)));
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_1, title_1);
WaitForPostedTasks();
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
SavedTabGroupTab* tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_1, tab->title());
EXPECT_EQ(url_1, tab->url());
proto::UrlRestriction url_restriction;
url_restriction.set_block_for_sync(true);
url_restriction.set_block_for_share(true);
url_restriction.set_block_if_domain_is_same(true);
metadata.set_any_metadata(optimization_guide::AnyWrapProto(url_restriction));
// Update tab and verify observers.
std::u16string title_2 = u"tab title 2";
GURL url_2 = GURL("http://www.example.com/xyz#2");
// True was returned by optimization guide.
EXPECT_CALL(*decider_,
CanApplyOptimization(
url_2, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kTrue,
ByRef(metadata)));
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
url_2, title_2);
WaitForPostedTasks();
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify updated tab.
tab = group->GetTab(local_tab_id_1_);
EXPECT_TRUE(tab);
EXPECT_EQ(title_1, tab->title());
EXPECT_EQ(url_1, tab->url());
}
TEST_F(TabGroupSyncServiceTest, MoveTab) {
base::HistogramTester histogram_tester;
auto local_tab_id_2 = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_2,
u"random tab title", GURL("www.google.com"),
std::nullopt);
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
auto* tab = group->GetTab(local_tab_id_2);
EXPECT_EQ(1u, tab->position());
// Move tab from position 1 to position 0.
tab_group_sync_service_->MoveTab(local_group_id_1_, local_tab_id_2, 0);
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
tab = group->GetTab(local_tab_id_2);
EXPECT_EQ(0u, tab->position());
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabsReordered.GroupCreateOrigin", 1u);
// Call API with a invalid tab ID.
tab_group_sync_service_->MoveTab(local_group_id_1_,
test::GenerateRandomTabID(), 0);
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabsReordered.GroupCreateOrigin", 1u);
}
TEST_F(TabGroupSyncServiceTest, OnTabSelected) {
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
base::HistogramTester histogram_tester;
base::Time test_start_time = base::Time::Now();
// Advance the clock, so when a tab is selected, it will get a more
// recent time than `test_start_time`.
task_environment_.AdvanceClock(base::Seconds(5));
// Add a new tab.
auto local_tab_id_2 = test::GenerateRandomTabID();
std::u16string tab_title_2 = u"random tab title";
tab_group_sync_service_->AddTab(local_group_id_1_, local_tab_id_2,
tab_title_2, GURL("www.google.com"),
std::nullopt);
{
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
CHECK(group);
// Local Tab 2 should start with no last_seen time.
EXPECT_FALSE(group->GetTab(local_tab_id_2)->last_seen_time().has_value());
}
EXPECT_CALL(*observer_,
OnTabSelected(Eq(std::set<LocalTabID>({local_tab_id_2}))));
// Select tab.
EXPECT_CALL(*coordinator_, GetSelectedTabs())
.WillRepeatedly(Return(std::set<LocalTabID>({local_tab_id_2})));
tab_group_sync_service_->OnTabSelected(local_group_id_1_, local_tab_id_2,
tab_title_2);
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
CHECK(group);
// Local Tab 2 should get a last_seen time.
const SavedTabGroupTab* tab = group->GetTab(local_tab_id_2);
EXPECT_TRUE(tab->last_seen_time().has_value());
EXPECT_GT(tab->last_seen_time().value(), test_start_time);
histogram_tester.ExpectTotalCount(
"TabGroups.Sync.TabGroup.TabSelected.GroupCreateOrigin", 1u);
}
TEST_F(TabGroupSyncServiceTest,
TabGroupUpdateFromSyncWillUpdateLastSeenTimestampOfFocusedTab) {
// Initialize a shared tab group with one tab. The tab doesn't have last seen
// timestamp set.
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
const SavedTabGroup* group = model_->Get(local_group_id_1_);
CHECK(group);
base::Uuid shared_group_id = group->saved_guid();
const SavedTabGroupTab* tab = group->GetTab(local_tab_id_1_);
EXPECT_FALSE(tab->last_seen_time().has_value());
// Fake that the tab is selected.
EXPECT_CALL(*coordinator_, GetSelectedTabs())
.WillRepeatedly(Return(std::set<LocalTabID>({local_tab_id_1_})));
// Update the group from sync. Since the tab is selected, it should
// result in updating the last seen timestamp.
TabGroupVisualData visual_data = test::CreateTabGroupVisualData();
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(shared_group_id),
Eq(TriggerSource::REMOTE)))
.Times(1);
model_->UpdatedVisualDataFromSync(shared_group_id, &visual_data);
WaitForPostedTasks();
EXPECT_TRUE(tab->last_seen_time().has_value());
}
TEST_F(TabGroupSyncServiceTest, OnTabSelectedForNonExistingTab) {
auto local_tab_group_id_2 = test::GenerateRandomTabGroupID();
auto local_tab_id_2 = test::GenerateRandomTabID();
auto group = tab_group_sync_service_->GetGroup(local_group_id_1_);
std::u16string tab_title_2 = u"random tab title";
EXPECT_CALL(*observer_,
OnTabSelected(Eq(std::set<LocalTabID>({local_tab_id_2}))))
.Times(3);
// Select tab.
EXPECT_CALL(*coordinator_, GetSelectedTabs())
.WillRepeatedly(Return(std::set<LocalTabID>({local_tab_id_2})));
tab_group_sync_service_->OnTabSelected(local_group_id_1_, local_tab_id_2,
tab_title_2);
tab_group_sync_service_->OnTabSelected(local_tab_group_id_2, local_tab_id_2,
tab_title_2);
tab_group_sync_service_->OnTabSelected(std::nullopt, local_tab_id_2,
tab_title_2);
}
TEST_F(TabGroupSyncServiceTest, RecordTabGroupEvent) {
base::HistogramTester histogram_tester;
EventDetails event_details(TabGroupEvent::kTabGroupOpened);
event_details.local_tab_group_id = local_group_id_1_;
event_details.opening_source = OpeningSource::kAutoOpenedFromSync;
tab_group_sync_service_->RecordTabGroupEvent(event_details);
histogram_tester.ExpectTotalCount("TabGroups.Sync.TabGroup.Opened.Reason",
1u);
}
TEST_F(TabGroupSyncServiceTest, UpdateArchivalStatus) {
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group.has_value());
// Verify the archive status is defaulted to off.
EXPECT_FALSE(group->archival_time().has_value());
// Expect the observers to be called each time the status is updated.
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(2);
// Set the archival status and verify.
tab_group_sync_service_->UpdateArchivalStatus(group_1_.saved_guid(), true);
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
WaitForPostedTasks();
EXPECT_TRUE(group->archival_time().has_value());
// Reset the archival status and verify.
tab_group_sync_service_->UpdateArchivalStatus(group_1_.saved_guid(), false);
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
WaitForPostedTasks();
EXPECT_FALSE(group->archival_time().has_value());
}
TEST_F(TabGroupSyncServiceTest, UpdateLocalTabId) {
auto tab_guid = group_1_.saved_tabs()[0].saved_tab_guid();
auto local_tab_id_2 = test::GenerateRandomTabID();
tab_group_sync_service_->UpdateLocalTabId(local_group_id_1_, tab_guid,
local_tab_id_2);
auto group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_TRUE(group.has_value());
EXPECT_EQ(1u, group->saved_tabs().size());
// Verify updated tab.
auto* updated_tab = group->GetTab(tab_guid);
EXPECT_TRUE(updated_tab);
EXPECT_EQ(local_tab_id_2, updated_tab->local_tab_id().value());
}
TEST_F(TabGroupSyncServiceTest, AddObserverBeforeInitialize) {
EXPECT_CALL(*observer_, OnInitialized()).Times(1);
model_->LoadStoredEntries(/*groups=*/{}, /*tabs=*/{});
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, AddObserverAfterInitialize) {
EXPECT_CALL(*observer_, OnInitialized()).Times(1);
model_->LoadStoredEntries(/*groups=*/{}, /*tabs=*/{});
WaitForPostedTasks();
tab_group_sync_service_->RemoveObserver(observer_.get());
EXPECT_CALL(*observer_, OnInitialized()).Times(1);
tab_group_sync_service_->AddObserver(observer_.get());
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupAddedFromRemoteSource) {
SavedTabGroup group_4 = test::CreateTestSavedTabGroup();
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(0);
model_->AddedFromSync(group_4);
// Verify that the observers are posted instead of directly notifying.
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(1);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupAddedFromLocalSource) {
SavedTabGroup group_4 = test::CreateTestSavedTabGroup();
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
model_->AddedLocally(group_4);
// Verify that the observers are posted instead of directly notifying.
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, SharedTabGroupAddedWillWaitForCollaboration) {
EXPECT_EQ(tab_group_sync_service_->GetAllGroups().size(), 3u);
// Create shared tab group 4 for which collaboration ID isn't yet available.
CollaborationId collaboration_id_1("foo_1");
SavedTabGroup group_4 = test::CreateTestSavedTabGroup();
group_4.SetCollaborationId(collaboration_id_1);
ON_CALL(*collaboration_finder_,
IsCollaborationAvailable(Eq(collaboration_id_1)))
.WillByDefault(testing::Return(false));
// Create shared tab group 5 for which collaboration ID is already available.
CollaborationId collaboration_id_2("foo_2");
SavedTabGroup group_5 = test::CreateTestSavedTabGroup();
group_5.SetCollaborationId(collaboration_id_2);
ON_CALL(*collaboration_finder_,
IsCollaborationAvailable(Eq(collaboration_id_2)))
.WillByDefault(testing::Return(true));
// Add both the groups to model from sync. Observers won't be notified for
// group 4 but will be notified for group 5.
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(0);
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_5.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(1);
model_->AddedFromSync(group_4);
model_->AddedFromSync(group_5);
WaitForPostedTasks();
// GetAllGroups will skip group 4 as it's collaboration isn't ready yet.
EXPECT_EQ(tab_group_sync_service_->GetAllGroups().size(), 4u);
// Send an update to group 4 from sync. The update won't be notified as the
// collaboration is pending.
TabGroupVisualData visual_data = test::CreateTabGroupVisualData();
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(0);
model_->UpdatedVisualDataFromSync(group_4.saved_guid(), &visual_data);
// Make the collaboration available for group 4. Observer will be notified.
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(1);
ON_CALL(*collaboration_finder_,
IsCollaborationAvailable(Eq(collaboration_id_1)))
.WillByDefault(testing::Return(true));
tab_group_sync_service_->OnCollaborationAvailable(collaboration_id_1);
WaitForPostedTasks();
EXPECT_EQ(tab_group_sync_service_->GetAllGroups().size(), 5u);
}
TEST_F(TabGroupSyncServiceTest, EmptyGroupAddedFromLocalSource) {
EXPECT_EQ(tab_group_sync_service_->GetAllGroups().size(), 3u);
SavedTabGroup group_4 = test::CreateTestSavedTabGroupWithNoTabs();
LocalTabGroupID tab_group_id = test::GenerateRandomTabGroupID();
group_4.SetLocalGroupId(tab_group_id);
// Add an empty group. Observers shouldn't get notified.
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
model_->AddedLocally(group_4);
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
WaitForPostedTasks();
// Empty group should be excluded from GetAllGroups().
EXPECT_EQ(tab_group_sync_service_->GetAllGroups().size(), 3u);
// Add a tab locally. Observers should get notified.
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
auto local_tab_id_2 = test::GenerateRandomTabID();
tab_group_sync_service_->AddTab(tab_group_id, local_tab_id_2,
u"random tab title", GURL("www.google.com"),
std::nullopt);
WaitForPostedTasks();
EXPECT_EQ(tab_group_sync_service_->GetAllGroups().size(), 4u);
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupUpdatedFromRemoteSource) {
TabGroupVisualData visual_data = test::CreateTabGroupVisualData();
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(0);
// Verify that the observers are posted instead of directly notifying.
model_->UpdatedVisualDataFromSync(group_1_.saved_guid(), &visual_data);
Sequence s;
EXPECT_CALL(*observer_,
BeforeTabGroupUpdateFromRemote(Eq(group_1_.saved_guid())))
.InSequence(s);
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::REMOTE)))
.InSequence(s);
EXPECT_CALL(*observer_,
AfterTabGroupUpdateFromRemote(Eq(group_1_.saved_guid())))
.InSequence(s);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupUpdatedFromLocalSource) {
TabGroupVisualData visual_data = test::CreateTabGroupVisualData();
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(0);
// Verify that the observers are posted instead of directly notifying.
model_->UpdateVisualDataLocally(group_1_.local_group_id().value(),
&visual_data);
EXPECT_CALL(*observer_, OnTabGroupUpdated(UuidEq(group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
EXPECT_CALL(*observer_, BeforeTabGroupUpdateFromRemote).Times(0);
EXPECT_CALL(*observer_, AfterTabGroupUpdateFromRemote).Times(0);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupUpdatedOnTabGroupIdMappingChange) {
// Close a group.
EXPECT_CALL(*observer_, OnTabGroupLocalIdChanged(Eq(group_1_.saved_guid()),
Eq(std::nullopt)))
.Times(1);
model_->OnGroupClosedInTabStrip(local_group_id_1_);
// Open a group.
LocalTabGroupID local_id_2 = test::GenerateRandomTabGroupID();
EXPECT_CALL(*observer_, OnTabGroupLocalIdChanged(Eq(group_2_.saved_guid()),
Eq(local_id_2)))
.Times(1);
model_->OnGroupOpenedInTabStrip(group_2_.saved_guid(), local_id_2);
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupsReordered) {
EXPECT_CALL(*observer_, OnTabGroupsReordered(Eq(TriggerSource::LOCAL)))
.Times(1);
model_->ReorderGroupLocally(group_1_.saved_guid(), 1);
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_EQ(1, group->position());
// Sync changes do not immediately update the positions. We use eventual
// consistency which means we must wait for other sync position changes to
// come in which will guarantee everything is in the right spot.
// For this test, it is okay to keep the original position, as long as we get
// the observer notification.
EXPECT_CALL(*observer_, OnTabGroupsReordered(Eq(TriggerSource::REMOTE)))
.Times(1);
model_->ReorderGroupFromSync(group_1_.saved_guid(), 0);
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_EQ(1, group->position());
}
TEST_F(TabGroupSyncServiceTest, TabIDMappingIsCleardOnGroupClose) {
auto group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(group->local_group_id().has_value());
EXPECT_TRUE(group->saved_tabs()[0].local_tab_id().has_value());
// Close a group.
model_->OnGroupClosedInTabStrip(local_group_id_1_);
// Verify that tab IDs are unmapped.
group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_FALSE(group->local_group_id().has_value());
EXPECT_FALSE(group->saved_tabs()[0].local_tab_id().has_value());
}
TEST_F(TabGroupSyncServiceTest,
EmptyGroupsAreExcludedFromGetCallAndObserverMethods) {
auto all_groups = tab_group_sync_service_->GetAllGroups();
EXPECT_EQ(all_groups.size(), 3u);
// Create a group with no tabs. Observers won't be notified.
SavedTabGroup group_4 = test::CreateTestSavedTabGroupWithNoTabs();
base::Uuid group_id = group_4.saved_guid();
EXPECT_CALL(*observer_,
OnTabGroupAdded(UuidEq(group_id), Eq(TriggerSource::REMOTE)))
.Times(0);
model_->AddedFromSync(group_4);
WaitForPostedTasks();
// Verify that GetAllGroups call will not return it.
all_groups = tab_group_sync_service_->GetAllGroups();
EXPECT_EQ(all_groups.size(), 3u);
// Update visuals. Observers still won't be notified.
EXPECT_CALL(*observer_,
OnTabGroupAdded(UuidEq(group_id), Eq(TriggerSource::REMOTE)))
.Times(0);
EXPECT_CALL(*observer_,
OnTabGroupUpdated(UuidEq(group_id), Eq(TriggerSource::REMOTE)))
.Times(0);
TabGroupVisualData visual_data = test::CreateTabGroupVisualData();
model_->UpdatedVisualDataFromSync(group_id, &visual_data);
WaitForPostedTasks();
// Add a tab to the group. Observers will be notified as an Add event.
EXPECT_CALL(*observer_,
OnTabGroupAdded(UuidEq(group_id), Eq(TriggerSource::REMOTE)))
.Times(1);
EXPECT_CALL(*observer_,
OnTabGroupUpdated(UuidEq(group_id), Eq(TriggerSource::REMOTE)))
.Times(0);
SavedTabGroupTab tab =
test::CreateSavedTabGroupTab("A_Link", u"Tab", group_id);
model_->AddTabToGroupFromSync(group_id, tab);
WaitForPostedTasks();
// Update visuals. Observers will be notified as an Update event.
EXPECT_CALL(*observer_,
OnTabGroupAdded(UuidEq(group_id), Eq(TriggerSource::REMOTE)))
.Times(0);
EXPECT_CALL(*observer_,
OnTabGroupUpdated(UuidEq(group_id), Eq(TriggerSource::REMOTE)))
.Times(1);
model_->UpdatedVisualDataFromSync(group_id, &visual_data);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupRemovedFromRemoteSource) {
// Removig group having local ID.
EXPECT_CALL(*observer_,
OnTabGroupRemoved(
testing::TypedEq<const LocalTabGroupID&>(local_group_id_1_),
Eq(TriggerSource::REMOTE)))
.Times(1);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_1_.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(1);
model_->RemovedFromSync(group_1_.saved_guid());
WaitForPostedTasks();
// Remove a group with no local ID.
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_2_.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(1);
model_->RemovedFromSync(group_2_.saved_guid());
WaitForPostedTasks();
// Try removing a group that doesn't exist.
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_1_.saved_guid()),
Eq(TriggerSource::REMOTE)))
.Times(0);
model_->RemovedFromSync(group_1_.saved_guid());
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupRemovedFromLocalSource) {
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_1_.saved_guid()),
Eq(TriggerSource::LOCAL)))
.Times(1);
model_->RemovedLocally(group_1_.local_group_id().value());
}
TEST_F(TabGroupSyncServiceTest, OnSyncBridgeUpdateTypeChanged) {
EXPECT_CALL(*observer_, OnSyncBridgeUpdateTypeChanged).Times(0);
model_->OnSyncBridgeUpdateTypeChanged(SyncBridgeUpdateType::kDisableSync);
testing::Mock::VerifyAndClearExpectations(observer_.get());
// Verify that the observers are posted instead of directly notifying.
EXPECT_CALL(*observer_, OnSyncBridgeUpdateTypeChanged(
Eq(SyncBridgeUpdateType::kDisableSync)))
.Times(1);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, TasksArePostedInTheSameSequenceAsOriginated) {
Sequence s;
EXPECT_CALL(*observer_, OnSyncBridgeUpdateTypeChanged(
Eq(SyncBridgeUpdateType::kInitialMerge)))
.InSequence(s);
EXPECT_CALL(*observer_, OnTabGroupAdded(UuidEq(group_4_.saved_guid()),
Eq(TriggerSource::REMOTE)))
.InSequence(s);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group_1_.saved_guid()),
Eq(TriggerSource::REMOTE)))
.InSequence(s);
EXPECT_CALL(*observer_, OnSyncBridgeUpdateTypeChanged(
Eq(SyncBridgeUpdateType::kDisableSync)))
.InSequence(s);
model_->OnSyncBridgeUpdateTypeChanged(SyncBridgeUpdateType::kInitialMerge);
model_->AddedFromSync(group_4_);
model_->RemovedFromSync(group_1_.saved_guid());
model_->OnSyncBridgeUpdateTypeChanged(SyncBridgeUpdateType::kDisableSync);
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, GetURLRestrictionFailed) {
GURL test_url("http://test.com/");
optimization_guide::OptimizationMetadata metadata;
{
// False was returned by optimization guide.
EXPECT_CALL(
*decider_,
CanApplyOptimization(
test_url, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kFalse,
ByRef(metadata)));
base::RunLoop run_loop;
tab_group_sync_service_->GetURLRestriction(
test_url, base::BindOnce([](const std::optional<proto::UrlRestriction>&
restriction) {
ASSERT_FALSE(restriction);
}).Then(run_loop.QuitClosure()));
run_loop.Run();
}
{
// URL was not found by optimization guide.
EXPECT_CALL(
*decider_,
CanApplyOptimization(
test_url, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kUnknown,
ByRef(metadata)));
base::RunLoop run_loop;
tab_group_sync_service_->GetURLRestriction(
test_url, base::BindOnce([](const std::optional<proto::UrlRestriction>&
restriction) {
ASSERT_FALSE(restriction);
}).Then(run_loop.QuitClosure()));
run_loop.Run();
}
{
// Optimization guide returns an empty metadata.
EXPECT_CALL(
*decider_,
CanApplyOptimization(
test_url, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kTrue,
ByRef(metadata)));
base::RunLoop run_loop;
tab_group_sync_service_->GetURLRestriction(
test_url, base::BindOnce([](const std::optional<proto::UrlRestriction>&
restriction) {
ASSERT_FALSE(restriction);
}).Then(run_loop.QuitClosure()));
run_loop.Run();
}
{
// Valid response.
proto::UrlRestriction url_restriction;
url_restriction.set_block_for_sync(true);
url_restriction.set_block_for_share(true);
metadata.set_any_metadata(
optimization_guide::AnyWrapProto(url_restriction));
EXPECT_CALL(
*decider_,
CanApplyOptimization(
test_url, optimization_guide::proto::SAVED_TAB_GROUP,
An<optimization_guide::OptimizationGuideDecisionCallback>()))
.WillOnce(base::test::RunOnceCallback<2>(
optimization_guide::OptimizationGuideDecision::kTrue,
ByRef(metadata)));
base::RunLoop run_loop;
tab_group_sync_service_->GetURLRestriction(
test_url, base::BindOnce([](const std::optional<proto::UrlRestriction>&
restriction) {
EXPECT_TRUE(restriction);
EXPECT_TRUE(restriction->block_for_sync());
EXPECT_TRUE(restriction->block_for_share());
}).Then(run_loop.QuitClosure()));
run_loop.Run();
}
}
TEST_F(TabGroupSyncServiceTest, SharedTabGroupTabTitleSanitizedWhenNavigate) {
MakeTabGroupShared(local_group_id_1_, syncer::CollaborationId("collab"));
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), SizeIs(1));
SavedTabGroupTab tab =
tab_group_sync_service_->GetGroup(local_group_id_1_)->saved_tabs()[0];
tab_group_sync_service_->UpdateLocalTabId(
local_group_id_1_, tab.saved_tab_guid(), local_tab_id_1_);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
GURL("https://foo.com"), u"title2");
tab = tab_group_sync_service_->GetGroup(local_group_id_1_)->saved_tabs()[0];
EXPECT_EQ(tab.title(), u"foo.com");
}
TEST_F(TabGroupSyncServiceTest, TabTitleSanitizedAfterMakeTabGroupShared) {
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
GURL("https://foo.com"), u"title");
MakeTabGroupShared(local_group_id_1_, syncer::CollaborationId("collab"));
EXPECT_EQ(
tab_group_sync_service_->GetGroup(local_group_id_1_)->saved_tabs().size(),
1u);
SavedTabGroupTab tab =
tab_group_sync_service_->GetGroup(local_group_id_1_)->saved_tabs()[0];
EXPECT_EQ(tab.title(), u"foo.com");
}
TEST_F(TabGroupSyncServiceTest, GetTabTitleFromOptGuide) {
feature_list_.InitWithFeatures({data_sharing::features::kDataSharingFeature},
{});
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
GURL("https://foo.com"), u"title");
EXPECT_CALL(*decider_,
CanApplyOptimization(
_, optimization_guide::proto::PAGE_ENTITIES,
Matcher<optimization_guide::OptimizationMetadata*>(_)))
.WillOnce(
DoAll(SetArgPointee<2>(GetPageEntitiesMetadata("alt1")),
Return(optimization_guide::OptimizationGuideDecision::kTrue)));
MakeTabGroupShared(local_group_id_1_, syncer::CollaborationId("collab"));
SavedTabGroupTab tab =
tab_group_sync_service_->GetGroup(local_group_id_1_)->saved_tabs()[0];
EXPECT_EQ(tab.title(), u"alt1");
EXPECT_CALL(*decider_,
CanApplyOptimization(
_, optimization_guide::proto::PAGE_ENTITIES,
Matcher<optimization_guide::OptimizationMetadata*>(_)))
.WillOnce(
DoAll(SetArgPointee<2>(GetPageEntitiesMetadata("alt2")),
Return(optimization_guide::OptimizationGuideDecision::kTrue)));
tab_group_sync_service_->UpdateLocalTabId(
local_group_id_1_, tab.saved_tab_guid(), local_tab_id_1_);
tab_group_sync_service_->NavigateTab(local_group_id_1_, local_tab_id_1_,
GURL("https://foo.com"), u"title2");
tab = tab_group_sync_service_->GetGroup(local_group_id_1_)->saved_tabs()[0];
EXPECT_EQ(tab.title(), u"alt2");
}
TEST_F(TabGroupSyncServiceTest, MakeTabGroupShared) {
ASSERT_EQ(group_1_.saved_tabs().size(), 1u);
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
model_->UpdateLastUpdaterCacheGuidForGroup(
kTestCacheGuid, local_group_id_1_,
group_1_.saved_tabs()[0].local_tab_id());
model_->UpdateLastUserInteractionTimeLocally(local_group_id_1_);
// `group_1_` is a copy hence it can't be used to verify updated fields.
std::optional<SavedTabGroup> originating_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(originating_group.has_value());
ASSERT_FALSE(originating_group->is_shared_tab_group());
ASSERT_TRUE(originating_group->position().has_value());
// Verify the fields which are not expected to be copied over to the shared
// group, apart from the local tab ID.
ASSERT_TRUE(originating_group->creator_cache_guid().has_value());
ASSERT_TRUE(originating_group->local_group_id().has_value());
ASSERT_TRUE(originating_group->last_updater_cache_guid().has_value());
ASSERT_FALSE(originating_group->last_user_interaction_time().is_null());
for (const SavedTabGroupTab& tab : originating_group->saved_tabs()) {
ASSERT_TRUE(tab.local_tab_id().has_value());
ASSERT_TRUE(tab.creator_cache_guid().has_value());
ASSERT_TRUE(tab.last_updater_cache_guid().has_value());
}
// Transition the saved tab group to a shared tab group, and excessively
// verify the contents of the shared group.
Sequence s;
EXPECT_CALL(*coordinator_, DisconnectLocalTabGroup(local_group_id_1_))
.InSequence(s);
EXPECT_CALL(*coordinator_, ConnectLocalTabGroup(_, local_group_id_1_))
.InSequence(s);
// Advance the clock to ensure that the shared group has a different
// creation time than the originating group.
task_environment_.FastForwardBy(base::Seconds(1));
EXPECT_CALL(*observer_, OnTabGroupMigrated(_, group_1_.saved_guid(),
TriggerSource::LOCAL));
EXPECT_CALL(*decider_, RegisterOptimizationTypes(ElementsAre(
optimization_guide::proto::PAGE_ENTITIES)))
.Times(1);
MakeTabGroupShared(local_group_id_1_,
syncer::CollaborationId("collaboration"));
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), SizeIs(1));
// The originating group should remain mostly unchanged.
originating_group = tab_group_sync_service_->GetGroup(group_1_.saved_guid());
ASSERT_TRUE(originating_group.has_value());
EXPECT_FALSE(originating_group->is_shared_tab_group());
EXPECT_EQ(originating_group->position(), group_1_.position());
EXPECT_EQ(originating_group->creator_cache_guid(), kTestCacheGuid);
EXPECT_EQ(originating_group->last_updater_cache_guid(), kTestCacheGuid);
EXPECT_FALSE(originating_group->last_user_interaction_time().is_null());
// However the originating group should be disconnected from the local tab
// group.
EXPECT_EQ(originating_group->local_group_id(), std::nullopt);
// Verify shared tab group fields.
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group.has_value());
EXPECT_NE(shared_group->saved_guid(), group_1_.saved_guid());
EXPECT_TRUE(shared_group->saved_guid().is_valid());
EXPECT_EQ(shared_group->collaboration_id(), CollaborationId("collaboration"));
EXPECT_EQ(shared_group->GetOriginatingTabGroupGuid(),
originating_group->saved_guid());
EXPECT_EQ(shared_group->local_group_id(), local_group_id_1_);
EXPECT_EQ(shared_group->position(), group_1_.position());
// Verify that both groups have the same fields.
EXPECT_EQ(shared_group->title(), group_1_.title());
EXPECT_EQ(shared_group->color(), group_1_.color());
// Verify that the shared group has updated fields.
EXPECT_GT(shared_group->creation_time(), originating_group->creation_time());
EXPECT_GT(shared_group->update_time(), originating_group->update_time());
EXPECT_EQ(shared_group->creator_cache_guid(), std::nullopt);
EXPECT_EQ(shared_group->last_updater_cache_guid(), std::nullopt);
EXPECT_TRUE(shared_group->last_user_interaction_time().is_null());
// Verify group tabs.
ASSERT_EQ(shared_group->saved_tabs().size(), group_1_.saved_tabs().size());
EXPECT_FALSE(shared_group->saved_tabs().empty());
for (size_t i = 0; i < shared_group->saved_tabs().size(); ++i) {
const SavedTabGroupTab& shared_tab = shared_group->saved_tabs()[i];
const SavedTabGroupTab& saved_tab = originating_group->saved_tabs()[i];
// Verify the same fields.
EXPECT_EQ(shared_tab.url(), saved_tab.url());
EXPECT_EQ(shared_tab.title(), saved_tab.title());
EXPECT_EQ(shared_tab.favicon(), saved_tab.favicon());
EXPECT_EQ(shared_tab.saved_group_guid(), shared_group->saved_guid());
// Verify updated fields.
EXPECT_NE(shared_tab.saved_tab_guid(), saved_tab.saved_tab_guid());
EXPECT_EQ(shared_tab.creator_cache_guid(), std::nullopt);
EXPECT_NE(saved_tab.creator_cache_guid(), std::nullopt);
EXPECT_EQ(shared_tab.last_updater_cache_guid(), std::nullopt);
EXPECT_NE(saved_tab.last_updater_cache_guid(), std::nullopt);
EXPECT_GT(shared_group->creation_time(), saved_tab.creation_time());
EXPECT_GT(shared_tab.update_time(), saved_tab.update_time());
EXPECT_NE(shared_tab.local_tab_id(), std::nullopt);
EXPECT_EQ(saved_tab.local_tab_id(), std::nullopt);
// The local tab ID should remain the same. Use `group_1_` to verify because
// it's a copy of the originating group before the migration.
EXPECT_EQ(shared_tab.local_tab_id(),
group_1_.saved_tabs()[i].local_tab_id());
// Do not verify the position of the original tab because its meaning
// differs for shared tab groups: it's the index of the tab in the shared
// group.
EXPECT_EQ(shared_tab.position(), i);
}
// The originating group will be removed after some time.
EXPECT_CALL(*observer_,
OnTabGroupRemoved(group_1_.saved_guid(), TriggerSource::LOCAL));
task_environment_.FastForwardBy(
GetOriginatingSavedGroupCleanUpTimeInterval());
}
TEST_F(TabGroupSyncServiceTest, ShouldRunCallbackOnMakeTabGroupShared) {
ASSERT_EQ(group_1_.saved_tabs().size(), 1u);
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
base::MockCallback<TabGroupSyncService::TabGroupSharingCallback>
mock_callback;
EXPECT_CALL(mock_callback,
Run(TabGroupSyncService::TabGroupSharingResult::kSuccess));
tab_group_sync_service_->MakeTabGroupShared(
local_group_id_1_, syncer::CollaborationId("collaboration"),
mock_callback.Get());
// The new group replaces the originating one asynchronously.
WaitForPostedTasks();
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), SizeIs(1));
// Simulate the group to be committed to the server.
model_->MarkTransitionedToShared(
model_->GetSharedTabGroupsOnly().front()->saved_guid());
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest,
MakeTabGroupShared_ShouldWaitForInitialMergeCompletion) {
ASSERT_EQ(group_1_.saved_tabs().size(), 1u);
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
base::MockCallback<TabGroupSyncService::TabGroupSharingCallback>
mock_callback;
// Mimic the state where we receive a MakeTabGroupShared call while user
// hasn't completed sign-in.
EXPECT_CALL(*mock_shared_processor(), TrackedGaiaId())
.WillRepeatedly(Return(GaiaId()));
tab_group_sync_service_->MakeTabGroupShared(
local_group_id_1_, syncer::CollaborationId("collaboration"),
mock_callback.Get());
WaitForPostedTasks();
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
// Mimic initial merge completion.
EXPECT_CALL(*mock_shared_processor(), TrackedGaiaId())
.WillRepeatedly(Return(GaiaId("some_gaia")));
model_->OnSyncBridgeUpdateTypeChanged(
SyncBridgeUpdateType::kCompletedInitialMergeThisSession);
WaitForPostedTasks();
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), SizeIs(1));
// Simulate the group to be committed to the server, which will invoke the
// client callback.
EXPECT_CALL(mock_callback,
Run(TabGroupSyncService::TabGroupSharingResult::kSuccess));
model_->MarkTransitionedToShared(
model_->GetSharedTabGroupsOnly().front()->saved_guid());
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, ShouldIgnoreUpdatesWhileTransitioningToShared) {
ASSERT_EQ(group_1_.saved_tabs().size(), 1u);
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
tab_group_sync_service_->MakeTabGroupShared(
local_group_id_1_, syncer::CollaborationId("collaboration"),
base::DoNothing());
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), SizeIs(1));
const SavedTabGroup* shared_group = model_->GetSharedTabGroupsOnly().front();
ASSERT_TRUE(shared_group->is_transitioning_to_shared());
// The group should ignore any updates to the model while transitioning.
EXPECT_CALL(*observer_, OnTabGroupUpdated).Times(0);
model_->MergeRemoteGroupMetadata(
shared_group->saved_guid(), u"New title", shared_group->color(),
/*position=*/std::nullopt, /*creator_cache_guid=*/std::nullopt,
/*last_updater_cache_guid=*/std::nullopt,
/*update_time=*/base::Time::Now(), /*updated_by=*/GaiaId("user_id"));
testing::Mock::VerifyAndClearExpectations(observer_.get());
// Once the group is transitioned, updates should be propagated.
model_->MarkTransitionedToShared(shared_group->saved_guid());
WaitForPostedTasks();
EXPECT_CALL(*observer_,
OnTabGroupUpdated(HasGuid(shared_group->saved_guid()), _));
model_->MergeRemoteGroupMetadata(
shared_group->saved_guid(), u"New title 2", shared_group->color(),
/*position=*/std::nullopt, /*creator_cache_guid=*/std::nullopt,
/*last_updater_cache_guid=*/std::nullopt,
/*update_time=*/base::Time::Now(), /*updated_by=*/GaiaId("user_id"));
WaitForPostedTasks();
}
TEST_F(TabGroupSyncServiceTest, ShouldTimeoutOnMakeTabGroupShared) {
ASSERT_EQ(group_1_.saved_tabs().size(), 1u);
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
base::MockCallback<TabGroupSyncService::TabGroupSharingCallback>
mock_callback;
EXPECT_CALL(mock_callback,
Run(TabGroupSyncService::TabGroupSharingResult::kTimedOut));
tab_group_sync_service_->MakeTabGroupShared(
local_group_id_1_, syncer::CollaborationId("collaboration"),
mock_callback.Get());
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), SizeIs(1));
WaitForPostedTasks();
task_environment_.FastForwardBy(base::Minutes(1));
WaitForPostedTasks();
// The shared group should be removed from the model while the originating
// group should remain.
EXPECT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
EXPECT_THAT(model_->Get(group_1_.saved_guid()), NotNull());
// The originating group should remain unchanged.
ASSERT_TRUE(
tab_group_sync_service_->GetGroup(group_1_.saved_guid()).has_value());
ASSERT_TRUE(tab_group_sync_service_->GetGroup(local_group_id_1_).has_value());
EXPECT_EQ(group_1_.saved_guid(),
tab_group_sync_service_->GetGroup(local_group_id_1_)->saved_guid());
}
TEST_F(TabGroupSyncServiceTest, MakeTabGroupShared_FinishMigrationOnStartup) {
ASSERT_EQ(group_1_.saved_tabs().size(), 1u);
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), IsEmpty());
SavedTabGroup shared_group =
group_1_.CloneAsSharedTabGroup(CollaborationId("collab"));
shared_group.MarkTransitionedToShared();
model_->AddedLocally(shared_group);
task_environment_.FastForwardBy(base::Minutes(1));
WaitForPostedTasks();
ASSERT_THAT(model_->GetSharedTabGroupsOnly(), SizeIs(1));
// The originating group should be disconnected from the local tab
// group and become hidden.
std::optional<SavedTabGroup> originating_group =
tab_group_sync_service_->GetGroup(group_1_.saved_guid());
EXPECT_TRUE(originating_group.has_value());
EXPECT_FALSE(originating_group->is_shared_tab_group());
EXPECT_EQ(originating_group->local_group_id(), std::nullopt);
EXPECT_TRUE(originating_group->is_hidden());
std::optional<SavedTabGroup> shared_tab_group =
tab_group_sync_service_->GetGroup(shared_group.saved_guid());
EXPECT_TRUE(shared_tab_group.has_value());
EXPECT_TRUE(shared_tab_group->is_shared_tab_group());
EXPECT_EQ(shared_tab_group->local_group_id(), local_group_id_1_);
EXPECT_FALSE(shared_tab_group->is_hidden());
}
TEST_F(TabGroupSyncServiceTest, AboutToUnShareTabGroup) {
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_FALSE(shared_group->is_transitioning_to_saved());
tab_group_sync_service_->AboutToUnShareTabGroup(local_group_id_1_,
base::DoNothing());
shared_group = tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_TRUE(shared_group->is_transitioning_to_saved());
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupUnShareFailed) {
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
// Unshare the tab group and fail it.
tab_group_sync_service_->AboutToUnShareTabGroup(local_group_id_1_,
base::DoNothing());
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_TRUE(shared_group->is_transitioning_to_saved());
tab_group_sync_service_->OnTabGroupUnShareComplete(local_group_id_1_, false);
shared_group = tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_TRUE(shared_group->is_transitioning_to_saved());
}
TEST_F(TabGroupSyncServiceTest, OnTabGroupUnShareSucceeded) {
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
// Unshare the tab group.
tab_group_sync_service_->AboutToUnShareTabGroup(local_group_id_1_,
base::DoNothing());
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_TRUE(shared_group->is_transitioning_to_saved());
// Transition the shared tab group to a saved tab group.
Sequence s;
EXPECT_CALL(*coordinator_, DisconnectLocalTabGroup(local_group_id_1_))
.InSequence(s);
EXPECT_CALL(*coordinator_, ConnectLocalTabGroup(_, local_group_id_1_))
.InSequence(s);
EXPECT_CALL(*observer_, OnTabGroupMigrated(_, shared_group->saved_guid(),
TriggerSource::LOCAL));
// Advance the clock to ensure that the new saved group has a different
// creation time than the shared group.
task_environment_.FastForwardBy(base::Seconds(1));
tab_group_sync_service_->OnTabGroupUnShareComplete(local_group_id_1_, true);
shared_group = tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_TRUE(shared_group->is_transitioning_to_saved());
// The new group replaces the originating one asynchronously.
WaitForPostedTasks();
// The originating group should have empty local group id now.
std::optional<SavedTabGroup> originating_group =
tab_group_sync_service_->GetGroup(shared_group->saved_guid());
ASSERT_TRUE(originating_group.has_value());
EXPECT_TRUE(originating_group->is_shared_tab_group());
EXPECT_EQ(originating_group->local_group_id(), std::nullopt);
std::optional<SavedTabGroup> saved_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
// Verify that both groups have the same fields.
EXPECT_EQ(shared_group->title(), saved_group->title());
EXPECT_EQ(shared_group->color(), saved_group->color());
EXPECT_EQ(shared_group->position(), saved_group->position());
EXPECT_EQ(saved_group->update_time(), shared_group->update_time());
// Verify that the shared group has updated fields.
ASSERT_FALSE(saved_group->is_shared_tab_group());
EXPECT_GT(saved_group->creation_time(), shared_group->creation_time());
EXPECT_EQ(saved_group->creator_cache_guid(), kTestCacheGuid);
EXPECT_EQ(saved_group->last_updater_cache_guid(), std::nullopt);
EXPECT_TRUE(saved_group->last_user_interaction_time().is_null());
// Verify shared tab group fields.
EXPECT_NE(saved_group->saved_guid(), shared_group->saved_guid());
EXPECT_TRUE(saved_group->saved_guid().is_valid());
EXPECT_EQ(saved_group->GetOriginatingTabGroupGuid(),
shared_group->saved_guid());
EXPECT_EQ(saved_group->local_group_id(), local_group_id_1_);
// Verify group tabs.
ASSERT_EQ(shared_group->saved_tabs().size(), group_1_.saved_tabs().size());
EXPECT_FALSE(shared_group->saved_tabs().empty());
for (size_t i = 0; i < shared_group->saved_tabs().size(); ++i) {
const SavedTabGroupTab& saved_tab = saved_group->saved_tabs()[i];
const SavedTabGroupTab& shared_tab = originating_group->saved_tabs()[i];
// Verify the same fields.
EXPECT_EQ(saved_tab.url(), shared_tab.url());
EXPECT_EQ(saved_tab.title(), shared_tab.title());
EXPECT_EQ(saved_tab.favicon(), shared_tab.favicon());
EXPECT_EQ(saved_tab.saved_group_guid(), saved_group->saved_guid());
// Verify updated fields.
EXPECT_NE(saved_tab.saved_tab_guid(), shared_tab.saved_tab_guid());
EXPECT_GT(saved_group->creation_time(), shared_tab.creation_time());
EXPECT_GT(saved_tab.update_time(), shared_tab.update_time());
EXPECT_NE(saved_tab.local_tab_id(), std::nullopt);
EXPECT_EQ(shared_tab.local_tab_id(), std::nullopt);
// Do not verify the position of the original tab because its meaning
// differs for shared tab groups: it's the index of the tab in the shared
// group.
EXPECT_EQ(saved_tab.position(), i);
}
}
TEST_F(TabGroupSyncServiceTest,
UnShareTabGroupWhenTransitioningGroupRemovedFromSync) {
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
// Unshare the tab group.
tab_group_sync_service_->AboutToUnShareTabGroup(local_group_id_1_,
base::DoNothing());
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_TRUE(shared_group->is_transitioning_to_saved());
// Transition the shared tab group to a saved tab group.
Sequence s;
EXPECT_CALL(*coordinator_, DisconnectLocalTabGroup(local_group_id_1_))
.InSequence(s);
EXPECT_CALL(*coordinator_, ConnectLocalTabGroup(_, local_group_id_1_))
.InSequence(s);
EXPECT_CALL(*observer_, OnTabGroupMigrated(_, shared_group->saved_guid(),
TriggerSource::LOCAL));
// Advance the clock to ensure that the new saved group has a different
// creation time than the shared group.
task_environment_.FastForwardBy(base::Seconds(1));
model_->RemovedFromSync(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_TRUE(shared_group->is_transitioning_to_saved());
// The new group replaces the originating one asynchronously.
WaitForPostedTasks();
// The originating group should have empty local group id now.
std::optional<SavedTabGroup> originating_group =
tab_group_sync_service_->GetGroup(shared_group->saved_guid());
ASSERT_TRUE(originating_group.has_value());
EXPECT_TRUE(originating_group->is_shared_tab_group());
EXPECT_EQ(originating_group->local_group_id(), std::nullopt);
std::optional<SavedTabGroup> saved_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
// Verify that both groups have the same fields.
EXPECT_EQ(shared_group->title(), saved_group->title());
EXPECT_EQ(shared_group->color(), saved_group->color());
EXPECT_EQ(shared_group->position(), saved_group->position());
// Verify that the shared group has updated fields.
ASSERT_FALSE(saved_group->is_shared_tab_group());
EXPECT_GT(saved_group->creation_time(), shared_group->creation_time());
EXPECT_GT(saved_group->update_time(), shared_group->update_time());
EXPECT_EQ(saved_group->creator_cache_guid(), kTestCacheGuid);
EXPECT_EQ(saved_group->last_updater_cache_guid(), std::nullopt);
EXPECT_TRUE(saved_group->last_user_interaction_time().is_null());
// Verify shared tab group fields.
EXPECT_NE(saved_group->saved_guid(), shared_group->saved_guid());
EXPECT_TRUE(saved_group->saved_guid().is_valid());
EXPECT_EQ(saved_group->GetOriginatingTabGroupGuid(),
shared_group->saved_guid());
EXPECT_EQ(saved_group->local_group_id(), local_group_id_1_);
// Verify group tabs.
ASSERT_EQ(shared_group->saved_tabs().size(), group_1_.saved_tabs().size());
EXPECT_FALSE(shared_group->saved_tabs().empty());
for (size_t i = 0; i < shared_group->saved_tabs().size(); ++i) {
const SavedTabGroupTab& saved_tab = saved_group->saved_tabs()[i];
const SavedTabGroupTab& shared_tab = originating_group->saved_tabs()[i];
// Verify the same fields.
EXPECT_EQ(saved_tab.url(), shared_tab.url());
EXPECT_EQ(saved_tab.title(), shared_tab.title());
EXPECT_EQ(saved_tab.favicon(), shared_tab.favicon());
EXPECT_EQ(saved_tab.saved_group_guid(), saved_group->saved_guid());
// Verify updated fields.
EXPECT_NE(saved_tab.saved_tab_guid(), shared_tab.saved_tab_guid());
EXPECT_GT(saved_group->creation_time(), shared_tab.creation_time());
EXPECT_GT(saved_tab.update_time(), shared_tab.update_time());
EXPECT_NE(saved_tab.local_tab_id(), std::nullopt);
EXPECT_EQ(shared_tab.local_tab_id(), std::nullopt);
// Do not verify the position of the original tab because its meaning
// differs for shared tab groups: it's the index of the tab in the shared
// group.
EXPECT_EQ(saved_tab.position(), i);
}
}
TEST_F(TabGroupSyncServiceTest, ShouldNotReturnOriginatingTabGroupOnRemoteAdd) {
// Simulate remote transition to shared tab group from `group_1_`.
SavedTabGroup shared_group = test::CreateTestSavedTabGroupWithNoTabs();
shared_group.SetCollaborationId(CollaborationId("collaboration"));
shared_group.SetOriginatingTabGroupGuid(
group_1_.saved_guid(),
/*use_originating_tab_group_guid=*/true);
shared_group.SetUpdatedByAttribution(kDefaultGaiaId);
model_->AddedFromSync(shared_group);
WaitForPostedTasks();
// The saved tab group should be present in GetAllGroups() while the shared
// one is not accessible.
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Contains(HasGuid(group_1_.saved_guid())));
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Not(Contains(IsSharedGroup())));
// Add new remote tabs to make the group available for the transition.
model_->AddTabToGroupFromSync(
shared_group.saved_guid(),
test::CreateSavedTabGroupTab("http://foo.com", u"title",
shared_group.saved_guid()));
WaitForPostedTasks();
// Only shared tab group should be returned now.
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Not(Contains(HasGuid(group_1_.saved_guid()))));
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Contains(HasGuid(shared_group.saved_guid())));
}
TEST_F(TabGroupSyncServiceTest, OnCollaborationRemoved) {
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_EQ(tab_group_sync_service_->GetAllGroups().size(), 3u);
ASSERT_TRUE(model_->Contains(group->saved_guid()));
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
ASSERT_TRUE(shared_group->is_shared_tab_group());
ASSERT_EQ(tab_group_sync_service_->GetAllGroups().size(), 3u);
ASSERT_TRUE(model_->Contains(group->saved_guid()));
ASSERT_TRUE(model_->Contains(shared_group->saved_guid()));
ASSERT_EQ(shared_group->saved_tabs().size(), 1u);
SavedTabGroupTab tab = shared_group->saved_tabs()[0];
// Observer should get 5 OnTabGroupRemoved() calls, first is the saved group,
// then 2 comes from the shared group with guid and local group id, then 2
// for updating UI.
Sequence s;
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
group->saved_guid()),
Eq(TriggerSource::LOCAL)))
.InSequence(s);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
shared_group->saved_guid()),
Eq(TriggerSource::LOCAL)))
.InSequence(s);
EXPECT_CALL(*observer_,
OnTabGroupRemoved(testing::TypedEq<const LocalTabGroupID&>(
shared_group->local_group_id().value()),
Eq(TriggerSource::LOCAL)))
.InSequence(s);
EXPECT_CALL(*observer_,
OnTabGroupRemoved(testing::TypedEq<const LocalTabGroupID&>(
shared_group->local_group_id().value()),
Eq(TriggerSource::REMOTE)))
.InSequence(s);
EXPECT_CALL(*observer_, OnTabGroupRemoved(testing::TypedEq<const base::Uuid&>(
shared_group->saved_guid()),
Eq(TriggerSource::REMOTE)))
.InSequence(s);
EXPECT_CALL(*mock_shared_processor(),
UntrackEntityForStorageKey(
shared_group->saved_guid().AsLowercaseString()))
.Times(1);
EXPECT_CALL(
*mock_shared_processor(),
UntrackEntityForStorageKey(tab.saved_tab_guid().AsLowercaseString()))
.Times(1);
tab_group_sync_service_->OnCollaborationRemoved(
syncer::CollaborationId(kCollaborationId));
EXPECT_FALSE(tab_group_sync_service_->GetGroup(local_group_id_1_));
EXPECT_EQ(tab_group_sync_service_->GetAllGroups().size(), 2u);
EXPECT_FALSE(model_->Contains(group->saved_guid()));
EXPECT_FALSE(model_->Contains(shared_group->saved_guid()));
}
TEST_F(TabGroupSyncServiceTest, OnLastSharedTabClosed) {
syncer::CollaborationId collaboration_id("collaboration_id");
MakeTabGroupShared(local_group_id_1_, collaboration_id);
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_EQ(1u, group->saved_tabs().size());
SavedTabGroupTab tab = group->saved_tabs()[0];
// Close the only tab in this group. One tab will be added, and the original
// tab will be removed.
EXPECT_CALL(*observer_,
BeforeTabGroupUpdateFromRemote(
testing::TypedEq<const base::Uuid&>(group->saved_guid())));
EXPECT_CALL(*observer_,
AfterTabGroupUpdateFromRemote(
testing::TypedEq<const base::Uuid&>(group->saved_guid())));
tab_group_sync_service_->OnLastTabClosed(
tab_group_sync_service_->GetGroup(local_group_id_1_).value());
group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_TRUE(group.has_value());
EXPECT_EQ(1u, group->saved_tabs().size());
EXPECT_NE(tab.saved_tab_guid(), group->saved_tabs()[0].saved_tab_guid());
}
class PinningTabGroupSyncServiceTest : public TabGroupSyncServiceTest {
public:
PinningTabGroupSyncServiceTest() = default;
PinningTabGroupSyncServiceTest(const PinningTabGroupSyncServiceTest&) =
delete;
PinningTabGroupSyncServiceTest& operator=(
const PinningTabGroupSyncServiceTest&) = delete;
};
TEST_F(PinningTabGroupSyncServiceTest, UpdateGroupPositionPinnedState) {
auto group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_TRUE(group.has_value());
const bool pinned_state = group->is_pinned();
tab_group_sync_service_->UpdateGroupPosition(group->saved_guid(),
!pinned_state, std::nullopt);
group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_NE(group->is_pinned(), pinned_state);
tab_group_sync_service_->UpdateGroupPosition(group->saved_guid(),
pinned_state, std::nullopt);
group = tab_group_sync_service_->GetGroup(local_group_id_1_);
EXPECT_EQ(group->is_pinned(), pinned_state);
}
TEST_F(PinningTabGroupSyncServiceTest, UpdateGroupPositionIndex) {
auto get_index = [&](const LocalTabGroupID& local_id) -> int {
std::vector<SavedTabGroup> groups = tab_group_sync_service_->GetAllGroups();
auto it = std::ranges::find_if(groups, [&](const SavedTabGroup& group) {
return group.local_group_id() == local_id;
});
if (it == groups.end()) {
return -1;
}
return std::distance(groups.begin(), it);
};
std::vector<SavedTabGroup> all_groups =
tab_group_sync_service_->GetAllGroups();
ASSERT_EQ(3u, all_groups.size());
tab_group_sync_service_->UpdateLocalTabGroupMapping(
all_groups[0].saved_guid(), test::GenerateRandomTabGroupID(),
OpeningSource::kUnknown);
tab_group_sync_service_->UpdateLocalTabGroupMapping(
all_groups[1].saved_guid(), test::GenerateRandomTabGroupID(),
OpeningSource::kUnknown);
tab_group_sync_service_->UpdateLocalTabGroupMapping(
all_groups[2].saved_guid(), test::GenerateRandomTabGroupID(),
OpeningSource::kUnknown);
// Groups are inserted FILO style (like a stack data structure).
all_groups = tab_group_sync_service_->GetAllGroups();
const LocalTabGroupID group_id_3 = all_groups[0].local_group_id().value();
const LocalTabGroupID group_id_2 = all_groups[1].local_group_id().value();
const LocalTabGroupID group_id_1 = all_groups[2].local_group_id().value();
const base::Uuid group_sync_id_3 = all_groups[0].saved_guid();
const base::Uuid group_sync_id_1 = all_groups[2].saved_guid();
EXPECT_EQ(0, get_index(group_id_3));
EXPECT_EQ(1, get_index(group_id_2));
EXPECT_EQ(2, get_index(group_id_1));
tab_group_sync_service_->UpdateGroupPosition(group_sync_id_3, std::nullopt,
2);
EXPECT_EQ(0, get_index(group_id_2));
EXPECT_EQ(1, get_index(group_id_1));
EXPECT_EQ(2, get_index(group_id_3));
tab_group_sync_service_->UpdateGroupPosition(group_sync_id_1, std::nullopt,
0);
EXPECT_EQ(0, get_index(group_id_1));
EXPECT_EQ(1, get_index(group_id_2));
EXPECT_EQ(2, get_index(group_id_3));
tab_group_sync_service_->UpdateGroupPosition(group_sync_id_3, std::nullopt,
1);
EXPECT_EQ(0, get_index(group_id_1));
EXPECT_EQ(1, get_index(group_id_3));
EXPECT_EQ(2, get_index(group_id_2));
}
TEST_F(TabGroupSyncServiceTest, MetricsOnSignin) {
base::HistogramTester histograms;
identity_test_environment_.MakePrimaryAccountAvailable(
"account@gmail.com", signin::ConsentLevel::kSignin);
base::HistogramTester::CountsMap expected_counts{
{"TabGroups.OnSignin.TotalTabGroupCount", 1},
{"TabGroups.OnSignin.OpenTabGroupCount", 1},
{"TabGroups.OnSignin.ClosedTabGroupCount", 1},
{"TabGroups.OnSignin.TotalTabGroupTabsCount", 1},
{"TabGroups.OnSignin.OpenTabGroupTabsCount", 1},
{"TabGroups.OnSignin.ClosedTabGroupTabsCount", 1}};
EXPECT_THAT(histograms.GetTotalCountsForPrefix("TabGroups.OnSignin."),
ContainerEq(expected_counts));
// Sync wasn't enabled, so no "OnSync" metrics should be recorded.
EXPECT_THAT(histograms.GetTotalCountsForPrefix("TabGroups.OnSync."),
IsEmpty());
}
TEST_F(TabGroupSyncServiceTest, MetricsOnSync) {
base::HistogramTester histograms;
identity_test_environment_.MakePrimaryAccountAvailable(
"account@gmail.com", signin::ConsentLevel::kSync);
// Turning on sync includes signing in, so both "OnSignin" and "OnSync"
// metrics should be recorded.
base::HistogramTester::CountsMap expected_signin_counts{
{"TabGroups.OnSignin.TotalTabGroupCount", 1},
{"TabGroups.OnSignin.OpenTabGroupCount", 1},
{"TabGroups.OnSignin.ClosedTabGroupCount", 1},
{"TabGroups.OnSignin.TotalTabGroupTabsCount", 1},
{"TabGroups.OnSignin.OpenTabGroupTabsCount", 1},
{"TabGroups.OnSignin.ClosedTabGroupTabsCount", 1}};
EXPECT_THAT(histograms.GetTotalCountsForPrefix("TabGroups.OnSignin."),
ContainerEq(expected_signin_counts));
base::HistogramTester::CountsMap expected_sync_counts{
{"TabGroups.OnSync.TotalTabGroupCount", 1},
{"TabGroups.OnSync.OpenTabGroupCount", 1},
{"TabGroups.OnSync.ClosedTabGroupCount", 1},
{"TabGroups.OnSync.TotalTabGroupTabsCount", 1},
{"TabGroups.OnSync.OpenTabGroupTabsCount", 1},
{"TabGroups.OnSync.ClosedTabGroupTabsCount", 1}};
EXPECT_THAT(histograms.GetTotalCountsForPrefix("TabGroups.OnSync."),
ContainerEq(expected_sync_counts));
}
// Tests that after transitioning from a saved tab group to a shared tab group,
// the shared tab group is the only tab group returned by GetAllGroups().
TEST_F(TabGroupSyncServiceTest, ShouldReturnSharedTabGroupOnly) {
ASSERT_THAT(tab_group_sync_service_->GetAllGroups(), SizeIs(3));
ASSERT_THAT(model_->saved_tab_groups(), SizeIs(3));
syncer::CollaborationId collaboration_id("collaboration");
ON_CALL(*collaboration_finder_,
IsCollaborationAvailable(Eq(collaboration_id)))
.WillByDefault(testing::Return(true));
MakeTabGroupShared(local_group_id_1_, collaboration_id);
const std::vector<SavedTabGroup> all_groups =
tab_group_sync_service_->GetAllGroups();
EXPECT_THAT(all_groups, SizeIs(3));
EXPECT_THAT(model_->saved_tab_groups(), SizeIs(4));
EXPECT_THAT(all_groups, Not(Contains(HasGuid(group_1_.saved_guid()))));
// The group is still accessible by ID.
EXPECT_NE(tab_group_sync_service_->GetGroup(group_1_.saved_guid()),
std::nullopt);
// Simulate the case that the originating group is removed from sync after
// the migration. It should have no impact on what is being returned from
// GetAllGroups().
model_->RemovedFromSync(group_1_.saved_guid());
WaitForPostedTasks();
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(), SizeIs(3));
EXPECT_THAT(model_->saved_tab_groups(), SizeIs(3));
}
TEST_F(TabGroupSyncServiceTest,
RemoteAddSharedGroupWhenOriginatingGroupIsClosed) {
// Simulate remote transition to shared tab group from `group_1_`.
SavedTabGroup shared_group = test::CreateTestSavedTabGroupWithNoTabs();
shared_group.SetCollaborationId(CollaborationId("collaboration"));
shared_group.SetOriginatingTabGroupGuid(
group_1_.saved_guid(),
/*use_originating_tab_group_guid=*/true);
shared_group.SetUpdatedByAttribution(kDefaultGaiaId);
// Close the group before the shared group is added by remote.
tab_group_sync_service_->RemoveLocalTabGroupMapping(
local_group_id_1_, ClosingSource::kClosedByUser);
model_->AddedFromSync(shared_group);
WaitForPostedTasks();
// The saved tab group should be present in GetAllGroups() while the shared
// one is not accessible.
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Contains(HasGuid(group_1_.saved_guid())));
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Not(Contains(IsSharedGroup())));
// Add new remote tabs to make the group available for the transition.
model_->AddTabToGroupFromSync(
shared_group.saved_guid(),
test::CreateSavedTabGroupTab("http://foo.com", u"title",
shared_group.saved_guid()));
WaitForPostedTasks();
// Only shared tab group should be returned now.
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Not(Contains(HasGuid(group_1_.saved_guid()))));
EXPECT_THAT(tab_group_sync_service_->GetAllGroups(),
Contains(HasGuid(shared_group.saved_guid())));
}
// Tests that saved tab group is returned if tab group migration didn't
// complete.
TEST_F(TabGroupSyncServiceTest, ShouldReturnSavedTabGroupDuringTransition) {
ASSERT_THAT(tab_group_sync_service_->GetAllGroups(), SizeIs(3));
ASSERT_THAT(model_->saved_tab_groups(), SizeIs(3));
syncer::CollaborationId collaboration_id("collaboration");
ON_CALL(*collaboration_finder_,
IsCollaborationAvailable(Eq(collaboration_id)))
.WillByDefault(testing::Return(true));
tab_group_sync_service_->MakeTabGroupShared(
local_group_id_1_, collaboration_id, base::DoNothing());
// During the transition, GetAllGroups() could also return 3 groups,
// including the original saved group.
std::vector<SavedTabGroup> all_groups =
tab_group_sync_service_->GetAllGroups();
EXPECT_THAT(all_groups, SizeIs(3));
EXPECT_THAT(model_->saved_tab_groups(), SizeIs(4));
EXPECT_THAT(all_groups, Contains(HasGuid(group_1_.saved_guid())));
// Simulate all shared tab groups as committed to the server.
for (const SavedTabGroup* group : model_->GetSharedTabGroupsOnly()) {
model_->MarkTransitionedToShared(group->saved_guid());
}
WaitForPostedTasks();
// Once committed, GetAllGroups() should still return 3 groups but a
// shared group instead of the original saved group.
all_groups = tab_group_sync_service_->GetAllGroups();
EXPECT_THAT(all_groups, SizeIs(3));
EXPECT_THAT(model_->saved_tab_groups(), SizeIs(4));
EXPECT_THAT(all_groups, Not(Contains(HasGuid(group_1_.saved_guid()))));
}
// Tests that after transitioning from a shared tab group to a saved tab group,
// the saved tab group is the only tab group returned by GetAllGroups().
TEST_F(TabGroupSyncServiceTest, ShouldReturnSavedTabGroupOnly) {
std::optional<SavedTabGroup> group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
MakeTabGroupShared(local_group_id_1_, kCollaborationId);
ASSERT_THAT(tab_group_sync_service_->GetAllGroups(), SizeIs(3));
ASSERT_THAT(model_->saved_tab_groups(), SizeIs(4));
ASSERT_TRUE(model_->Contains(group->saved_guid()));
std::optional<SavedTabGroup> shared_group =
tab_group_sync_service_->GetGroup(local_group_id_1_);
// Unshare the tab group.
tab_group_sync_service_->AboutToUnShareTabGroup(local_group_id_1_,
base::DoNothing());
tab_group_sync_service_->OnTabGroupUnShareComplete(local_group_id_1_, true);
// During the transition, GetAllGroups() should return 3 groups, including
// the original shared group.
std::vector<SavedTabGroup> all_groups =
tab_group_sync_service_->GetAllGroups();
EXPECT_THAT(all_groups, SizeIs(3));
EXPECT_THAT(model_->saved_tab_groups(), SizeIs(4));
EXPECT_THAT(all_groups, Contains(HasGuid(shared_group->saved_guid())));
EXPECT_FALSE(model_->Contains(group->saved_guid()));
WaitForPostedTasks();
all_groups = tab_group_sync_service_->GetAllGroups();
EXPECT_THAT(all_groups, SizeIs(3));
EXPECT_THAT(model_->saved_tab_groups(), SizeIs(4));
EXPECT_THAT(all_groups, Not(Contains(HasGuid(shared_group->saved_guid()))));
}
TEST_F(TabGroupSyncServiceTest,
LoadSharedTabGroupsOnStartup_WillRegisterPageEntitiesOptimizationType) {
SavedTabGroup shared_group(test::CreateTestSavedTabGroup());
CollaborationId collaboration_id("foo");
shared_group.SetCollaborationId(collaboration_id);
EXPECT_CALL(*observer_, OnInitialized()).Times(1);
EXPECT_CALL(*decider_, RegisterOptimizationTypes(ElementsAre(
optimization_guide::proto::PAGE_ENTITIES)))
.Times(1);
model_->LoadStoredEntries(
/*groups=*/{shared_group},
/*tabs=*/{});
task_environment_.AdvanceClock(GetOriginatingSavedGroupCleanUpTimeInterval());
task_environment_.FastForwardBy(base::Seconds(10));
WaitForPostedTasks();
}
TEST_F(
TabGroupSyncServiceTest,
LoadSharedTabGroupsOnStartup_WillNotRegisterPageEntitiesOptimizationType) {
SavedTabGroup saved_group(test::CreateTestSavedTabGroup());
EXPECT_CALL(*observer_, OnInitialized()).Times(1);
EXPECT_CALL(*decider_, RegisterOptimizationTypes(ElementsAre(
optimization_guide::proto::PAGE_ENTITIES)))
.Times(0);
model_->LoadStoredEntries(
/*groups=*/{saved_group},
/*tabs=*/{});
task_environment_.AdvanceClock(GetOriginatingSavedGroupCleanUpTimeInterval());
task_environment_.FastForwardBy(base::Seconds(10));
WaitForPostedTasks();
}
class EmptyTabGroupSyncServiceTest : public TabGroupSyncServiceTest {
public:
void MaybeInitializeTestGroups() override {}
};
TEST_F(EmptyTabGroupSyncServiceTest,
TestModelLoadAndExtractionOfSharedTabGroupsForMessaging) {
ASSERT_EQ(model_->Count(), 0);
tab_group_sync_service_->SetIsInitializedForTesting(false);
CollaborationId collaboration_id_1("foo");
CollaborationId collaboration_id_2("bar");
SavedTabGroup saved_tab_group(test::CreateTestSavedTabGroup());
SavedTabGroup shared_group_1(test::CreateTestSavedTabGroup());
shared_group_1.SetCollaborationId(collaboration_id_1);
SavedTabGroup shared_group_2(test::CreateTestSavedTabGroup());
shared_group_2.SetCollaborationId(collaboration_id_1);
SavedTabGroup shared_group_3(test::CreateTestSavedTabGroup());
shared_group_3.SetCollaborationId(collaboration_id_2);
SavedTabGroup shared_group_4(test::CreateTestSavedTabGroup());
shared_group_4.SetCollaborationId(collaboration_id_2);
SavedTabGroup shared_group_5(test::CreateTestSavedTabGroup());
shared_group_5.SetCollaborationId(collaboration_id_2);
model_->LoadStoredEntries(
/*groups=*/{saved_tab_group, shared_group_1, shared_group_2,
shared_group_3, shared_group_4, shared_group_5},
/*tabs=*/{});
task_environment_.RunUntilIdle();
// Verify model internals.
ASSERT_TRUE(model_->Contains(saved_tab_group.saved_guid()));
ASSERT_TRUE(model_->Contains(shared_group_1.saved_guid()));
ASSERT_TRUE(model_->Contains(shared_group_2.saved_guid()));
ASSERT_TRUE(model_->Contains(shared_group_3.saved_guid()));
ASSERT_TRUE(model_->Contains(shared_group_4.saved_guid()));
ASSERT_TRUE(model_->Contains(shared_group_5.saved_guid()));
ASSERT_EQ(model_->Count(), 6);
// Retrieve groups and verify that it does not contain the saved tab group.
std::unique_ptr<std::vector<SavedTabGroup>> shared_groups_at_startup =
tab_group_sync_service_
->TakeSharedTabGroupsAvailableAtStartupForMessaging();
EXPECT_EQ(shared_groups_at_startup.get()->size(), 5U);
// We do not want to store this state forever, so verify that it is gone.
EXPECT_EQ(tab_group_sync_service_
->TakeSharedTabGroupsAvailableAtStartupForMessaging(),
nullptr);
// We expect all shared groups to be present.
std::set<base::Uuid> expected_guids = {
shared_group_1.saved_guid(), shared_group_2.saved_guid(),
shared_group_3.saved_guid(), shared_group_4.saved_guid(),
shared_group_5.saved_guid()};
for (const SavedTabGroup& shared_group : *(shared_groups_at_startup.get())) {
EXPECT_TRUE(expected_guids.erase(shared_group.saved_guid()))
<< "Unexpected GUID " << shared_group.saved_guid()
<< " found among groups";
}
// Add helpful debug information in case of expectation error.
std::vector<std::string> guid_strings;
for (const base::Uuid& guid : expected_guids) {
guid_strings.push_back(guid.AsLowercaseString());
}
// Verify that all GUIDs were found for shared tab groups.
EXPECT_TRUE(expected_guids.empty())
<< "Not all GUIDs were found: " << base::JoinString(guid_strings, ", ");
}
TEST_F(EmptyTabGroupSyncServiceTest,
TestHadSharedTabGroupsOnStartup_OpenGroups) {
SavedTabGroup shared_group_1(test::CreateTestSavedTabGroup());
shared_group_1.SetCollaborationId(CollaborationId("collaboration"));
shared_group_1.SetLocalGroupId(test::GenerateRandomTabGroupID());
model_->LoadStoredEntries(
/*groups=*/{shared_group_1},
/*tabs=*/{});
task_environment_.RunUntilIdle();
EXPECT_TRUE(tab_group_sync_service_->HadSharedTabGroupsLastSession(
/*open_shared_tab_groups=*/false));
EXPECT_TRUE(tab_group_sync_service_->HadSharedTabGroupsLastSession(
/*open_shared_tab_groups=*/true));
}
TEST_F(EmptyTabGroupSyncServiceTest,
TestHadSharedTabGroupsOnStartup_NoOpenGroups) {
SavedTabGroup shared_group_1(test::CreateTestSavedTabGroup());
shared_group_1.SetCollaborationId(CollaborationId("collaboration"));
model_->LoadStoredEntries(
/*groups=*/{shared_group_1},
/*tabs=*/{});
task_environment_.RunUntilIdle();
EXPECT_TRUE(tab_group_sync_service_->HadSharedTabGroupsLastSession(
/*open_shared_tab_groups=*/false));
EXPECT_FALSE(tab_group_sync_service_->HadSharedTabGroupsLastSession(
/*open_shared_tab_groups=*/true));
}
TEST_F(EmptyTabGroupSyncServiceTest, TestHadSharedTabGroupsOnStartup_NoGroups) {
model_->LoadStoredEntries(
/*groups=*/{},
/*tabs=*/{});
task_environment_.RunUntilIdle();
EXPECT_FALSE(tab_group_sync_service_->HadSharedTabGroupsLastSession(
/*open_shared_tab_groups=*/false));
EXPECT_FALSE(tab_group_sync_service_->HadSharedTabGroupsLastSession(
/*open_shared_tab_groups=*/true));
}
} // namespace tab_groups
|