File: chrome_hid_delegate_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1223 lines) | stat: -rw-r--r-- 47,425 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/hid/chrome_hid_delegate.h"

#include <memory>
#include <string_view>

#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/test_future.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "chrome/browser/hid/hid_chooser_context.h"
#include "chrome/browser/hid/hid_chooser_context_factory.h"
#include "chrome/browser/hid/hid_connection_tracker.h"
#include "chrome/browser/hid/hid_connection_tracker_factory.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/embedded_worker_instance_test_harness.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/buildflags/buildflags.h"
#include "google_apis/gaia/gaia_id.h"
#include "services/device/public/cpp/test/fake_hid_manager.h"
#include "services/device/public/cpp/test/hid_test_util.h"
#include "services/device/public/cpp/test/test_report_descriptors.h"
#include "services/device/public/mojom/hid.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/hid/hid.mojom.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/command_line.h"
#include "base/values.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/scoped_user_manager.h"
#endif

namespace {

using ::base::test::RunClosure;
using ::base::test::TestFuture;
using ::testing::ElementsAre;
using ::testing::NiceMock;
using ::testing::UnorderedElementsAre;

constexpr std::string_view kDefaultTestUrl{"https://www.google.com"};
constexpr std::string_view kCrossOriginTestUrl{"https://www.chromium.org"};
constexpr char kTestUserEmail[] = "user@example.com";

#if BUILDFLAG(ENABLE_EXTENSIONS)
constexpr std::string_view kPrivilegedExtensionId{
    "ckcendljdlmgnhghiaomidhiiclmapok"};
constexpr std::string_view kExtensionDocumentFileName{"index.html"};
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

MATCHER_P(HasGuid, matcher, "") {
  return ExplainMatchResult(matcher, arg->guid, result_listener);
}

device::mojom::HidDeviceInfoPtr CreateFakeDevice() {
  auto device = device::CreateDeviceFromReportDescriptor(
      /*vendor_id=*/0x1234, /*product_id=*/0xabcd,
      device::TestReportDescriptors::JabraLink380c());

  // Ensure `serial_number` is unique.
  device->serial_number = device->guid;

  return device;
}

// Create a partially-initialized device.
device::mojom::HidDeviceInfoPtr CreateIncompleteFakeDevice() {
  auto device = CreateFakeDevice();
  EXPECT_GT(device->collections.size(), 1u);
  device->collections.pop_back();
  return device;
}

device::mojom::HidDeviceInfoPtr CreateFakeFidoDevice() {
  return device::CreateDeviceFromReportDescriptor(
      /*vendor_id=*/0x1234,
      /*product_id=*/0xabcd, device::TestReportDescriptors::FidoU2fHid());
}

// A mock HidManagerClient implementation that can be used to listen for HID
// device connection events.
class MockHidManagerClient : public HidChooserContext::HidManagerClient {
 public:
  MockHidManagerClient() = default;
  MockHidManagerClient(const MockHidManagerClient&) = delete;
  MockHidManagerClient& operator=(const MockHidManagerClient&) = delete;
  ~MockHidManagerClient() override = default;

  mojo::PendingAssociatedRemote<HidManagerClient> BindReceiverAndPassRemote() {
    auto client = receiver_.BindNewEndpointAndPassRemote();
    receiver_.set_disconnect_handler(base::BindOnce(
        &MockHidManagerClient::OnConnectionError, base::Unretained(this)));
    return client;
  }

  MOCK_METHOD1(DeviceAdded, void(device::mojom::HidDeviceInfoPtr));
  MOCK_METHOD1(DeviceRemoved, void(device::mojom::HidDeviceInfoPtr));
  MOCK_METHOD1(DeviceChanged, void(device::mojom::HidDeviceInfoPtr));
  MOCK_METHOD0(OnHidChooserContextShutdown, void());
  MOCK_METHOD0(ConnectionError, void());
  void OnConnectionError() {
    receiver_.reset();
    ConnectionError();
  }

 private:
  mojo::AssociatedReceiver<HidManagerClient> receiver_{this};
};

// A fake HidConnectionClient implementation.
class FakeHidConnectionClient : public device::mojom::HidConnectionClient {
 public:
  FakeHidConnectionClient() = default;
  FakeHidConnectionClient(FakeHidConnectionClient&) = delete;
  FakeHidConnectionClient& operator=(FakeHidConnectionClient&) = delete;
  ~FakeHidConnectionClient() override = default;

  void Bind(
      mojo::PendingReceiver<device::mojom::HidConnectionClient> receiver) {
    receiver_.Bind(std::move(receiver));
  }

  // mojom::HidConnectionClient:
  void OnInputReport(uint8_t report_id,
                     const std::vector<uint8_t>& buffer) override {}

 private:
  mojo::Receiver<device::mojom::HidConnectionClient> receiver_{this};
};

class MockHidConnectionTracker : public HidConnectionTracker {
 public:
  explicit MockHidConnectionTracker(Profile* profile)
      : HidConnectionTracker(profile) {}
  ~MockHidConnectionTracker() override = default;

  MOCK_METHOD(void, IncrementConnectionCount, (const url::Origin&), (override));
  MOCK_METHOD(void, DecrementConnectionCount, (const url::Origin&), (override));
};

class ChromeHidTestHelper {
 public:
  void SimulateDeviceServiceCrash() {
    hid_manager_->SimulateConnectionError();
    hid_manager_.reset();
    base::RunLoop().RunUntilIdle();

    // Re-bind a new fake HidManager so tests don't create a real one.
    BindHidManager();
  }

  void AddDevice(const device::mojom::HidDeviceInfoPtr& device) {
    hid_manager_->AddDevice(device.Clone());
  }

  void ChangeDevice(const device::mojom::HidDeviceInfoPtr& device) {
    hid_manager_->ChangeDevice(device.Clone());
  }

  void RemoveDevice(const device::mojom::HidDeviceInfoPtr& device) {
    hid_manager_->RemoveDevice(device->guid);
  }

  HidChooserContext* GetChooserContext() {
    return HidChooserContextFactory::GetForProfile(profile_);
  }

  virtual void ConnectToService(
      mojo::PendingReceiver<blink::mojom::HidService> receiver) = 0;

  void BindHidManager() {
    EXPECT_FALSE(hid_manager_);
    hid_manager_ = std::make_unique<device::FakeHidManager>();
    mojo::PendingRemote<device::mojom::HidManager> pending_remote;
    hid_manager_->Bind(pending_remote.InitWithNewPipeAndPassReceiver());
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    GetChooserContext()->SetHidManagerForTesting(std::move(pending_remote),
                                                 devices_future.GetCallback());
    EXPECT_TRUE(devices_future.Wait());
  }

#if BUILDFLAG(IS_CHROMEOS)
  const user_manager::User* SetUpUserManager() {
    // On ChromeOS a user account is needed in order to check whether the user
    // account is affiliated with the device owner for the purposes of applying
    // enterprise policy.
    constexpr GaiaId::Literal kTestUserGaiaId("1111111111");
    auto fake_user_manager = std::make_unique<ash::FakeChromeUserManager>();
    auto* fake_user_manager_ptr = fake_user_manager.get();
    scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
        std::move(fake_user_manager));

    auto account_id =
        AccountId::FromUserEmailGaiaId(kTestUserEmail, kTestUserGaiaId);
    const user_manager::User* user = fake_user_manager_ptr->AddUser(account_id);

    fake_user_manager_ptr->LoginUser(account_id);

    return user;
  }

  void TearDownUserManager() { scoped_user_manager_.reset(); }
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(ENABLE_EXTENSIONS)
  // Creates a fake extension with the specified `extension_id` so that it can
  // exercise behaviors that are only enabled for privileged extensions.
  scoped_refptr<const extensions::Extension> CreateExtensionWithId(
      std::string_view extension_id) {
    auto manifest =
        base::Value::Dict()
            .Set("name", "Fake extension")
            .Set("description", "For testing.")
            .Set("version", "0.1")
            .Set("manifest_version", 2)
            .Set("web_accessible_resources",
                 base::Value::List().Append(kExtensionDocumentFileName));
    scoped_refptr<const extensions::Extension> extension =
        extensions::ExtensionBuilder()
            .SetManifest(std::move(manifest))
            .SetID(std::string(extension_id))
            .Build();
    if (!extension) {
      return nullptr;
    }
    extensions::TestExtensionSystem* extension_system =
        static_cast<extensions::TestExtensionSystem*>(
            extensions::ExtensionSystem::Get(profile_));
    extension_system->CreateExtensionService(
        base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
    extensions::ExtensionRegistrar::Get(profile_)->AddExtension(extension);
    return extension;
  }
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

  void SetUpWebPageOriginUrl() { origin_url_ = GURL(kDefaultTestUrl); }

  void SetUpExtensionOriginUrl() {
    extension_ = CreateExtensionWithId(kPrivilegedExtensionId);
    origin_url_ = extension_->origin().GetURL();
  }

  virtual void SetUpOriginUrl() = 0;

  BrowserContextKeyedServiceFactory::TestingFactory
  GetHidConnectionTrackerTestingFactory() {
    return base::BindRepeating([](content::BrowserContext* browser_context) {
      return static_cast<std::unique_ptr<KeyedService>>(
          std::make_unique<testing::NiceMock<MockHidConnectionTracker>>(
              Profile::FromBrowserContext(browser_context)));
    });
  }

  void SetUpHidConnectionTracker() {
    // Even MockHidConnectionTracker can be lazily created in ChromeHidDelegate,
    // we intentionally create it ahead of time so that we can test EXPECT_CALL
    // for invoking mock method for the first time.
    hid_connection_tracker_ = static_cast<MockHidConnectionTracker*>(
        HidConnectionTrackerFactory::GetForProfile(profile_, /*create=*/true));
  }

  MockHidConnectionTracker& hid_connection_tracker() {
    return *hid_connection_tracker_;
  }

  MockHidManagerClient& hid_manager_client() { return hid_manager_client_; }

  void TestHidServiceNotConnected() {
    base::RunLoop run_loop;
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());
    hid_service.set_disconnect_handler(run_loop.QuitClosure());
    run_loop.Run();
    EXPECT_FALSE(hid_service.is_connected());
  }

  void TestAddChangeRemoveDevice() {
    const auto origin = url::Origin::Create(origin_url_);
    // Connect a device with one of its collections missing.
    auto incomplete_device = CreateIncompleteFakeDevice();
    AddDevice(incomplete_device);

    // Grant permission to access `incomplete_device` from `origin`.
    GetChooserContext()->GrantDevicePermission(origin, *incomplete_device);

    // Create the HidService and register a mock client to receive
    // notifications on device connections and disconnections.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());
    hid_service->RegisterClient(
        hid_manager_client().BindReceiverAndPassRemote());

    // Call GetDevices to ensure the service is started and the client is set.
    {
      TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
      hid_service->GetDevices(devices_future.GetCallback());
      ASSERT_THAT(devices_future.Get(),
                  ElementsAre(HasGuid(incomplete_device->guid)));
      EXPECT_EQ(devices_future.Get().front()->collections.size(),
                incomplete_device->collections.size());
    }

    // Update the device with the missing collection.
    auto complete_device = CreateFakeDevice();
    complete_device->guid = incomplete_device->guid;
    complete_device->serial_number = incomplete_device->serial_number;
    TestFuture<device::mojom::HidDeviceInfoPtr> device_changed_future;
    EXPECT_CALL(hid_manager_client(), DeviceChanged).WillOnce([&](auto d) {
      device_changed_future.SetValue(std::move(d));
    });
    ChangeDevice(complete_device);
    EXPECT_EQ(device_changed_future.Get()->guid, complete_device->guid);

    // Call GetDevices and make sure there is still only one device with the
    // same `guid` but the complete device info is returned.
    {
      TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
      hid_service->GetDevices(devices_future.GetCallback());
      ASSERT_THAT(devices_future.Get(),
                  ElementsAre(HasGuid(incomplete_device->guid)));
      EXPECT_EQ(devices_future.Get().front()->collections.size(),
                complete_device->collections.size());
    }

    // Disconnect the devices. The mock client should be notified.
    TestFuture<device::mojom::HidDeviceInfoPtr> device_removed_future;
    EXPECT_CALL(hid_manager_client(), DeviceRemoved).WillOnce([&](auto d) {
      device_removed_future.SetValue(std::move(d));
    });
    RemoveDevice(incomplete_device);
    EXPECT_EQ(device_removed_future.Get()->guid, incomplete_device->guid);

    // Reconnect the device. The mock client should be notified.
    TestFuture<device::mojom::HidDeviceInfoPtr> device_added_future;
    EXPECT_CALL(hid_manager_client(), DeviceAdded).WillOnce([&](auto d) {
      device_added_future.SetValue(std::move(d));
    });
    AddDevice(complete_device);
    EXPECT_EQ(device_added_future.Get()->guid, incomplete_device->guid);
  }

  void TestNoPermissionDevice() {
    const auto origin = url::Origin::Create(origin_url_);

    // Connect two devices.
    auto allowed_device1 = CreateFakeDevice();
    AddDevice(allowed_device1);
    auto other_device1 = CreateFakeDevice();
    AddDevice(other_device1);

    // Grant permission to access `allowed_device1` from `origin`.
    GetChooserContext()->GrantDevicePermission(origin, *allowed_device1);

    // Create the HidService and register a mock client to receive
    // notifications on device connections and disconnections.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());
    hid_service->RegisterClient(
        hid_manager_client().BindReceiverAndPassRemote());

    // Call GetDevices to ensure the service is started and the client is set.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_THAT(devices_future.Get(),
                ElementsAre(HasGuid(allowed_device1->guid)));

    // Connect two more devices.
    auto allowed_device2 = CreateFakeDevice();
    AddDevice(allowed_device2);
    auto other_device2 = CreateFakeDevice();
    AddDevice(other_device2);

    // Grant permission to access `allowed_device2` from `origin`.
    GetChooserContext()->GrantDevicePermission(origin, *allowed_device2);

    // Disconnect all four devices. The mock client should be notified only
    // for the devices it has permission to access.
    TestFuture<device::mojom::HidDeviceInfoPtr> device_removed_future;
    EXPECT_CALL(hid_manager_client(), DeviceRemoved)
        .Times(2)
        .WillRepeatedly(
            [&](auto d) { device_removed_future.SetValue(std::move(d)); });
    RemoveDevice(allowed_device1);
    RemoveDevice(allowed_device2);
    RemoveDevice(other_device1);
    RemoveDevice(other_device2);
    EXPECT_EQ(device_removed_future.Take()->guid, allowed_device1->guid);
    EXPECT_EQ(device_removed_future.Take()->guid, allowed_device2->guid);

    // Reconnect all four devices. The mock client should be notified only for
    // the devices it has permission to access.
    TestFuture<device::mojom::HidDeviceInfoPtr> device_added_future;
    EXPECT_CALL(hid_manager_client(), DeviceAdded)
        .Times(2)
        .WillRepeatedly(
            [&](auto d) { device_added_future.SetValue(std::move(d)); });
    AddDevice(allowed_device1);
    AddDevice(allowed_device2);
    AddDevice(other_device1);
    AddDevice(other_device2);
    EXPECT_EQ(device_added_future.Take()->guid, allowed_device1->guid);
    EXPECT_EQ(device_added_future.Take()->guid, allowed_device2->guid);
  }

  void TestReconnectHidService() {
    const auto origin = url::Origin::Create(origin_url_);

    // Connect two devices. Configure `ephemeral_device` with no serial number
    // so it is not eligible for persistent permissions.
    auto device = CreateFakeDevice();
    auto ephemeral_device = CreateFakeDevice();
    ephemeral_device->serial_number = "";
    AddDevice(device);
    AddDevice(ephemeral_device);

    // Grant permission for `origin` to access both devices. `device` is
    // eligible for persistent permissions and `ephemeral_device` is only
    // eligible for ephemeral permissions.
    GetChooserContext()->GrantDevicePermission(origin, *device);
    GetChooserContext()->GrantDevicePermission(origin, *ephemeral_device);

    // Create the HidService and register a mock client to receive
    // notifications on device connections and disconnections. Call `GetDevices`
    // to ensure the service is started and the client is set.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());
    hid_service->RegisterClient(
        hid_manager_client().BindReceiverAndPassRemote());
    {
      TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
      hid_service->GetDevices(devices_future.GetCallback());
      EXPECT_THAT(devices_future.Get(),
                  UnorderedElementsAre(HasGuid(device->guid),
                                       HasGuid(ephemeral_device->guid)));
    }

    // Both permissions are granted.
    EXPECT_TRUE(GetChooserContext()->HasDevicePermission(origin, *device));
    EXPECT_TRUE(
        GetChooserContext()->HasDevicePermission(origin, *ephemeral_device));

    // Simulate a device service crash.
    base::RunLoop loop;
    EXPECT_CALL(hid_manager_client(), ConnectionError).WillOnce([&]() {
      loop.Quit();
    });
    SimulateDeviceServiceCrash();
    loop.Run();

    // The ephemeral permission is revoked.
    EXPECT_TRUE(GetChooserContext()->HasDevicePermission(origin, *device));
    EXPECT_FALSE(
        GetChooserContext()->HasDevicePermission(origin, *ephemeral_device));

    // Add a new device eligible for persistent permissions.
    auto another_device = CreateFakeDevice();
    AddDevice(another_device);
    EXPECT_CALL(hid_manager_client(), DeviceAdded).Times(0);
    base::RunLoop().RunUntilIdle();

    // Grant the device permission while the service is off.
    GetChooserContext()->GrantDevicePermission(origin, *another_device);

    // `mock_client` is not notified when `device` is removed because the
    // service is off.
    RemoveDevice(device);
    EXPECT_CALL(hid_manager_client(), DeviceRemoved).Times(0);
    base::RunLoop().RunUntilIdle();

    // Reconnect the service.
    hid_service.reset();
    testing::Mock::VerifyAndClearExpectations(&hid_manager_client());
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());
    hid_service->RegisterClient(
        hid_manager_client().BindReceiverAndPassRemote());
    {
      TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
      hid_service->GetDevices(devices_future.GetCallback());
      EXPECT_THAT(devices_future.Get(),
                  ElementsAre(HasGuid(another_device->guid)));
    }

    // The persistent permissions are still granted.
    EXPECT_TRUE(GetChooserContext()->HasDevicePermission(origin, *device));
    EXPECT_TRUE(
        GetChooserContext()->HasDevicePermission(origin, *another_device));
    EXPECT_FALSE(
        GetChooserContext()->HasDevicePermission(origin, *ephemeral_device));
  }

  void TestRevokeDevicePermission() {
    const auto origin = url::Origin::Create(origin_url_);

    // Connect a device.
    auto device = CreateFakeDevice();
    AddDevice(device);

    // Create the `HidService`.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());

    // Call GetDevices to ensure the service is started.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_TRUE(devices_future.Get().empty());

    // Grant permission to access the connected device.
    GetChooserContext()->GrantDevicePermission(origin, *device);
    auto objects = GetChooserContext()->GetGrantedObjects(origin);
    ASSERT_EQ(1u, objects.size());

    // Open a connection to `device`.
    FakeHidConnectionClient connection_client;
    mojo::PendingRemote<device::mojom::HidConnectionClient>
        hid_connection_client;
    connection_client.Bind(
        hid_connection_client.InitWithNewPipeAndPassReceiver());
    TestFuture<mojo::PendingRemote<device::mojom::HidConnection>>
        pending_remote_future;
    if (supports_hid_connection_tracker_) {
      EXPECT_CALL(hid_connection_tracker(), IncrementConnectionCount(origin));
    }
    hid_service->Connect(device->guid, std::move(hid_connection_client),
                         pending_remote_future.GetCallback());
    mojo::Remote<device::mojom::HidConnection> connection;
    connection.Bind(pending_remote_future.Take());
    ASSERT_TRUE(connection);

    // Revoke the permission. The device should be disconnected.
    base::RunLoop disconnect_loop;
    connection.set_disconnect_handler(disconnect_loop.QuitClosure());

    base::RunLoop decrement_connection_count_loop;
    if (supports_hid_connection_tracker_) {
      EXPECT_CALL(hid_connection_tracker(), DecrementConnectionCount(origin))
          .WillOnce(RunClosure(decrement_connection_count_loop.QuitClosure()));
    }

    GetChooserContext()->RevokeDevicePermission(origin, *device);
    disconnect_loop.Run();
    if (supports_hid_connection_tracker_) {
      decrement_connection_count_loop.Run();
    }
  }

  void TestRevokeDevicePermissionEphemeral() {
    const auto origin = url::Origin::Create(origin_url_);

    // Connect a device. Configure it with no serial number so it is not
    // eligible for persistent permissions.
    auto device = CreateFakeDevice();
    device->serial_number = "";
    AddDevice(device);

    // Create the `HidService`.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());

    // Call GetDevices to ensure the service is started.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_TRUE(devices_future.Get().empty());

    // Grant permission to access the connected device.
    GetChooserContext()->GrantDevicePermission(origin, *device);
    auto objects = GetChooserContext()->GetGrantedObjects(origin);
    ASSERT_EQ(1u, objects.size());

    // Open a connection to `device`.
    FakeHidConnectionClient connection_client;
    mojo::PendingRemote<device::mojom::HidConnectionClient>
        hid_connection_client;
    connection_client.Bind(
        hid_connection_client.InitWithNewPipeAndPassReceiver());
    TestFuture<mojo::PendingRemote<device::mojom::HidConnection>>
        pending_remote_future;
    if (supports_hid_connection_tracker_) {
      EXPECT_CALL(hid_connection_tracker(), IncrementConnectionCount(origin));
    }
    hid_service->Connect(device->guid, std::move(hid_connection_client),
                         pending_remote_future.GetCallback());
    mojo::Remote<device::mojom::HidConnection> connection;
    connection.Bind(pending_remote_future.Take());
    ASSERT_TRUE(connection);

    // Revoke the permission. The device should be disconnected.
    base::RunLoop disconnect_loop;
    connection.set_disconnect_handler(disconnect_loop.QuitClosure());

    base::RunLoop decrement_connection_count_loop;
    if (supports_hid_connection_tracker_) {
      EXPECT_CALL(hid_connection_tracker(), DecrementConnectionCount(origin))
          .WillOnce(RunClosure(decrement_connection_count_loop.QuitClosure()));
    }

    GetChooserContext()->RevokeDevicePermission(origin, *device);
    disconnect_loop.Run();
    if (supports_hid_connection_tracker_) {
      decrement_connection_count_loop.Run();
    }
  }

  void TestConnectAndDisconnect(content::WebContents* web_contents) {
    const auto origin = url::Origin::Create(origin_url_);

    // Create the `HidService`.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());

    // Connect a device.
    auto device = CreateFakeDevice();
    AddDevice(device);

    // Grant permission to access `device` from `origin`.
    GetChooserContext()->GrantDevicePermission(origin, *device);

    // Call `GetDevices` and expect the device to be returned.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_THAT(devices_future.Take(), ElementsAre(HasGuid(device->guid)));

    if (web_contents) {
      // The `WebContents` should not indicate we are connected to a device.
      EXPECT_FALSE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }

    // Open a connection to `device`.
    FakeHidConnectionClient connection_client;
    mojo::PendingRemote<device::mojom::HidConnectionClient>
        hid_connection_client;
    connection_client.Bind(
        hid_connection_client.InitWithNewPipeAndPassReceiver());
    TestFuture<mojo::PendingRemote<device::mojom::HidConnection>>
        pending_remote_future;
    if (supports_hid_connection_tracker_) {
      EXPECT_CALL(hid_connection_tracker(), IncrementConnectionCount(origin));
    }
    hid_service->Connect(device->guid, std::move(hid_connection_client),
                         pending_remote_future.GetCallback());
    mojo::Remote<device::mojom::HidConnection> connection;
    connection.Bind(pending_remote_future.Take());
    ASSERT_TRUE(connection);

    if (web_contents) {
      // Now the `WebContents` should indicate we are connected to a device.
      EXPECT_TRUE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }

    // Close `connection` and check that the `WebContents` no longer indicates
    // we are connected.
    // If the scenario supports HidConnectionTracker, we can avoid using
    // RunUntilIdle() by using HidConnectionTracker::DecrementConnectionCount()
    // as it will be called in the disconnect path.
    if (supports_hid_connection_tracker_) {
      base::RunLoop decrement_connection_count_loop;
      EXPECT_CALL(hid_connection_tracker(), DecrementConnectionCount(origin))
          .WillOnce(RunClosure(decrement_connection_count_loop.QuitClosure()));
      connection.reset();
      decrement_connection_count_loop.Run();
    } else {
      connection.reset();
      base::RunLoop().RunUntilIdle();
    }

    if (web_contents) {
      EXPECT_FALSE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }
  }

  void TestConnectAndRemove(content::WebContents* web_contents) {
    const auto origin = url::Origin::Create(origin_url_);

    // Create the `HidService`.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());
    hid_service->RegisterClient(
        hid_manager_client().BindReceiverAndPassRemote());

    // Connect a device.
    auto device = CreateFakeDevice();
    AddDevice(device);

    // Grant permission to access `device` from `origin`.
    GetChooserContext()->GrantDevicePermission(origin, *device);

    // Call `GetDevices` and expect the device to be returned.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_THAT(devices_future.Take(), ElementsAre(HasGuid(device->guid)));

    if (web_contents) {
      // The `WebContents` should not indicate we are connected to a device.
      EXPECT_FALSE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }

    // Open a connection to `device`.
    FakeHidConnectionClient connection_client;
    mojo::PendingRemote<device::mojom::HidConnectionClient>
        hid_connection_client;
    connection_client.Bind(
        hid_connection_client.InitWithNewPipeAndPassReceiver());
    TestFuture<mojo::PendingRemote<device::mojom::HidConnection>>
        pending_remote_future;
    if (supports_hid_connection_tracker_) {
      EXPECT_CALL(hid_connection_tracker(), IncrementConnectionCount(origin));
    }
    hid_service->Connect(device->guid, std::move(hid_connection_client),
                         pending_remote_future.GetCallback());
    mojo::Remote<device::mojom::HidConnection> connection;
    connection.Bind(pending_remote_future.Take());
    ASSERT_TRUE(connection);

    if (web_contents) {
      // Now the `WebContents` should indicate we are connected to a device.
      EXPECT_TRUE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }

    // Remove `device` and check that the `WebContents` no longer indicates we
    // are connected.
    // If the scenario supports HidConnectionTracker, we can avoid using
    // RunUntilIdle() by using HidConnectionTracker::DecrementConnectionCount()
    // as it will be called in the remove device path.
    if (supports_hid_connection_tracker_) {
      base::RunLoop decrement_connection_count_loop;
      EXPECT_CALL(hid_connection_tracker(), DecrementConnectionCount(origin))
          .WillOnce(RunClosure(decrement_connection_count_loop.QuitClosure()));
      RemoveDevice(device);
      decrement_connection_count_loop.Run();
    } else {
      RemoveDevice(device);
      base::RunLoop().RunUntilIdle();
    }

    if (web_contents) {
      EXPECT_FALSE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }
  }

#if BUILDFLAG(ENABLE_EXTENSIONS)
  void TestFidoDeviceAllowedWithPrivilegedOrigin(
      content::WebContents* web_contents) {
    auto origin = url::Origin::Create(origin_url_);
    // Connect a FIDO device.
    auto device = CreateFakeFidoDevice();
    AddDevice(device);

    // Grant permission to access `device` from `privileged_origin`.
    GetChooserContext()->GrantDevicePermission(origin, *device);

    // Create the `HidService`.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());

    // Call `GetDevices` and expect the device to be returned.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_THAT(devices_future.Take(), ElementsAre(HasGuid(device->guid)));

    if (web_contents) {
      // The `WebContents` should not indicate we are connected to a device.
      EXPECT_FALSE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }

    // Open a connection to `device`.
    FakeHidConnectionClient connection_client;
    mojo::PendingRemote<device::mojom::HidConnectionClient>
        hid_connection_client;
    connection_client.Bind(
        hid_connection_client.InitWithNewPipeAndPassReceiver());
    TestFuture<mojo::PendingRemote<device::mojom::HidConnection>>
        pending_remote_future;
    hid_service->Connect(device->guid, std::move(hid_connection_client),
                         pending_remote_future.GetCallback());
    mojo::Remote<device::mojom::HidConnection> connection;
    connection.Bind(pending_remote_future.Take());
    ASSERT_TRUE(connection);

    if (web_contents) {
      // Now the `WebContents` should indicate we are connected to a device.
      EXPECT_TRUE(web_contents->IsCapabilityActive(
          content::WebContentsCapabilityType::kHID));
    }
  }
#endif

  void TestConnectionTrackerOpenDeviceNoConnectionCountUpdate() {
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());
    auto origin = url::Origin::Create(origin_url_);
    auto device = CreateFakeDevice();
    AddDevice(device);
    GetChooserContext()->GrantDevicePermission(origin, *device);

    // Call |GetDevices| and expect the device to be returned.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_THAT(devices_future.Take(), ElementsAre(HasGuid(device->guid)));

    EXPECT_CALL(hid_connection_tracker(), IncrementConnectionCount(origin))
        .Times(0);
    // Open a connection to `device`.
    FakeHidConnectionClient connection_client;
    mojo::PendingRemote<device::mojom::HidConnectionClient>
        hid_connection_client;
    connection_client.Bind(
        hid_connection_client.InitWithNewPipeAndPassReceiver());
    TestFuture<mojo::PendingRemote<device::mojom::HidConnection>>
        pending_remote_future;
    hid_service->Connect(device->guid, std::move(hid_connection_client),
                         pending_remote_future.GetCallback());
    mojo::Remote<device::mojom::HidConnection> connection;
    connection.Bind(pending_remote_future.Take());
    ASSERT_TRUE(connection);
  }

 protected:
  raw_ptr<TestingProfile, DanglingUntriaged> profile_ = nullptr;
  GURL origin_url_;
  raw_ptr<MockHidConnectionTracker, DanglingUntriaged> hid_connection_tracker_ =
      nullptr;
  // This flag is expected to be set to true only for the scenario of extension
  // origin and kEnableWebHidOnExtensionServiceWorker enabled.
  bool supports_hid_connection_tracker_ = false;

 private:
  std::unique_ptr<device::FakeHidManager> hid_manager_;
#if BUILDFLAG(IS_CHROMEOS)
  std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;
#endif
  scoped_refptr<const extensions::Extension> extension_;
  MockHidManagerClient hid_manager_client_;
};

class ChromeHidDelegateRenderFrameTestBase
    : public ChromeRenderViewHostTestHarness,
      public ChromeHidTestHelper {
 public:
  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();
#if BUILDFLAG(IS_CHROMEOS)
    const user_manager::User* user = SetUpUserManager();
    TestingProfile::Builder builder;
    testing_profile_ = builder.Build();
    profile_ = testing_profile_.get();
    ash::ProfileHelper::Get()->SetUserToProfileMappingForTesting(
        user, profile_.get());
#else
    profile_manager_ = std::make_unique<TestingProfileManager>(
        TestingBrowserProcess::GetGlobal());
    ASSERT_TRUE(profile_manager_->SetUp());
    // TODO(crbug.com/40249783): Pass testing factory when creating profile.
    // Ideally, we should be able to pass testing factory when calling profile
    // manager's CreateTestingProfile. However, due to the fact that:
    // 1) TestingProfile::TestingProfile(...) will call BrowserContextShutdown
    // as part of setting testing factory.
    // 2) HidConnectionTrackerFactory::BrowserContextShutdown() at some point
    // need valid profile_metrics::GetBrowserProfileType() as part of
    // HidConnectionTrackerFactory::GetForProfile().
    // It will hit failure in profile_metrics::GetBrowserProfileType() because
    // the profile is not initialized properly before setting testing factory.
    // As a result, here create a profile then call SetTestingFactory to inject
    // MockHidConnectionTracker.
    profile_ = profile_manager_->CreateTestingProfile(kTestUserEmail);
#endif
    HidConnectionTrackerFactory::GetInstance()->SetTestingFactory(
        profile_, GetHidConnectionTrackerTestingFactory());

    ASSERT_TRUE(profile_);
    SetUpHidConnectionTracker();
    // Create a new web contents for `profile_`.
    SetContents(
        content::WebContentsTester::CreateTestWebContents(profile_, nullptr));
    BindHidManager();
    SetUpOriginUrl();
    NavigateAndCommit(origin_url_);
  }

  void TearDown() override {
    DeleteContents();
#if BUILDFLAG(IS_CHROMEOS)
    testing_profile_.reset();
    TearDownUserManager();
#else
    profile_manager_->DeleteAllTestingProfiles();
    profile_manager_.reset();
#endif
    profile_ = nullptr;
    ChromeRenderViewHostTestHarness::TearDown();
    hid_connection_tracker_ = nullptr;
  }

  // ChromeHidTestHelper
  void ConnectToService(
      mojo::PendingReceiver<blink::mojom::HidService> receiver) override {
    content::RenderFrameHostTester::For(main_rfh())
        ->CreateHidServiceForTesting(std::move(receiver));
  }

  void TestConnectAndNavigateCrossDocument(content::WebContents* web_contents) {
    // The test assumes the previous page gets deleted after navigation,
    // disconnecting the device. Disable back/forward cache to ensure that it
    // doesn't get preserved in the cache.
    // TODO(crbug.com/40232335): Integrate WebHID with bfcache and remove this.
    content::DisableBackForwardCacheForTesting(
        web_contents, content::BackForwardCache::TEST_REQUIRES_NO_CACHING);

    const auto origin = url::Origin::Create(origin_url_);

    // Create the `HidService`.
    mojo::Remote<blink::mojom::HidService> hid_service;
    ConnectToService(hid_service.BindNewPipeAndPassReceiver());

    // Connect a device.
    auto device = CreateFakeDevice();
    AddDevice(device);

    // Grant permission to access `device` from `origin`.
    GetChooserContext()->GrantDevicePermission(origin, *device);

    // Call `GetDevices` and expect the device to be returned.
    TestFuture<std::vector<device::mojom::HidDeviceInfoPtr>> devices_future;
    hid_service->GetDevices(devices_future.GetCallback());
    EXPECT_THAT(devices_future.Take(), ElementsAre(HasGuid(device->guid)));

    // The `WebContents` should not indicate we are connected to a device.
    EXPECT_FALSE(web_contents->IsCapabilityActive(
        content::WebContentsCapabilityType::kHID));

    // Open a connection to `device`.
    FakeHidConnectionClient connection_client;
    mojo::PendingRemote<device::mojom::HidConnectionClient>
        hid_connection_client;
    connection_client.Bind(
        hid_connection_client.InitWithNewPipeAndPassReceiver());
    TestFuture<mojo::PendingRemote<device::mojom::HidConnection>>
        pending_remote_future;
    hid_service->Connect(device->guid, std::move(hid_connection_client),
                         pending_remote_future.GetCallback());
    mojo::Remote<device::mojom::HidConnection> connection;
    connection.Bind(pending_remote_future.Take());
    ASSERT_TRUE(connection);

    // Now the `WebContents` should indicate we are connected to a device.
    EXPECT_TRUE(web_contents->IsCapabilityActive(
        content::WebContentsCapabilityType::kHID));

    // Perform a cross-document navigation. The `WebContents` should no longer
    // indicate we are connected.
    NavigateAndCommit(GURL(kCrossOriginTestUrl));
    base::RunLoop().RunUntilIdle();

    EXPECT_FALSE(web_contents->IsCapabilityActive(
        content::WebContentsCapabilityType::kHID));
  }

 private:
#if BUILDFLAG(IS_CHROMEOS)
  std::unique_ptr<TestingProfile> testing_profile_;
#else
  std::unique_ptr<TestingProfileManager> profile_manager_;
#endif
};

class ChromeHidDelegateRenderFrameTest
    : public ChromeHidDelegateRenderFrameTestBase {
  // ChromeHidTestHelper
  void SetUpOriginUrl() override { SetUpWebPageOriginUrl(); }
};

class ChromeHidDelegateServiceWorkerTestBase
    : public content::EmbeddedWorkerInstanceTestHarness,
      public ChromeHidTestHelper {
 public:
  void SetUp() override {
    content::EmbeddedWorkerInstanceTestHarness::SetUp();
#if BUILDFLAG(IS_CHROMEOS)
    SetUpUserManager();
#endif
    SetUpHidConnectionTracker();
    BindHidManager();
    SetUpOriginUrl();
    StartWorker();
  }

  void TearDown() override {
    StopWorker();
#if BUILDFLAG(IS_CHROMEOS)
    TearDownUserManager();
#endif
    content::EmbeddedWorkerInstanceTestHarness::TearDown();
  }

  // ChromeHidTestHelper
  void ConnectToService(
      mojo::PendingReceiver<blink::mojom::HidService> receiver) override {
    BindHidServiceToWorker(origin_url_, std::move(receiver));
  }

  void StartWorker() {
    auto worker_url =
        GURL(base::StringPrintf("%s/worker.js", origin_url_.spec().c_str()));
    CreateAndStartWorker(origin_url_, worker_url);

    // Wait until tasks triggered by ServiceWorkerHidDelegateObserver settle.
    base::RunLoop().RunUntilIdle();
  }

  void StopWorker() { StopAndResetWorker(); }

  // content::EmbeddedWorkerInstanceTestHarness
  std::unique_ptr<content::BrowserContext> CreateBrowserContext() override {
    auto builder = TestingProfile::Builder();
    auto testing_profile = builder.Build();
    profile_ = testing_profile.get();
    // TODO(crbug.com/40249783): Pass testing factory when creating profile.
    // Ideally, we should use TestingProfile::Builder::AddTestingFactory to
    // inject MockHidConnectionTracker. However, due to the fact that:
    // 1) TestingProfile::TestingProfile(...) will call BrowserContextShutdown
    //    as part of setting testing factory.
    // 2) HidConnectionTrackerFactory::BrowserContextShutdown() at some point
    //    need valid profile_metrics::GetBrowserProfileType() as part of
    //    HidConnectionTrackerFactory::GetForProfile().
    // It will hit failure in profile_metrics::GetBrowserProfileType() due to
    // profile is not initialized properly before setting testing factory. As a
    // result, here create a profile then call SetTestingFactory to inject
    // MockHidConnectionTracker.
    HidConnectionTrackerFactory::GetInstance()->SetTestingFactory(
        profile_, GetHidConnectionTrackerTestingFactory());
    return testing_profile;
  }

 private:
  ScopedTestingLocalState testing_local_state_{
      TestingBrowserProcess::GetGlobal()};
};

class ChromeHidDelegateServiceWorkerTest
    : public ChromeHidDelegateServiceWorkerTestBase {
 public:
  // ChromeHidTestHelper
  void SetUpOriginUrl() override { SetUpWebPageOriginUrl(); }
};

#if BUILDFLAG(ENABLE_EXTENSIONS)

class ChromeHidDelegateExtensionServiceWorkerTest
    : public ChromeHidDelegateServiceWorkerTestBase {
 public:
  ChromeHidDelegateExtensionServiceWorkerTest() {
    supports_hid_connection_tracker_ = true;
  }
  // ChromeHidTestHelper
  void SetUpOriginUrl() override { SetUpExtensionOriginUrl(); }
};

class ChromeHidDelegateExtensionRenderFrameTest
    : public ChromeHidDelegateRenderFrameTestBase {
 public:
  ChromeHidDelegateExtensionRenderFrameTest() {
    supports_hid_connection_tracker_ = true;
  }
  // ChromeHidTestHelper
  void SetUpOriginUrl() override { SetUpExtensionOriginUrl(); }
};
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

}  // namespace

TEST_F(ChromeHidDelegateRenderFrameTest, AddChangeRemoveDevice) {
  TestAddChangeRemoveDevice();
}

TEST_F(ChromeHidDelegateRenderFrameTest, NoPermissionDevice) {
  TestNoPermissionDevice();
}

TEST_F(ChromeHidDelegateRenderFrameTest, ReconnectHidService) {
  TestReconnectHidService();
}

TEST_F(ChromeHidDelegateRenderFrameTest, RevokeDevicePermission) {
  TestRevokeDevicePermission();
}

TEST_F(ChromeHidDelegateRenderFrameTest, RevokeDevicePermissionEphemeral) {
  TestRevokeDevicePermissionEphemeral();
  ;
}

TEST_F(ChromeHidDelegateRenderFrameTest, ConnectAndDisconnect) {
  TestConnectAndDisconnect(web_contents());
}

TEST_F(ChromeHidDelegateRenderFrameTest, ConnectAndRemove) {
  TestConnectAndRemove(web_contents());
}

TEST_F(ChromeHidDelegateRenderFrameTest, ConnectAndNavigateCrossDocument) {
  TestConnectAndNavigateCrossDocument(web_contents());
}

TEST_F(ChromeHidDelegateServiceWorkerTest, HidServiceNotConnected) {
  TestHidServiceNotConnected();
}

#if BUILDFLAG(ENABLE_EXTENSIONS)
TEST_F(ChromeHidDelegateExtensionRenderFrameTest,
       FidoDeviceAllowedWithPrivilegedOrigin) {
  TestFidoDeviceAllowedWithPrivilegedOrigin(web_contents());
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest, AddChangeRemoveDevice) {
  TestAddChangeRemoveDevice();
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest, NoPermissionDevice) {
  TestNoPermissionDevice();
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest, ReconnectHidService) {
  TestReconnectHidService();
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest, RevokeDevicePermission) {
  TestRevokeDevicePermission();
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest,
       RevokeDevicePermissionEphemeral) {
  TestRevokeDevicePermissionEphemeral();
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest, ConnectAndDisconnect) {
  TestConnectAndDisconnect(web_contents());
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest, ConnectAndRemove) {
  TestConnectAndRemove(web_contents());
}

TEST_F(ChromeHidDelegateExtensionRenderFrameTest,
       ConnectAndNavigateCrossDocument) {
  TestConnectAndNavigateCrossDocument(web_contents());
}

TEST_F(ChromeHidDelegateExtensionServiceWorkerTest, AddChangeRemoveDevice) {
  TestAddChangeRemoveDevice();
}

TEST_F(ChromeHidDelegateExtensionServiceWorkerTest, NoPermissionDevice) {
  TestNoPermissionDevice();
}

TEST_F(ChromeHidDelegateExtensionServiceWorkerTest, ReconnectHidService) {
  TestReconnectHidService();
}

TEST_F(ChromeHidDelegateExtensionServiceWorkerTest, RevokeDevicePermission) {
  TestRevokeDevicePermission();
}

TEST_F(ChromeHidDelegateExtensionServiceWorkerTest,
       RevokeDevicePermissionEphemeral) {
  TestRevokeDevicePermissionEphemeral();
}

TEST_F(ChromeHidDelegateExtensionServiceWorkerTest, ConnectAndDisconnect) {
  TestConnectAndDisconnect(/*web_contents=*/nullptr);
}

TEST_F(ChromeHidDelegateExtensionServiceWorkerTest, ConnectAndRemove) {
  TestConnectAndRemove(/*web_contents=*/nullptr);
}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

TEST(ChromeHidDelegateBrowserContextTest, BrowserContextIsNull) {
  ChromeHidDelegate chrome_hid_delegate;
  url::Origin origin = url::Origin::Create(GURL(kDefaultTestUrl));
  EXPECT_FALSE(chrome_hid_delegate.CanRequestDevicePermission(
      /*browser_context=*/nullptr, origin));
  EXPECT_FALSE(chrome_hid_delegate.HasDevicePermission(
      /*browser_context=*/nullptr, /*render_frame_host=*/nullptr, origin,
      device::mojom::HidDeviceInfo()));
  EXPECT_EQ(nullptr,
            chrome_hid_delegate.GetHidManager(/*browser_context=*/nullptr));
  EXPECT_EQ(nullptr, chrome_hid_delegate.GetDeviceInfo(
                         /*browser_context=*/nullptr,
                         base::Uuid::GenerateRandomV4().AsLowercaseString()));
  EXPECT_FALSE(chrome_hid_delegate.IsFidoAllowedForOrigin(
      /*browser_context=*/nullptr, origin));
}