File: system_web_app_manager_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (2014 lines) | stat: -rw-r--r-- 82,319 bytes parent folder | download | duplicates (3)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/system_web_apps/system_web_app_manager.h"

#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/shelf_item_delegate.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/webui/system_apps/public/system_web_app_type.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/gtest_tags.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_factory.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/browser_app_launcher.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/chromevox_test_utils.h"
#include "chrome/browser/ash/accessibility/speech_monitor.h"
#include "chrome/browser/ash/app_list/app_list_client_impl.h"
#include "chrome/browser/ash/app_list/app_list_model_updater.h"
#include "chrome/browser/ash/app_list/test/chrome_app_list_test_support.h"
#include "chrome/browser/ash/extensions/default_app_order.h"
#include "chrome/browser/ash/file_manager/file_manager_test_util.h"
#include "chrome/browser/ash/file_manager/volume.h"
#include "chrome/browser/ash/system_web_apps/test_support/system_web_app_browsertest_base.h"
#include "chrome/browser/ash/system_web_apps/test_support/test_system_web_app_installation.h"
#include "chrome/browser/extensions/scoped_test_mv2_enabler.h"
#include "chrome/browser/file_system_access/file_system_access_permission_request_manager.h"
#include "chrome/browser/policy/system_features_disable_list_policy_handler.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_util.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/web_applications/proto/web_app.pb.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_command_manager.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_icon_manager.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/permissions/permission_util.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/test_launcher.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/common/constants.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/context_menu/context_menu.mojom.h"
#include "ui/base/idle/idle.h"
#include "ui/base/idle/scoped_set_idle_state.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/display/display.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/test/event_generator.h"

namespace ash {

namespace {

// Helper to call AppServiceProxyFactory::GetForProfile().
apps::AppServiceProxyBase* GetAppServiceProxy(Profile* profile) {
  // Crash if there is no AppService support for |profile|. GetForProfile() will
  // DumpWithoutCrashing, which will not fail a test. No codepath should trigger
  // that in normal operation.
  DCHECK(
      apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile));
  return apps::AppServiceProxyFactory::GetForProfile(profile);
}

}  // namespace

using SystemWebAppManagerBrowserTestBasicInstall =
    SystemWebAppManagerBrowserTest;

// Test that System Apps install correctly with a manifest.
IN_PROC_BROWSER_TEST_P(SystemWebAppManagerBrowserTestBasicInstall, Install) {
  WaitForTestSystemAppInstall();

  // Don't wait for page load because we want to verify AppController identifies
  // the System Web App before when the app loads.
  Browser* app_browser;
  LaunchAppWithoutWaiting(GetAppType(), &app_browser);

  webapps::AppId app_id = app_browser->app_controller()->app_id();
  EXPECT_EQ(GetManager().GetAppIdForSystemApp(GetAppType()), app_id);
  EXPECT_TRUE(GetManager().IsSystemWebApp(app_id));

  Profile* profile = app_browser->profile();
  web_app::WebAppRegistrar& registrar =
      web_app::WebAppProvider::GetForTest(profile)->registrar_unsafe();

  EXPECT_EQ("Test System App", registrar.GetAppShortName(app_id));
  EXPECT_EQ(SkColorSetRGB(0, 0xFF, 0), registrar.GetAppThemeColor(app_id));
  EXPECT_TRUE(registrar.HasExternalAppWithInstallSource(
      app_id, web_app::ExternalInstallSource::kSystemInstalled));
  EXPECT_EQ(registrar.FindBestAppWithUrlInScope(
                content::GetWebUIURL("test-system-app/"),
                web_app::WebAppFilter::InstalledInOperatingSystemForTesting()),
            app_id);

  GetAppServiceProxy(browser()->profile())
      ->AppRegistryCache()
      .ForOneApp(app_id, [](const apps::AppUpdate& update) {
        EXPECT_TRUE(update.ShowInLauncher().value_or(false));
        EXPECT_TRUE(update.ShowInSearch().value_or(false));
        EXPECT_FALSE(update.ShowInManagement().value_or(true));
        EXPECT_EQ(apps::Readiness::kReady, update.Readiness());
      });
}

// Check the toolbar is not shown for system web apps for pages on the chrome://
// scheme but is shown off the chrome:// scheme.
IN_PROC_BROWSER_TEST_P(SystemWebAppManagerBrowserTest,
                       ToolbarVisibilityForSystemWebApp) {
  WaitForTestSystemAppInstall();

  // Don't wait for page load because we want to verify the toolbar is hidden
  // when the window first opens.
  Browser* app_browser;
  LaunchAppWithoutWaiting(GetAppType(), &app_browser);

  // In scope, the toolbar should not be visible.
  EXPECT_FALSE(app_browser->app_controller()->ShouldShowCustomTabBar());

  // Out of scope chrome:// URL.
  GURL out_of_scope_chrome_page("chrome://foo");
  content::NavigateToURLBlockUntilNavigationsComplete(
      app_browser->tab_strip_model()->GetActiveWebContents(),
      out_of_scope_chrome_page, 1);
  EXPECT_TRUE(app_browser->app_controller()->ShouldShowCustomTabBar());

  // Even though the url is secure it is not being served over chrome:// so a
  // toolbar should be shown.
  GURL off_scheme_page("https://example.com");
  content::NavigateToURLBlockUntilNavigationsComplete(
      app_browser->tab_strip_model()->GetActiveWebContents(), off_scheme_page,
      1);
  EXPECT_TRUE(app_browser->app_controller()->ShouldShowCustomTabBar());

  // URL has been added to be within scope for the SWA.
  GURL in_scope_for_swa_page("https://example.com/in-scope");
  content::NavigateToURLBlockUntilNavigationsComplete(
      app_browser->tab_strip_model()->GetActiveWebContents(),
      in_scope_for_swa_page, 1);
  EXPECT_FALSE(app_browser->app_controller()->ShouldShowCustomTabBar());
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerBrowserTest, LaunchMetricsWork) {
  WaitForTestSystemAppInstall();

  base::HistogramTester histograms;

  content::TestNavigationObserver navigation_observer(GetStartUrl());
  navigation_observer.StartWatchingNewWebContents();

  ash::SystemAppLaunchParams params;
  params.launch_source = apps::LaunchSource::kFromAppListGrid;
  LaunchSystemWebAppAsync(browser()->profile(), GetAppType(), params);

  navigation_observer.Wait();
  histograms.ExpectTotalCount("Apps.DefaultAppLaunch.FromAppListGrid", 1);
  histograms.ExpectUniqueSample("Apps.DefaultAppLaunch.FromAppListGrid", 39, 1);
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerBrowserTest,
                       LaunchMetricsWorkFromAppProxy) {
  WaitForTestSystemAppInstall();

  base::HistogramTester histograms;
  content::TestNavigationObserver navigation_observer(GetStartUrl());
  navigation_observer.StartWatchingNewWebContents();

  auto* proxy = GetAppServiceProxy(browser()->profile());

  proxy->Launch(GetManager().GetAppIdForSystemApp(GetAppType()).value(),
                ui::EF_NONE, apps::LaunchSource::kFromAppListGrid,
                std::make_unique<apps::WindowInfo>(display::kDefaultDisplayId));
  navigation_observer.Wait();

  histograms.ExpectTotalCount("Apps.DefaultAppLaunch.FromAppListGrid", 1);
  histograms.ExpectUniqueSample("Apps.DefaultAppLaunch.FromAppListGrid", 39, 1);
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerBrowserTest,
                       LaunchMetricsWorkWithIntent) {
  WaitForTestSystemAppInstall();

  base::HistogramTester histograms;
  content::TestNavigationObserver navigation_observer(GetStartUrl());
  navigation_observer.StartWatchingNewWebContents();

  auto* proxy = GetAppServiceProxy(browser()->profile());
  auto intent = std::make_unique<apps::Intent>(apps_util::kIntentActionView);
  intent->mime_type = "text/plain";

  proxy->LaunchAppWithIntent(
      GetManager().GetAppIdForSystemApp(GetAppType()).value(), ui::EF_NONE,
      std::move(intent), apps::LaunchSource::kFromAppListGrid,
      std::make_unique<apps::WindowInfo>(display::kDefaultDisplayId),
      base::DoNothing());
  navigation_observer.Wait();

  histograms.ExpectTotalCount("Apps.DefaultAppLaunch.FromAppListGrid", 1);
  histograms.ExpectUniqueSample("Apps.DefaultAppLaunch.FromAppListGrid", 39, 1);
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerBrowserTest, UpdatesLaunchStats) {
  WaitForTestSystemAppInstall();
  auto app_id = GetManager().GetAppIdForSystemApp(GetAppType()).value();

  content::TestNavigationObserver navigation_observer(GetStartUrl());
  navigation_observer.StartWatchingNewWebContents();

  base::Time launch_start_time = base::Time::Now();

  ash::SystemAppLaunchParams params;
  params.launch_source = apps::LaunchSource::kFromAppListGrid;
  LaunchSystemWebAppAsync(browser()->profile(), GetAppType(), params);

  navigation_observer.Wait();

  auto* proxy = GetAppServiceProxy(browser()->profile());
  EXPECT_TRUE(proxy->AppRegistryCache().ForOneApp(
      app_id,
      [&](const apps::AppUpdate& update) {
        EXPECT_GE(update.LastLaunchTime(), launch_start_time);
      }))
      << "Expect app to exist";
}

class SystemWebAppManagerLaunchWithUrlBrowserTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerLaunchWithUrlBrowserTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppLaunchWithUrl());
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerLaunchWithUrlBrowserTest,
                       LaunchWithCallback) {
  WaitForTestSystemAppInstall();
  content::TestNavigationObserver navigation_observer(GetStartUrl());
  navigation_observer.StartWatchingNewWebContents();
  ash::SystemAppLaunchParams params;
  params.launch_source = apps::LaunchSource::kFromOtherApp;
  params.url = GetStartUrl();
  bool is_called = false;
  LaunchSystemWebAppAsync(
      browser()->profile(), GetAppType(), params, nullptr,
      base::BindLambdaForTesting(
          [&is_called](apps::LaunchResult&& callback_result) {
            is_called = true;
          }));
  navigation_observer.Wait();
  EXPECT_TRUE(is_called);
}

class SystemWebAppManagerFileHandlingBrowserTestBase
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  using IncludeLaunchDirectory =
      TestSystemWebAppInstallation::IncludeLaunchDirectory;

  explicit SystemWebAppManagerFileHandlingBrowserTestBase(
      IncludeLaunchDirectory include_launch_directory) {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppThatReceivesLaunchFiles(
            include_launch_directory));
  }

  content::WebContents* LaunchApp(std::vector<base::FilePath> launch_files,
                                  bool wait_for_load = true) {
    apps::AppLaunchParams params = LaunchParamsForApp(GetAppType());
    params.launch_source = apps::LaunchSource::kFromChromeInternal;
    params.override_url = GetStartUrl();
    params.launch_files = std::move(launch_files);

    return SystemWebAppBrowserTestBase::LaunchApp(std::move(params));
  }

  content::WebContents* LaunchAppWithoutWaiting(
      std::vector<base::FilePath> launch_files) {
    apps::AppLaunchParams params = LaunchParamsForApp(GetAppType());
    params.launch_source = apps::LaunchSource::kFromChromeInternal;
    params.override_url = GetStartUrl();
    params.launch_files = std::move(launch_files);

    return SystemWebAppBrowserTestBase::LaunchAppWithoutWaiting(
        std::move(params));
  }

  // Must be called before WaitAndExposeLaunchParamsToWindow. This sets up the
  // promise used to wait for launchParam callback.
  [[nodiscard]] ::testing::AssertionResult PrepareToReceiveLaunchParams(
      content::WebContents* web_contents) {
    return content::ExecJs(
        web_contents,
        "window.launchParamsPromise = new Promise(resolve => {"
        "  window.resolveLaunchParamsPromise = resolve;"
        "});"
        "launchQueue.setConsumer(launchParams => {"
        "  window.resolveLaunchParamsPromise(launchParams);"
        "  window.resolveLaunchParamsPromise = null;"
        "});");
  }

  // Must be called after PrepareToReceiveLaunchParams. This method waits for
  // launchParams being received, the stores it to a |js_property_name| on JS
  // window object.
  [[nodiscard]] ::testing::AssertionResult WaitAndExposeLaunchParamsToWindow(
      content::WebContents* web_contents,
      const std::string js_property_name = "launchParams") {
    return content::ExecJs(
        web_contents,
        content::JsReplace("window.launchParamsPromise.then(launchParams => {"
                           "  window[$1] = launchParams"
                           "})",
                           js_property_name));
  }

 private:
  base::test::ScopedFeatureList scoped_feature_web_app_provider_type_;
};

class SystemWebAppManagerLaunchFilesBrowserTest
    : public SystemWebAppManagerFileHandlingBrowserTestBase {
 public:
  SystemWebAppManagerLaunchFilesBrowserTest()
      : SystemWebAppManagerFileHandlingBrowserTestBase(
            IncludeLaunchDirectory::kNo) {}
};

// Check launch files are passed to application.
IN_PROC_BROWSER_TEST_P(SystemWebAppManagerLaunchFilesBrowserTest,
                       LaunchFilesForSystemWebApp) {
  WaitForTestSystemAppInstall();

  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir temp_directory;
  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  base::FilePath temp_file_path;
  ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.GetPath(),
                                             &temp_file_path));

  // First launch.
  content::WebContents* web_contents = LaunchApp({temp_file_path});

  // Check the App is launched with the correct launch file.
  EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
  EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents, "launchParams1"));
  EXPECT_EQ(
      temp_file_path.BaseName().AsUTF8Unsafe(),
      content::EvalJs(web_contents, "window.launchParams1.files[0].name"));

  // Second launch.
  base::FilePath temp_file_path2;
  ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.GetPath(),
                                             &temp_file_path2));

  // The second launch reuses the opened application. It should pass the
  // launchParams to the opened page, and return the same content::WebContents*.
  EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
  EXPECT_EQ(web_contents, LaunchAppWithoutWaiting({temp_file_path2}));
  EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents, "launchParams2"));

  // Second launch_files are correct.
  EXPECT_EQ(
      temp_file_path2.BaseName().AsUTF8Unsafe(),
      content::EvalJs(web_contents, "window.launchParams2.files[0].name"));
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerLaunchFilesBrowserTest,
                       LaunchMetricsWorks) {
  WaitForTestSystemAppInstall();

  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir temp_directory;
  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  base::FilePath temp_file_path;
  ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.GetPath(),
                                             &temp_file_path));

  base::HistogramTester histograms;

  content::TestNavigationObserver navigation_observer(GetStartUrl());
  navigation_observer.StartWatchingNewWebContents();

  ash::SystemAppLaunchParams params;
  params.launch_paths = {temp_file_path};
  params.launch_source = apps::LaunchSource::kFromOtherApp;
  LaunchSystemWebAppAsync(browser()->profile(), GetAppType(), params);

  navigation_observer.Wait();
  histograms.ExpectTotalCount("Apps.DefaultAppLaunch.FromOtherApp", 1);
}

class SystemWebAppManagerLaunchDirectoryBrowserTest
    : public SystemWebAppManagerFileHandlingBrowserTestBase {
 public:
  SystemWebAppManagerLaunchDirectoryBrowserTest()
      : SystemWebAppManagerFileHandlingBrowserTestBase(
            IncludeLaunchDirectory::kYes) {}

  // Returns the content of |file_handle_or_promise| file handle.
  [[nodiscard]] content::EvalJsResult ReadContentFromJsFileHandle(
      content::WebContents* web_contents,
      const std::string& file_handle_or_promise) {
    return content::EvalJs(web_contents,
                           "Promise.resolve(" + file_handle_or_promise + ")" +
                               ".then(async fileHandle => {"
                               "  const file = await fileHandle.getFile();"
                               "  return file.text();"
                               "})");
  }

  // Writes |content_to_write| to |file_handle_or_promise| file handle.
  [[nodiscard]] ::testing::AssertionResult WriteContentToJsFileHandle(
      content::WebContents* web_contents,
      const std::string& file_handle_or_promise,
      const std::string& content_to_write) {
    return content::ExecJs(
        web_contents,
        content::JsReplace(
            "Promise.resolve(" + file_handle_or_promise + ")" +
                ".then(async (fileHandle) => {"
                "  const writable = await fileHandle.createWritable();"
                "  await writable.write($1);"
                "  await writable.close();"
                "})",
            content_to_write));
  }

  // Remove file by |file_name| from |dir_handle_or_promise| directory handle.
  [[nodiscard]] ::testing::AssertionResult RemoveFileFromJsDirectoryHandle(
      content::WebContents* web_contents,
      const std::string& dir_handle_or_promise,
      const std::string& file_name) {
    return content::ExecJs(
        web_contents, content::JsReplace(
                          "Promise.resolve(" + dir_handle_or_promise + ")" +
                              ".then(dir_handle => dir_handle.removeEntry($1))",
                          file_name));
  }

  std::string ReadFileContent(const base::FilePath& path) {
    std::string content;
    EXPECT_TRUE(base::ReadFileToString(path, &content));
    return content;
  }

  // Launch the App with |base_dir| and a file inside this directory, then test
  // SWA can 1) read and write to the launch file; 2) read and write to other
  // files inside the launch directory; 3) read and write to the launch
  // directory (i.e. list and delete files).
  void TestPermissionsForLaunchDirectory(const base::FilePath& base_dir) {
    base::ScopedAllowBlockingForTesting allow_blocking;

    // Create the launch file, which stores 4 characters "test".
    base::FilePath launch_file_path;
    ASSERT_TRUE(base::CreateTemporaryFileInDir(base_dir, &launch_file_path));
    ASSERT_TRUE(base::WriteFile(launch_file_path, "test"));

    // Launch the App.
    content::WebContents* web_contents = LaunchApp({launch_file_path});

    // Launch directories and files passed to system web apps should
    // automatically be granted write permission. Users should not get
    // permission prompts. So we auto deny them (if they show up).
    FileSystemAccessPermissionRequestManager::FromWebContents(web_contents)
        ->set_auto_response_for_test(permissions::PermissionAction::DENIED);

    // Wait for launchParams.
    EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
    EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents));

    // Check we can read and write to the launch file.
    std::string launch_file_js_handle = "window.launchParams.files[1]";
    EXPECT_EQ("test",
              ReadContentFromJsFileHandle(web_contents, launch_file_js_handle));
    EXPECT_TRUE(WriteContentToJsFileHandle(web_contents, launch_file_js_handle,
                                           "js_written"));
    EXPECT_EQ("js_written", ReadFileContent(launch_file_path));

    // Check we can read and write to a different file inside the directory.
    // Note, this also checks we can read the launch directory, using
    // directory_handle.getFileHandle().
    base::FilePath non_launch_file_path;
    ASSERT_TRUE(
        base::CreateTemporaryFileInDir(base_dir, &non_launch_file_path));
    ASSERT_TRUE(base::WriteFile(non_launch_file_path, "test2"));

    std::string non_launch_file_js_handle =
        content::JsReplace("window.launchParams.files[0].getFileHandle($1)",
                           non_launch_file_path.BaseName().AsUTF8Unsafe());
    EXPECT_EQ("test2", ReadContentFromJsFileHandle(web_contents,
                                                   non_launch_file_js_handle));
    EXPECT_TRUE(WriteContentToJsFileHandle(
        web_contents, non_launch_file_js_handle, "js_written2"));
    EXPECT_EQ("js_written2", ReadFileContent(non_launch_file_path));

    // Check the launch file can be deleted.
    std::string launch_dir_js_handle = "window.launchParams.files[0]";
    EXPECT_TRUE(RemoveFileFromJsDirectoryHandle(
        web_contents, launch_dir_js_handle,
        launch_file_path.BaseName().AsUTF8Unsafe()));
    EXPECT_FALSE(base::PathExists(launch_file_path));

    // Check the non-launch file can be deleted.
    EXPECT_TRUE(RemoveFileFromJsDirectoryHandle(
        web_contents, launch_dir_js_handle,
        non_launch_file_path.BaseName().AsUTF8Unsafe()));
    EXPECT_FALSE(base::PathExists(non_launch_file_path));

    // Check a file can be created.
    std::string new_file_js_handle = content::JsReplace(
        "window.launchParams.files[0].getFileHandle($1, {create:true})",
        "new_file");
    EXPECT_TRUE(WriteContentToJsFileHandle(web_contents, new_file_js_handle,
                                           "js_new_file"));
    EXPECT_EQ("js_new_file", ReadFileContent(base_dir.AppendASCII("new_file")));
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerLaunchDirectoryBrowserTest,
                       LaunchDirectoryForSystemWebApp) {
  WaitForTestSystemAppInstall();

  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir temp_directory;
  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  base::FilePath temp_file_path;
  ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory.GetPath(),
                                             &temp_file_path));

  // First launch.
  content::WebContents* web_contents = LaunchApp({temp_file_path});
  EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
  EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents, "launchParams1"));

  // Check launch directory and launch files are correct.
  EXPECT_EQ("directory", content::EvalJs(web_contents,
                                         "window.launchParams1.files[0].kind"));
  EXPECT_EQ(
      temp_directory.GetPath().BaseName().AsUTF8Unsafe(),
      content::EvalJs(web_contents, "window.launchParams1.files[0].name"));
  EXPECT_EQ("file", content::EvalJs(web_contents,
                                    "window.launchParams1.files[1].kind"));
  EXPECT_EQ(
      temp_file_path.BaseName().AsUTF8Unsafe(),
      content::EvalJs(web_contents, "window.launchParams1.files[1].name"));

  // Second launch.
  base::ScopedTempDir temp_directory2;
  ASSERT_TRUE(temp_directory2.CreateUniqueTempDir());
  base::FilePath temp_file_path2;
  ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory2.GetPath(),
                                             &temp_file_path2));

  // The second launch reuses the opened application. It should pass the
  // launchParams to the opened page, and return the same content::WebContents*.
  EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
  EXPECT_EQ(web_contents, LaunchAppWithoutWaiting({temp_file_path2}));
  EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents, "launchParams2"));

  // Check the second launch directory and launch files are correct.
  EXPECT_EQ("directory", content::EvalJs(web_contents,
                                         "window.launchParams2.files[0].kind"));
  EXPECT_EQ(
      temp_directory2.GetPath().BaseName().AsUTF8Unsafe(),
      content::EvalJs(web_contents, "window.launchParams2.files[0].name"));
  EXPECT_EQ("file", content::EvalJs(web_contents,
                                    "window.launchParams2.files[1].kind"));
  EXPECT_EQ(
      temp_file_path2.BaseName().AsUTF8Unsafe(),
      content::EvalJs(web_contents, "window.launchParams2.files[1].name"));
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerLaunchDirectoryBrowserTest,
                       ReadWritePermissions_OrdinaryDirectory) {
  WaitForTestSystemAppInstall();

  // Test for ordinary directory.
  base::ScopedAllowBlockingForTesting allow_blocking;
  base::ScopedTempDir temp_directory;
  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  TestPermissionsForLaunchDirectory(temp_directory.GetPath());
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerLaunchDirectoryBrowserTest,
                       ReadWritePermissions_SensitiveDirectory) {
  WaitForTestSystemAppInstall();

  // Test for sensitive directory (which are otherwise blocked by
  // FileSystemAccess API). It is safe to use |chrome::DIR_DEFAULT_DOWNLOADS|,
  // because InProcBrowserTest fixture sets up different download directory for
  // each test cases.
  base::ScopedAllowBlockingForTesting allow_blocking;
  base::FilePath sensitive_dir;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &sensitive_dir));
  ASSERT_TRUE(base::DirectoryExists(sensitive_dir));
  TestPermissionsForLaunchDirectory(sensitive_dir);
}

// Base class for testing File Handling and File System Access with Chrome OS
// File System Provider features.
class SystemWebAppManagerLaunchDirectoryFileSystemProviderBrowserTest
    : public SystemWebAppManagerLaunchDirectoryBrowserTest {
 public:
  [[nodiscard]] content::EvalJsResult FileHandleIsGif(
      content::WebContents* web_contents,
      const std::string& file_handle_or_promise) {
    return content::EvalJs(
        web_contents,
        "Promise.resolve(" + file_handle_or_promise + ")" +
            ".then(async fileHandle => {"
            "  const file = await fileHandle.getFile();"
            "  const arrayBuf = await file.arrayBuffer();"
            "  const bytes = new Uint8Array(arrayBuf.slice(0, 3));"
            "  return bytes[0] === 0x47     /* G */"
            "      && bytes[1] === 0x49     /* I */"
            "      && bytes[2] === 0x46;    /* F */"
            "});");
  }

  [[nodiscard]] content::EvalJsResult FileHandleIsPng(
      content::WebContents* web_contents,
      const std::string& file_handle_or_promise) {
    return content::EvalJs(
        web_contents,
        "Promise.resolve(" + file_handle_or_promise + ")" +
            ".then(async fileHandle => {"
            "  const file = await fileHandle.getFile();"
            "  const arrayBuf = await file.arrayBuffer();"
            "  const bytes = new Uint8Array(arrayBuf.slice(0, 4));"
            "  return bytes[0] === 0x89     /* 0x89 */"
            "      && bytes[1] === 0x50     /* P */"
            "      && bytes[2] === 0x4E     /* N */"
            "      && bytes[3] === 0x47;    /* G */"
            "});");
  }

  // Returns whether the file is written.
  [[nodiscard]] ::testing::AssertionResult CanWriteFile(
      content::WebContents* web_contents,
      const std::string& file_handle_or_promise) {
    return content::ExecJs(
        web_contents,
        "Promise.resolve(" + file_handle_or_promise + ")" +
            ".then(async fileHandle => {"
            "  const writable = await fileHandle.createWritable();"
            "  await writable.write('test');"
            "  await writable.close();"
            "});");
  }

  void InstallTestFileSystemProvider(Profile* profile) {
    volume_ = file_manager::test::InstallFileSystemProviderChromeApp(profile);
  }

  base::FilePath GetFileSystemProviderFilePath(const std::string& file_name) {
    return volume_->mount_path().AppendASCII(file_name);
  }

 private:
  base::WeakPtr<file_manager::Volume> volume_;

  // TODO(https://crbug.com/40804030): Remove this when updated to use MV3.
  extensions::ScopedTestMV2Enabler mv2_enabler_;
};

IN_PROC_BROWSER_TEST_P(
    SystemWebAppManagerLaunchDirectoryFileSystemProviderBrowserTest,
    LaunchFromFileSystemProvider_ReadFiles) {
  Profile* profile = browser()->profile();

  WaitForTestSystemAppInstall();
  InstallTestFileSystemProvider(profile);

  // Launch from FileSystemProvider path.
  const char kTestGifFile[] = "readwrite.gif";
  const char kTestPngFile[] = "readonly.png";
  const base::FilePath launch_file =
      GetFileSystemProviderFilePath(kTestGifFile);

  content::WebContents* web_contents = LaunchApp({launch_file});
  EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
  EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents, "launchParams"));

  // Check the launch file is the one we expect, and we can read the file.
  EXPECT_EQ(kTestGifFile,
            content::EvalJs(web_contents, "window.launchParams.files[1].name"));
  EXPECT_EQ(true,
            FileHandleIsGif(web_contents, "window.launchParams.files[1]"));

  // Check we can list the directory.
  EXPECT_EQ(base::StrCat({kTestPngFile, ";", kTestGifFile}),
            content::EvalJs(
                web_contents,
                "(async function() {"
                "  let fileNames = [];"
                "  const files = await window.launchParams.files[0].keys();"
                "  for await (const name of files)"
                "    fileNames.push(name);"
                "  return fileNames.sort().join(';');"
                "})();"));

  // Verify we can read a file (other than launch file) inside the directory.
  EXPECT_EQ(true, FileHandleIsPng(
                      web_contents,
                      content::JsReplace(
                          "window.launchParams.files[0].getFileHandle($1)",
                          kTestPngFile)));
}

// Test that the File System Access implementation doesn't cause a crash when
// writing to readonly files.
IN_PROC_BROWSER_TEST_P(
    SystemWebAppManagerLaunchDirectoryFileSystemProviderBrowserTest,
    LaunchFromFileSystemProvider_WriteFileFails) {
  Profile* profile = browser()->profile();

  WaitForTestSystemAppInstall();
  InstallTestFileSystemProvider(profile);

  content::WebContents* web_contents =
      LaunchApp({GetFileSystemProviderFilePath("readonly.png")});

  EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
  EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents, "launchParams"));

  // Try to write the file.
  EXPECT_FALSE(CanWriteFile(web_contents, "window.launchParams.files[1]"));

  // Do a no-op JavaScript to check the page is still operational. If the page
  // crashed, the following call will fail.
  EXPECT_TRUE(content::ExecJs(web_contents, "(function(){})();"));
}

// Test that the File System Access implementation doesn't cause a crash when
// deleting readonly files.
IN_PROC_BROWSER_TEST_P(
    SystemWebAppManagerLaunchDirectoryFileSystemProviderBrowserTest,
    LaunchFromFileSystemProvider_DeleteFileFails) {
  Profile* profile = browser()->profile();

  WaitForTestSystemAppInstall();
  InstallTestFileSystemProvider(profile);

  content::WebContents* web_contents =
      LaunchApp({GetFileSystemProviderFilePath("readonly.png")});

  EXPECT_TRUE(PrepareToReceiveLaunchParams(web_contents));
  EXPECT_TRUE(WaitAndExposeLaunchParamsToWindow(web_contents, "launchParams"));

  // Deleting the file should fail.
  EXPECT_FALSE(content::ExecJs(
      web_contents,
      content::JsReplace("window.launchParams.files[0].removeEntry($1)",
                         "readonly.png")));

  // Do a no-op JavaScript to check the page is still operational. If the page
  // crashed, the following call will fail.
  EXPECT_TRUE(content::ExecJs(web_contents, "(function() {})();"));
}

class SystemWebAppManagerNotShownInLauncherTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerNotShownInLauncherTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppNotShownInLauncher());
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerNotShownInLauncherTest,
                       NotShownInLauncher) {
  WaitForTestSystemAppInstall();

  webapps::AppId app_id =
      GetManager().GetAppIdForSystemApp(GetAppType()).value();

  GetAppServiceProxy(browser()->profile())
      ->AppRegistryCache()
      .ForOneApp(app_id, [](const apps::AppUpdate& update) {
        EXPECT_FALSE(update.ShowInLauncher().value_or(true));
      });
  // The |AppList| should have all apps visible in the launcher, apps get
  // removed from the |AppList| when they are hidden.
  AppListClientImpl* client = AppListClientImpl::GetInstance();
  ASSERT_TRUE(client);
  AppListModelUpdater* model_updater = ::test::GetModelUpdater(client);
  const ChromeAppListItem* mock_app = model_updater->FindItem(app_id);
  // |mock_app| shouldn't be found in |AppList| because it should be hidden in
  // launcher.
  EXPECT_FALSE(mock_app);
}

class SystemWebAppManagerNotShownInSearchTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerNotShownInSearchTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppNotShownInSearch());
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerNotShownInSearchTest,
                       NotShownInSearch) {
  WaitForTestSystemAppInstall();
  webapps::AppId app_id =
      GetManager().GetAppIdForSystemApp(GetAppType()).value();

  GetAppServiceProxy(browser()->profile())
      ->AppRegistryCache()
      .ForOneApp(app_id, [](const apps::AppUpdate& update) {
        EXPECT_FALSE(update.ShowInSearch().value_or(true));
      });
}

class SystemWebAppManagerHandlesFileOpenIntentsTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerHandlesFileOpenIntentsTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppThatHandlesFileOpenIntents());
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerHandlesFileOpenIntentsTest,
                       HandlesFileOpenIntents) {
  WaitForTestSystemAppInstall();
  webapps::AppId app_id =
      GetManager().GetAppIdForSystemApp(GetAppType()).value();

  GetAppServiceProxy(browser()->profile())
      ->AppRegistryCache()
      .ForOneApp(app_id, [](const apps::AppUpdate& update) {
        EXPECT_TRUE(update.HandlesIntents().value_or(false));
      });
}

class SystemWebAppManagerAdditionalSearchTermsTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerAdditionalSearchTermsTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithAdditionalSearchTerms());
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerAdditionalSearchTermsTest,
                       AdditionalSearchTerms) {
  WaitForTestSystemAppInstall();
  webapps::AppId app_id =
      GetManager().GetAppIdForSystemApp(GetAppType()).value();

  // AdditionalSearchTerms is flaky on Windows as it's a Chrome OS feature.
  GetAppServiceProxy(browser()->profile())
      ->AppRegistryCache()
      .ForOneApp(app_id, [](const apps::AppUpdate& update) {
        EXPECT_EQ(std::vector<std::string>({"Security"}),
                  update.AdditionalSearchTerms());
      });
}

class SystemWebAppManagerHasTabStripWithNewTabButtonTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerHasTabStripWithNewTabButtonTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithTabStrip(
            /*has_tab_strip=*/true, /*hide_new_tab_button=*/false));
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerHasTabStripWithNewTabButtonTest,
                       ShouldHaveTabStripWithNewTabButton) {
  WaitForTestSystemAppInstall();

  Browser* browser;
  EXPECT_TRUE(LaunchApp(GetAppType(), &browser));
  EXPECT_TRUE(browser->app_controller()->has_tab_strip());
  EXPECT_FALSE(browser->app_controller()->ShouldHideNewTabButton());
}

class SystemWebAppManagerHasTabStripWithHiddenNewTabButtonTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerHasTabStripWithHiddenNewTabButtonTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithTabStrip(
            /*has_tab_strip=*/true, /*hide_new_tab_button=*/true));
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerHasTabStripWithHiddenNewTabButtonTest,
                       HasTabStripWithNoNewTabButton) {
  WaitForTestSystemAppInstall();

  Browser* browser;
  EXPECT_TRUE(LaunchApp(GetAppType(), &browser));
  EXPECT_TRUE(browser->app_controller()->has_tab_strip());
  EXPECT_TRUE(browser->app_controller()->ShouldHideNewTabButton());
}

class SystemWebAppManagerHasNoTabStripWithNewTabButtonTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerHasNoTabStripWithNewTabButtonTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithTabStrip(
            /*has_tab_strip=*/false, /*hide_new_tab_button=*/false));
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerHasNoTabStripWithNewTabButtonTest,
                       HasNoTabStripWithNoNewTabButton) {
  WaitForTestSystemAppInstall();

  Browser* browser;
  EXPECT_TRUE(LaunchApp(GetAppType(), &browser));
  EXPECT_FALSE(browser->app_controller()->has_tab_strip());
  EXPECT_TRUE(browser->app_controller()->ShouldHideNewTabButton());
}

class SystemWebAppManagerHasNoTabStripWithHiddenNewTabButtonTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerHasNoTabStripWithHiddenNewTabButtonTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithTabStrip(
            /*has_tab_strip=*/false, /*hide_new_tab_button=*/true));
  }
};

IN_PROC_BROWSER_TEST_P(
    SystemWebAppManagerHasNoTabStripWithHiddenNewTabButtonTest,
    HasNoTabStripWithNoNewTabButton) {
  WaitForTestSystemAppInstall();

  Browser* browser;
  EXPECT_TRUE(LaunchApp(GetAppType(), &browser));
  EXPECT_FALSE(browser->app_controller()->has_tab_strip());
  EXPECT_TRUE(browser->app_controller()->ShouldHideNewTabButton());
}

// We only support custom bounds on Chrome OS.
class SystemWebAppManagerDefaultBoundsTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerDefaultBoundsTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithDefaultBounds(
            kDefaultBounds));
  }

 protected:
  const gfx::Rect kDefaultBounds = {0, 0, 333, 444};
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerDefaultBoundsTest, HasDefaultBounds) {
  WaitForTestSystemAppInstall();

  Browser* browser;
  EXPECT_TRUE(LaunchApp(GetAppType(), &browser));
  EXPECT_EQ(kDefaultBounds, browser->app_controller()->GetDefaultBounds());
  EXPECT_EQ(kDefaultBounds, browser->window()->GetBounds());
}

// Tests that SWA are correctly uninstalled across restarts.
class SystemWebAppManagerUninstallBrowserTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerUninstallBrowserTest() {
    if (content::IsPreTest()) {
      // Use an app with FileHandling enabled since it will perform extra setup
      // steps.
      SetSystemWebAppInstallation(
          TestSystemWebAppInstallation::SetUpAppThatReceivesLaunchFiles(
              TestSystemWebAppInstallation::IncludeLaunchDirectory::kNo));
    } else {
      SetSystemWebAppInstallation(
          TestSystemWebAppInstallation::SetUpWithoutApps());
    }
  }
  ~SystemWebAppManagerUninstallBrowserTest() override = default;
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerUninstallBrowserTest, PRE_Uninstall) {
  WaitForTestSystemAppInstall();
  EXPECT_TRUE(GetManager().GetAppIdForSystemApp(GetAppType()).has_value());
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerUninstallBrowserTest, Uninstall) {
  WaitForTestSystemAppInstall();
  EXPECT_TRUE(GetManager().GetAppIds().empty());

  auto* app_service_proxy =
      apps::AppServiceProxyFactory::GetForProfile(browser()->profile());

  bool swa_found = false;
  app_service_proxy->AppRegistryCache().ForEachApp(
      [&](const apps::AppUpdate& app) {
        if ((app.AppType() == apps::AppType::kSystemWeb ||
             app.AppType() == apps::AppType::kWeb) &&
            apps_util::IsInstalled(app.Readiness())) {
          swa_found = true;
        }
      });
  EXPECT_FALSE(swa_found);
}

// Test that all registered System Apps can be re-installed.
class SystemWebAppManagerInstallAllAppsBrowserTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerInstallAllAppsBrowserTest() {
    features_.InitAndEnableFeature(features::kEnableAllSystemWebApps);
  }
  ~SystemWebAppManagerInstallAllAppsBrowserTest() override = default;

 private:
  base::test::ScopedFeatureList features_;
};

// TODO(crbug.com/40162953): At the moment, PRE_Test failures aren't
// reported in test summary, thus won't fail the CI build job. So we need a
// ordinary test to fail the job and block CQ.
//
// Technically speaking, this test can merge into PRE_Upgrade if the
// aforementioned crbug is fixed.
IN_PROC_BROWSER_TEST_P(SystemWebAppManagerInstallAllAppsBrowserTest,
                       BasicConsistencyCheck) {
  // Wait for apps to install before performing assertions, otherwise the test
  // might flake. See https://crbug.com/1286600#c6.
  GetManager().InstallSystemAppsForTesting();

  const auto& app_map = GetManager().system_app_delegates();
  ASSERT_GT(app_map.size(), 0U);

  for (const auto& type_and_info : app_map) {
    // Check all system app types has a corresponding SystemWebAppDataProto
    // entry defined.
    EXPECT_TRUE(SystemWebAppDataProto_SystemWebAppType_IsValid(
        static_cast<SystemWebAppDataProto_SystemWebAppType>(
            type_and_info.first)))
        << "Please make sure you have added a corresponding entry to "
           "SystemWebAppDataProto when adding a new System Web App.";

    // Check app's install_url and start_url are from the same origin.
    //
    // TODO(crbug.com/40709016): Include OS Settings in this check.
    //
    // OS Settings uses a different install_url origin (by mistake) which are
    // persisted to disk. We can't fix it until the above crbug is fixed.
    // Without fixing the above bug, non-fresh profiles will run into
    // https://crbug.com/1220354.
    if (type_and_info.first != SystemWebAppType::SETTINGS) {
      EXPECT_TRUE(url::IsSameOriginWith(
          type_and_info.second->GetInstallUrl(),
          type_and_info.second->GetWebAppInfo()->start_url()));
    }

    // Check app's web app shortcuts fields is self-consistent.
    auto install_info = type_and_info.second->GetWebAppInfo();
    EXPECT_EQ(install_info->shortcuts_menu_icon_bitmaps.size(),
              install_info->shortcuts_menu_item_infos.size());
  }

  // Check each SWA app has their own unique origin (i.e. doesn't share origin
  // with a different app).
  std::set<url::Origin> install_url_origins;
  std::set<url::Origin> start_url_origins;
  for (const auto& type_and_info : app_map) {
    auto install_url_origin =
        url::Origin::Create(type_and_info.second->GetInstallUrl());
    EXPECT_EQ(0u, install_url_origins.count(install_url_origin))
        << "System web app's install_url origin should be unique.";
    install_url_origins.insert(install_url_origin);

    auto start_url_origin =
        url::Origin::Create(type_and_info.second->GetWebAppInfo()->start_url());
    EXPECT_EQ(0u, start_url_origins.count(start_url_origin))
        << "System web app's start_url origin should be unique.";
    start_url_origins.insert(start_url_origin);
  }

  // Check apps (other than Terminal, which is published by its own App
  // publisher) are exposed in AppService.
  for (const auto& type_and_info : app_map) {
    if (type_and_info.first == SystemWebAppType::TERMINAL)
      continue;

    std::optional<std::string> app_id =
        GetManager().GetAppIdForSystemApp(type_and_info.first);
    EXPECT_TRUE(app_id);

    bool app_found = false;
    apps::AppServiceProxyFactory::GetForProfile(browser()->profile())
        ->AppRegistryCache()
        .ForOneApp(*app_id, [&](const apps::AppUpdate& app) {
          app_found = true;
          EXPECT_EQ(
              app.Name(),
              base::UTF16ToUTF8(type_and_info.second->GetWebAppInfo()->title));
        });
    EXPECT_TRUE(app_found) << "System Web App "
                           << type_and_info.second->GetInternalName()
                           << " can't be found in AppService after install.";
  }

  // Verify that all system web apps which are enabled by default and appear in
  // the launcher have an explicit launcher position set.
  std::vector<std::string> app_order;
  chromeos::default_app_order::Get(&app_order);

  // Demo/testing apps don't need a launcher position.
  const base::flat_set<SystemWebAppType> kLauncherPositionExemptTypes = {
      SystemWebAppType::SAMPLE};

  for (const auto& [app_type, app_delegate] : app_map) {
    if (app_delegate->IsAppEnabled() && app_delegate->ShouldShowInLauncher() &&
        !base::Contains(kLauncherPositionExemptTypes, app_type)) {
      EXPECT_TRUE(base::Contains(app_order,
                                 GetManager().GetAppIdForSystemApp(app_type)))
          << "System app '" << app_delegate->GetInternalName()
          << "' appears in the launcher but does not have an app order "
             "definition. Its app ID should be added to GetDefault() in "
             "//chrome/browser/ash/extensions/default_app_order.cc, which "
             "should match the order in go/default-apps";
    }
  }

  // Verify that all system web apps have an icon.
  for (const auto& [_, delegate] : app_map) {
    const auto info = delegate->GetWebAppInfo();
    EXPECT_FALSE(info->manifest_icons.empty())
        << delegate->GetInternalName() << " needs a manifest icon";
    EXPECT_FALSE(delegate->GetWebAppInfo()->icon_bitmaps.empty())
        << delegate->GetInternalName() << " needs an icon bitmap";
  }
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerInstallAllAppsBrowserTest, Upgrade) {
  GetManager().InstallSystemAppsForTesting();
  const auto& app_ids = GetManager().GetAppIds();

  EXPECT_EQ(GetManager().system_app_delegates().size(), app_ids.size());

  // Some system web apps keep their resources (e.g. html pages) in real
  // Chrome OS images. Here we test a few apps whose resources are bundled in
  // chrome and always available. These apps are able to cover the code path we
  // execute when launching the app.
  const SystemWebAppType apps_to_launch[] = {
      SystemWebAppType::SETTINGS,
      SystemWebAppType::MEDIA,  // Uses File Handling with launch directory
  };

  for (const auto& type : apps_to_launch) {
    EXPECT_TRUE(LaunchApp(type));
  }
}

class SystemWebAppManagerChromeUntrustedTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerChromeUntrustedTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpChromeUntrustedApp());
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerChromeUntrustedTest, Install) {
  WaitForTestSystemAppInstall();

  // Don't wait for page load because we want to verify AppController identifies
  // the System Web App before the app loads.
  Browser* app_browser;
  LaunchAppWithoutWaiting(GetAppType(), &app_browser);

  webapps::AppId app_id =
      GetManager().GetAppIdForSystemApp(GetAppType()).value();
  EXPECT_EQ(app_id, app_browser->app_controller()->app_id());
  EXPECT_TRUE(GetManager().IsSystemWebApp(app_id));

  Profile* profile = app_browser->profile();
  web_app::WebAppRegistrar& registrar =
      web_app::WebAppProvider::GetForTest(profile)->registrar_unsafe();

  EXPECT_EQ("Test System App Untrusted", registrar.GetAppShortName(app_id));
  EXPECT_EQ(SkColorSetRGB(0xFF, 0, 0), registrar.GetAppThemeColor(app_id));
  EXPECT_TRUE(registrar.HasExternalAppWithInstallSource(
      app_id, web_app::ExternalInstallSource::kSystemInstalled));
  EXPECT_EQ(registrar.FindBestAppWithUrlInScope(
                GURL("chrome-untrusted://test-system-app/"),
                web_app::WebAppFilter::InstalledInOperatingSystemForTesting()),
            app_id);
}

class SystemWebAppManagerOriginTrialsBrowserTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerOriginTrialsBrowserTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithEnabledOriginTrials(
            OriginTrialsMap({{GetOrigin(main_url_), main_url_trials_},
                             {GetOrigin(trial_url_), trial_url_trials_}})));
  }

  ~SystemWebAppManagerOriginTrialsBrowserTest() override = default;

 protected:
  std::unique_ptr<content::WebContents> CreateTestWebContents() {
    content::WebContents::CreateParams create_params(browser()->profile());
    return content::WebContents::Create(create_params);
  }

  const std::vector<std::string> main_url_trials_ = {"Frobulate"};
  const std::vector<std::string> trial_url_trials_ = {"FrobulateNavigation"};

  const GURL main_url_ = GURL("chrome://test-system-app/pwa.html");
  const GURL trial_url_ = GURL("chrome://test-subframe/title2.html");
  const GURL notrial_url_ = GURL("chrome://notrial-subframe/title3.html");

 private:
  url::Origin GetOrigin(const GURL& url) { return url::Origin::Create(url); }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerOriginTrialsBrowserTest,
                       ForceEnabledOriginTrials_FirstNavigationIntoPage) {
  WaitForTestSystemAppInstall();
  auto app_id = GetManager().GetAppIdForSystemApp(GetAppType()).value();

  std::unique_ptr<content::WebContents> web_contents_owned =
      CreateTestWebContents();
  content::WebContents* web_contents = web_contents_owned.get();
  browser()->tab_strip_model()->AppendWebContents(std::move(web_contents_owned),
                                                  /*foreground=*/true);
  auto& tab_helper = *web_app::WebAppTabHelper::FromWebContents(web_contents);

  // Simulate when first navigating into app's launch url.
  {
    content::MockNavigationHandle mock_nav_handle(main_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(true);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials(main_url_trials_));
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
    ASSERT_EQ(app_id, *web_app::WebAppTabHelper::GetAppId(web_contents));
  }

  // Simulate loading app's embedded child-frame that has origin trials.
  {
    content::MockNavigationHandle mock_nav_handle(trial_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(false);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials(trial_url_trials_));
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
  }

  // Simulate loading app's embedded child-frame that has no origin trial.
  {
    content::MockNavigationHandle mock_nav_handle(notrial_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(false);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials).Times(0);
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
  }
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerOriginTrialsBrowserTest,
                       ForceEnabledOriginTrials_IntraDocumentNavigation) {
  WaitForTestSystemAppInstall();
  auto app_id = GetManager().GetAppIdForSystemApp(GetAppType()).value();

  std::unique_ptr<content::WebContents> web_contents_owned =
      CreateTestWebContents();
  content::WebContents* web_contents = web_contents_owned.get();
  browser()->tab_strip_model()->AppendWebContents(std::move(web_contents_owned),
                                                  /*foreground=*/true);
  auto& tab_helper = *web_app::WebAppTabHelper::FromWebContents(web_contents);

  // Simulate when first navigating into app's launch url.
  {
    content::MockNavigationHandle mock_nav_handle(main_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(true);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials(main_url_trials_));
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
    ASSERT_EQ(app_id, *web_app::WebAppTabHelper::GetAppId(web_contents));
  }

  // Simulate same-document navigation.
  {
    content::MockNavigationHandle mock_nav_handle(main_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(true);
    mock_nav_handle.set_is_same_document(true);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials).Times(0);
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
  }
}

// This test checks origin trials are correctly enabled for navigations on the
// main frame, this test checks:
// - The app's main page |main_url_| has OT.
// - The iframe page |trial_url_| has OT, only if it is embedded by the app.
// - When navigating from a cross-origin page to the app's main page, the main
// page has OT.
IN_PROC_BROWSER_TEST_P(SystemWebAppManagerOriginTrialsBrowserTest,
                       ForceEnabledOriginTrials_Navigation) {
  WaitForTestSystemAppInstall();
  auto app_id = GetManager().GetAppIdForSystemApp(GetAppType()).value();

  std::unique_ptr<content::WebContents> web_contents_owned =
      CreateTestWebContents();
  content::WebContents* web_contents = web_contents_owned.get();
  browser()->tab_strip_model()->AppendWebContents(std::move(web_contents_owned),
                                                  /*foreground=*/true);
  auto& tab_helper = *web_app::WebAppTabHelper::FromWebContents(web_contents);

  // Simulate when first navigating into app's launch url.
  {
    content::MockNavigationHandle mock_nav_handle(main_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(true);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials(main_url_trials_));
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
    ASSERT_EQ(app_id, *web_app::WebAppTabHelper::GetAppId(web_contents));
  }

  // Simulate navigating to a different site without origin trials.
  {
    content::MockNavigationHandle mock_nav_handle(notrial_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(true);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials).Times(0);
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
    ASSERT_EQ(nullptr, web_app::WebAppTabHelper::GetAppId(web_contents));
  }

  // Simulate navigating back to a SWA with origin trials.
  {
    content::MockNavigationHandle mock_nav_handle(main_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(true);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials(main_url_trials_));
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
    ASSERT_EQ(app_id, *web_app::WebAppTabHelper::GetAppId(web_contents));
  }

  // Simulate navigating the main frame to a url embedded by SWA. This url has
  // origin trials when embedded by SWA. However, when this url is loaded in the
  // main frame, it should not get origin trials.
  {
    content::MockNavigationHandle mock_nav_handle(trial_url_, nullptr);
    mock_nav_handle.set_is_in_primary_main_frame(true);
    mock_nav_handle.set_is_same_document(false);
    EXPECT_CALL(mock_nav_handle, ForceEnableOriginTrials).Times(0);
    tab_helper.ReadyToCommitNavigation(&mock_nav_handle);
    ASSERT_EQ(nullptr, web_app::WebAppTabHelper::GetAppId(web_contents));
  }
}

class SystemWebAppManagerAppSuspensionBrowserTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerAppSuspensionBrowserTest() = default;

  apps::Readiness GetAppReadiness(const webapps::AppId& app_id) {
    apps::Readiness readiness;
    bool app_found =
        GetAppServiceProxy(browser()->profile())
            ->AppRegistryCache()
            .ForOneApp(app_id, [&readiness](const apps::AppUpdate& update) {
              readiness = update.Readiness();
            });
    CHECK(app_found);
    return readiness;
  }

  std::optional<apps::IconKey> GetAppIconKey(const webapps::AppId& app_id) {
    std::optional<apps::IconKey> icon_key;
    bool app_found =
        GetAppServiceProxy(browser()->profile())
            ->AppRegistryCache()
            .ForOneApp(app_id, [&icon_key](const apps::AppUpdate& update) {
              icon_key = update.IconKey();
            });
    CHECK(app_found);
    return icon_key;
  }
};

// Tests that System Apps can be suspended when the policy is set before the app
// is installed.
IN_PROC_BROWSER_TEST_P(SystemWebAppManagerAppSuspensionBrowserTest,
                       AppSuspendedBeforeInstall) {
  ASSERT_FALSE(GetManager()
                   .GetAppIdForSystemApp(SystemWebAppType::SETTINGS)
                   .has_value());
  {
    ScopedListPrefUpdate update(
        TestingBrowserProcess::GetGlobal()->local_state(),
        policy::policy_prefs::kSystemFeaturesDisableList);
    update->Append(static_cast<int>(policy::SystemFeature::kOsSettings));
  }
  WaitForTestSystemAppInstall();
  std::optional<webapps::AppId> settings_id =
      GetManager().GetAppIdForSystemApp(SystemWebAppType::SETTINGS);
  DCHECK(settings_id.has_value());

  EXPECT_EQ(apps::Readiness::kDisabledByPolicy, GetAppReadiness(*settings_id));
  EXPECT_TRUE(apps::IconEffects::kBlocked &
              GetAppIconKey(*settings_id)->icon_effects);

  {
    ScopedListPrefUpdate update(
        TestingBrowserProcess::GetGlobal()->local_state(),
        policy::policy_prefs::kSystemFeaturesDisableList);
    update->clear();
  }
  SystemWebAppManager::GetWebAppProvider(browser()->profile())
      ->command_manager()
      .AwaitAllCommandsCompleteForTesting();
  EXPECT_EQ(apps::Readiness::kReady, GetAppReadiness(*settings_id));
  EXPECT_FALSE(apps::IconEffects::kBlocked &
               GetAppIconKey(*settings_id)->icon_effects);
}

// Tests that System Apps can be suspended when the policy is set after the app
// is installed.
IN_PROC_BROWSER_TEST_P(SystemWebAppManagerAppSuspensionBrowserTest,
                       AppSuspendedAfterInstall) {
  base::AddFeatureIdTagToTestResult(
      "screenplay-44570758-2d0f-4ed9-8172-102244523249");

  WaitForTestSystemAppInstall();
  std::optional<webapps::AppId> settings_id =
      GetManager().GetAppIdForSystemApp(SystemWebAppType::SETTINGS);
  DCHECK(settings_id.has_value());
  EXPECT_EQ(apps::Readiness::kReady, GetAppReadiness(*settings_id));

  {
    ScopedListPrefUpdate update(
        TestingBrowserProcess::GetGlobal()->local_state(),
        policy::policy_prefs::kSystemFeaturesDisableList);
    update->Append(static_cast<int>(policy::SystemFeature::kOsSettings));
  }
  SystemWebAppManager::GetWebAppProvider(browser()->profile())
      ->command_manager()
      .AwaitAllCommandsCompleteForTesting();

  EXPECT_EQ(apps::Readiness::kDisabledByPolicy, GetAppReadiness(*settings_id));
  EXPECT_TRUE(apps::IconEffects::kBlocked &
              GetAppIconKey(*settings_id)->icon_effects);

  {
    ScopedListPrefUpdate update(
        TestingBrowserProcess::GetGlobal()->local_state(),
        policy::policy_prefs::kSystemFeaturesDisableList);
    update->clear();
  }
  SystemWebAppManager::GetWebAppProvider(browser()->profile())
      ->command_manager()
      .AwaitAllCommandsCompleteForTesting();
  EXPECT_EQ(apps::Readiness::kReady, GetAppReadiness(*settings_id));
  EXPECT_FALSE(apps::IconEffects::kBlocked &
               GetAppIconKey(*settings_id)->icon_effects);
}
INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerAppSuspensionBrowserTest);

class SystemWebAppManagerShortcutTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerShortcutTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithShortcuts());
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerShortcutTest, ShortcutUrl) {
  WaitForTestSystemAppInstall();
  webapps::AppId app_id =
      GetManager()
          .GetAppIdForSystemApp(SystemWebAppType::SHORTCUT_CUSTOMIZATION)
          .value();
  Browser* browser;
  content::WebContents* web_contents =
      LaunchApp(SystemWebAppType::SHORTCUT_CUSTOMIZATION, &browser);
  EXPECT_TRUE(web_contents);

  std::unique_ptr<ui::SimpleMenuModel> menu_model;
  {
    ShelfModel* const shelf_model = ShelfModel::Get();
    PinAppWithIDToShelf(app_id);
    ShelfItemDelegate* const delegate =
        shelf_model->GetShelfItemDelegate(ShelfID(app_id));
    base::RunLoop run_loop;
    delegate->GetContextMenu(
        display::Display::GetDefaultDisplay().id(),
        base::BindLambdaForTesting(
            [&run_loop,
             &menu_model](std::unique_ptr<ui::SimpleMenuModel> model) {
              menu_model = std::move(model);
              run_loop.Quit();
            }));
    run_loop.Run();
  }

  auto check_shortcut = [&menu_model](size_t index, int shortcut_index,
                                      const std::u16string& label) {
    EXPECT_EQ(menu_model->GetTypeAt(index), ui::MenuModel::TYPE_COMMAND);
    EXPECT_EQ(menu_model->GetCommandIdAt(index),
              LAUNCH_APP_SHORTCUT_FIRST + shortcut_index);
    EXPECT_EQ(menu_model->GetLabelAt(index), label);
  };

  // Shortcuts appear last in the context menu.
  check_shortcut(menu_model->GetItemCount() - 3, 0, u"One");
  // menu_model->GetItemCount() - 2 is used by a separator
  check_shortcut(menu_model->GetItemCount() - 1, 1, u"Two");

  const int command_id = LAUNCH_APP_SHORTCUT_FIRST + 1;
  content::LoadStopObserver url_observer(web_contents);
  menu_model->ActivatedAt(menu_model->GetIndexOfCommandId(command_id).value(),
                          ui::EF_LEFT_MOUSE_BUTTON);
  url_observer.Wait();
}

class SystemWebAppManagerBackgroundTaskTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerBackgroundTaskTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppWithBackgroundTask());
  }

  void WaitForSystemAppsBackgroundTasksStart() {
    base::RunLoop run_loop;
    SystemWebAppManager::Get(browser()->profile())
        ->on_tasks_started()
        .Post(FROM_HERE, run_loop.QuitClosure());

    run_loop.Run();
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerBackgroundTaskTest, TimerFires) {
  // The SystemWebAppManager gets created in the Setup(), in the test
  // constructor, and the background tasks get created during synchronize.
  // Ideally, we'd make a TestNavigationObserver in the constructor, but they
  // have to be single threaded, and throw a check fail. There's a race
  // condition here because the background tasks are fired as callbacks in
  // response to the install finishing. So, we wait for the apps to be
  // installed, then wait on the navigation. A cleaner solution would be to have
  // a hook in the background pages to detect the navigation as an event. That's
  // a little too much work for one test though, and since this is mostly tested
  // in unittests, this is probably enough.
  content::TestNavigationObserver navigation_observer(
      GURL("chrome://test-system-app/page2.html"));
  navigation_observer.StartWatchingNewWebContents();
  ui::ScopedSetIdleState idle(ui::IDLE_STATE_IDLE);

  WaitForSystemAppsBackgroundTasksStart();

  auto& tasks = GetManager().GetBackgroundTasksForTesting();
  auto* timer = tasks[0]->get_timer_for_testing();
  EXPECT_EQ(base::Seconds(120), timer->GetCurrentDelay());
  EXPECT_EQ(SystemWebAppBackgroundTask::INITIAL_WAIT,
            tasks[0]->get_state_for_testing());
  // The "Immediate" timer waits for 2 minutes, and it's really hard to mock
  // time properly in a browser test, so just fire the thing now. We're not
  // testing that base::Timer works.
  timer->FireNow();

  navigation_observer.Wait();
  EXPECT_TRUE(timer->IsRunning());
  EXPECT_EQ(1u, tasks.size());
  EXPECT_TRUE(tasks[0]->open_immediately_for_testing());
  EXPECT_EQ(base::Days(1), tasks[0]->period_for_testing());
  EXPECT_EQ(1u, tasks[0]->timer_activated_count_for_testing());
  EXPECT_EQ(SystemWebAppBackgroundTask::WAIT_PERIOD,
            tasks[0]->get_state_for_testing());
  EXPECT_EQ(base::Days(1), timer->GetCurrentDelay());
}

class SystemWebAppManagerContextMenuBrowserTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppManagerContextMenuBrowserTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppsForContestMenuTest());
  }
  ~SystemWebAppManagerContextMenuBrowserTest() override = default;

 protected:
  std::unique_ptr<TestRenderViewContextMenu> CreateContextMenu(
      content::WebContents* web_contents,
      const GURL& link_href) {
    content::ContextMenuParams params;
    params.unfiltered_link_url = link_href;
    params.link_url = link_href;
    params.src_url = link_href;
    params.link_text = std::u16string();
    params.media_type = blink::mojom::ContextMenuDataMediaType::kNone;
    params.page_url = web_contents->GetVisibleURL();
    params.source_type = ui::mojom::MenuSourceType::kNone;
    auto menu = std::make_unique<TestRenderViewContextMenu>(
        *web_contents->GetPrimaryMainFrame(), params);
    menu->Init();
    return menu;
  }

  // See TestSystemWebAppInstallation::SetUpAppsForContestMenuTest.
  const SystemWebAppType kAppTypeSingleWindow = SystemWebAppType::SETTINGS;
  const SystemWebAppType kAppTypeMultiWindow = SystemWebAppType::FILE_MANAGER;
  const SystemWebAppType kAppTypeSingleWindowTabStrip = SystemWebAppType::MEDIA;
  const SystemWebAppType kAppTypeMultiWindowTabStrip = SystemWebAppType::HELP;
};

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerContextMenuBrowserTest,
                       LinkToAppItself) {
  WaitForTestSystemAppInstall();

  {
    // Single window, no tab strip.
    auto* web_contents = LaunchApp(kAppTypeSingleWindow);
    auto menu =
        CreateContextMenu(web_contents, web_contents->GetLastCommittedURL());
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }

  {
    // Single window, has tab strip.
    auto* web_contents = LaunchApp(kAppTypeSingleWindowTabStrip);
    auto menu =
        CreateContextMenu(web_contents, web_contents->GetLastCommittedURL());
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }

  {
    // Multi window, no tab strip.
    auto* web_contents = LaunchApp(kAppTypeMultiWindow);
    auto menu =
        CreateContextMenu(web_contents, web_contents->GetLastCommittedURL());
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }

  {
    // Multi window, has tab strip.
    auto* web_contents = LaunchApp(kAppTypeMultiWindowTabStrip);
    auto menu =
        CreateContextMenu(web_contents, web_contents->GetLastCommittedURL());
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerContextMenuBrowserTest,
                       LinkToOtherSystemWebApp) {
  WaitForTestSystemAppInstall();

  {
    // Typical SWA, single window, no tab strip.
    auto* web_contents = LaunchApp(kAppTypeSingleWindow);
    auto menu = CreateContextMenu(web_contents,
                                  GetStartUrl(kAppTypeSingleWindowTabStrip));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }

  {
    // Deliberately test on a multi-window, tab-strip app to cover edge cases.
    auto* web_contents = LaunchApp(kAppTypeMultiWindowTabStrip);
    auto menu =
        CreateContextMenu(web_contents, GetStartUrl(kAppTypeMultiWindow));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }
}

IN_PROC_BROWSER_TEST_P(SystemWebAppManagerContextMenuBrowserTest, WebLink) {
  WaitForTestSystemAppInstall();

  GURL kWebUrl = GURL("https://example.com/");

  {
    // Typical SWA, single window, no tab strip.
    auto* web_contents = LaunchApp(kAppTypeSingleWindow);
    auto menu = CreateContextMenu(web_contents, kWebUrl);
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }

  {
    // Deliberately test on a multi-window, tab-strip app to cover edge cases.
    auto* web_contents = LaunchApp(kAppTypeMultiWindowTabStrip);
    auto menu = CreateContextMenu(web_contents, kWebUrl);
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
    EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD));
    EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
  }
}

class SystemWebAppSingleWindowTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppSingleWindowTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpStandaloneSingleWindowApp());
  }
  ~SystemWebAppSingleWindowTest() override = default;
};

IN_PROC_BROWSER_TEST_P(SystemWebAppSingleWindowTest, WindowReuse) {
  WaitForTestSystemAppInstall();

  content::WebContents* web_contents = LaunchApp(GetAppType());

  // Second launch reuses the window.
  EXPECT_EQ(web_contents, LaunchAppWithoutWaiting(GetAppType()));

  // Third launch reuses the window despite different URL.
  apps::AppLaunchParams params = LaunchParamsForApp(GetAppType());
  params.override_url = GURL("https://example.com/in-scope");
  EXPECT_EQ(web_contents, LaunchAppWithoutWaiting(std::move(params)));
}

class SystemWebAppAccessibilityTest : public SystemWebAppSingleWindowTest {};

IN_PROC_BROWSER_TEST_P(SystemWebAppAccessibilityTest,
                       CanCycleToWindowControlButtons) {
  ChromeVoxTestUtils chromevox_test_utils;
  chromevox_test_utils.EnableChromeVox();
  WaitForTestSystemAppInstall();

  // Launch the app so it shows up in shelf.
  Browser* app_browser;
  gfx::NativeWindow app_window;

  chromevox_test_utils.sm()->Call([&]() {
    LaunchApp(GetAppType(), &app_browser);
    app_window = app_browser->window()->GetNativeWindow();
    // F6 to switch pane.
    ui::test::EventGenerator generator(app_window->GetRootWindow(), app_window);
    generator.PressAndReleaseKey(ui::VKEY_F6, ui::EF_FINAL);
  });
  chromevox_test_utils.sm()->ExpectSpeech("Test System App");
  chromevox_test_utils.sm()->ExpectSpeech("Application");

  // Launcher-B to find minimize button.
  chromevox_test_utils.sm()->Call([&]() {
    // Search+B to switch pane.
    ui::test::EventGenerator generator(app_window->GetRootWindow());
    generator.PressAndReleaseKeyAndModifierKeys(
        ui::VKEY_B, ui::EF_COMMAND_DOWN | ui::EF_FINAL);
  });
  chromevox_test_utils.sm()->ExpectSpeech("Minimize");
  chromevox_test_utils.sm()->ExpectSpeech("Button");

  // Start the actions.
  chromevox_test_utils.sm()->Replay();
}

class SystemWebAppAbortsLaunchTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppAbortsLaunchTest() {
    SetSystemWebAppInstallation(
        TestSystemWebAppInstallation::SetUpAppThatAbortsLaunch());
  }
  ~SystemWebAppAbortsLaunchTest() override = default;
};

IN_PROC_BROWSER_TEST_P(SystemWebAppAbortsLaunchTest, LaunchAborted) {
  WaitForTestSystemAppInstall();

  LaunchSystemWebAppAsync(browser()->profile(), GetAppType());

  EXPECT_EQ(0U, GetSystemWebAppBrowserCount(GetAppType()));
}

class SystemWebAppIconHealthMetricsTest
    : public TestProfileTypeMixin<SystemWebAppBrowserTestBase> {
 public:
  SystemWebAppIconHealthMetricsTest() {
    // Only reinstall on version change, so we don't force reinstall
    // and overwrite the broken icon.
    auto installation = TestSystemWebAppInstallation::SetUpAppWithValidIcons();
    installation->set_update_policy(
        SystemWebAppManager::UpdatePolicy::kOnVersionChange);

    SetSystemWebAppInstallation(std::move(installation));
  }
  ~SystemWebAppIconHealthMetricsTest() override = default;

 protected:
  static constexpr char kIconsAreHealthyHistogramName[] =
      "Webapp.SystemApps.IconsAreHealthyInSession";
  base::HistogramTester tester_;

  void WaitForInstallAndIconCheck() {
    WaitForTestSystemAppInstall();

    base::RunLoop run_loop;
    SystemWebAppManager::Get(browser()->profile())
        ->on_icon_check_completed()
        .Post(FROM_HERE, run_loop.QuitClosure());
    run_loop.Run();
  }
};

IN_PROC_BROWSER_TEST_P(SystemWebAppIconHealthMetricsTest, ReportsMetrics) {
  WaitForInstallAndIconCheck();

  tester_.ExpectBucketCount(kIconsAreHealthyHistogramName, true, 1);
  // Given SWA install with no broken icon, pref should report no broken icons.
  EXPECT_FALSE(browser()->profile()->GetPrefs()->GetBoolean(
      SystemWebAppManager::kSystemWebAppSessionHasBrokenIconsPrefName));
}

IN_PROC_BROWSER_TEST_P(SystemWebAppIconHealthMetricsTest,
                       PRE_PRE_ReinstallFixesBrokenIcon) {
  WaitForInstallAndIconCheck();

  // Given SWA install with no broken icon, pref should report no broken icons.
  CHECK_EQ(
      false,
      browser()->profile()->GetPrefs()->GetBoolean(
          SystemWebAppManager::kSystemWebAppSessionHasBrokenIconsPrefName));

  // Intentionally break icons by corrupting the on-disk icon file.
  auto app_id = GetManager().GetAppIdForSystemApp(GetAppType()).value();
  base::FilePath icon_path =
      SystemWebAppManager::GetWebAppProvider(browser()->profile())
          ->icon_manager()
          .GetIconFilePathForTesting(app_id, web_app::IconPurpose::ANY, 32);

  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    base::WriteFile(icon_path, "Not a PNG file");
  }

  // Restart to let SystemWebAppManager perform the check.
}

IN_PROC_BROWSER_TEST_P(SystemWebAppIconHealthMetricsTest,
                       PRE_ReinstallFixesBrokenIcon) {
  WaitForInstallAndIconCheck();

  tester_.ExpectBucketCount(kIconsAreHealthyHistogramName, false, 1);

  // TODO(crbug.com/40162953): Change CHECK_EQ to EXPECT_TRUE when
  // assertions report correctly as test failure in PRE_TESTs.

  // Icon check should update pref to report broken icons.
  CHECK_EQ(
      true,
      browser()->profile()->GetPrefs()->GetBoolean(
          SystemWebAppManager::kSystemWebAppSessionHasBrokenIconsPrefName));
}

IN_PROC_BROWSER_TEST_P(SystemWebAppIconHealthMetricsTest,
                       ReinstallFixesBrokenIcon) {
  WaitForInstallAndIconCheck();

  tester_.ExpectBucketCount(kIconsAreHealthyHistogramName, true, 1);
  tester_.ExpectBucketCount(
      SystemWebAppManager::kIconsFixedOnReinstallHistogramName, true, 1);
}

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerBrowserTestBasicInstall);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerLaunchFilesBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerLaunchDirectoryBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerLaunchDirectoryFileSystemProviderBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerNotShownInLauncherTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerNotShownInSearchTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerHandlesFileOpenIntentsTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerAdditionalSearchTermsTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerChromeUntrustedTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerOriginTrialsBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerUninstallBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerInstallAllAppsBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerShortcutTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerBackgroundTaskTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerHasTabStripWithNewTabButtonTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerHasTabStripWithHiddenNewTabButtonTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerHasNoTabStripWithNewTabButtonTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerHasNoTabStripWithHiddenNewTabButtonTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerDefaultBoundsTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppSingleWindowTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppAccessibilityTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppAbortsLaunchTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppIconHealthMetricsTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerContextMenuBrowserTest);

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    SystemWebAppManagerLaunchWithUrlBrowserTest);

}  // namespace ash