File: encrypted_media_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 (1867 lines) | stat: -rw-r--r-- 76,477 bytes parent folder | download | duplicates (5)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <tuple>
#include <utility>

#include "base/command_line.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/media/clear_key_cdm_test_helper.h"
#include "chrome/browser/media/media_browsertest.h"
#include "chrome/browser/media/test_license_server.h"
#include "chrome/browser/media/wv_test_license_server_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/test_launcher_utils.h"
#include "components/prefs/pref_service.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/variations/variations_switches.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "media/base/key_system_names.h"
#include "media/base/key_systems.h"
#include "media/base/media_switches.h"
#include "media/base/test_data_util.h"
#include "media/cdm/clear_key_cdm_common.h"
#include "media/cdm/supported_cdm_versions.h"
#include "media/media_buildflags.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "third_party/widevine/cdm/buildflags.h"
#include "third_party/widevine/cdm/widevine_cdm_common.h"
#include "ui/gl/gl_switches.h"

#if BUILDFLAG(IS_WIN)
#include <mfapi.h>

#include "base/win/windows_version.h"
#include "chrome/browser/media/media_foundation_service_monitor.h"
#include "content/public/browser/gpu_data_manager.h"
#include "gpu/config/gpu_info.h"
#include "media/audio/win/core_audio_util_win.h"
#include "media/base/win/mf_feature_checks.h"
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
const char kExternalClearKeyInitializeFailKeySystem[] =
    "org.chromium.externalclearkey.initializefail";
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

// Sessions to load.
const char kNoSessionToLoad[] = "";
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
const char kPersistentLicense[] = "PersistentLicense";
const char kUnknownSession[] = "UnknownSession";
#endif

// EME-specific test results and errors.
const char kUnitTestSuccess[] = "UNIT_TEST_SUCCESS";
const char16_t kEmeUnitTestFailure16[] = u"UNIT_TEST_FAILURE";
const char kEmeNotSupportedError[] = "NOTSUPPORTEDERROR";
const char16_t kEmeNotSupportedError16[] = u"NOTSUPPORTEDERROR";
const char16_t kEmeGenerateRequestFailed[] = u"EME_GENERATEREQUEST_FAILED";
const char16_t kEmeSessionNotFound16[] = u"EME_SESSION_NOT_FOUND";
const char16_t kEmeLoadFailed[] = u"EME_LOAD_FAILED";
const char kEmeUpdateFailed[] = "EME_UPDATE_FAILED";
const char16_t kEmeUpdateFailed16[] = u"EME_UPDATE_FAILED";
const char16_t kEmeErrorEvent[] = u"EME_ERROR_EVENT";
const char16_t kEmeMessageUnexpectedType[] = u"EME_MESSAGE_UNEXPECTED_TYPE";
const char16_t kEmeRenewalMissingHeader[] = u"EME_RENEWAL_MISSING_HEADER";
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
const char kEmeSessionClosedAndError[] = "EME_SESSION_CLOSED_AND_ERROR";
const char kEmeSessionNotFound[] = "EME_SESSION_NOT_FOUND";
#if BUILDFLAG(IS_CHROMEOS)
const char kEmeUnitTestFailure[] = "UNIT_TEST_FAILURE";
#endif
#endif

const char kDefaultEmePlayer[] = "eme_player.html";
const char kDefaultMseOnlyEmePlayer[] = "mse_different_containers.html";

#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_PROPRIETARY_CODECS) && \
    BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
static constexpr wchar_t kDolbyVisionProfile5[] = L"dvhe.05";
static constexpr wchar_t kDolbyVisionProfile8[] = L"dvhe.08";
#endif

#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_PROPRIETARY_CODECS)
const char kProtectedContentIdPrefPath[] =
    "profile.default_content_setting_values.protected_media_identifier";
const char kProtectedContentIdExceptionPrefPath[] =
    "profile.content_settings.exceptions.protected_media_identifier";
const int kAllowProtectedContentId = 1;
const int kDisallowProtectedContentId = 2;
#endif  // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_PROPRIETARY_CODECS)

// The type of video src used to load media.
enum class SrcType { SRC, MSE };

// Must be in sync with CONFIG_CHANGE_TYPE in eme_player_js/global.js
enum class ConfigChangeType {
  CLEAR_TO_CLEAR = 0,
  CLEAR_TO_ENCRYPTED = 1,
  ENCRYPTED_TO_CLEAR = 2,
  ENCRYPTED_TO_ENCRYPTED = 3,
};

// Whether the video should be not played or played once or twice.
enum class PlayCount { ZERO = 0, ONCE = 1, TWICE = 2 };

using testing::Bool;
using testing::Combine;
using testing::Eq;
using testing::Not;
using testing::Pair;
using testing::UnitTest;
using testing::UnorderedElementsAre;
using testing::Values;
using testing::WithParamInterface;
using ukm::builders::Media_EME_ApiPromiseRejection;
using ukm::builders::Media_EME_CdmMetrics;
using ukm::builders::Media_EME_CreateMediaKeys;
using ukm::builders::Media_EME_RequestMediaKeySystemAccess;
using ukm::builders::Media_EME_Usage;

// Base class for encrypted media tests.
class EncryptedMediaTestBase : public MediaBrowserTest {
 public:
  // Occasionally these tests are timing out (see crbug.com/1444672).
  // Overriding the setup/run/teardown methods so that the failures will log
  // when these happen to see if we can get a better understanding of what
  // causes the timeout.
  void SetUp() override {
    LOG(INFO) << __func__;
    MediaBrowserTest::SetUp();
  }

  void TearDown() override {
    LOG(INFO) << __func__;
    MediaBrowserTest::TearDown();
  }

  void SetUpOnMainThread() override {
    LOG(INFO) << __func__;
    MediaBrowserTest::SetUpOnMainThread();
  }

  void TearDownOnMainThread() override {
    LOG(INFO) << __func__;
    MediaBrowserTest::TearDownOnMainThread();
  }

  bool RequiresClearKeyCdm(const std::string& key_system) {
    if (key_system == media::kExternalClearKeyKeySystem) {
      return true;
    }
    // Treat `media::kMediaFoundationClearKeyKeySystem` as a separate key system
    // only for Windows
#if BUILDFLAG(IS_WIN)
    if (key_system == media::kMediaFoundationClearKeyKeySystem) {
      return false;
    }
#endif  // BUILDFLAG(IS_WIN)
    std::string prefix = std::string(media::kExternalClearKeyKeySystem) + '.';
    return key_system.substr(0, prefix.size()) == prefix;
  }

#if BUILDFLAG(IS_WIN)
  bool IsMediaFoundationClearKey(const std::string& key_system) {
    return (key_system == media::kMediaFoundationClearKeyKeySystem);
  }
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(ENABLE_WIDEVINE)
  bool IsWidevine(const std::string& key_system) {
    return key_system == kWidevineKeySystem;
  }
#endif  // BUILDFLAG(ENABLE_WIDEVINE)

  void RunEncryptedMediaTestPage(const std::string& html_page,
                                 const std::string& key_system,
                                 const base::StringPairs& query_params,
                                 const std::string& expected_title) {
    base::StringPairs new_query_params = query_params;
    StartLicenseServerIfNeeded(key_system, &new_query_params);
    RunMediaTestPage(html_page, new_query_params, expected_title, true);
  }

  // Tests |html_page| using |media_file| and |key_system|.
  // When |session_to_load| is not empty, the test will try to load
  // |session_to_load| with stored keys, instead of creating a new session
  // and trying to update it with licenses.
  // When |force_invalid_response| is true, the test will provide invalid
  // responses, which should trigger errors.
  // TODO(xhwang): Find an easier way to pass multiple configuration test
  // options.
  void RunEncryptedMediaTest(const std::string& html_page,
                             const std::string& media_file,
                             const std::string& key_system,
                             SrcType src_type,
                             const std::string& session_to_load,
                             bool force_invalid_response,
                             PlayCount play_count,
                             const std::string& expected_title) {
    base::StringPairs query_params;
    query_params.emplace_back("mediaFile", media_file);
    query_params.emplace_back("mediaType",
                              media::GetMimeTypeForFile(media_file));
    query_params.emplace_back("keySystem", key_system);
    if (src_type == SrcType::MSE) {
      query_params.emplace_back("useMSE", "1");
    }
    if (force_invalid_response) {
      query_params.emplace_back("forceInvalidResponse", "1");
    }
    if (!session_to_load.empty()) {
      query_params.emplace_back("sessionToLoad", session_to_load);
    }
    query_params.emplace_back(
        "playCount", base::NumberToString(static_cast<int>(play_count)));
    RunEncryptedMediaTestPage(html_page, key_system, query_params,
                              expected_title);
  }

  void RunSimpleEncryptedMediaTest(const std::string& media_file,
                                   const std::string& key_system,
                                   SrcType src_type,
                                   PlayCount play_count) {
    std::string expected_title = media::kEndedTitle;
    if (!IsPlayBackPossible(key_system)) {
      expected_title = kEmeUpdateFailed;
    }

    RunEncryptedMediaTest(kDefaultEmePlayer, media_file, key_system, src_type,
                          kNoSessionToLoad, false, play_count, expected_title);
    // Check KeyMessage received for all key systems.
    EXPECT_EQ(true, content::EvalJs(
                        browser()->tab_strip_model()->GetActiveWebContents(),
                        "document.querySelector('video').receivedKeyMessage;"));
  }

  void RunEncryptedMediaMultipleFileTest(const std::string& key_system,
                                         const std::string& video_file,
                                         const std::string& audio_file,
                                         const std::string& expected_title) {
    if (!IsPlayBackPossible(key_system)) {
      LOG(INFO) << "Skipping test - Test requires video playback.";
      return;
    }

    base::StringPairs query_params;
    query_params.emplace_back("keySystem", key_system);
    query_params.emplace_back("runEncrypted", "1");
    if (!video_file.empty()) {
      query_params.emplace_back("videoFile", video_file);
      query_params.emplace_back("videoFormat",
                                media::GetMimeTypeForFile(video_file));
    }
    if (!audio_file.empty()) {
      query_params.emplace_back("audioFile", audio_file);
      query_params.emplace_back("audioFormat",
                                media::GetMimeTypeForFile(audio_file));
    }

    RunEncryptedMediaTestPage(kDefaultMseOnlyEmePlayer, key_system,
                              query_params, expected_title);
  }

  // Starts a license server if available for the |key_system| and adds a
  // 'licenseServerURL' query parameter to |query_params|.
  void StartLicenseServerIfNeeded(const std::string& key_system,
                                  base::StringPairs* query_params) {
    std::unique_ptr<TestLicenseServerConfig> config =
        GetServerConfig(key_system);
    if (!config) {
      return;
    }
    license_server_ = std::make_unique<TestLicenseServer>(std::move(config));
    {
      base::ScopedAllowBlockingForTesting allow_blocking;
      EXPECT_TRUE(license_server_->Start());
    }
    query_params->push_back(
        std::make_pair("licenseServerURL", license_server_->GetServerURL()));
  }

  bool IsPlayBackPossible(const std::string& key_system) {
#if BUILDFLAG(ENABLE_WIDEVINE)
    if (IsWidevine(key_system) && !GetServerConfig(key_system)) {
      return false;
    }
#endif  // BUILDFLAG(ENABLE_WIDEVINE)
    return true;
  }

  std::unique_ptr<TestLicenseServerConfig> GetServerConfig(
      const std::string& key_system) {
#if BUILDFLAG(ENABLE_WIDEVINE)
    if (IsWidevine(key_system)) {
      std::unique_ptr<TestLicenseServerConfig> config(
          new WVTestLicenseServerConfig);
      if (config->IsPlatformSupported()) {
        return config;
      }
    }
#endif  // BUILDFLAG(ENABLE_WIDEVINE)
    return nullptr;
  }

 protected:
  std::unique_ptr<TestLicenseServer> license_server_;

  // We want to fail quickly when a test fails because an error is encountered.
  void AddWaitForTitles(content::TitleWatcher* title_watcher) override {
    MediaBrowserTest::AddWaitForTitles(title_watcher);
    title_watcher->AlsoWaitForTitle(kEmeUnitTestFailure16);
    title_watcher->AlsoWaitForTitle(kEmeNotSupportedError16);
    title_watcher->AlsoWaitForTitle(kEmeGenerateRequestFailed);
    title_watcher->AlsoWaitForTitle(kEmeSessionNotFound16);
    title_watcher->AlsoWaitForTitle(kEmeLoadFailed);
    title_watcher->AlsoWaitForTitle(kEmeUpdateFailed16);
    title_watcher->AlsoWaitForTitle(kEmeErrorEvent);
    title_watcher->AlsoWaitForTitle(kEmeMessageUnexpectedType);
    title_watcher->AlsoWaitForTitle(kEmeRenewalMissingHeader);
  }

#if BUILDFLAG(IS_CHROMEOS)
  void SetUpCommandLine(base::CommandLine* command_line) override {
    MediaBrowserTest::SetUpCommandLine(command_line);

    // Persistent license is supported on ChromeOS when protected media
    // identifier is allowed which involves a user action. Use this switch to
    // always allow the identifier for testing purpose. Note that the test page
    // is hosted on "127.0.0.1". See net::EmbeddedTestServer for details.
    command_line->AppendSwitchASCII(
        switches::kUnsafelyAllowProtectedMediaIdentifierForDomain, "127.0.0.1");
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
  void SetUpDefaultCommandLine(base::CommandLine* command_line) override {
    InProcessBrowserTest::SetUpDefaultCommandLine(command_line);
    command_line->RemoveSwitch(switches::kDisableComponentUpdate);
  }
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

  void SetUpCommandLineForKeySystem(
      const std::string& key_system,
      base::CommandLine* command_line,
      const std::vector<base::test::FeatureRefAndParams>&
          enable_additional_features = {}) {
    if (GetServerConfig(key_system)) {
      // Since the web and license servers listen on different ports, we need to
      // disable web-security to send license requests to the license server.
      // TODO(shadi): Add port forwarding to the test web server configuration.
      command_line->AppendSwitch(switches::kDisableWebSecurity);
    }

    // TODO(crbug.com/40787541): WhatsNewUI might be causing timeouts.
    command_line->AppendSwitch(switches::kNoFirstRun);

    std::vector<base::test::FeatureRefAndParams> enabled_features;

    for (auto feature : enable_additional_features) {
      enabled_features.emplace_back(feature);
    }

#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
    if (RequiresClearKeyCdm(key_system)) {
      RegisterClearKeyCdm(command_line);
      enabled_features.push_back({media::kExternalClearKeyForTesting, {}});
    }
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

#if BUILDFLAG(IS_WIN)
    if (IsMediaFoundationClearKey(key_system)) {
      RegisterMediaFoundationClearKeyCdm(enabled_features);
      enabled_features.push_back(
          {media::kHardwareSecureDecryptionExperiment, {}});

      base::FieldTrialParams fallback_params;
      fallback_params["per_site"] = "true";
      enabled_features.emplace_back(media::kHardwareSecureDecryptionFallback,
                                    fallback_params);

      // To enable MediaFoundation playback, tests should run on a hardware GPU
      // other than use a software OpenGL implementation. This can be configured
      // via `switches::kUseGpuInTests` or `--use-gpu-in-tests`.
      if (command_line->HasSwitch(switches::kUseGpuInTests)) {
        // TODO(crbug.com/40896253): Investigate why the video playback doesn't
        // work with `switches::kDisableGpu` and remove this line if possible.
        // For now, `switches::kDisableGpu` should not be set. Otherwise,
        // the video playback will not work with software rendering. Note that
        // this switch is appended to browser_tests.exe by force as a workaround
        // of http://crbug.com/687387.
        command_line->RemoveSwitch(switches::kDisableGpu);
      }
    }
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)
    enabled_features.push_back({media::kPlatformEncryptedDolbyVision, {}});
#endif  // BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)

    scoped_feature_list_.InitWithFeaturesAndParameters(enabled_features, {});
  }

  base::test::ScopedFeatureList scoped_feature_list_;
};

#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
// Tests encrypted media playback using ExternalClearKey key system with a
// specific library CDM interface version as test parameter:
// - int: CDM interface version to test
class ECKEncryptedMediaTest : public EncryptedMediaTestBase,
                              public WithParamInterface<int> {
 public:
  int GetCdmInterfaceVersion() { return GetParam(); }

  // We use special |key_system| names to do non-playback related tests,
  // e.g. media::kExternalClearKeyPlatformVerificationTestKeySystem is used to
  // test the platform verification.
  void TestNonPlaybackCases(const std::string& key_system,
                            const std::string& expected_title) {
    // Since we do not test playback, arbitrarily choose a test file and source
    // type.
    RunEncryptedMediaTest(kDefaultEmePlayer, "bear-a_enc-a.webm", key_system,
                          SrcType::SRC, kNoSessionToLoad, false,
                          PlayCount::ONCE, expected_title);
  }

  void TestPlaybackCase(const std::string& key_system,
                        const std::string& session_to_load,
                        const std::string& expected_title) {
    RunEncryptedMediaTest(kDefaultEmePlayer, "bear-320x240-v_enc-v.webm",
                          key_system, SrcType::MSE, session_to_load, false,
                          PlayCount::ONCE, expected_title);
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);

    SetUpCommandLineForKeySystem(media::kExternalClearKeyKeySystem,
                                 command_line);
    // Override enabled CDM interface version for testing.
    command_line->AppendSwitchASCII(
        switches::kOverrideEnabledCdmInterfaceVersion,
        base::NumberToString(GetCdmInterfaceVersion()));
  }
};

// Tests FileIO capabilities in Encrypted Media with the following test
// parameters:
// - int: CDM interface version to test
class ECKEncryptedMediaFileIOTest : public EncryptedMediaTestBase,
                                    public WithParamInterface<int> {
 public:
  int GetCdmInterfaceVersion() { return GetParam(); }

  void TestPlaybackCase(const std::string& key_system,
                        const std::string& session_to_load,
                        const std::string& expected_title) {
    RunEncryptedMediaTest(kDefaultEmePlayer, "bear-320x240-v_enc-v.webm",
                          key_system, SrcType::MSE, session_to_load, false,
                          PlayCount::ONCE, expected_title);
  }
  // We use special |key_system| names to do non-playback related tests,
  // e.g. media::kExternalClearKeyFileIOTestKeySystem is used to test file IO.
  void TestNonPlaybackCases(const std::string& key_system,
                            const std::string& expected_title) {
    // Since we do not test playback, arbitrarily choose a test file and source
    // type.
    RunEncryptedMediaTest(kDefaultEmePlayer, "bear-a_enc-a.webm", key_system,
                          SrcType::SRC, kNoSessionToLoad, false,
                          PlayCount::ONCE, expected_title);
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);

    SetUpCommandLineForKeySystem(media::kExternalClearKeyKeySystem,
                                 command_line);

    // Override enabled CDM interface version for testing.
    command_line->AppendSwitchASCII(
        switches::kOverrideEnabledCdmInterfaceVersion,
        base::NumberToString(GetCdmInterfaceVersion()));
  }
};

class ECKEncryptedMediaReportMetricsTest : public EncryptedMediaTestBase,
                                           public WithParamInterface<int> {
 public:
  int GetCdmInterfaceVersion() { return GetParam(); }

  void SetUpOnMainThread() override {
    ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
  }

  void TearDown() override {
    auto report_metric_entries = ukm_recorder_->GetEntries(
        Media_EME_CdmMetrics::kEntryName,
        {
            Media_EME_CdmMetrics::kCertificateSerialNumberName,
            Media_EME_CdmMetrics::kDecoderBypassBlockCountName,
            Media_EME_CdmMetrics::kLicenseSdkVersionName,
            Media_EME_CdmMetrics::kNumberOfOnMessageEventsName,
            Media_EME_CdmMetrics::kNumberOfUpdateCallsName,
        });

    // The ReportMetrics functionality only works in v11 and onwards, but verify
    // that in v10, RecordUkm is not called and the Ukm is not set.
    if (GetCdmInterfaceVersion() > 10) {
      EXPECT_EQ(report_metric_entries.size(), 1u);

      EXPECT_EQ(ukm::GetSourceIdType(report_metric_entries[0].source_id),
                ukm::SourceIdType::CDM_ID);

      // The ClearKey cdm does not report kCertificateSerialNumber or
      // kDecoderBypassBlockCount, so the entries should not even be set in the
      // ukm data.
      EXPECT_THAT(
          report_metric_entries[0].metrics,
          UnorderedElementsAre(
              Pair(Media_EME_CdmMetrics::kLicenseSdkVersionName, 12345),
              Pair(Media_EME_CdmMetrics::kNumberOfOnMessageEventsName, 1),
              Pair(Media_EME_CdmMetrics::kNumberOfUpdateCallsName, 1)));
    } else {
      EXPECT_EQ(report_metric_entries.size(), 0u);
    }
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);

    SetUpCommandLineForKeySystem(media::kExternalClearKeyKeySystem,
                                 command_line);
    // Override enabled CDM interface version for testing.
    command_line->AppendSwitchASCII(
        switches::kOverrideEnabledCdmInterfaceVersion,
        base::NumberToString(GetCdmInterfaceVersion()));
  }

  std::unique_ptr<ukm::TestAutoSetUkmRecorder> ukm_recorder_;
};

// Tests encrypted media playback with output protection using ExternalClearKey
// key system with a specific display surface to be captured specified as the
// test parameter.
class ECKEncryptedMediaOutputProtectionTest
    : public EncryptedMediaTestBase,
      public WithParamInterface<const char*> {
 public:
  void TestOutputProtection(bool create_recorder_before_media_keys) {
#if BUILDFLAG(IS_CHROMEOS)
    // QueryOutputProtectionStatus() is known to fail on Linux Chrome OS builds.
    std::string expected_title = kEmeUnitTestFailure;
#else
    std::string expected_title = kUnitTestSuccess;
#endif

    base::StringPairs query_params;
    if (create_recorder_before_media_keys) {
      query_params.emplace_back("createMediaRecorderBeforeMediaKeys", "1");
    }
    RunMediaTestPage("eme_and_get_display_media.html", query_params,
                     expected_title, /*http=*/true,
                     /*with_transient_activation=*/true);
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);
    SetUpCommandLineForKeySystem(media::kExternalClearKeyKeySystem,
                                 command_line);
    // The output protection tests create a MediaRecorder on a MediaStream,
    // so this allows for a fake stream to be created.
    command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
    command_line->AppendSwitchASCII(
        switches::kUseFakeDeviceForMediaStream,
        base::StringPrintf("display-media-type=%s", GetParam()));
  }
};

class ECKIncognitoEncryptedMediaTest : public EncryptedMediaTestBase {
 public:
  void TestNonPlaybackCases(const std::string& key_system,
                            const std::string& expected_title) {
    // Since we do not test playback, arbitrarily choose a test file and source
    // type.
    RunEncryptedMediaTest(kDefaultEmePlayer, "bear-a_enc-a.webm", key_system,
                          SrcType::SRC, kNoSessionToLoad, false,
                          PlayCount::ONCE, expected_title);
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);
    SetUpCommandLineForKeySystem(media::kExternalClearKeyKeySystem,
                                 command_line);
    command_line->AppendSwitch(switches::kIncognito);
  }
};

class ECKIncognitoEncryptedMediaFileIOTest : public EncryptedMediaTestBase {
 public:
  // We use special |key_system| names to do non-playback related tests,
  // e.g. media::kExternalClearKeyFileIOTestKeySystem is used to test file IO.
  void TestNonPlaybackCases(const std::string& key_system,
                            const std::string& expected_title) {
    // Since we do not test playback, arbitrarily choose a test file and source
    // type.
    RunEncryptedMediaTest(kDefaultEmePlayer, "bear-a_enc-a.webm", key_system,
                          SrcType::SRC, kNoSessionToLoad, false,
                          PlayCount::ONCE, expected_title);
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);
    SetUpCommandLineForKeySystem(media::kExternalClearKeyKeySystem,
                                 command_line);
    command_line->AppendSwitch(switches::kIncognito);
  }
};
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

#if BUILDFLAG(IS_WIN)
// Tests encrypted media playback using ClearKey key system while
// the MediaFoundationForClear feature is enabled. This ensures
// proper renderer selection occurs when Media Foundation Renderer
// is set as the default but the playback requires another (e.g.
// default) renderer.
// TODO(crbug.com/40267198): We should create a browser test suite
// intended explicitly for Media Foundation scenarios and move
// the MFClearEncryptedMediaTest tests there.
class MFClearEncryptedMediaTest : public EncryptedMediaTestBase {
 public:
  void TestSimplePlayback(const std::string& encrypted_media) {
    RunSimpleEncryptedMediaTest(encrypted_media, media::kClearKeyKeySystem,
                                SrcType::SRC, PlayCount::ONCE);
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);
    // Add MediaFoundationClearPlayback feature to enablement list.
    std::vector<base::test::FeatureRefAndParams> mf_clear;
    mf_clear.push_back({media::kMediaFoundationClearPlayback, {}});
    SetUpCommandLineForKeySystem(media::kExternalClearKeyKeySystem,
                                 command_line, mf_clear);
  }
};
#endif  // BUILDFLAG(IS_WIN)

// A base class for parameterized encrypted media tests. Subclasses must
// override `CurrentKeySystem()` and `CurrentSourceType()`.
class ParameterizedEncryptedMediaTestBase : public EncryptedMediaTestBase {
 public:
  virtual std::string CurrentKeySystem() = 0;
  virtual SrcType CurrentSourceType() = 0;

  void TestSimplePlayback(const std::string& encrypted_media) {
    RunSimpleEncryptedMediaTest(encrypted_media, CurrentKeySystem(),
                                CurrentSourceType(), PlayCount::ONCE);
  }

  void TestMultiplePlayback(const std::string& encrypted_media) {
    DCHECK(IsPlayBackPossible(CurrentKeySystem()));
    RunEncryptedMediaTest(kDefaultEmePlayer, encrypted_media,
                          CurrentKeySystem(), CurrentSourceType(),
                          kNoSessionToLoad, false, PlayCount::TWICE,
                          media::kEndedTitle);
  }

  void RunInvalidResponseTest() {
    RunEncryptedMediaTest(kDefaultEmePlayer, "bear-320x240-av_enc-av.webm",
                          CurrentKeySystem(), CurrentSourceType(),
                          kNoSessionToLoad, true, PlayCount::ONCE,
                          kEmeUpdateFailed);
  }

  void TestFrameSizeChange() {
    RunEncryptedMediaTest("encrypted_frame_size_change.html",
                          "frame_size_change-av_enc-v.webm", CurrentKeySystem(),
                          CurrentSourceType(), kNoSessionToLoad, false,
                          PlayCount::ONCE, media::kEndedTitle);
  }

  void TestConfigChange(ConfigChangeType config_change_type) {
    DCHECK_EQ(CurrentSourceType(), SrcType::MSE)
        << "Config change only happens when using MSE.";
    DCHECK(IsPlayBackPossible(CurrentKeySystem()))
        << "ConfigChange test requires video playback.";

    base::StringPairs query_params;
    query_params.emplace_back("keySystem", CurrentKeySystem());
    query_params.emplace_back(
        "configChangeType",
        base::NumberToString(static_cast<int>(config_change_type)));
    RunEncryptedMediaTestPage("mse_config_change.html", CurrentKeySystem(),
                              query_params, media::kEndedTitle);
  }

  void TestPolicyCheck() {
    base::StringPairs query_params;
    // We do not care about playback so choose an arbitrary media file.
    std::string media_file = "bear-a_enc-a.webm";
    query_params.emplace_back("mediaFile", media_file);
    query_params.emplace_back("mediaType",
                              media::GetMimeTypeForFile(media_file));
    if (CurrentSourceType() == SrcType::MSE) {
      query_params.emplace_back("useMSE", "1");
    }
    query_params.emplace_back("keySystem", CurrentKeySystem());
    query_params.emplace_back("policyCheck", "1");
    RunEncryptedMediaTestPage(kDefaultEmePlayer, CurrentKeySystem(),
                              query_params, kUnitTestSuccess);
  }

  void TestDifferentContainers(const std::string& video_media_file,
                               const std::string& audio_media_file) {
    DCHECK_EQ(CurrentSourceType(), SrcType::MSE);
    RunEncryptedMediaMultipleFileTest(CurrentKeySystem(), video_media_file,
                                      audio_media_file, media::kEndedTitle);
  }

  void DisableEncryptedMedia() {
    PrefService* pref_service = browser()->profile()->GetPrefs();
    pref_service->SetBoolean(prefs::kEnableEncryptedMedia, false);
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);
    SetUpCommandLineForKeySystem(CurrentKeySystem(), command_line);
  }
};

// Tests encrypted media playback with a combination of parameters:
// - char*: Key system name.
// - SrcType: Use MSE or SRC.
//
// Note:
// 1. Only parameterized (*_P) tests can be used. Non-parameterized (*_F)
// tests will crash at GetParam().
// 2. For key systems backed by library CDMs, the latest CDM interface version
// supported by both the CDM and Chromium will be used.
class EncryptedMediaTest
    : public ParameterizedEncryptedMediaTestBase,
      public WithParamInterface<std::tuple<const char*, SrcType>> {
 public:
  std::string CurrentKeySystem() override { return std::get<0>(GetParam()); }
  SrcType CurrentSourceType() override { return std::get<1>(GetParam()); }
};

// Similar to EncryptedMediaTest, but the source type is always MSE. This is
// needed because many tests can only work with MSE (not with SRC), e.g.
// encrypted MP4, see http://crbug.com/170793. Use this class for those tests so
// we don't have to start the test and then skip it.
class MseEncryptedMediaTest : public ParameterizedEncryptedMediaTestBase,
                              public WithParamInterface<const char*> {
 public:
  std::string CurrentKeySystem() override { return GetParam(); }
  SrcType CurrentSourceType() override { return SrcType::MSE; }
};

INSTANTIATE_TEST_SUITE_P(MSE_ClearKey,
                         EncryptedMediaTest,
                         Combine(Values(media::kClearKeyKeySystem),
                                 Values(SrcType::MSE)));

INSTANTIATE_TEST_SUITE_P(MSE_ClearKey,
                         MseEncryptedMediaTest,
                         Values(media::kClearKeyKeySystem));

// External Clear Key is currently only used on platforms that use library CDMs.
#if BUILDFLAG(ENABLE_LIBRARY_CDMS)
INSTANTIATE_TEST_SUITE_P(SRC_ExternalClearKey,
                         EncryptedMediaTest,
                         Combine(Values(media::kExternalClearKeyKeySystem),
                                 Values(SrcType::SRC)));

INSTANTIATE_TEST_SUITE_P(MSE_ExternalClearKey,
                         EncryptedMediaTest,
                         Combine(Values(media::kExternalClearKeyKeySystem),
                                 Values(SrcType::MSE)));

INSTANTIATE_TEST_SUITE_P(MSE_ExternalClearKey,
                         MseEncryptedMediaTest,
                         Values(media::kExternalClearKeyKeySystem));
#else   // BUILDFLAG(ENABLE_LIBRARY_CDMS)
// To reduce test time, only run ClearKey SRC tests when we are not running
// ExternalClearKey SRC tests.
INSTANTIATE_TEST_SUITE_P(SRC_ClearKey,
                         EncryptedMediaTest,
                         Combine(Values(media::kClearKeyKeySystem),
                                 Values(SrcType::SRC)));
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

#if BUILDFLAG(BUNDLE_WIDEVINE_CDM)
INSTANTIATE_TEST_SUITE_P(MSE_Widevine,
                         EncryptedMediaTest,
                         Combine(Values(kWidevineKeySystem),
                                 Values(SrcType::MSE)));

INSTANTIATE_TEST_SUITE_P(MSE_Widevine,
                         MseEncryptedMediaTest,
                         Values(kWidevineKeySystem));
#endif  // BUILDFLAG(BUNDLE_WIDEVINE_CDM)

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioClearVideo_WebM) {
  TestSimplePlayback("bear-320x240-av_enc-a.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoAudio_WebM) {
  TestSimplePlayback("bear-320x240-av_enc-av.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoClearAudio_WebM) {
  TestSimplePlayback("bear-320x240-av_enc-v.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VP9Video_WebM_Fullsample) {
  TestSimplePlayback("bear-320x240-v-vp9_fullsample_enc-v.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VP9Video_WebM_Subsample) {
  TestSimplePlayback("bear-320x240-v-vp9_subsample_enc-v.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoAudio_WebM_Opus) {
  TestSimplePlayback("bear-320x240-opus-av_enc-av.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoClearAudio_WebM_Opus) {
  TestSimplePlayback("bear-320x240-opus-av_enc-v.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_Multiple_VideoAudio_WebM) {
  if (!IsPlayBackPossible(CurrentKeySystem())) {
    GTEST_SKIP() << "Playback_Multiple test requires playback.";
  }

  TestMultiplePlayback("bear-320x240-av_enc-av.webm");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, Playback_AudioOnly_MP4_FLAC) {
  TestSimplePlayback("bear-flac-cenc.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, Playback_AudioOnly_MP4_OPUS) {
  TestSimplePlayback("bear-opus-cenc.mp4");
}

// TODO(crbug.com/40116010): Flaky on multiple platforms.
IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       DISABLED_Playback_VideoOnly_MP4_VP9) {
  TestSimplePlayback("bear-320x240-v_frag-vp9-cenc.mp4");
}

#if BUILDFLAG(IS_MAC)
// TODO(crbug.com/40197943): Fails on dcheck-enabled builds on 11.0.
#define MAYBE_Playback_VideoOnly_WebM_VP9Profile2 \
  DISABLED_Playback_VideoOnly_WebM_VP9Profile2
#else
#define MAYBE_Playback_VideoOnly_WebM_VP9Profile2 \
  Playback_VideoOnly_WebM_VP9Profile2
#endif
IN_PROC_BROWSER_TEST_P(EncryptedMediaTest,
                       MAYBE_Playback_VideoOnly_WebM_VP9Profile2) {
  TestSimplePlayback("bear-320x240-v-vp9_profile2_subsample_cenc-v.webm");
}

#if BUILDFLAG(IS_MAC)
// TODO(crbug.com/40197943): Fails on dcheck-enabled builds on 11.0.
#define MAYBE_Playback_VideoOnly_MP4_VP9Profile2 \
  DISABLED_Playback_VideoOnly_MP4_VP9Profile2
#else
#define MAYBE_Playback_VideoOnly_MP4_VP9Profile2 \
  Playback_VideoOnly_MP4_VP9Profile2
#endif
IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       MAYBE_Playback_VideoOnly_MP4_VP9Profile2) {
  TestSimplePlayback("bear-320x240-v-vp9_profile2_subsample_cenc-v.mp4");
}

#if BUILDFLAG(ENABLE_AV1_DECODER)

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoOnly_WebM_AV1) {
  TestSimplePlayback("bear-av1-cenc.webm");
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoOnly_WebM_AV1_10bit) {
  TestSimplePlayback("bear-av1-320x180-10bit-cenc.webm");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, Playback_VideoOnly_MP4_AV1) {
  TestSimplePlayback("bear-av1-cenc.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       Playback_VideoOnly_MP4_AV1_10bit) {
  TestSimplePlayback("bear-av1-320x180-10bit-cenc.mp4");
}

#endif  // BUILDFLAG(ENABLE_AV1_DECODER)

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, InvalidResponseKeyError) {
  RunInvalidResponseTest();
}

// This is not really an "encrypted" media test. Keep it here for completeness.
IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, ConfigChangeVideo_ClearToClear) {
  LOG(INFO) << UnitTest::GetInstance()->current_test_info()->name();
  if (!IsPlayBackPossible(CurrentKeySystem())) {
    GTEST_SKIP() << "ConfigChange test requires video playback.";
  }

  TestConfigChange(ConfigChangeType::CLEAR_TO_CLEAR);
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       ConfigChangeVideo_ClearToEncrypted) {
  LOG(INFO) << UnitTest::GetInstance()->current_test_info()->name();
  if (!IsPlayBackPossible(CurrentKeySystem())) {
    GTEST_SKIP() << "ConfigChange test requires video playback.";
  }

  TestConfigChange(ConfigChangeType::CLEAR_TO_ENCRYPTED);
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       ConfigChangeVideo_EncryptedToClear) {
  LOG(INFO) << UnitTest::GetInstance()->current_test_info()->name();
  if (!IsPlayBackPossible(CurrentKeySystem())) {
    GTEST_SKIP() << "ConfigChange test requires video playback.";
  }

  TestConfigChange(ConfigChangeType::ENCRYPTED_TO_CLEAR);
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       ConfigChangeVideo_EncryptedToEncrypted) {
  LOG(INFO) << UnitTest::GetInstance()->current_test_info()->name();
  if (!IsPlayBackPossible(CurrentKeySystem())) {
    GTEST_SKIP() << "ConfigChange test requires video playback.";
  }

  TestConfigChange(ConfigChangeType::ENCRYPTED_TO_ENCRYPTED);
}

IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, FrameSizeChangeVideo) {
  LOG(INFO) << UnitTest::GetInstance()->current_test_info()->name();
  if (!IsPlayBackPossible(CurrentKeySystem())) {
    GTEST_SKIP() << "FrameSizeChange test requires video playback.";
  }

  TestFrameSizeChange();
}

// Only use MSE since this is independent to the demuxer.
IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, PolicyCheck) {
  TestPolicyCheck();
}

// Only use MSE since this is independent to the demuxer.
IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, RemoveTemporarySession) {
  if (!IsPlayBackPossible(CurrentKeySystem())) {
    GTEST_SKIP() << "RemoveTemporarySession test requires license server.";
  }

  base::StringPairs query_params{{"keySystem", CurrentKeySystem()}};
  RunEncryptedMediaTestPage("eme_remove_session_test.html", CurrentKeySystem(),
                            query_params, media::kEndedTitle);
}

// Only use MSE since this is independent to the demuxer.
IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, EncryptedMediaDisabled) {
  DisableEncryptedMedia();

  // Clear Key key system is always supported.
  std::string expected_title = media::IsClearKey(CurrentKeySystem())
                                   ? media::kEndedTitle
                                   : kEmeNotSupportedError;

  RunEncryptedMediaTest(kDefaultEmePlayer, "bear-a_enc-a.webm",
                        CurrentKeySystem(), CurrentSourceType(),
                        kNoSessionToLoad, false, PlayCount::ONCE,
                        expected_title);
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, Playback_Check_Ukm) {
  bool is_widevine =
#if BUILDFLAG(ENABLE_WIDEVINE)
      IsWidevine(CurrentKeySystem());
#else
      false;
#endif  // BUILDFLAG(ENABLE_WIDEVINE)

  ukm::TestAutoSetUkmRecorder ukm_recorder;
  TestSimplePlayback("bear-320x240-av_enc-a.webm");

  // Check Media_EME_RequestMediaKeySystemAccess UKM. It is only recorded for
  // Widevine.
  auto request_entries = ukm_recorder.GetEntries(
      Media_EME_RequestMediaKeySystemAccess::kEntryName,
      {Media_EME_RequestMediaKeySystemAccess::kIsAdFrameName,
       Media_EME_RequestMediaKeySystemAccess::kIsCrossOriginName,
       Media_EME_RequestMediaKeySystemAccess::kIsFromMediaCapabilitiesName,
       Media_EME_RequestMediaKeySystemAccess::kIsTopFrameName,
       Media_EME_RequestMediaKeySystemAccess::kKeySystemName,
       Media_EME_RequestMediaKeySystemAccess::kVideoCapabilitiesName,
       Media_EME_RequestMediaKeySystemAccess::
           kVideoCapabilities_HasEmptyRobustnessName,
       Media_EME_RequestMediaKeySystemAccess::
           kVideoCapabilities_HasHwSecureAllRobustnessName});
  if (is_widevine) {
    EXPECT_EQ(request_entries.size(), 1u);
    EXPECT_THAT(
        request_entries[0].metrics,
        UnorderedElementsAre(
            Pair(Media_EME_RequestMediaKeySystemAccess::kIsAdFrameName,
                 /*false=*/0),
            Pair(Media_EME_RequestMediaKeySystemAccess::kIsCrossOriginName,
                 /*false=*/0),
            Pair(Media_EME_RequestMediaKeySystemAccess::
                     kIsFromMediaCapabilitiesName,
                 /*false=*/0),
            Pair(Media_EME_RequestMediaKeySystemAccess::kIsTopFrameName,
                 /*true=*/1),
            Pair(Media_EME_RequestMediaKeySystemAccess::kKeySystemName,
                 /*blink::KeySystemForUkmLegacy::kWidevine=*/1),
            Pair(Media_EME_RequestMediaKeySystemAccess::kVideoCapabilitiesName,
                 /*true=*/1),
            Pair(Media_EME_RequestMediaKeySystemAccess::
                     kVideoCapabilities_HasEmptyRobustnessName,
                 /*true=*/1),
            Pair(Media_EME_RequestMediaKeySystemAccess::
                     kVideoCapabilities_HasHwSecureAllRobustnessName,
                 /*false=*/0)));
  } else {
    // Not Widevine, so nothing should be recorded.
    EXPECT_EQ(request_entries.size(), 0u);
  }

  // Check Media_EME_CreateMediaKeys UKM. It is also only recorded for Widevine.
  auto create_entries =
      ukm_recorder.GetEntries(Media_EME_CreateMediaKeys::kEntryName,
                              {Media_EME_CreateMediaKeys::kIsAdFrameName,
                               Media_EME_CreateMediaKeys::kIsCrossOriginName,
                               Media_EME_CreateMediaKeys::kIsTopFrameName,
                               Media_EME_CreateMediaKeys::kKeySystemName});
  if (is_widevine) {
    EXPECT_EQ(create_entries.size(), 1u);
    EXPECT_THAT(create_entries[0].metrics,
                UnorderedElementsAre(
                    Pair(Media_EME_CreateMediaKeys::kIsAdFrameName,
                         /*false=*/0),
                    Pair(Media_EME_CreateMediaKeys::kIsCrossOriginName,
                         /*false=*/0),
                    Pair(Media_EME_CreateMediaKeys::kIsTopFrameName,
                         /*true=*/1),
                    Pair(Media_EME_CreateMediaKeys::kKeySystemName,
                         /*blink::KeySystemForUkmLegacy::kWidevine=*/1)));
  } else {
    // Not Widevine, so nothing should be recorded.
    EXPECT_EQ(create_entries.size(), 0u);
  }

  // Check Media_EME_Usage UKM. It is recorded for all key systems. Currently
  // only update() will be called during playback.
  auto usage_entries = ukm_recorder.GetEntries(
      Media_EME_Usage::kEntryName,
      {Media_EME_Usage::kApiName, Media_EME_Usage::kIsPersistentSessionName,
       Media_EME_Usage::kKeySystemName,
       Media_EME_Usage::kUseHardwareSecureCodecsName});
  EXPECT_EQ(usage_entries.size(), 1u);
  EXPECT_THAT(
      usage_entries[0].metrics,
      UnorderedElementsAre(
          Pair(Media_EME_Usage::kApiName, /*blink::EmeApiType::kUpdate=*/6),
          Pair(Media_EME_Usage::kIsPersistentSessionName, /*false=*/0),
          Pair(Media_EME_Usage::kKeySystemName,
               media::GetKeySystemIntForUKM(CurrentKeySystem())),
          Pair(Media_EME_Usage::kUseHardwareSecureCodecsName, /*false=*/0)));

  // Check Media_EME_ApiPromiseRejection. With Widevine if there is no license
  // server Update() will fail. With ClearKey a valid response is provided, so
  // there is no failure.
  auto promise_entries = ukm_recorder.GetEntries(
      Media_EME_ApiPromiseRejection::kEntryName,
      {Media_EME_ApiPromiseRejection::kApiName,
       Media_EME_ApiPromiseRejection::kKeySystemName,
       Media_EME_ApiPromiseRejection::kSystemCodeName,
       Media_EME_ApiPromiseRejection::kUseHardwareSecureCodecsName});
  if (is_widevine && !license_server_) {
    // Update() should have failed due to invalid license response.
    EXPECT_EQ(promise_entries.size(), 1u);
    // Media_EME_ApiPromiseRejection::kSystemCodeName is key system
    // implementation specific, so exact value may change.
    EXPECT_THAT(
        promise_entries[0].metrics,
        UnorderedElementsAre(
            Pair(Media_EME_ApiPromiseRejection::kApiName,
                 /*blink::EmeApiType::kUpdate=*/6),
            Pair(Media_EME_ApiPromiseRejection::kKeySystemName,
                 media::GetKeySystemIntForUKM(CurrentKeySystem())),
            Pair(Media_EME_ApiPromiseRejection::kSystemCodeName, Not(Eq(0))),
            Pair(Media_EME_ApiPromiseRejection::kUseHardwareSecureCodecsName,
                 /*false=*/0)));
  } else {
    // No error with ClearKey or with Widevine if a license server is available.
    EXPECT_EQ(promise_entries.size(), 0u);
  }
}

#if BUILDFLAG(USE_PROPRIETARY_CODECS)

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, Playback_VideoOnly_MP4) {
  TestSimplePlayback("bear-640x360-v_frag-cenc.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, Playback_VideoOnly_MP4_MDAT) {
  TestSimplePlayback("bear-640x360-v_frag-cenc-mdat.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest, Playback_Encryption_CBCS) {
  TestSimplePlayback("bear-640x360-v_frag-cbcs.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       Playback_EncryptedVideo_MP4_ClearAudio_WEBM) {
  TestDifferentContainers("bear-640x360-v_frag-cenc.mp4",
                          "bear-320x240-audio-only.webm");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       Playback_ClearVideo_WEBM_EncryptedAudio_MP4) {
  TestDifferentContainers("bear-320x240-video-only.webm",
                          "bear-640x360-a_frag-cenc.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       Playback_EncryptedVideo_WEBM_EncryptedAudio_MP4) {
  TestDifferentContainers("bear-320x240-v_enc-v.webm",
                          "bear-640x360-a_frag-cenc.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       Playback_EncryptedVideo_CBCS_EncryptedAudio_CENC) {
  TestDifferentContainers("bear-640x360-v_frag-cbcs.mp4",
                          "bear-640x360-a_frag-cenc.mp4");
}

IN_PROC_BROWSER_TEST_P(MseEncryptedMediaTest,
                       Playback_EncryptedVideo_CENC_EncryptedAudio_CBCS) {
  TestDifferentContainers("bear-640x360-v_frag-cenc.mp4",
                          "bear-640x360-a_frag-cbcs.mp4");
}

#endif  // BUILDFLAG(USE_PROPRIETARY_CODECS)

#if BUILDFLAG(ENABLE_LIBRARY_CDMS)

// Test CDM_10 through CDM_12.
static_assert(media::CheckSupportedCdmInterfaceVersions(10, 12),
              "Mismatch between implementation and test coverage");
INSTANTIATE_TEST_SUITE_P(CDM_10, ECKEncryptedMediaTest, Values(10));
INSTANTIATE_TEST_SUITE_P(CDM_11, ECKEncryptedMediaTest, Values(11));
INSTANTIATE_TEST_SUITE_P(CDM_12, ECKEncryptedMediaTest, Values(12));
INSTANTIATE_TEST_SUITE_P(CDM_10, ECKEncryptedMediaFileIOTest, Values(10));
INSTANTIATE_TEST_SUITE_P(CDM_11, ECKEncryptedMediaFileIOTest, Values(11));
INSTANTIATE_TEST_SUITE_P(CDM_12, ECKEncryptedMediaFileIOTest, Values(12));

INSTANTIATE_TEST_SUITE_P(CDM_10,
                         ECKEncryptedMediaReportMetricsTest,
                         Values(10));
INSTANTIATE_TEST_SUITE_P(CDM_11,
                         ECKEncryptedMediaReportMetricsTest,
                         Values(11));
INSTANTIATE_TEST_SUITE_P(CDM_12,
                         ECKEncryptedMediaReportMetricsTest,
                         Values(12));

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaReportMetricsTest, RecordUkmTest) {
  RunSimpleEncryptedMediaTest("bear-320x240-av_enc-a.webm",
                              media::kExternalClearKeyKeySystem, SrcType::SRC,
                              PlayCount::ONCE);
  base::RunLoop().RunUntilIdle();
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, InitializeCDMFail) {
  TestNonPlaybackCases(kExternalClearKeyInitializeFailKeySystem,
                       kEmeNotSupportedError);
}

// TODO(crbug.com/40105240): Failing on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_CDMCrashDuringDecode DISABLED_CDMCrashDuringDecode
#else
#define MAYBE_CDMCrashDuringDecode CDMCrashDuringDecode
#endif
// When CDM crashes, we should still get a decode error and all sessions should
// be closed.
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, MAYBE_CDMCrashDuringDecode) {
  TestNonPlaybackCases(media::kExternalClearKeyCrashKeySystem,
                       kEmeSessionClosedAndError);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, PlatformVerificationTest) {
  TestNonPlaybackCases(
      media::kExternalClearKeyPlatformVerificationTestKeySystem,
      kUnitTestSuccess);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaFileIOTest, FileIOTest) {
  TestNonPlaybackCases(media::kExternalClearKeyFileIOTestKeySystem,
                       kUnitTestSuccess);
}

// Intermittent leaks on ASan/LSan runs: crbug.com/889923
#if defined(LEAK_SANITIZER) || defined(ADDRESS_SANITIZER)
#define MAYBE_MessageTypeTest DISABLED_MessageTypeTest
#else
#define MAYBE_MessageTypeTest MessageTypeTest
#endif
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, MAYBE_MessageTypeTest) {
  TestPlaybackCase(media::kExternalClearKeyMessageTypeTestKeySystem,
                   kNoSessionToLoad, media::kEndedTitle);
  // Expects 3 message types: 'license-request', 'license-renewal' and
  // 'individualization-request'.
  EXPECT_EQ(3,
            content::EvalJs(
                browser()->tab_strip_model()->GetActiveWebContents(),
                "document.querySelector('video').receivedMessageTypes.size;"));
}

// Exercises Storage Path, so use ECKEncryptedMediaFileIOTest
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaFileIOTest, LoadPersistentLicense) {
  TestPlaybackCase(media::kExternalClearKeyKeySystem, kPersistentLicense,
                   media::kEndedTitle);
}

// Exercises Storage Path, so use ECKEncryptedMediaFileIOTest
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaFileIOTest, LoadUnknownSession) {
  TestPlaybackCase(media::kExternalClearKeyKeySystem, kUnknownSession,
                   kEmeSessionNotFound);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, LoadSessionAfterClose) {
  base::StringPairs query_params{
      {"keySystem", media::kExternalClearKeyKeySystem}};
  RunEncryptedMediaTestPage("eme_load_session_after_close_test.html",
                            media::kExternalClearKeyKeySystem, query_params,
                            media::kEndedTitle);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, DecryptOnly_VideoAudio_WebM) {
  RunSimpleEncryptedMediaTest("bear-320x240-av_enc-av.webm",
                              media::kExternalClearKeyDecryptOnlyKeySystem,
                              SrcType::MSE, PlayCount::ONCE);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, DecryptOnly_VideoOnly_MP4_VP9) {
  RunSimpleEncryptedMediaTest("bear-320x240-v_frag-vp9-cenc.mp4",
                              media::kExternalClearKeyDecryptOnlyKeySystem,
                              SrcType::MSE, PlayCount::ONCE);
}

#if BUILDFLAG(USE_PROPRIETARY_CODECS)

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, DecryptOnly_VideoOnly_MP4_CBCS) {
  // 'cbcs' decryption is only supported on CDM 10 or later as long as
  // the appropriate buildflag is enabled.
  std::string expected_result =
      GetCdmInterfaceVersion() >= 10 ? media::kEndedTitle : media::kErrorTitle;
  RunEncryptedMediaTest(kDefaultEmePlayer, "bear-640x360-v_frag-cbcs.mp4",
                        media::kExternalClearKeyDecryptOnlyKeySystem,
                        SrcType::MSE, kNoSessionToLoad, false, PlayCount::ONCE,
                        expected_result);
}

// Encryption Scheme tests. ClearKey key system is covered in
// content/browser/media/encrypted_media_browsertest.cc.
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, Playback_Encryption_CENC) {
  RunEncryptedMediaMultipleFileTest(
      media::kExternalClearKeyKeySystem, "bear-640x360-v_frag-cenc.mp4",
      "bear-640x360-a_frag-cenc.mp4", media::kEndedTitle);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, Playback_Encryption_CBC1) {
  RunEncryptedMediaMultipleFileTest(media::kExternalClearKeyKeySystem,
                                    "bear-640x360-v_frag-cbc1.mp4",
                                    std::string(), media::kErrorTitle);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, Playback_Encryption_CENS) {
  RunEncryptedMediaMultipleFileTest(media::kExternalClearKeyKeySystem,
                                    "bear-640x360-v_frag-cens.mp4",
                                    std::string(), media::kErrorTitle);
}

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, Playback_Encryption_CBCS) {
  // 'cbcs' decryption is only supported on CDM 10 or later as long as
  // the appropriate buildflag is enabled.
  std::string expected_result =
      GetCdmInterfaceVersion() >= 10 ? media::kEndedTitle : media::kErrorTitle;
  RunEncryptedMediaMultipleFileTest(
      media::kExternalClearKeyKeySystem, "bear-640x360-v_frag-cbcs.mp4",
      "bear-640x360-a_frag-cbcs.mp4", expected_result);
}

#endif  // BUILDFLAG(USE_PROPRIETARY_CODECS)

#if BUILDFLAG(ENABLE_CDM_HOST_VERIFICATION)
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, VerifyCdmHostTest) {
  TestNonPlaybackCases(media::kExternalClearKeyVerifyCdmHostTestKeySystem,
                       kUnitTestSuccess);
}
#endif  // BUILDFLAG(ENABLE_CDM_HOST_VERIFICATION)

IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, StorageIdTest) {
  TestNonPlaybackCases(media::kExternalClearKeyStorageIdTestKeySystem,
                       kUnitTestSuccess);
}

// TODO(crbug.com/40601162): Times out in debug builds.
// TODO(crbug.com/40916095): Test flakiness.
#if !defined(NDEBUG) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_MultipleCdmTypes DISABLED_MultipeCdmTypes
#else
#define MAYBE_MultipleCdmTypes MultipeCdmTypes
#endif
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaTest, MAYBE_MultipleCdmTypes) {
  base::StringPairs empty_query_params;
  RunMediaTestPage("multiple_cdm_types.html", empty_query_params,
                   media::kEndedTitle, true);
}

// Output Protection Tests. Run with different capture inputs. "monitor"
// simulates the whole screen being captured. "window" simulates the Chrome
// window being captured. "browser" simulates the current Chrome tab being
// captured.

INSTANTIATE_TEST_SUITE_P(Capture_Monitor,
                         ECKEncryptedMediaOutputProtectionTest,
                         Values("monitor"));
INSTANTIATE_TEST_SUITE_P(Capture_Window,
                         ECKEncryptedMediaOutputProtectionTest,
                         Values("window"));
INSTANTIATE_TEST_SUITE_P(Capture_Browser,
                         ECKEncryptedMediaOutputProtectionTest,
                         Values("browser"));

// TODO(crbug.com/40671674): Failing on Win.
#if BUILDFLAG(IS_WIN)
#define MAYBE_BeforeMediaKeys DISABLED_BeforeMediaKeys
#else
#define MAYBE_BeforeMediaKeys BeforeMediaKeys
#endif
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaOutputProtectionTest,
                       MAYBE_BeforeMediaKeys) {
  TestOutputProtection(/*create_recorder_before_media_keys=*/true);
}

// TODO(crbug.com/40671674): Failing on Win.
#if BUILDFLAG(IS_WIN)
#define MAYBE_AfterMediaKeys DISABLED_AfterMediaKeys
#else
#define MAYBE_AfterMediaKeys AfterMediaKeys
#endif
IN_PROC_BROWSER_TEST_P(ECKEncryptedMediaOutputProtectionTest,
                       MAYBE_AfterMediaKeys) {
  TestOutputProtection(/*create_recorder_before_media_keys=*/false);
}

// Incognito tests. Ideally we would run all above tests in incognito mode to
// ensure that everything works. However, that would add a lot of extra tests
// that aren't really testing anything different, as normal playback does not
// save anything to disk. Instead we are only running the tests that actually
// have the CDM do file access.

IN_PROC_BROWSER_TEST_F(ECKIncognitoEncryptedMediaFileIOTest, FileIO) {
  // Try the FileIO test using the default CDM API while running in incognito.
  TestNonPlaybackCases(media::kExternalClearKeyFileIOTestKeySystem,
                       kUnitTestSuccess);
}

IN_PROC_BROWSER_TEST_F(ECKIncognitoEncryptedMediaTest, LoadSessionAfterClose) {
  // Loading a session should work in incognito mode.
  base::StringPairs query_params{
      {"keySystem", media::kExternalClearKeyKeySystem}};
  RunEncryptedMediaTestPage("eme_load_session_after_close_test.html",
                            media::kExternalClearKeyKeySystem, query_params,
                            media::kEndedTitle);
}
#endif  // BUILDFLAG(ENABLE_LIBRARY_CDMS)

#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(MFClearEncryptedMediaTest, Playback_AudioClearVideo) {
  TestSimplePlayback("bear-320x240-av_enc-a.webm");
}

IN_PROC_BROWSER_TEST_F(MFClearEncryptedMediaTest, Playback_VideoAudio) {
  TestSimplePlayback("bear-320x240-av_enc-av.webm");
}

IN_PROC_BROWSER_TEST_F(MFClearEncryptedMediaTest, Playback_VideoClearAudio) {
  TestSimplePlayback("bear-320x240-av_enc-v.webm");
}
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_PROPRIETARY_CODECS)
// MediaFoundation Clear Key Key System uses Windows Media Foundation's decoders
// and H264 is always supported.
class MediaFoundationEncryptedMediaTest : public EncryptedMediaTestBase {
 public:
  void TestMediaFoundationPlayback(const std::string& encrypted_media) {
    RunSimpleEncryptedMediaTest(encrypted_media,
                                media::kMediaFoundationClearKeyKeySystem,
                                SrcType::MSE, PlayCount::ONCE);
  }

  void TestMediaFoundationMultipleFilePlayback(const std::string& video_file,
                                               const std::string& audio_file) {
    std::string expected_title = media::kEndedTitle;
    if (!IsPlayBackPossible(media::kMediaFoundationClearKeyKeySystem)) {
      expected_title = kEmeUpdateFailed;
    }

    base::StringPairs query_params;
    const auto video_format = media::GetMimeTypeForFile(video_file);
    const auto audio_format = media::GetMimeTypeForFile(audio_file);
    const auto media_type =
        media::GetMimeTypeForFile(audio_file + ";" + video_file);
    query_params.emplace_back("keySystem",
                              media::kMediaFoundationClearKeyKeySystem);
    query_params.emplace_back("runEncrypted", "1");
    query_params.emplace_back("useMSE", "1");
    query_params.emplace_back("playCount", "1");
    query_params.emplace_back("videoFile", video_file);
    query_params.emplace_back("videoFormat", video_format);
    query_params.emplace_back("audioFile", audio_file);
    query_params.emplace_back("audioFormat", audio_format);
    query_params.emplace_back("mediaType", media_type);
    RunEncryptedMediaTestPage(kDefaultEmePlayer,
                              media::kMediaFoundationClearKeyKeySystem,
                              query_params, media::kEndedTitle);

    // Check KeyMessage received for all key systems.
    EXPECT_EQ(true, content::EvalJs(
                        browser()->tab_strip_model()->GetActiveWebContents(),
                        "document.querySelector('video').receivedKeyMessage;"));
  }

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    EncryptedMediaTestBase::SetUpCommandLine(command_line);
    SetUpCommandLineForKeySystem(media::kMediaFoundationClearKeyKeySystem,
                                 command_line);
  }

  bool IsMediaFoundationEncryptedPlaybackSupported() {
    bool is_mediafoundation_encrypted_playback_supported =
        media::SupportMediaFoundationEncryptedPlayback();
    bool use_gpu_in_tests = base::CommandLine::ForCurrentProcess()->HasSwitch(
        switches::kUseGpuInTests);
    bool disable_gpu = base::CommandLine::ForCurrentProcess()->HasSwitch(
        switches::kDisableGpu);

    const auto& gpu_info = content::GpuDataManager::GetInstance()->GetGPUInfo();
    const auto& active_gpu = gpu_info.active_gpu();
    LOG(INFO) << "active_gpu.vendor_id=" << active_gpu.vendor_id;
    LOG(INFO) << "active_gpu.device_id=" << active_gpu.device_id;
    LOG(INFO) << "active_gpu.driver_version=" << active_gpu.driver_version;
    LOG(INFO) << "gpu_info.gl_vendor=" << gpu_info.gl_vendor;
    LOG(INFO) << "gpu_info.gl_renderer=" << gpu_info.gl_renderer;
    LOG(INFO) << "switches::kDisableGpuDriverBugWorkarounds="
              << base::CommandLine::ForCurrentProcess()->HasSwitch(
                     switches::kDisableGpuDriverBugWorkarounds);

    bool is_playback_supported =
        is_mediafoundation_encrypted_playback_supported && use_gpu_in_tests &&
        !disable_gpu;
    LOG(INFO) << "is_mediafoundation_encrypted_playback_supported="
              << is_mediafoundation_encrypted_playback_supported
              << ", use_gpu_in_tests=" << use_gpu_in_tests
              << ", disable_gpu=" << disable_gpu;

    // Run test only if the test machine supports MediaFoundation playback.
    // Otherwise, NotSupportedError or the failure to create D3D11 device is
    // expected.
    if (!is_playback_supported) {
      LOG(INFO) << "Test method "
                << UnitTest::GetInstance()->current_test_info()->name()
                << " is inconclusive since MediaFoundation playback is not "
                   "supported.";

      if (!is_mediafoundation_encrypted_playback_supported) {
        auto os_version = static_cast<int>(base::win::GetVersion());
        LOG(INFO) << "os_version=" << os_version;
      }

      if (!use_gpu_in_tests) {
        LOG(INFO) << "MediaFoundation playback will not work without a "
                     "hardware GPU. Use `--use-gpu-in-tests` flag.";
      }
    }

    return is_playback_supported;
  }

  bool IsDefaultAudioOutputDeviceAvailable() {
    auto default_audio_output_device_id =
        media::CoreAudioUtil::GetDefaultOutputDeviceID();
    LOG(INFO) << "default_audio_output_device_id="
              << default_audio_output_device_id;

    if (default_audio_output_device_id.empty()) {
      LOG(INFO) << "No default audio output device available!";
      return false;
    }

    return true;
  }

#if BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)
  bool IsVideoDecoderSupported(const GUID& video_decoder_guid) {
    MFT_REGISTER_TYPE_INFO inputInfo{MFMediaType_Video, video_decoder_guid};
    IMFActivate** activates;
    unsigned int numActivates = 0;
    auto result = MFTEnumEx(MFT_CATEGORY_VIDEO_DECODER, MFT_ENUM_FLAG_SYNCMFT,
                            &inputInfo, nullptr, &activates, &numActivates);
    if (result != S_OK || numActivates == 0) {
      LOG(INFO) << "No decoders found!";
      return false;
    }

    return true;
  }

  bool IsVideoRendererEffectSupported(const wchar_t* profile) {
    bool supported = false;
    IMFActivate** activates = nullptr;
    unsigned int numActivates = 0;
    auto result = MFTEnumEx(MFT_CATEGORY_VIDEO_RENDERER_EFFECT,
                            MFT_ENUM_FLAG_SORTANDFILTER, nullptr, nullptr,
                            &activates, &numActivates);
    if (result != S_OK || numActivates == 0) {
      LOG(INFO) << "No video renderer effect found!";
      return false;
    }

    for (unsigned int i = 0; i < numActivates && !supported; i++) {
      PROPVARIANT var;
      PropVariantInit(&var);
      auto hr = activates[i]->GetItem(MFT_ENUM_VIDEO_RENDERER_EXTENSION_PROFILE,
                                      &var);
      if (hr == S_OK && var.vt == VARTYPE(VT_VECTOR | VT_LPWSTR)) {
        for (unsigned long j = 0; j < var.calpwstr.cElems; j++) {
          auto elem = *(var.calpwstr.pElems + j);
          if (_wcsicmp(elem, profile) == 0) {
            supported = true;
            break;
          }
        }
      }

      PropVariantClear(&var);
    }

    if (*activates) {
      (*activates)->Release();
    }

    return supported;
  }

  // |profile| is a Dolby Vision renderer profile string which has always 7
  // characters in MediaFoundation. For HEVC based profiles, it's either
  // "dvhe.xx" or "dvh1.xx" where "xx" is a two-digit profile number. Note the
  // DolbyVision level is ignored. See "Table 1: Dolby Vision bitstream
  // profiles" at
  // `https://professionalsupport.dolby.com/s/article/What-is-Dolby-Vision-Profile`.
  bool IsDolbyVisionEncryptedPlaybackSupported(const wchar_t* profile) {
    // Dolby Vision video playback requires both HEVC and Dolby Vision extension
    // codecs.
    bool is_hevc_decoder_supported =
        IsVideoDecoderSupported(MFVideoFormat_HEVC);
    bool is_dolbyvision_supported = IsVideoRendererEffectSupported(profile);
    LOG(INFO) << "is_hevc_decoder_supported=" << is_hevc_decoder_supported;
    LOG(INFO) << "is_dolbyvision_supported=" << is_dolbyvision_supported;

    if (!is_hevc_decoder_supported || !is_dolbyvision_supported) {
      LOG(INFO)
          << "Test method "
          << UnitTest::GetInstance()->current_test_info()->name()
          << " is inconclusive since DolbyVisionEncrypted playback is not "
             "supported.";
      return false;
    }

    return true;
  }
#endif  // BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)
};

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_ClearLeadEncryptedCencVideo_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  TestMediaFoundationPlayback("bear-640x360-v_frag-cenc.mp4");  // H.264
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_ClearLeadEncryptedCbcsVideo_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  TestMediaFoundationPlayback("bear-640x360-v_frag-cbcs.mp4");  // H.264
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_EncryptedCencVideoAudio_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  TestMediaFoundationMultipleFilePlayback(
      "bear-640x360-v_frag-cenc.mp4",   // H.264
      "bear-640x360-a_frag-cenc.mp4");  // MP4 AAC
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_EncryptedCencAudio_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  std::string expected_title = media::kEndedTitle;

  // TODO(crbug.com/40270855): "Activate failed to create mediasink
  // (0xC00D36FA)" kPlaybackError is expected when playing encrypted audio only
  // content if no audio device. Remove this temporary fix for test machines
  // once the permenent solution is implemented (i.e., a null sink for no audio
  // device).
  if (!IsDefaultAudioOutputDeviceAvailable()) {
    LOG(INFO)
        << "Test method "
        << UnitTest::GetInstance()->current_test_info()->name()
        << " is expected to receive an error since there is no default audio "
           "output device.";
    expected_title = media::kErrorTitle;
  }

  RunEncryptedMediaTest(
      kDefaultEmePlayer, "bear-640x360-a_frag-cenc.mp4",  // MP4 AAC audio only
      media::kMediaFoundationClearKeyKeySystem, SrcType::MSE, kNoSessionToLoad,
      false, PlayCount::ONCE, expected_title);
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_EncryptedAv1CencAudio_MediaTypeUnsupported) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  // MediaFoundation Clear Key Key System doesn't support AV1 videos
  // (codecs-"av01.0.04M.08"). See AddMediaFoundationClearKey() in
  // components/cdm/renderer/key_system_support_update.cc
  RunEncryptedMediaTest(
      kDefaultEmePlayer, "bear-av1-cenc.mp4", /*codecs="av01.0.04M.08"*/
      media::kMediaFoundationClearKeyKeySystem, SrcType::MSE, kNoSessionToLoad,
      false, PlayCount::ONCE, kEmeNotSupportedError);
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       FallbackTest_KeySystemNotSupported) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  // MediaFoundationServiceMonitor gets lazily initialized in
  // media_foundation_widevine_cdm_component_installer which is not call by the
  // browser tests. Lazily initialize it here.
  MediaFoundationServiceMonitor::GetInstance();

  const char* fallback_expected_title = media::kEndedTitle;

  RunMediaTestPage("media_foundation_fallback.html",
                   {{"keySystem", media::kMediaFoundationClearKeyKeySystem}},
                   fallback_expected_title, /*http=*/true);
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       ProtectedContentIdSettingAllowed) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  PrefService* prefs = browser()->profile()->GetPrefs();
  ASSERT_TRUE(prefs);

  prefs->SetInteger(kProtectedContentIdPrefPath, kAllowProtectedContentId);

  RunEncryptedMediaTest(kDefaultEmePlayer, "bear-640x360-v_frag-cbcs.mp4",
                        media::kMediaFoundationClearKeyKeySystem, SrcType::MSE,
                        kNoSessionToLoad, false, PlayCount::ONCE,
                        media::kEndedTitle);
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       ProtectedContentIdSettingDisallowed) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  PrefService* prefs = browser()->profile()->GetPrefs();
  ASSERT_TRUE(prefs);


  prefs->SetInteger(kProtectedContentIdPrefPath, kDisallowProtectedContentId);

  RunEncryptedMediaTest(kDefaultEmePlayer, "bear-640x360-v_frag-cbcs.mp4",
                        media::kMediaFoundationClearKeyKeySystem, SrcType::MSE,
                        kNoSessionToLoad, false, PlayCount::ONCE,
                        kEmeNotSupportedError);
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       ProtectedContentIdCustomSettingAllowed) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  PrefService* prefs = browser()->profile()->GetPrefs();
  ASSERT_TRUE(prefs);

  // Disable protected media identifier by default.
  prefs->SetInteger(kProtectedContentIdPrefPath, kDisallowProtectedContentId);

  // Enable 127.0.0.1 as an exception.
  prefs->SetDict(
      kProtectedContentIdExceptionPrefPath,
      base::Value::Dict().Set(
          "http://127.0.0.1,*",
          base::Value::Dict().Set("setting", kAllowProtectedContentId)));

  RunEncryptedMediaTest(kDefaultEmePlayer, "bear-640x360-v_frag-cbcs.mp4",
                        media::kMediaFoundationClearKeyKeySystem, SrcType::MSE,
                        kNoSessionToLoad, false, PlayCount::ONCE,
                        media::kEndedTitle);
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       ProtectedContentIdCustomSettingDisallowed) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  PrefService* prefs = browser()->profile()->GetPrefs();
  ASSERT_TRUE(prefs);

  // Enable protected media identifier by default.
  prefs->SetInteger(kProtectedContentIdPrefPath, kAllowProtectedContentId);

  // Disable 127.0.0.1 as an exception.
  prefs->SetDict(
      kProtectedContentIdExceptionPrefPath,
      base::Value::Dict().Set(
          "http://127.0.0.1,*",
          base::Value::Dict().Set("setting", kDisallowProtectedContentId)));

  RunEncryptedMediaTest(kDefaultEmePlayer, "bear-640x360-v_frag-cbcs.mp4",
                        media::kMediaFoundationClearKeyKeySystem, SrcType::MSE,
                        kNoSessionToLoad, false, PlayCount::ONCE,
                        kEmeNotSupportedError);
}

#if BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)
IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_DolbyVisionProfile5CencVideo_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  if (!IsDolbyVisionEncryptedPlaybackSupported(kDolbyVisionProfile5)) {
    GTEST_SKIP() << "DolbyVisionEncryptedPlayback not supported on device.";
  }

  // DolbyVision Profile 5
  TestMediaFoundationPlayback(
      "color_pattern_24_dvhe_05_1920x1080-3sec-frag-cenc.mp4");
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_DolbyVisionProfile81CencVideo_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  if (!IsDolbyVisionEncryptedPlaybackSupported(kDolbyVisionProfile8)) {
    GTEST_SKIP() << "DolbyVisionEncryptedPlayback not supported on device.";
  }

  // DolbyVision Profile 8.1
  TestMediaFoundationPlayback(
      "color_pattern_24_dvhe_081_1920x1080-3sec-frag-cenc.mp4");
}

IN_PROC_BROWSER_TEST_F(MediaFoundationEncryptedMediaTest,
                       Playback_DolbyVisionProfile5CencClearLeadVideo_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  if (!IsDolbyVisionEncryptedPlaybackSupported(kDolbyVisionProfile5)) {
    GTEST_SKIP() << "DolbyVisionEncryptedPlayback not supported on device.";
  }

  // DolbyVision Profile 5
  TestMediaFoundationPlayback(
      "color_pattern_24_dvhe_05_1920x1080-3sec-frag-cenc-clearlead-2sec.mp4");
}

IN_PROC_BROWSER_TEST_F(
    MediaFoundationEncryptedMediaTest,
    Playback_DolbyVisionProfile81CencClearLeadVideo_Success) {
  if (!IsMediaFoundationEncryptedPlaybackSupported()) {
    GTEST_SKIP() << "MediaFoundationEncryptedPlayback not supported on device.";
  }

  if (!IsDolbyVisionEncryptedPlaybackSupported(kDolbyVisionProfile8)) {
    GTEST_SKIP() << "DolbyVisionEncryptedPlayback not supported on device.";
  }

  // DolbyVision Profile 8.1
  TestMediaFoundationPlayback(
      "color_pattern_24_dvhe_081_1920x1080-3sec-frag-cenc-clearlead-2sec.mp4");
}

#endif  // BUILDFLAG(ENABLE_PLATFORM_ENCRYPTED_DOLBY_VISION)

#endif  // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_PROPRIETARY_CODECS)