File: nearby_share_contact_manager_impl_unittest.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 (696 lines) | stat: -rw-r--r-- 30,983 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager_impl.h"

#include <algorithm>
#include <memory>
#include <optional>
#include <random>
#include <set>
#include <string>

#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/nearby_sharing/client/fake_nearby_share_client.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_prefs.h"
#include "chrome/browser/nearby_sharing/contacts/fake_nearby_share_contact_downloader.h"
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_downloader.h"
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_downloader_impl.h"
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager.h"
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contacts_sorter.h"
#include "chrome/browser/nearby_sharing/local_device_data/fake_nearby_share_local_device_data_manager.h"
#include "chromeos/ash/components/nearby/common/scheduling/fake_nearby_scheduler.h"
#include "chromeos/ash/components/nearby/common/scheduling/fake_nearby_scheduler_factory.h"
#include "chromeos/ash/components/nearby/common/scheduling/nearby_scheduler_factory.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_share_settings.mojom-test-utils.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_share_settings.mojom.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/nearby/sharing/proto/rpc_resources.pb.h"

namespace {

const uint32_t kTestNumUnreachableContactsFilteredOut = 123;
const char kTestContactIdPrefix[] = "id_";
const char kTestContactEmailPrefix[] = "email_";
const char kTestContactPhonePrefix[] = "phone_";
const char kTestDefaultDeviceName[] = "Josh's Chromebook";
const char kTestProfileUserName[] = "test@google.com";
const char* kTestPersonNames[] = {"BBB BBB", "CCC CCC", "AAA AAA"};

// From nearby_share_contact_manager_impl.cc.
constexpr base::TimeDelta kContactUploadPeriod = base::Hours(24);
constexpr base::TimeDelta kContactDownloadPeriod = base::Hours(12);
constexpr base::TimeDelta kContactDownloadRpcTimeout = base::Seconds(60);

std::string GetTestContactId(size_t index) {
  return kTestContactIdPrefix + base::NumberToString(index);
}
std::string GetTestContactEmail(size_t index) {
  return kTestContactEmailPrefix + base::NumberToString(index);
}
std::string GetTestContactPhone(size_t index) {
  return kTestContactPhonePrefix + base::NumberToString(index);
}

std::set<std::string> TestContactIds(size_t num_contacts) {
  std::set<std::string> ids;
  for (size_t i = 0; i < num_contacts; ++i) {
    ids.insert(GetTestContactId(i));
  }
  return ids;
}

std::vector<nearby::sharing::proto::ContactRecord> TestContactRecordList(
    size_t num_contacts) {
  std::vector<nearby::sharing::proto::ContactRecord> contact_list;
  for (size_t i = 0; i < num_contacts; ++i) {
    nearby::sharing::proto::ContactRecord contact;
    contact.set_id(GetTestContactId(i));
    contact.set_image_url("https://www.google.com/");
    contact.set_person_name(kTestPersonNames[i % 3]);
    contact.set_is_reachable(true);
    // only one of these fields should be set...
    switch ((i % 3)) {
      case 0:
        contact.add_identifiers()->set_account_name(GetTestContactEmail(i));
        break;
      case 1:
        contact.add_identifiers()->set_phone_number(GetTestContactPhone(i));
        break;
      case 2:
        contact.add_identifiers()->set_obfuscated_gaia("4938tyah");
        break;
    }
    contact_list.push_back(contact);
  }
  return contact_list;
}

// Converts a list of ContactRecord protos, along with the allowlist, into a
// list of Contact protos. To enable self-sharing across devices, we expect the
// local device to include itself in the contact list as an allowed contact.
// Partially from nearby_share_contact_manager_impl.cc.
std::vector<nearby::sharing::proto::Contact> BuildContactListToUpload(
    const std::set<std::string>& allowed_contact_ids,
    const std::vector<nearby::sharing::proto::ContactRecord>& contact_records) {
  std::vector<nearby::sharing::proto::Contact> contacts;
  for (const auto& contact_record : contact_records) {
    bool is_selected = base::Contains(allowed_contact_ids, contact_record.id());
    for (const auto& identifier : contact_record.identifiers()) {
      nearby::sharing::proto::Contact contact;
      contact.mutable_identifier()->CopyFrom(identifier);
      contact.set_is_selected(is_selected);
      contacts.push_back(contact);
    }
  }

  // Add self to list of contacts.
  nearby::sharing::proto::Contact contact;
  contact.mutable_identifier()->set_account_name(kTestProfileUserName);
  contact.set_is_selected(true);
  contacts.push_back(contact);

  return contacts;
}

std::vector<nearby::sharing::proto::ContactRecord> MojoContactsToProto(
    const std::vector<nearby_share::mojom::ContactRecordPtr>& mojo_contacts) {
  std::vector<nearby::sharing::proto::ContactRecord> proto_contacts;
  proto_contacts.reserve(mojo_contacts.size());
  for (const nearby_share::mojom::ContactRecordPtr& mojo_contact :
       mojo_contacts) {
    nearby::sharing::proto::ContactRecord proto_contact;
    proto_contact.set_id(mojo_contact->id);
    proto_contact.set_person_name(mojo_contact->person_name);
    proto_contact.set_image_url(mojo_contact->image_url.spec());
    proto_contact.set_is_reachable(true);
    for (const auto& identifier : mojo_contact->identifiers) {
      auto* proto_identifier = proto_contact.add_identifiers();
      switch (identifier->which()) {
        case nearby_share::mojom::ContactIdentifier::Tag::kAccountName:
          proto_identifier->set_account_name(identifier->get_account_name());
          break;
        case nearby_share::mojom::ContactIdentifier::Tag::kObfuscatedGaia:
          proto_identifier->set_obfuscated_gaia(
              identifier->get_obfuscated_gaia());
          break;
        case nearby_share::mojom::ContactIdentifier::Tag::kPhoneNumber:
          proto_identifier->set_phone_number(identifier->get_phone_number());
          break;
      }
    }
    proto_contacts.push_back(proto_contact);
  }
  return proto_contacts;
}

void VerifyDownloadNotificationContacts(
    const std::set<std::string>& expected_allowed_contact_ids,
    const std::vector<nearby::sharing::proto::ContactRecord>&
        expected_unordered_contacts,
    const std::set<std::string>& notification_allowed_contact_ids,
    const std::vector<nearby::sharing::proto::ContactRecord>&
        notification_contacts) {
  EXPECT_EQ(expected_allowed_contact_ids, notification_allowed_contact_ids);
  EXPECT_EQ(expected_unordered_contacts.size(), notification_contacts.size());

  // Verify that observers receive contacts in sorted order.
  std::vector<nearby::sharing::proto::ContactRecord> expected_ordered_contacts =
      expected_unordered_contacts;
  SortNearbyShareContactRecords(&expected_ordered_contacts);
  for (size_t i = 0; i < expected_ordered_contacts.size(); ++i) {
    EXPECT_EQ(expected_ordered_contacts[i].SerializeAsString(),
              notification_contacts[i].SerializeAsString());
  }
}

class TestDownloadContactsObserver
    : public nearby_share::mojom::DownloadContactsObserver {
 public:
  void OnContactsDownloaded(
      const std::vector<std::string>& allowed_contacts,
      std::vector<nearby_share::mojom::ContactRecordPtr> contacts,
      uint32_t num_unreachable_contacts_filtered_out) override {
    allowed_contacts_ = allowed_contacts;
    contacts_ = std::move(contacts);
    num_unreachable_contacts_filtered_out_ =
        num_unreachable_contacts_filtered_out;
    on_contacts_downloaded_called_ = true;
  }

  void OnContactsDownloadFailed() override {
    on_contacts_download_failed_called_ = true;
  }

  std::vector<std::string> allowed_contacts_;
  std::vector<nearby_share::mojom::ContactRecordPtr> contacts_;
  uint32_t num_unreachable_contacts_filtered_out_;
  bool on_contacts_downloaded_called_ = false;
  bool on_contacts_download_failed_called_ = false;
  mojo::Receiver<nearby_share::mojom::DownloadContactsObserver> receiver_{this};
};

}  // namespace

class NearbyShareContactManagerImplTest
    : public ::testing::Test,
      public NearbyShareContactManager::Observer {
 protected:
  struct AllowlistChangedNotification {
    bool were_contacts_added_to_allowlist;
    bool were_contacts_removed_from_allowlist;
  };
  struct ContactsDownloadedNotification {
    std::set<std::string> allowed_contact_ids;
    std::vector<nearby::sharing::proto::ContactRecord> contacts;
    uint32_t num_unreachable_contacts_filtered_out;
  };
  struct ContactsUploadedNotification {
    bool did_contacts_change_since_last_upload;
  };

  NearbyShareContactManagerImplTest()
      : local_device_data_manager_(kTestDefaultDeviceName) {}

  ~NearbyShareContactManagerImplTest() override = default;

  void SetUp() override {
    RegisterNearbySharingPrefs(pref_service_.registry());
    ash::nearby::NearbySchedulerFactory::SetFactoryForTesting(
        &scheduler_factory_);
    NearbyShareContactDownloaderImpl::Factory::SetFactoryForTesting(
        &downloader_factory_);

    manager_ = NearbyShareContactManagerImpl::Factory::Create(
        kTestProfileUserName, &pref_service_, &http_client_factory_,
        &local_device_data_manager_);
    manager_awaiter_ =
        std::make_unique<nearby_share::mojom::ContactManagerAsyncWaiter>(
            manager_.get());
    VerifySchedulerInitialization();
    manager_->AddObserver(this);
    manager_->AddDownloadContactsObserver(
        mojo_observer_.receiver_.BindNewPipeAndPassRemote());
  }

  void TearDown() override {
    manager_awaiter_.reset();
    manager_->RemoveObserver(this);
    manager_.reset();
    ash::nearby::NearbySchedulerFactory::SetFactoryForTesting(nullptr);
    NearbyShareContactDownloaderImpl::Factory::SetFactoryForTesting(nullptr);
  }

  void DownloadContacts() {
    // Manually reset these before each download.
    mojo_observer_.on_contacts_downloaded_called_ = false;
    mojo_observer_.on_contacts_download_failed_called_ = false;

    // Verify that the scheduler is sent a request.
    size_t num_requests =
        download_and_upload_scheduler()->num_immediate_requests();
    manager_->DownloadContacts();
    EXPECT_EQ(num_requests + 1,
              download_and_upload_scheduler()->num_immediate_requests());
  }

  void SucceedDownload(
      const std::vector<nearby::sharing::proto::ContactRecord>& contacts,
      const std::set<std::string>& expected_allowed_contact_ids,
      bool expect_upload) {
    TriggerDownloadScheduler();

    size_t num_handled_results =
        download_and_upload_scheduler()->handled_results().size();
    size_t num_download_notifications =
        contacts_downloaded_notifications_.size();
    size_t num_upload_contacts_calls =
        local_device_data_manager_.upload_contacts_calls().size();

    latest_downloader()->Succeed(contacts,
                                 kTestNumUnreachableContactsFilteredOut);

    VerifyDownloadNotificationSent(
        /*initial_num_notifications=*/num_download_notifications,
        expected_allowed_contact_ids, contacts);

    // Verify that contacts start uploading if the needed.
    EXPECT_EQ(num_upload_contacts_calls + (expect_upload ? 1 : 0),
              local_device_data_manager_.upload_contacts_calls().size());

    // Verify that the success result is sent to the download/upload scheduler
    // if a subsequent upload isn't required.
    EXPECT_EQ(num_handled_results + (expect_upload ? 0 : 1),
              download_and_upload_scheduler()->handled_results().size());
    if (!expect_upload) {
      EXPECT_TRUE(download_and_upload_scheduler()->handled_results().back());
    }
  }

  void FailDownload() {
    TriggerDownloadScheduler();

    // Fail download and verify that the result is sent to the scheduler.
    size_t num_handled_results =
        download_and_upload_scheduler()->handled_results().size();
    latest_downloader()->Fail();
    EXPECT_EQ(num_handled_results + 1,
              download_and_upload_scheduler()->handled_results().size());
    EXPECT_FALSE(download_and_upload_scheduler()->handled_results().back());

    // Verify the mojo observer was called as well.
    mojo_observer_.receiver_.FlushForTesting();
    EXPECT_FALSE(mojo_observer_.on_contacts_downloaded_called_);
    EXPECT_TRUE(mojo_observer_.on_contacts_download_failed_called_);
  }

  void MakePeriodicUploadRequest() {
    periodic_upload_scheduler()->InvokeRequestCallback();
    periodic_upload_scheduler()->SetIsWaitingForResult(true);
  }

  void FinishUpload(
      bool success,
      const std::vector<nearby::sharing::proto::Contact>& expected_contacts) {
    FakeNearbyShareLocalDeviceDataManager::UploadContactsCall& call =
        local_device_data_manager_.upload_contacts_calls().back();

    // Ordering doesn't matter. Otherwise, because of internal sorting,
    // comparison would be difficult.
    ASSERT_EQ(expected_contacts.size(), call.contacts.size());
    base::flat_set<std::string> expected_contacts_set;
    base::flat_set<std::string> call_contacts_set;
    for (size_t i = 0; i < expected_contacts.size(); ++i) {
      expected_contacts_set.insert(expected_contacts[i].SerializeAsString());
      call_contacts_set.insert(call.contacts[i].SerializeAsString());
    }

    // Invoke upload callback from local device data manager.
    size_t num_upload_notifications = contacts_uploaded_notifications_.size();
    size_t num_download_and_upload_handled_results =
        download_and_upload_scheduler()->handled_results().size();
    size_t num_periodic_upload_handeled_results =
        periodic_upload_scheduler()->handled_results().size();
    std::move(call.callback).Run(success);

    // Verify upload notification was sent on success.
    EXPECT_EQ(num_upload_notifications + (success ? 1 : 0),
              contacts_uploaded_notifications_.size());
    if (success) {
      // We only expect uploads to occur if contacts have changed since the last
      // upload or is a periodic upload was requested.
      EXPECT_TRUE(contacts_uploaded_notifications_.back()
                      .did_contacts_change_since_last_upload ||
                  periodic_upload_scheduler()->IsWaitingForResult());

      if (periodic_upload_scheduler()->IsWaitingForResult()) {
        EXPECT_EQ(num_periodic_upload_handeled_results + 1,
                  periodic_upload_scheduler()->handled_results().size());
        EXPECT_TRUE(periodic_upload_scheduler()->handled_results().back());
        periodic_upload_scheduler()->SetIsWaitingForResult(false);
      } else {
        EXPECT_EQ(num_periodic_upload_handeled_results,
                  periodic_upload_scheduler()->handled_results().size());
      }
    }

    // Verify that result is sent to download/upload scheduler.
    EXPECT_EQ(num_download_and_upload_handled_results + 1,
              download_and_upload_scheduler()->handled_results().size());
    EXPECT_EQ(success,
              download_and_upload_scheduler()->handled_results().back());
  }

  void SetAllowedContacts(const std::set<std::string>& allowed_contact_ids,
                          bool expect_allowlist_changed) {
    size_t num_download_and_upload_requests =
        download_and_upload_scheduler()->num_immediate_requests();

    manager_->SetAllowedContacts(allowed_contact_ids);

    // Verify download/upload requested if allowlist changed.
    EXPECT_EQ(
        num_download_and_upload_requests + (expect_allowlist_changed ? 1 : 0),
        download_and_upload_scheduler()->num_immediate_requests());
  }

  PrefService* pref_service() { return &pref_service_; }

 private:
  // NearbyShareContactManager::Observer:
  void OnContactsDownloaded(
      const std::set<std::string>& allowed_contact_ids,
      const std::vector<nearby::sharing::proto::ContactRecord>& contacts,
      uint32_t num_unreachable_contacts_filtered_out) override {
    ContactsDownloadedNotification notification;
    notification.allowed_contact_ids = allowed_contact_ids;
    notification.contacts = contacts;
    contacts_downloaded_notifications_.push_back(notification);
  }
  void OnContactsUploaded(bool did_contacts_change_since_last_upload) override {
    ContactsUploadedNotification notification;
    notification.did_contacts_change_since_last_upload =
        did_contacts_change_since_last_upload;
    contacts_uploaded_notifications_.push_back(notification);
  }

  FakeNearbyShareContactDownloader* latest_downloader() {
    return downloader_factory_.instances().back();
  }

  ash::nearby::FakeNearbyScheduler* periodic_upload_scheduler() {
    return scheduler_factory_.pref_name_to_periodic_instance()
        .at(prefs::kNearbySharingSchedulerPeriodicContactUploadPrefName)
        .fake_scheduler;
  }

  ash::nearby::FakeNearbyScheduler* download_and_upload_scheduler() {
    return scheduler_factory_.pref_name_to_periodic_instance()
        .at(prefs::kNearbySharingSchedulerContactDownloadAndUploadPrefName)
        .fake_scheduler;
  }

  // Verify scheduler input parameters.
  void VerifySchedulerInitialization() {
    ash::nearby::FakeNearbySchedulerFactory::PeriodicInstance
        download_and_upload_scheduler_instance =
            scheduler_factory_.pref_name_to_periodic_instance().at(
                prefs::kNearbySharingSchedulerContactDownloadAndUploadPrefName);
    EXPECT_TRUE(download_and_upload_scheduler_instance.fake_scheduler);
    EXPECT_EQ(kContactDownloadPeriod,
              download_and_upload_scheduler_instance.request_period);
    EXPECT_TRUE(download_and_upload_scheduler_instance.retry_failures);
    EXPECT_TRUE(download_and_upload_scheduler_instance.require_connectivity);
    EXPECT_EQ(&pref_service_,
              download_and_upload_scheduler_instance.pref_service);

    ash::nearby::FakeNearbySchedulerFactory::PeriodicInstance
        periodic_upload_scheduler_instance =
            scheduler_factory_.pref_name_to_periodic_instance().at(
                prefs::kNearbySharingSchedulerPeriodicContactUploadPrefName);
    EXPECT_TRUE(periodic_upload_scheduler_instance.fake_scheduler);
    EXPECT_EQ(kContactUploadPeriod,
              periodic_upload_scheduler_instance.request_period);
    EXPECT_FALSE(periodic_upload_scheduler_instance.retry_failures);
    EXPECT_TRUE(periodic_upload_scheduler_instance.require_connectivity);
    EXPECT_EQ(&pref_service_, periodic_upload_scheduler_instance.pref_service);
  }

  void TriggerDownloadScheduler() {
    // Fire scheduler and verify downloader creation.
    size_t num_downloaders = downloader_factory_.instances().size();
    download_and_upload_scheduler()->InvokeRequestCallback();
    EXPECT_EQ(num_downloaders + 1, downloader_factory_.instances().size());
    EXPECT_EQ(kContactDownloadRpcTimeout, downloader_factory_.latest_timeout());
    EXPECT_EQ(&http_client_factory_,
              downloader_factory_.latest_client_factory());
    EXPECT_EQ(local_device_data_manager_.GetId(),
              latest_downloader()->device_id());
  }

  void VerifyDownloadNotificationSent(
      size_t initial_num_notifications,
      const std::set<std::string>& expected_allowed_contact_ids,
      const std::vector<nearby::sharing::proto::ContactRecord>&
          expected_unordered_contacts) {
    EXPECT_EQ(initial_num_notifications + 1,
              contacts_downloaded_notifications_.size());

    // Verify notification sent to regular (not mojo) observers.
    VerifyDownloadNotificationContacts(
        expected_allowed_contact_ids, expected_unordered_contacts,
        contacts_downloaded_notifications_.back().allowed_contact_ids,
        contacts_downloaded_notifications_.back().contacts);

    // Verify notification sent to mojo observers.
    mojo_observer_.receiver_.FlushForTesting();
    EXPECT_TRUE(mojo_observer_.on_contacts_downloaded_called_);
    EXPECT_FALSE(mojo_observer_.on_contacts_download_failed_called_);
    VerifyDownloadNotificationContacts(
        expected_allowed_contact_ids, expected_unordered_contacts,
        std::set<std::string>(mojo_observer_.allowed_contacts_.begin(),
                              mojo_observer_.allowed_contacts_.end()),
        MojoContactsToProto(mojo_observer_.contacts_));
  }

  TestDownloadContactsObserver mojo_observer_;
  std::vector<AllowlistChangedNotification> allowlist_changed_notifications_;
  std::vector<ContactsDownloadedNotification>
      contacts_downloaded_notifications_;
  std::vector<ContactsUploadedNotification> contacts_uploaded_notifications_;
  sync_preferences::TestingPrefServiceSyncable pref_service_;
  FakeNearbyShareClientFactory http_client_factory_;
  FakeNearbyShareLocalDeviceDataManager local_device_data_manager_;
  ash::nearby::FakeNearbySchedulerFactory scheduler_factory_;
  FakeNearbyShareContactDownloader::Factory downloader_factory_;
  std::unique_ptr<NearbyShareContactManager> manager_;
  std::unique_ptr<nearby_share::mojom::ContactManagerAsyncWaiter>
      manager_awaiter_;
  content::BrowserTaskEnvironment task_environment_;
};

TEST_F(NearbyShareContactManagerImplTest, SetAllowlist) {
  // Add initial allowed contacts.
  SetAllowedContacts(TestContactIds(/*num_contacts=*/3u),
                     /*expect_allowlist_changed=*/true);
  // Remove last allowed contact.
  SetAllowedContacts(TestContactIds(/*num_contacts=*/2u),
                     /*expect_allowlist_changed=*/true);
  // Add back last allowed contact.
  SetAllowedContacts(TestContactIds(/*num_contacts=*/3u),
                     /*expect_allowlist_changed=*/true);
  // Set list without any changes.
  SetAllowedContacts(TestContactIds(/*num_contacts=*/3u),
                     /*expect_allowlist_changed=*/false);
}

TEST_F(NearbyShareContactManagerImplTest, DownloadContacts_WithFirstUpload) {
  std::vector<nearby::sharing::proto::ContactRecord> contact_records =
      TestContactRecordList(/*num_contacts=*/4u);
  std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
  SetAllowedContacts(allowlist, /*expect_allowlist_changed=*/true);

  // Because contacts have never been uploaded, a subsequent upload should be
  // requested, which succeeds.
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));

  // When contacts are downloaded again, we decect that contacts have not
  // changed, so no upload should be made
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/false);
}

TEST_F(NearbyShareContactManagerImplTest,
       DownloadContacts_DetectContactListChanged) {
  std::vector<nearby::sharing::proto::ContactRecord> contact_records =
      TestContactRecordList(/*num_contacts=*/3u);
  std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
  SetAllowedContacts(allowlist, /*expect_allowlist_changed=*/true);

  // Because contacts have never been uploaded, a subsequent upload is
  // requested, which succeeds.
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));

  // When contacts are downloaded again, we decect that contacts have changed
  // since the last upload.
  contact_records = TestContactRecordList(/*num_contacts=*/4u);
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));
}

TEST_F(NearbyShareContactManagerImplTest,
       DownloadContacts_DetectAllowlistChanged) {
  std::vector<nearby::sharing::proto::ContactRecord> contact_records =
      TestContactRecordList(/*num_contacts=*/3u);
  std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
  SetAllowedContacts(allowlist, /*expect_allowlist_changed=*/true);

  // Because contacts have never been uploaded, a subsequent upload is
  // requested, which succeeds.
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));

  // When contacts are downloaded again, we decect that the allowlist has
  // changed since the last upload.
  allowlist = TestContactIds(/*num_contacts=*/1u);
  SetAllowedContacts(allowlist, /*expect_allowlist_changed=*/true);
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));
}

TEST_F(NearbyShareContactManagerImplTest,
       DownloadContacts_PeriodicUploadRequest) {
  std::vector<nearby::sharing::proto::ContactRecord> contact_records =
      TestContactRecordList(/*num_contacts=*/3u);
  std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
  SetAllowedContacts(allowlist, /*expect_allowlist_changed=*/true);

  // Because contacts have never been uploaded, a subsequent upload is
  // requested, which succeeds.
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true, /*expected_contacts=*/BuildContactListToUpload(
                   allowlist, contact_records));

  // Because device records on the server will be removed after a few days if
  // the device does not contact the server, we ensure that contacts are
  // uploaded periodically. Make that request now. Contacts will be uploaded
  // after the next contact download. It will not force a download now, however.
  MakePeriodicUploadRequest();

  // When contacts are downloaded again, we decect that contacts have not
  // changed. However, we expect an upload because a periodic request was made.
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
}

TEST_F(NearbyShareContactManagerImplTest, DownloadContacts_FailDownload) {
  DownloadContacts();
  FailDownload();
}

TEST_F(NearbyShareContactManagerImplTest, DownloadContacts_RetryFailedUpload) {
  std::vector<nearby::sharing::proto::ContactRecord> contact_records =
      TestContactRecordList(/*num_contacts=*/3u);
  std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
  SetAllowedContacts(allowlist, /*expect_allowlist_changed=*/true);

  // Because contacts have never been uploaded, a subsequent upload is
  // requested, which succeeds.
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));

  // When contacts are downloaded again, we decect that contacts have changed
  // since the last upload. Fail this upload.
  contact_records = TestContactRecordList(/*num_contacts=*/4u);
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/false,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));

  // When contacts are downloaded again, we should continue to indicate that
  // contacts have changed since the last upload, and attempt another upload.
  // (In other words, this tests that the contact-upload hash isn't updated
  // prematurely.)
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));
}

TEST_F(NearbyShareContactManagerImplTest, ContactUploadHash) {
  EXPECT_EQ(std::string(), pref_service()->GetString(
                               prefs::kNearbySharingContactUploadHashPrefName));

  std::vector<nearby::sharing::proto::ContactRecord> contact_records =
      TestContactRecordList(/*num_contacts=*/10u);
  std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
  SetAllowedContacts(allowlist, /*expect_allowlist_changed=*/true);
  DownloadContacts();
  SucceedDownload(contact_records, allowlist, /*expect_upload=*/true);
  FinishUpload(/*success=*/true,
               /*expected_contacts=*/BuildContactListToUpload(allowlist,
                                                              contact_records));

  // Hardcode expected contact upload hash to ensure that hashed value is
  // consistent across process starts. If this test starts to fail, check one of
  // the following:
  //   1. Did the test data change? No worries; just update this hash value.
  //   2. Did the hashing function change? As long as the function is stable
  //      across (most) process starts, then everything is okay; just update
  //      this hash value. A changed hash value will result in an extra server
  //      call, so as long as the value is stable for the most part, it's okay.
  const char kExpectedHash[] =
      "2355E1D4DF5B40CE03373D1EE590FE28D2A47B8B6F6EC4567CB770668A2DFC07";
  EXPECT_EQ(kExpectedHash, pref_service()->GetString(
                               prefs::kNearbySharingContactUploadHashPrefName));

  // Try a few different permutations of contacts to ensure that the hash is
  // invariant under ordering.
  auto rng = std::default_random_engine{};
  for (size_t i = 0; i < 10u; ++i) {
    DownloadContacts();

    // We do not expect an upload because the contacts did not change in any way
    // other than ordering.
    std::vector<nearby::sharing::proto::ContactRecord> shuffled_contacts =
        contact_records;
    std::shuffle(shuffled_contacts.begin(), shuffled_contacts.end(), rng);
    SucceedDownload(shuffled_contacts, allowlist, /*expect_upload=*/false);
    EXPECT_EQ(kExpectedHash,
              pref_service()->GetString(
                  prefs::kNearbySharingContactUploadHashPrefName));
  }
}