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

#include "third_party/blink/renderer/modules/media_capabilities/media_capabilities.h"

#include <math.h>
#include <stdint.h>

#include <algorithm>

#include "base/memory/raw_ptr.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "media/base/media_switches.h"
#include "media/base/video_codecs.h"
#include "media/learning/common/media_learning_tasks.h"
#include "media/learning/common/target_histogram.h"
#include "media/learning/mojo/public/mojom/learning_task_controller.mojom-blink.h"
#include "media/mojo/clients/mojo_video_encoder_metrics_provider.h"
#include "media/mojo/mojom/media_metrics_provider.mojom-blink.h"
#include "media/mojo/mojom/media_types.mojom-blink.h"
#include "media/mojo/mojom/video_decode_perf_history.mojom-blink.h"
#include "media/mojo/mojom/watch_time_recorder.mojom-blink.h"
#include "media/mojo/mojom/webrtc_video_perf.mojom-blink.h"
#include "media/video/mock_gpu_video_accelerator_factories.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_tester.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_audio_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_capabilities_decoding_info.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_capabilities_info.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_decoding_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_encoding_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_configuration.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_video_encoder_factory.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/wtf_size_t.h"
#include "third_party/googletest/src/googlemock/include/gmock/gmock-actions.h"
#include "ui/gfx/geometry/size.h"

using ::media::learning::FeatureValue;
using ::media::learning::ObservationCompletion;
using ::media::learning::TargetValue;
using ::testing::_;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::Unused;

namespace blink {

namespace {

// Simulating the browser-side service.
class MockPerfHistoryService
    : public media::mojom::blink::VideoDecodePerfHistory {
 public:
  void BindRequest(mojo::ScopedMessagePipeHandle handle) {
    receiver_.Bind(
        mojo::PendingReceiver<media::mojom::blink::VideoDecodePerfHistory>(
            std::move(handle)));
    receiver_.set_disconnect_handler(base::BindOnce(
        &MockPerfHistoryService::OnConnectionError, base::Unretained(this)));
  }

  void OnConnectionError() { receiver_.reset(); }

  // media::mojom::blink::VideoDecodePerfHistory implementation:
  MOCK_METHOD2(GetPerfInfo,
               void(media::mojom::blink::PredictionFeaturesPtr features,
                    GetPerfInfoCallback got_info_cb));

 private:
  mojo::Receiver<media::mojom::blink::VideoDecodePerfHistory> receiver_{this};
};

class MockWebrtcPerfHistoryService
    : public media::mojom::blink::WebrtcVideoPerfHistory {
 public:
  void BindRequest(mojo::ScopedMessagePipeHandle handle) {
    receiver_.Bind(
        mojo::PendingReceiver<media::mojom::blink::WebrtcVideoPerfHistory>(
            std::move(handle)));
    receiver_.set_disconnect_handler(
        base::BindOnce(&MockWebrtcPerfHistoryService::OnConnectionError,
                       base::Unretained(this)));
  }

  void OnConnectionError() { receiver_.reset(); }

  // media::mojom::blink::WebrtcVideoPerfHistory implementation:
  MOCK_METHOD3(GetPerfInfo,
               void(media::mojom::blink::WebrtcPredictionFeaturesPtr features,
                    int frames_per_second,
                    GetPerfInfoCallback got_info_cb));

 private:
  mojo::Receiver<media::mojom::blink::WebrtcVideoPerfHistory> receiver_{this};
};

class MockLearningTaskControllerService
    : public media::learning::mojom::blink::LearningTaskController {
 public:
  void BindRequest(mojo::PendingReceiver<
                   media::learning::mojom::blink::LearningTaskController>
                       pending_receiver) {
    receiver_.Bind(std::move(pending_receiver));
    receiver_.set_disconnect_handler(
        base::BindOnce(&MockLearningTaskControllerService::OnConnectionError,
                       base::Unretained(this)));
  }

  void OnConnectionError() { receiver_.reset(); }

  bool is_bound() const { return receiver_.is_bound(); }

  // media::mojom::blink::LearningTaskController implementation:
  MOCK_METHOD3(BeginObservation,
               void(const base::UnguessableToken& id,
                    const WTF::Vector<FeatureValue>& features,
                    const std::optional<TargetValue>& default_target));
  MOCK_METHOD2(CompleteObservation,
               void(const base::UnguessableToken& id,
                    const ObservationCompletion& completion));
  MOCK_METHOD1(CancelObservation, void(const base::UnguessableToken& id));
  MOCK_METHOD2(UpdateDefaultTarget,
               void(const base::UnguessableToken& id,
                    const std::optional<TargetValue>& default_target));
  MOCK_METHOD2(PredictDistribution,
               void(const WTF::Vector<FeatureValue>& features,
                    PredictDistributionCallback callback));

 private:
  mojo::Receiver<media::learning::mojom::blink::LearningTaskController>
      receiver_{this};
};

class FakeMediaMetricsProvider
    : public media::mojom::blink::MediaMetricsProvider {
 public:
  // Raw pointers to services owned by the test.
  FakeMediaMetricsProvider(
      MockLearningTaskControllerService* bad_window_service,
      MockLearningTaskControllerService* nnr_service)
      : bad_window_service_(bad_window_service), nnr_service_(nnr_service) {}

  ~FakeMediaMetricsProvider() override = default;

  void BindRequest(mojo::ScopedMessagePipeHandle handle) {
    receiver_.Bind(
        mojo::PendingReceiver<media::mojom::blink::MediaMetricsProvider>(
            std::move(handle)));
    receiver_.set_disconnect_handler(base::BindOnce(
        &FakeMediaMetricsProvider::OnConnectionError, base::Unretained(this)));
  }

  void OnConnectionError() { receiver_.reset(); }

  // mojom::WatchTimeRecorderProvider implementation:
  void AcquireWatchTimeRecorder(
      media::mojom::blink::PlaybackPropertiesPtr properties,
      mojo::PendingReceiver<media::mojom::blink::WatchTimeRecorder> receiver)
      override {
    FAIL();
  }
  void AcquireVideoDecodeStatsRecorder(
      mojo::PendingReceiver<media::mojom::blink::VideoDecodeStatsRecorder>
          receiver) override {
    FAIL();
  }
  void AcquireLearningTaskController(
      const WTF::String& taskName,
      mojo::PendingReceiver<
          media::learning::mojom::blink::LearningTaskController>
          pending_receiver) override {
    if (taskName == media::learning::tasknames::kConsecutiveBadWindows) {
      bad_window_service_->BindRequest(std::move(pending_receiver));
      return;
    }

    if (taskName == media::learning::tasknames::kConsecutiveNNRs) {
      nnr_service_->BindRequest(std::move(pending_receiver));
      return;
    }
    FAIL();
  }
  void AcquirePlaybackEventsRecorder(
      mojo::PendingReceiver<media::mojom::blink::PlaybackEventsRecorder>
          receiver) override {
    FAIL();
  }
  void Initialize(bool is_mse,
                  media::mojom::MediaURLScheme url_scheme,
                  media::mojom::MediaStreamType media_stream_type) override {}
  void OnStarted(media::mojom::blink::PipelineStatusPtr status) override {}
  void OnError(media::mojom::blink::PipelineStatusPtr status) override {}
  void OnFallback(::media::mojom::blink::PipelineStatusPtr status) override {}
  void SetIsEME() override {}
  void SetTimeToMetadata(base::TimeDelta elapsed) override {}
  void SetTimeToFirstFrame(base::TimeDelta elapsed) override {}
  void SetTimeToPlayReady(base::TimeDelta elapsed) override {}
  void SetContainerName(
      media::mojom::blink::MediaContainerName container_name) override {}
  void SetRendererType(
      media::mojom::blink::RendererType renderer_type) override {}
  void SetDemuxerType(media::mojom::DemuxerType demuxer_type) override {}
  void SetKeySystem(const String& key_system) override {}
  void SetHasWaitingForKey() override {}
  void SetIsHardwareSecure() override {}
  void SetHasPlayed() override {}
  void SetHaveEnough() override {}
  void SetHasAudio(media::mojom::AudioCodec audio_codec) override {}
  void SetHasVideo(media::mojom::VideoCodec video_codec) override {}
  void SetVideoPipelineInfo(
      media::mojom::blink::VideoPipelineInfoPtr info) override {}
  void SetAudioPipelineInfo(
      media::mojom::blink::AudioPipelineInfoPtr info) override {}

 private:
  mojo::Receiver<media::mojom::blink::MediaMetricsProvider> receiver_{this};
  raw_ptr<MockLearningTaskControllerService, DanglingUntriaged>
      bad_window_service_;
  raw_ptr<MockLearningTaskControllerService, DanglingUntriaged> nnr_service_;
};

// Simple helper for saving back-end callbacks for pending decodingInfo() calls.
// Callers can then manually fire the callbacks, gaining fine-grain control of
// the timing and order of their arrival.
class CallbackSaver {
 public:
  void SavePerfHistoryCallback(
      media::mojom::blink::PredictionFeaturesPtr features,
      MockPerfHistoryService::GetPerfInfoCallback got_info_cb) {
    perf_history_cb_ = std::move(got_info_cb);
  }

  void SaveBadWindowCallback(
      Vector<media::learning::FeatureValue> features,
      MockLearningTaskControllerService::PredictDistributionCallback
          predict_cb) {
    bad_window_cb_ = std::move(predict_cb);
  }

  void SaveNnrCallback(
      Vector<media::learning::FeatureValue> features,
      MockLearningTaskControllerService::PredictDistributionCallback
          predict_cb) {
    nnr_cb_ = std::move(predict_cb);
  }

  void SaveGpuFactoriesNotifyCallback(base::OnceClosure cb) {
    gpu_factories_notify_cb_ = std::move(cb);
  }

  MockPerfHistoryService::GetPerfInfoCallback& perf_history_cb() {
    return perf_history_cb_;
  }

  MockLearningTaskControllerService::PredictDistributionCallback&
  bad_window_cb() {
    return bad_window_cb_;
  }

  MockLearningTaskControllerService::PredictDistributionCallback& nnr_cb() {
    return nnr_cb_;
  }

  base::OnceClosure& gpu_factories_notify_cb() {
    return gpu_factories_notify_cb_;
  }

 private:
  MockPerfHistoryService::GetPerfInfoCallback perf_history_cb_;
  MockLearningTaskControllerService::PredictDistributionCallback bad_window_cb_;
  MockLearningTaskControllerService::PredictDistributionCallback nnr_cb_;
  base::OnceClosure gpu_factories_notify_cb_;
};

class MockPlatform : public TestingPlatformSupport {
 public:
  MockPlatform() = default;
  ~MockPlatform() override = default;

  MOCK_METHOD0(GetGpuFactories, media::GpuVideoAcceleratorFactories*());
};

// This would typically be a test fixture, but we need it to be
// STACK_ALLOCATED() in order to use V8TestingScope, and we can't force that on
// whatever gtest class instantiates the fixture.
class MediaCapabilitiesTestContext {
  STACK_ALLOCATED();

 public:
  MediaCapabilitiesTestContext() {
    perf_history_service_ = std::make_unique<MockPerfHistoryService>();
    webrtc_perf_history_service_ =
        std::make_unique<MockWebrtcPerfHistoryService>();
    bad_window_service_ = std::make_unique<MockLearningTaskControllerService>();
    nnr_service_ = std::make_unique<MockLearningTaskControllerService>();
    fake_metrics_provider_ = std::make_unique<FakeMediaMetricsProvider>(
        bad_window_service_.get(), nnr_service_.get());

    CHECK(v8_scope_.GetExecutionContext()
              ->GetBrowserInterfaceBroker()
              .SetBinderForTesting(
                  media::mojom::blink::MediaMetricsProvider::Name_,
                  base::BindRepeating(
                      &FakeMediaMetricsProvider::BindRequest,
                      base::Unretained(fake_metrics_provider_.get()))));

    CHECK(v8_scope_.GetExecutionContext()
              ->GetBrowserInterfaceBroker()
              .SetBinderForTesting(
                  media::mojom::blink::VideoDecodePerfHistory::Name_,
                  base::BindRepeating(
                      &MockPerfHistoryService::BindRequest,
                      base::Unretained(perf_history_service_.get()))));

    CHECK(v8_scope_.GetExecutionContext()
              ->GetBrowserInterfaceBroker()
              .SetBinderForTesting(
                  media::mojom::blink::WebrtcVideoPerfHistory::Name_,
                  base::BindRepeating(
                      &MockWebrtcPerfHistoryService::BindRequest,
                      base::Unretained(webrtc_perf_history_service_.get()))));

    media_capabilities_ = MediaCapabilities::mediaCapabilities(
        *v8_scope_.GetWindow().navigator());
  }

  ~MediaCapabilitiesTestContext() {
    CHECK(v8_scope_.GetExecutionContext()
              ->GetBrowserInterfaceBroker()
              .SetBinderForTesting(
                  media::mojom::blink::MediaMetricsProvider::Name_, {}));

    CHECK(v8_scope_.GetExecutionContext()
              ->GetBrowserInterfaceBroker()
              .SetBinderForTesting(
                  media::mojom::blink::VideoDecodePerfHistory::Name_, {}));

    CHECK(v8_scope_.GetExecutionContext()
              ->GetBrowserInterfaceBroker()
              .SetBinderForTesting(
                  media::mojom::blink::WebrtcVideoPerfHistory::Name_, {}));
  }

  ExceptionState& GetExceptionState() { return v8_scope_.GetExceptionState(); }

  ScriptState* GetScriptState() const { return v8_scope_.GetScriptState(); }

  v8::Isolate* GetIsolate() const { return GetScriptState()->GetIsolate(); }

  MediaCapabilities* GetMediaCapabilities() const {
    return media_capabilities_.Get();
  }

  MockPerfHistoryService* GetPerfHistoryService() const {
    return perf_history_service_.get();
  }

  MockWebrtcPerfHistoryService* GetWebrtcPerfHistoryService() const {
    return webrtc_perf_history_service_.get();
  }

  MockLearningTaskControllerService* GetBadWindowService() const {
    return bad_window_service_.get();
  }

  MockLearningTaskControllerService* GetNnrService() const {
    return nnr_service_.get();
  }

  MockPlatform& GetMockPlatform() { return *mock_platform_; }

  void VerifyAndClearMockExpectations() {
    testing::Mock::VerifyAndClearExpectations(GetPerfHistoryService());
    testing::Mock::VerifyAndClearExpectations(GetWebrtcPerfHistoryService());
    testing::Mock::VerifyAndClearExpectations(GetNnrService());
    testing::Mock::VerifyAndClearExpectations(GetBadWindowService());
    testing::Mock::VerifyAndClearExpectations(&GetMockPlatform());
  }

 private:
  V8TestingScope v8_scope_;
  ScopedTestingPlatformSupport<MockPlatform> mock_platform_;
  std::unique_ptr<MockPerfHistoryService> perf_history_service_;
  std::unique_ptr<MockWebrtcPerfHistoryService> webrtc_perf_history_service_;
  std::unique_ptr<FakeMediaMetricsProvider> fake_metrics_provider_;
  Persistent<MediaCapabilities> media_capabilities_;
  std::unique_ptr<MockLearningTaskControllerService> bad_window_service_;
  std::unique_ptr<MockLearningTaskControllerService> nnr_service_;
};

// |kVideoContentType|, |kCodec|, and |kCodecProfile| must match.
const char kVideoContentType[] = "video/webm; codecs=\"vp09.00.10.08\"";
const char kAudioContentType[] = "audio/webm; codecs=\"opus\"";
const media::VideoCodecProfile kCodecProfile = media::VP9PROFILE_PROFILE0;
const media::VideoCodec kCodec = media::VideoCodec::kVP9;
const double kFramerate = 20.5;
const int kWidth = 3840;
const int kHeight = 2160;
const int kBitrate = 2391000;
const char kWebrtcVideoContentType[] = "video/VP9; profile-id=\"0\"";
const char kWebrtcAudioContentType[] = "audio/opus";

// Construct AudioConfig using the constants above.
template <class T>
T* CreateAudioConfig(const char content_type[], const char type[]) {
  auto* audio_config = MakeGarbageCollected<AudioConfiguration>();
  audio_config->setContentType(content_type);
  auto* decoding_config = MakeGarbageCollected<T>();
  decoding_config->setType(type);
  decoding_config->setAudio(audio_config);
  return decoding_config;
}

// Construct media-source AudioConfig using the constants above.
MediaDecodingConfiguration* CreateAudioDecodingConfig() {
  return CreateAudioConfig<MediaDecodingConfiguration>(kAudioContentType,
                                                       "media-source");
}

// Construct webrtc decoding AudioConfig using the constants above.
MediaDecodingConfiguration* CreateWebrtcAudioDecodingConfig() {
  return CreateAudioConfig<MediaDecodingConfiguration>(kWebrtcAudioContentType,
                                                       "webrtc");
}

// Construct webrtc decoding AudioConfig using the constants above.
MediaEncodingConfiguration* CreateWebrtcAudioEncodingConfig() {
  return CreateAudioConfig<MediaEncodingConfiguration>(kWebrtcAudioContentType,
                                                       "webrtc");
}

// Construct VideoConfig using the constants above.
template <class T>
T* CreateVideoConfig(const char content_type[], const char type[]) {
  auto* video_config = MakeGarbageCollected<VideoConfiguration>();
  video_config->setFramerate(kFramerate);
  video_config->setContentType(content_type);
  video_config->setWidth(kWidth);
  video_config->setHeight(kHeight);
  video_config->setBitrate(kBitrate);
  auto* decoding_config = MakeGarbageCollected<T>();
  decoding_config->setType(type);
  decoding_config->setVideo(video_config);
  return decoding_config;
}

// Construct media-source VideoConfig using the constants above.
MediaDecodingConfiguration* CreateDecodingConfig() {
  return CreateVideoConfig<MediaDecodingConfiguration>(kVideoContentType,
                                                       "media-source");
}

// Construct webrtc decoding VideoConfig using the constants above.
MediaDecodingConfiguration* CreateWebrtcDecodingConfig() {
  return CreateVideoConfig<MediaDecodingConfiguration>(kWebrtcVideoContentType,
                                                       "webrtc");
}

// Construct webrtc encoding VideoConfig using the constants above.
MediaEncodingConfiguration* CreateWebrtcEncodingConfig() {
  return CreateVideoConfig<MediaEncodingConfiguration>(kWebrtcVideoContentType,
                                                       "webrtc");
}

// Construct PredicitonFeatures matching the CreateDecodingConfig, using the
// constants above.
media::mojom::blink::PredictionFeatures CreateFeatures() {
  media::mojom::blink::PredictionFeatures features;
  features.profile =
      static_cast<media::mojom::blink::VideoCodecProfile>(kCodecProfile);
  features.video_size = gfx::Size(kWidth, kHeight);
  features.frames_per_sec = kFramerate;

  // Not set by any tests so far. Choosing sane defaults to mirror production
  // code.
  features.key_system = "";
  features.use_hw_secure_codecs = false;

  return features;
}

Vector<media::learning::FeatureValue> CreateFeaturesML() {
  media::mojom::blink::PredictionFeatures features = CreateFeatures();

  // FRAGILE: Order here MUST match order in
  // WebMediaPlayerImpl::UpdateSmoothnessHelper().
  // TODO(chcunningham): refactor into something more robust.
  Vector<media::learning::FeatureValue> ml_features(
      {media::learning::FeatureValue(static_cast<int>(kCodec)),
       media::learning::FeatureValue(kCodecProfile),
       media::learning::FeatureValue(kWidth),
       media::learning::FeatureValue(kFramerate)});

  return ml_features;
}

// Construct WebrtcPredicitonFeatures matching the CreateWebrtc{Decoding,
// Encoding}Config, using the constants above.
media::mojom::blink::WebrtcPredictionFeatures CreateWebrtcFeatures(
    bool is_decode) {
  media::mojom::blink::WebrtcPredictionFeatures features;
  features.is_decode_stats = is_decode;
  features.profile =
      static_cast<media::mojom::blink::VideoCodecProfile>(kCodecProfile);
  features.video_pixels = kWidth * kHeight;
  return features;
}

// Types of smoothness predictions.
enum class PredictionType {
  kDB,
  kBadWindow,
  kNnr,
  kGpuFactories,
};

// Makes a TargetHistogram with single count at |target_value|.
media::learning::TargetHistogram MakeHistogram(double target_value) {
  media::learning::TargetHistogram histogram;
  histogram += media::learning::TargetValue(target_value);
  return histogram;
}

// Makes DB (PerfHistoryService) callback for use with gtest WillOnce().
// Callback will verify |features| matches |expected_features| and run with
// provided values for |is_smooth| and |is_power_efficient|.
testing::Action<void(media::mojom::blink::PredictionFeaturesPtr,
                     MockPerfHistoryService::GetPerfInfoCallback)>
DbCallback(const media::mojom::blink::PredictionFeatures& expected_features,
           bool is_smooth,
           bool is_power_efficient) {
  return [=](media::mojom::blink::PredictionFeaturesPtr features,
             MockPerfHistoryService::GetPerfInfoCallback got_info_cb) {
    EXPECT_TRUE(features->Equals(expected_features));
    std::move(got_info_cb).Run(is_smooth, is_power_efficient);
  };
}

// Makes ML (LearningTaskControllerService) callback for use with gtest
// WillOnce(). Callback will verify |features| matches |expected_features| and
// run a TargetHistogram containing a single count for |histogram_target|.
testing::Action<void(
    const Vector<media::learning::FeatureValue>&,
    MockLearningTaskControllerService::PredictDistributionCallback predict_cb)>
MlCallback(const Vector<media::learning::FeatureValue>& expected_features,
           double histogram_target) {
  return [=](const Vector<media::learning::FeatureValue>& features,
             MockLearningTaskControllerService::PredictDistributionCallback
                 predict_cb) {
    EXPECT_EQ(features, expected_features);
    std::move(predict_cb).Run(MakeHistogram(histogram_target));
  };
}

// Makes DB (WebrtcPerfHistoryService) callback for use with gtest WillOnce().
// Callback will verify |features| and |framerate| matches |expected_features|
// and |expected_framreate| and run with provided values for |is_smooth|.
testing::Action<void(media::mojom::blink::WebrtcPredictionFeaturesPtr,
                     int32_t,
                     MockWebrtcPerfHistoryService::GetPerfInfoCallback)>
WebrtcDbCallback(
    const media::mojom::blink::WebrtcPredictionFeatures& expected_features,
    double expected_framerate,
    bool is_smooth) {
  return [=](media::mojom::blink::WebrtcPredictionFeaturesPtr features,
             int framerate,
             MockWebrtcPerfHistoryService::GetPerfInfoCallback got_info_cb) {
    EXPECT_TRUE(features->Equals(expected_features));
    EXPECT_EQ(framerate, base::ClampRound(expected_framerate));
    std::move(got_info_cb).Run(is_smooth);
  };
}

testing::Action<void(base::OnceClosure)> GpuFactoriesNotifyCallback() {
  return [](base::OnceClosure cb) { std::move(cb).Run(); };
}

// Helper to constructs field trial params with given ML prediction thresholds.
base::FieldTrialParams MakeMlParams(double bad_window_threshold,
                                    double nnr_threshold) {
  base::FieldTrialParams params;
  params[MediaCapabilities::kLearningBadWindowThresholdParamName] =
      base::NumberToString(bad_window_threshold);
  params[MediaCapabilities::kLearningNnrThresholdParamName] =
      base::NumberToString(nnr_threshold);
  return params;
}

// Wrapping decodingInfo() call for readability. Await resolution of the promise
// and return its info.
MediaCapabilitiesInfo* DecodingInfo(
    const MediaDecodingConfiguration* decoding_config,
    MediaCapabilitiesTestContext* context) {
  auto promise = context->GetMediaCapabilities()->decodingInfo(
      context->GetScriptState(), decoding_config, context->GetExceptionState());

  ScriptPromiseTester tester(context->GetScriptState(), promise);
  tester.WaitUntilSettled();

  CHECK(!tester.IsRejected()) << " Cant get info from rejected promise.";

  return NativeValueTraits<MediaCapabilitiesInfo>::NativeValue(
      context->GetIsolate(), tester.Value().V8Value(),
      context->GetExceptionState());
}

// Wrapping encodingInfo() call for readability. Await resolution of the promise
// and return its info.
MediaCapabilitiesInfo* EncodingInfo(
    const MediaEncodingConfiguration* encoding_config,
    MediaCapabilitiesTestContext* context) {
  auto promise = context->GetMediaCapabilities()->encodingInfo(
      context->GetScriptState(), encoding_config, context->GetExceptionState());

  ScriptPromiseTester tester(context->GetScriptState(), promise);
  tester.WaitUntilSettled();

  CHECK(!tester.IsRejected()) << " Cant get info from rejected promise.";

  return NativeValueTraits<MediaCapabilitiesInfo>::NativeValue(
      context->GetIsolate(), tester.Value().V8Value(),
      context->GetExceptionState());
}
}  // namespace

TEST(MediaCapabilitiesTests, BasicAudio) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  const MediaDecodingConfiguration* kDecodingConfig =
      CreateAudioDecodingConfig();
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
}

// Other tests will assume these match. Test to be sure they stay in sync.
TEST(MediaCapabilitiesTests, ConfigMatchesFeatures) {
  test::TaskEnvironment task_environment;
  const MediaDecodingConfiguration* kDecodingConfig = CreateDecodingConfig();
  const media::mojom::blink::PredictionFeatures kFeatures = CreateFeatures();

  EXPECT_TRUE(kDecodingConfig->video()->contentType().Contains("vp09.00"));
  EXPECT_EQ(static_cast<media::VideoCodecProfile>(kFeatures.profile),
            media::VP9PROFILE_PROFILE0);
  EXPECT_EQ(kCodecProfile, media::VP9PROFILE_PROFILE0);

  EXPECT_EQ(kDecodingConfig->video()->framerate(), kFeatures.frames_per_sec);
  EXPECT_EQ(kDecodingConfig->video()->width(),
            static_cast<uint32_t>(kFeatures.video_size.width()));
  EXPECT_EQ(kDecodingConfig->video()->height(),
            static_cast<uint32_t>(kFeatures.video_size.height()));
}

// Test that non-integer framerate isn't truncated by IPC.
// https://crbug.com/1024399
TEST(MediaCapabilitiesTests, NonIntegerFramerate) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      // Enabled features.
      {},
      // Disabled ML predictions + GpuFactories (just use DB).
      {media::kMediaCapabilitiesQueryGpuFactories,
       media::kMediaLearningSmoothnessExperiment});

  const auto* kDecodingConfig = CreateDecodingConfig();
  const media::mojom::blink::PredictionFeatures kFeatures = CreateFeatures();

  // FPS for this test must not be a whole number. Assert to ensure the default
  // config meets that condition.
  ASSERT_NE(fmod(kDecodingConfig->video()->framerate(), 1), 0);

  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce([&](media::mojom::blink::PredictionFeaturesPtr features,
                    MockPerfHistoryService::GetPerfInfoCallback got_info_cb) {
        // Explicitly check for frames_per_sec equality.
        // PredictionFeatures::Equals() will not catch loss of precision if
        // frames_per_sec is made to be int (currently a double).
        EXPECT_EQ(features->frames_per_sec, kFramerate);

        // Check that other things match as well.
        EXPECT_TRUE(features->Equals(kFeatures));

        std::move(got_info_cb).Run(/*smooth*/ true, /*power_efficient*/ true);
      });

  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
}

// Test smoothness predictions from DB (PerfHistoryService).
TEST(MediaCapabilitiesTests, PredictWithJustDB) {
  test::TaskEnvironment task_environment;
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      // Enabled features.
      {},
      // Disabled ML predictions + GpuFactories (just use DB).
      {media::kMediaCapabilitiesQueryGpuFactories,
       media::kMediaLearningSmoothnessExperiment});

  MediaCapabilitiesTestContext context;
  const auto* kDecodingConfig = CreateDecodingConfig();
  const media::mojom::blink::PredictionFeatures kFeatures = CreateFeatures();

  // ML services should not be called for prediction.
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .Times(0);
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _)).Times(0);

  // DB alone (PerfHistoryService) should be called. Signal smooth=true and
  // power_efficient = false.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ true, /*power_eff*/ false));
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());

  // Verify DB call was made. ML services should not even be bound.
  testing::Mock::VerifyAndClearExpectations(context.GetPerfHistoryService());
  EXPECT_FALSE(context.GetBadWindowService()->is_bound());
  EXPECT_FALSE(context.GetNnrService()->is_bound());

  // Repeat test with inverted smooth and power_efficient results.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*power_eff*/ true));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, PredictPowerEfficientWithGpuFactories) {
  test::TaskEnvironment task_environment;
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeatures(
      // Enable GpuFactories for power predictions.
      {media::kMediaCapabilitiesQueryGpuFactories},
      // Disable ML predictions (may/may not be disabled by default).
      {media::kMediaLearningSmoothnessExperiment});

  MediaCapabilitiesTestContext context;
  const auto* kDecodingConfig = CreateDecodingConfig();
  const media::mojom::blink::PredictionFeatures kFeatures = CreateFeatures();

  // Setup DB to return powerEfficient = false. We later verify that opposite
  // response from GpuFactories overrides the DB.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*power_eff*/ false));

  auto mock_gpu_factories =
      std::make_unique<media::MockGpuVideoAcceleratorFactories>(nullptr);
  ON_CALL(context.GetMockPlatform(), GetGpuFactories())
      .WillByDefault(Return(mock_gpu_factories.get()));

  // First, lets simulate the scenario where we ask before support is known. The
  // async path should notify us when the info arrives. We then get GpuFactroies
  // again and learn the config is supported.
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories()).Times(2);
  {
    // InSequence because we EXPECT two calls to IsDecoderSupportKnown with
    // different return values.
    InSequence s;
    EXPECT_CALL(*mock_gpu_factories, IsDecoderSupportKnown())
        .WillOnce(Return(false));
    EXPECT_CALL(*mock_gpu_factories, NotifyDecoderSupportKnown(_))
        .WillOnce(GpuFactoriesNotifyCallback());

    // MediaCapabilities calls IsDecoderSupportKnown() once, and
    // GpuVideoAcceleratorFactories::IsDecoderConfigSupported() also calls it
    // once internally.
    EXPECT_CALL(*mock_gpu_factories, IsDecoderSupportKnown())
        .Times(2)
        .WillRepeatedly(Return(true));
    EXPECT_CALL(*mock_gpu_factories, IsDecoderConfigSupported(_))
        .WillOnce(
            Return(media::GpuVideoAcceleratorFactories::Supported::kTrue));
  }

  // Info should be powerEfficient, preferring response of GpuFactories over
  // the DB.
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->powerEfficient());
  EXPECT_FALSE(info->smooth());
  context.VerifyAndClearMockExpectations();
  testing::Mock::VerifyAndClearExpectations(mock_gpu_factories.get());

  // Now expect a second query with support is already known to be false. Set
  // DB to respond with the opposite answer.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*power_eff*/ true));
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories());
  EXPECT_CALL(*mock_gpu_factories, IsDecoderSupportKnown())
      .Times(2)
      .WillRepeatedly(Return(true));
  EXPECT_CALL(*mock_gpu_factories, IsDecoderConfigSupported(_))
      .WillRepeatedly(
          Return(media::GpuVideoAcceleratorFactories::Supported::kFalse));

  // Info should be NOT powerEfficient, preferring response of GpuFactories over
  // the DB.
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->powerEfficient());
  EXPECT_FALSE(info->smooth());
  context.VerifyAndClearMockExpectations();
  testing::Mock::VerifyAndClearExpectations(mock_gpu_factories.get());
}

// Test with smoothness predictions coming solely from "bad window" ML service.
TEST(MediaCapabilitiesTests, PredictWithBadWindowMLService) {
  test::TaskEnvironment task_environment;
  // Enable ML predictions with thresholds. -1 disables the NNR predictor.
  const double kBadWindowThreshold = 2;
  const double kNnrThreshold = -1;
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeaturesAndParameters(
      // Enabled features w/ parameters
      {{media::kMediaLearningSmoothnessExperiment,
        MakeMlParams(kBadWindowThreshold, kNnrThreshold)}},
      // Disabled GpuFactories (use DB for power).
      {media::kMediaCapabilitiesQueryGpuFactories});

  MediaCapabilitiesTestContext context;
  const auto* kDecodingConfig = CreateDecodingConfig();
  const media::mojom::blink::PredictionFeatures kFeatures = CreateFeatures();
  const Vector<media::learning::FeatureValue> kFeaturesML = CreateFeaturesML();

  // ML is enabled, but DB should still be called for power efficiency (false).
  // Its smoothness value (true) should be ignored in favor of ML prediction.
  // Only bad window service should be asked for a prediction. Expect
  // smooth=false because bad window prediction is equal to its threshold.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ true, /*efficient*/ false));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kBadWindowThreshold));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _)).Times(0);
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
  // NNR service should not be bound when NNR predictions disabled.
  EXPECT_FALSE(context.GetNnrService()->is_bound());
  context.VerifyAndClearMockExpectations();

  // Same as above, but invert all signals. Expect smooth=true because bad
  // window prediction is now less than its threshold.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*efficient*/ true));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kBadWindowThreshold - 0.25));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _)).Times(0);
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
  EXPECT_FALSE(context.GetNnrService()->is_bound());
  context.VerifyAndClearMockExpectations();

  // Same as above, but predict zero bad windows. Expect smooth=true because
  // zero is below the threshold.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*efficient*/ true));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, /* bad windows */ 0));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _)).Times(0);
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
  EXPECT_FALSE(context.GetNnrService()->is_bound());
  context.VerifyAndClearMockExpectations();
}

// Test with smoothness predictions coming solely from "NNR" ML service.
TEST(MediaCapabilitiesTests, PredictWithNnrMLService) {
  test::TaskEnvironment task_environment;
  // Enable ML predictions with thresholds. -1 disables the bad window
  // predictor.
  const double kBadWindowThreshold = -1;
  const double kNnrThreshold = 5;
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeaturesAndParameters(
      // Enabled both ML services.
      {{media::kMediaLearningSmoothnessExperiment,
        MakeMlParams(kBadWindowThreshold, kNnrThreshold)}},
      // Disabled features (use DB for power efficiency)
      {media::kMediaCapabilitiesQueryGpuFactories});

  MediaCapabilitiesTestContext context;
  const auto* kDecodingConfig = CreateDecodingConfig();
  const media::mojom::blink::PredictionFeatures kFeatures = CreateFeatures();
  const Vector<media::learning::FeatureValue> kFeaturesML = CreateFeaturesML();

  // ML is enabled, but DB should still be called for power efficiency (false).
  // Its smoothness value (true) should be ignored in favor of ML prediction.
  // Only NNR service should be asked for a prediction. Expect smooth=false
  // because NNR prediction is equal to its threshold.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ true, /*efficient*/ false));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .Times(0);
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kNnrThreshold));
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
  // Bad window service should not be bound when NNR predictions disabled.
  EXPECT_FALSE(context.GetBadWindowService()->is_bound());
  context.VerifyAndClearMockExpectations();

  // Same as above, but invert all signals. Expect smooth=true because NNR
  // prediction is now less than its threshold.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*efficient*/ true));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .Times(0);
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kNnrThreshold - 0.01));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
  EXPECT_FALSE(context.GetBadWindowService()->is_bound());
  context.VerifyAndClearMockExpectations();

  // Same as above, but predict zero NNRs. Expect smooth=true because zero is
  // below the threshold.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*efficient*/ true));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .Times(0);
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, /* NNRs */ 0));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
  EXPECT_FALSE(context.GetBadWindowService()->is_bound());
  context.VerifyAndClearMockExpectations();
}

// Test with combined smoothness predictions from both ML services.
TEST(MediaCapabilitiesTests, PredictWithBothMLServices) {
  test::TaskEnvironment task_environment;
  // Enable ML predictions with thresholds.
  const double kBadWindowThreshold = 2;
  const double kNnrThreshold = 1;
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeaturesAndParameters(
      // Enabled both ML services.
      {{media::kMediaLearningSmoothnessExperiment,
        MakeMlParams(kBadWindowThreshold, kNnrThreshold)}},
      // Disabled features (use DB for power efficiency)
      {media::kMediaCapabilitiesQueryGpuFactories});

  MediaCapabilitiesTestContext context;
  const auto* kDecodingConfig = CreateDecodingConfig();
  const media::mojom::blink::PredictionFeatures kFeatures = CreateFeatures();
  const Vector<media::learning::FeatureValue> kFeaturesML = CreateFeaturesML();

  // ML is enabled, but DB should still be called for power efficiency (false).
  // Its smoothness value (true) should be ignored in favor of ML predictions.
  // Both ML services should be called for prediction. In both cases we exceed
  // the threshold, such that smooth=false.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ true, /*efficient*/ false));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kBadWindowThreshold + 0.5));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kNnrThreshold + 0.5));
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
  context.VerifyAndClearMockExpectations();

  // Make another call to DecodingInfo with one "bad window" prediction
  // indicating smooth=false, while nnr prediction indicates smooth=true. Verify
  // resulting info predicts false, as the logic should OR the false signals.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ true, /*efficient*/ false));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kBadWindowThreshold + 0.5));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kNnrThreshold / 2));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
  context.VerifyAndClearMockExpectations();

  // Same as above, but invert predictions from ML services. Outcome should
  // still be smooth=false (logic is ORed).
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ true, /*efficient*/ false));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kBadWindowThreshold / 2));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kNnrThreshold + 0.5));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
  context.VerifyAndClearMockExpectations();

  // This time both ML services agree smooth=true while DB predicts
  // smooth=false. Expect info->smooth() = true, as only ML predictions matter
  // when ML experiment enabled.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*efficient*/ true));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kBadWindowThreshold / 2));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kNnrThreshold / 2));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
  context.VerifyAndClearMockExpectations();

  // Same as above, but with ML services predicting exactly their respective
  // thresholds. Now expect info->smooth() = false - reaching the threshold is
  // considered not smooth.
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(DbCallback(kFeatures, /*smooth*/ false, /*efficient*/ true));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kBadWindowThreshold));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(MlCallback(kFeaturesML, kNnrThreshold));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
  context.VerifyAndClearMockExpectations();
}

// Simulate a call to DecodingInfo with smoothness predictions arriving in the
// specified |callback_order|. Ensure that promise resolves correctly only after
// all callbacks have arrived.
void RunCallbackPermutationTest(std::vector<PredictionType> callback_order) {
  // Enable ML predictions with thresholds.
  const double kBadWindowThreshold = 2;
  const double kNnrThreshold = 3;
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeaturesAndParameters(
      // Enabled features w/ parameters
      {{media::kMediaLearningSmoothnessExperiment,
        MakeMlParams(kBadWindowThreshold, kNnrThreshold)},
       {media::kMediaCapabilitiesQueryGpuFactories, {}}},
      // Disabled features.
      {});

  MediaCapabilitiesTestContext context;
  const auto* kDecodingConfig = CreateDecodingConfig();
  auto mock_gpu_factories =
      std::make_unique<media::MockGpuVideoAcceleratorFactories>(nullptr);

  // DB and both ML services should be called. Save their callbacks.
  CallbackSaver cb_saver;
  EXPECT_CALL(*context.GetPerfHistoryService(), GetPerfInfo(_, _))
      .WillOnce(Invoke(&cb_saver, &CallbackSaver::SavePerfHistoryCallback));
  EXPECT_CALL(*context.GetBadWindowService(), PredictDistribution(_, _))
      .WillOnce(Invoke(&cb_saver, &CallbackSaver::SaveBadWindowCallback));
  EXPECT_CALL(*context.GetNnrService(), PredictDistribution(_, _))
      .WillOnce(Invoke(&cb_saver, &CallbackSaver::SaveNnrCallback));

  // GpuFactories should also be called. Set it up to be async with arrival of
  // support info. Save the "notify" callback.
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .WillRepeatedly(Return(mock_gpu_factories.get()));
  {
    // InSequence because we EXPECT two calls to IsDecoderSupportKnown with
    // different return values.
    InSequence s;
    EXPECT_CALL(*mock_gpu_factories, IsDecoderSupportKnown())
        .WillOnce(Return(false));
    EXPECT_CALL(*mock_gpu_factories, NotifyDecoderSupportKnown(_))
        .WillOnce(
            Invoke(&cb_saver, &CallbackSaver::SaveGpuFactoriesNotifyCallback));
    // MediaCapabilities calls IsDecoderSupportKnown() once, and
    // GpuVideoAcceleratorFactories::IsDecoderConfigSupported() also calls it
    // once internally.
    EXPECT_CALL(*mock_gpu_factories, IsDecoderSupportKnown())
        .Times(2)
        .WillRepeatedly(Return(true));
    EXPECT_CALL(*mock_gpu_factories, IsDecoderConfigSupported(_))
        .WillRepeatedly(
            Return(media::GpuVideoAcceleratorFactories::Supported::kFalse));
  }

  // Call decodingInfo() to kick off the calls to prediction services.
  auto promise = context.GetMediaCapabilities()->decodingInfo(
      context.GetScriptState(), kDecodingConfig, context.GetExceptionState());
  ScriptPromiseTester tester(context.GetScriptState(), promise);

  // Callbacks should all be saved after mojo's pending tasks have run.
  test::RunPendingTasks();
  ASSERT_TRUE(cb_saver.perf_history_cb() && cb_saver.bad_window_cb() &&
              cb_saver.nnr_cb() && cb_saver.gpu_factories_notify_cb());

  // Complete callbacks in whatever order.
  for (size_t i = 0; i < callback_order.size(); ++i) {
    switch (callback_order[i]) {
      case PredictionType::kDB:
        std::move(cb_saver.perf_history_cb()).Run(true, true);
        break;
      case PredictionType::kBadWindow:
        std::move(cb_saver.bad_window_cb())
            .Run(MakeHistogram(kBadWindowThreshold - 0.25));
        break;
      case PredictionType::kNnr:
        std::move(cb_saver.nnr_cb()).Run(MakeHistogram(kNnrThreshold + 0.5));
        break;
      case PredictionType::kGpuFactories:
        std::move(cb_saver.gpu_factories_notify_cb()).Run();
        break;
    }

    // Give callbacks/tasks a chance to run.
    test::RunPendingTasks();

    // Promise should only be resolved once the final callback has run.
    if (i < callback_order.size() - 1) {
      ASSERT_FALSE(tester.IsFulfilled());
    } else {
      ASSERT_TRUE(tester.IsFulfilled());
    }
  }

  ASSERT_FALSE(tester.IsRejected()) << " Cant get info from rejected promise.";
  MediaCapabilitiesInfo* info =
      NativeValueTraits<MediaCapabilitiesInfo>::NativeValue(
          context.GetIsolate(), tester.Value().V8Value(),
          context.GetExceptionState());

  // Smooth=false because NNR prediction exceeds threshold.
  EXPECT_FALSE(info->smooth());
  // DB predicted power_efficient = true, but GpuFactories overrides w/ false.
  EXPECT_FALSE(info->powerEfficient());
}

// Test that decodingInfo() behaves correctly for all orderings/timings of the
// underlying prediction services.
TEST(MediaCapabilitiesTests, PredictionCallbackPermutations) {
  test::TaskEnvironment task_environment;
  std::vector<PredictionType> callback_order(
      {PredictionType::kDB, PredictionType::kBadWindow, PredictionType::kNnr,
       PredictionType::kGpuFactories});
  do {
    RunCallbackPermutationTest(callback_order);
  } while (std::next_permutation(callback_order.begin(), callback_order.end()));
}

// WebRTC decodingInfo tests.
TEST(MediaCapabilitiesTests, WebrtcDecodingBasicAudio) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));

  const MediaDecodingConfiguration* kDecodingConfig =
      CreateWebrtcAudioDecodingConfig();
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcDecodingUnsupportedAudio) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));

  const MediaDecodingConfiguration* kDecodingConfig =
      CreateAudioConfig<MediaDecodingConfiguration>("audio/FooCodec", "webrtc");
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

// Other tests will assume these match. Test to be sure they stay in sync.
TEST(MediaCapabilitiesTests, WebrtcConfigMatchesFeatures) {
  test::TaskEnvironment task_environment;
  const MediaDecodingConfiguration* kDecodingConfig =
      CreateWebrtcDecodingConfig();
  const MediaEncodingConfiguration* kEncodingConfig =
      CreateWebrtcEncodingConfig();
  const media::mojom::blink::WebrtcPredictionFeatures kDecodeFeatures =
      CreateWebrtcFeatures(/*is_decode=*/true);
  const media::mojom::blink::WebrtcPredictionFeatures kEncodeFeatures =
      CreateWebrtcFeatures(/*is_decode=*/false);

  EXPECT_TRUE(kDecodeFeatures.is_decode_stats);
  EXPECT_FALSE(kEncodeFeatures.is_decode_stats);

  EXPECT_TRUE(kDecodingConfig->video()->contentType().Contains("video/VP9"));
  EXPECT_TRUE(kEncodingConfig->video()->contentType().Contains("video/VP9"));
  EXPECT_EQ(static_cast<media::VideoCodecProfile>(kDecodeFeatures.profile),
            media::VP9PROFILE_PROFILE0);
  EXPECT_EQ(static_cast<media::VideoCodecProfile>(kEncodeFeatures.profile),
            media::VP9PROFILE_PROFILE0);
  EXPECT_EQ(kCodecProfile, media::VP9PROFILE_PROFILE0);

  EXPECT_EQ(
      kDecodingConfig->video()->width() * kDecodingConfig->video()->height(),
      static_cast<uint32_t>(kDecodeFeatures.video_pixels));
  EXPECT_EQ(
      kEncodingConfig->video()->width() * kEncodingConfig->video()->height(),
      static_cast<uint32_t>(kEncodeFeatures.video_pixels));
}

// Test smoothness predictions from DB (WebrtcPerfHistoryService).
TEST(MediaCapabilitiesTests, WebrtcDecodingBasicVideo) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));
  const auto* kDecodingConfig = CreateWebrtcDecodingConfig();
  const media::mojom::blink::WebrtcPredictionFeatures kFeatures =
      CreateWebrtcFeatures(/*is_decode=*/true);

  // WebrtcPerfHistoryService should be queried for smoothness. Signal
  // smooth=true.
  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(WebrtcDbCallback(kFeatures, kFramerate, /*is_smooth=*/true));
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());

  // Verify DB call was made.
  testing::Mock::VerifyAndClearExpectations(
      context.GetWebrtcPerfHistoryService());

  // Repeat test with smooth=false.
  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(WebrtcDbCallback(kFeatures, kFramerate, /*is_smooth=*/false));
  info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcDecodingUnsupportedVideo) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));

  const MediaDecodingConfiguration* kDecodingConfig =
      CreateVideoConfig<MediaDecodingConfiguration>("video/FooCodec", "webrtc");

  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  EXPECT_FALSE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcDecodingSpatialScalability) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));

  auto* decoding_config = CreateWebrtcDecodingConfig();
  auto* video_config = decoding_config->getVideoOr(nullptr);
  video_config->setSpatialScalability(false);
  const media::mojom::blink::WebrtcPredictionFeatures kFeatures =
      CreateWebrtcFeatures(/*is_decode=*/true);

  // WebrtcPerfHistoryService should be queried for smoothness. Signal
  // smooth=true.
  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(WebrtcDbCallback(kFeatures, kFramerate, /*is_smooth=*/true));
  MediaCapabilitiesInfo* info = DecodingInfo(decoding_config, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());

  // Verify DB call was made.
  testing::Mock::VerifyAndClearExpectations(
      context.GetWebrtcPerfHistoryService());

  // Repeat test with spatialScalability=true.
  video_config->setSpatialScalability(true);
  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(WebrtcDbCallback(kFeatures, kFramerate, /*is_smooth=*/false));
  info = DecodingInfo(decoding_config, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

// WebRTC encodingInfo tests.
TEST(MediaCapabilitiesTests, WebrtcEncodingBasicAudio) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));

  const MediaEncodingConfiguration* kEncodingConfig =
      CreateWebrtcAudioEncodingConfig();
  MediaCapabilitiesInfo* info = EncodingInfo(kEncodingConfig, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcEncodingUnsupportedAudio) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));
  const MediaEncodingConfiguration* kEncodingConfig =
      CreateAudioConfig<MediaEncodingConfiguration>("audio/FooCodec", "webrtc");
  MediaCapabilitiesInfo* info = EncodingInfo(kEncodingConfig, &context);
  EXPECT_FALSE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

// Test smoothness predictions from DB (WebrtcPerfHistoryService).
TEST(MediaCapabilitiesTests, WebrtcEncodingBasicVideo) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));
  const auto* kEncodingConfig = CreateWebrtcEncodingConfig();
  const media::mojom::blink::WebrtcPredictionFeatures kFeatures =
      CreateWebrtcFeatures(/*is_decode=*/false);

  // WebrtcPerfHistoryService should be queried for smoothness. Signal
  // smooth=true.
  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(WebrtcDbCallback(kFeatures, kFramerate, /*is_smooth=*/true));
  MediaCapabilitiesInfo* info = EncodingInfo(kEncodingConfig, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());

  // Verify DB call was made.
  testing::Mock::VerifyAndClearExpectations(
      context.GetWebrtcPerfHistoryService());

  // Repeat test with smooth=false.
  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(WebrtcDbCallback(kFeatures, kFramerate, /*is_smooth=*/false));
  info = EncodingInfo(kEncodingConfig, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcEncodingUnsupportedVideo) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));

  const MediaEncodingConfiguration* kEncodingConfig =
      CreateVideoConfig<MediaEncodingConfiguration>("video/FooCodec", "webrtc");

  MediaCapabilitiesInfo* info = EncodingInfo(kEncodingConfig, &context);
  EXPECT_FALSE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcEncodingScalabilityMode) {
  test::TaskEnvironment task_environment;
  MediaCapabilitiesTestContext context;
  EXPECT_CALL(context.GetMockPlatform(), GetGpuFactories())
      .Times(testing::AtMost(1));
  auto* encoding_config = CreateWebrtcEncodingConfig();
  auto* video_config = encoding_config->getVideoOr(nullptr);
  video_config->setScalabilityMode("L3T3_KEY");
  const media::mojom::blink::WebrtcPredictionFeatures kFeatures =
      CreateWebrtcFeatures(/*is_decode=*/false);

  // WebrtcPerfHistoryService should be queried for smoothness. Signal
  // smooth=true.
  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(WebrtcDbCallback(kFeatures, kFramerate, /*is_smooth=*/true));
  MediaCapabilitiesInfo* info = EncodingInfo(encoding_config, &context);
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());

  // Verify DB call was made.
  testing::Mock::VerifyAndClearExpectations(
      context.GetWebrtcPerfHistoryService());

  // Repeat with unsupported mode.
  video_config->setScalabilityMode("L3T2_Foo");
  info = EncodingInfo(encoding_config, &context);
  EXPECT_FALSE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_FALSE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcDecodePowerEfficientIsSmooth) {
  test::TaskEnvironment task_environment;
  // Set up a custom decoding info handler with a GPU factory that returns
  // supported and powerEfficient.
  MediaCapabilitiesTestContext context;
  auto mock_gpu_factories =
      std::make_unique<media::MockGpuVideoAcceleratorFactories>(nullptr);
  WebrtcDecodingInfoHandler decoding_info_handler(
      blink::CreateWebrtcVideoDecoderFactory(
          mock_gpu_factories.get(),
          Platform::Current()->GetRenderingColorSpace(), base::DoNothing()),
      blink::CreateWebrtcAudioDecoderFactory());

  context.GetMediaCapabilities()->set_webrtc_decoding_info_handler_for_test(
      &decoding_info_handler);

  EXPECT_CALL(*mock_gpu_factories, IsDecoderSupportKnown())
      .WillOnce(Return(true));
  EXPECT_CALL(*mock_gpu_factories, IsDecoderConfigSupported(_))
      .WillOnce(Return(media::GpuVideoAcceleratorFactories::Supported::kTrue));

  const auto* kDecodingConfig = CreateWebrtcDecodingConfig();
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  // Expect that powerEfficient==true implies that smooth==true without querying
  // perf history.
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcDecodeOverridePowerEfficientIsSmooth) {
  test::TaskEnvironment task_environment;
  // Override the default behavior using a field trial. Query smooth from perf
  // history regardless the value of powerEfficient.
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeaturesAndParameters(
      // Enabled features w/ parameters
      {{media::kWebrtcMediaCapabilitiesParameters,
        {{MediaCapabilities::kWebrtcDecodeSmoothIfPowerEfficientParamName,
          "false"}}}},
      // Disabled features.
      {});

  // Set up a custom decoding info handler with a GPU factory that returns
  // supported and powerEfficient.
  MediaCapabilitiesTestContext context;
  media::MockGpuVideoAcceleratorFactories mock_gpu_factories(nullptr);
  WebrtcDecodingInfoHandler decoding_info_handler(
      blink::CreateWebrtcVideoDecoderFactory(
          &mock_gpu_factories, Platform::Current()->GetRenderingColorSpace(),
          base::DoNothing()),
      blink::CreateWebrtcAudioDecoderFactory());
  context.GetMediaCapabilities()->set_webrtc_decoding_info_handler_for_test(
      &decoding_info_handler);

  EXPECT_CALL(mock_gpu_factories, IsDecoderSupportKnown())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_gpu_factories, IsDecoderConfigSupported(_))
      .WillOnce(Return(media::GpuVideoAcceleratorFactories::Supported::kTrue));

  const auto* kDecodingConfig = CreateWebrtcDecodingConfig();
  media::mojom::blink::WebrtcPredictionFeatures expected_features =
      CreateWebrtcFeatures(/*is_decode=*/true);
  expected_features.hardware_accelerated = true;

  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(
          WebrtcDbCallback(expected_features, kFramerate, /*is_smooth=*/false));
  MediaCapabilitiesInfo* info = DecodingInfo(kDecodingConfig, &context);
  // Expect powerEfficient is true but smooth returned from perf history is
  // false.
  EXPECT_TRUE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());
}

TEST(MediaCapabilitiesTests, WebrtcEncodePowerEfficientIsSmooth) {
  test::TaskEnvironment task_environment;
  // Set up a custom decoding info handler with a GPU factory that returns
  // supported and powerEfficient.
  MediaCapabilitiesTestContext context;
  media::MockGpuVideoAcceleratorFactories mock_gpu_factories(nullptr);

  auto video_encoder_factory =
      std::make_unique<RTCVideoEncoderFactory>(&mock_gpu_factories, nullptr);
  // Ensure all the profiles in our mock GPU factory are allowed.
  video_encoder_factory->clear_disabled_profiles_for_testing();

  WebrtcEncodingInfoHandler encoding_info_handler(
      std::move(video_encoder_factory),
      blink::CreateWebrtcAudioEncoderFactory());
  context.GetMediaCapabilities()->set_webrtc_encoding_info_handler_for_test(
      &encoding_info_handler);

  EXPECT_CALL(mock_gpu_factories, IsEncoderSupportKnown())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_gpu_factories, GetVideoEncodeAcceleratorSupportedProfiles())
      .WillOnce(Return(media::VideoEncodeAccelerator::SupportedProfiles{
          {media::VP9PROFILE_PROFILE0, gfx::Size(kWidth, kHeight)}}));

  const auto* kEncodingConfig = CreateWebrtcEncodingConfig();
  MediaCapabilitiesInfo* info = EncodingInfo(kEncodingConfig, &context);
  // Expect that powerEfficient==true implies that smooth==true without querying
  // perf history.
  EXPECT_TRUE(info->supported());
  EXPECT_TRUE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());

  // RTCVideoEncoderFactory destroys MojoVideoEncoderMetricsProvider on the
  // task runner of GpuVideoAcceleratorFactories.
  EXPECT_CALL(mock_gpu_factories, GetTaskRunner())
      .WillOnce(Return(base::SequencedTaskRunner::GetCurrentDefault()));
}

TEST(MediaCapabilitiesTests, WebrtcEncodeOverridePowerEfficientIsSmooth) {
  test::TaskEnvironment task_environment;
  // Override the default behavior using a field trial. Query smooth from perf
  // history regardless the value of powerEfficient.
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitWithFeaturesAndParameters(
      // Enabled features w/ parameters
      {{media::kWebrtcMediaCapabilitiesParameters,
        {{MediaCapabilities::kWebrtcEncodeSmoothIfPowerEfficientParamName,
          "false"}}}},
      // Disabled features.
      {});

  // Set up a custom decoding info handler with a GPU factory that returns
  // supported and powerEfficient.
  MediaCapabilitiesTestContext context;
  media::MockGpuVideoAcceleratorFactories mock_gpu_factories(nullptr);

  auto video_encoder_factory =
      std::make_unique<RTCVideoEncoderFactory>(&mock_gpu_factories, nullptr);
  // Ensure all the profiles in our mock GPU factory are allowed.
  video_encoder_factory->clear_disabled_profiles_for_testing();

  WebrtcEncodingInfoHandler encoding_info_handler(
      std::move(video_encoder_factory),
      blink::CreateWebrtcAudioEncoderFactory());
  context.GetMediaCapabilities()->set_webrtc_encoding_info_handler_for_test(
      &encoding_info_handler);

  EXPECT_CALL(mock_gpu_factories, IsEncoderSupportKnown())
      .WillOnce(Return(true));
  EXPECT_CALL(mock_gpu_factories, GetVideoEncodeAcceleratorSupportedProfiles())
      .WillOnce(Return(media::VideoEncodeAccelerator::SupportedProfiles{
          {media::VP9PROFILE_PROFILE0, gfx::Size(kWidth, kHeight)}}));

  const auto* kEncodingConfig = CreateWebrtcEncodingConfig();
  media::mojom::blink::WebrtcPredictionFeatures expected_features =
      CreateWebrtcFeatures(/*is_decode=*/false);
  expected_features.hardware_accelerated = true;

  EXPECT_CALL(*context.GetWebrtcPerfHistoryService(), GetPerfInfo(_, _, _))
      .WillOnce(
          WebrtcDbCallback(expected_features, kFramerate, /*is_smooth=*/false));
  MediaCapabilitiesInfo* info = EncodingInfo(kEncodingConfig, &context);
  // Expect powerEfficient is true but smooth returned from perf history is
  // false.
  EXPECT_TRUE(info->supported());
  EXPECT_FALSE(info->smooth());
  EXPECT_TRUE(info->powerEfficient());

  // RTCVideoEncoderFactory destroys MojoVideoEncoderMetricsProvider on the
  // task runner of GpuVideoAcceleratorFactories.
  EXPECT_CALL(mock_gpu_factories, GetTaskRunner())
      .WillOnce(Return(base::SequencedTaskRunner::GetCurrentDefault()));
}

}  // namespace blink