File: ukm_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1666 lines) | stat: -rw-r--r-- 69,478 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/system/sys_info.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/metrics/chrome_metrics_service_client.h"
#include "chrome/browser/metrics/chrome_metrics_services_manager_client.h"
#include "chrome/browser/metrics/testing/metrics_consent_override.h"
#include "chrome/browser/metrics/testing/metrics_reporting_pref_helper.h"
#include "chrome/browser/metrics/testing/sync_metrics_test_utils.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/test/integration/secondary_account_helper.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/unified_consent/unified_consent_service_factory.h"
#include "components/metrics/demographics/demographic_metrics_provider.h"
#include "components/metrics/demographics/demographic_metrics_test_utils.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/signin_buildflags.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/sync/base/features.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_service_impl.h"
#include "components/sync/service/sync_token_status.h"
#include "components/sync/test/fake_server_network_resources.h"
#include "components/ukm/ukm_recorder_observer.h"
#include "components/ukm/ukm_service.h"
#include "components/ukm/ukm_test_helper.h"
#include "components/unified_consent/unified_consent_service.h"
#include "components/version_info/version_info.h"
#include "components/webui/chrome_urls/pref_names.h"
#include "content/public/browser/browsing_data_remover.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/browsing_data_remover_test_util.h"
#include "content/public/test/navigation_handle_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "services/network/test/test_network_quality_tracker.h"
#include "third_party/metrics_proto/ukm/report.pb.h"
#include "third_party/metrics_proto/user_demographics.pb.h"
#include "url/url_constants.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#else
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "chrome/browser/flags/android/chrome_session_state.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/browser/ui/android/tab_model/tab_model_observer.h"
#include "chrome/browser/ui/android/tab_model/tab_model_test_helper.h"
#include "chrome/test/base/android/android_browser_test.h"
#include "content/public/browser/web_contents.h"
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/profiles/profile_picker.h"
#include "chrome/browser/ui/profiles/profile_ui_test_utils.h"
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)

namespace metrics {
namespace {

#if !BUILDFLAG(IS_ANDROID)
typedef Browser* PlatformBrowser;
#else
typedef std::unique_ptr<TestTabModel> PlatformBrowser;
#endif  // !BUILDFLAG(IS_ANDROID)

// Clears the specified data using BrowsingDataRemover.
void ClearBrowsingData(Profile* profile) {
  content::BrowsingDataRemover* remover = profile->GetBrowsingDataRemover();
  content::BrowsingDataRemoverCompletionObserver observer(remover);
  remover->RemoveAndReply(
      base::Time(), base::Time::Max(),
      chrome_browsing_data_remover::DATA_TYPE_HISTORY,
      content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, &observer);
  observer.BlockUntilCompletion();
  // Make sure HistoryServiceObservers have a chance to be notified.
  content::RunAllTasksUntilIdle();
}

ukm::UkmService* GetUkmService() {
  return g_browser_process->GetMetricsServicesManager()->GetUkmService();
}

class TestUkmRecorderObserver : public ukm::UkmRecorderObserver {
 public:
  explicit TestUkmRecorderObserver(ukm::UkmRecorderImpl* ukm_recorder)
      : ukm_recorder_(ukm_recorder) {
    ukm_recorder_->AddUkmRecorderObserver(base::flat_set<uint64_t>(), this);
  }

  ~TestUkmRecorderObserver() override {
    ukm_recorder_->RemoveUkmRecorderObserver(this);
  }

  void OnEntryAdded(ukm::mojom::UkmEntryPtr entry) override {}

  void OnUpdateSourceURL(ukm::SourceId source_id,
                         const std::vector<GURL>& urls) override {}

  void OnPurgeRecordingsWithUrlScheme(const std::string& url_scheme) override {}

  void OnPurge() override {}

  void ExpectAllowedStateChanged(ukm::UkmConsentState expected_state) {
    expected_allowed_ = expected_state;
    base::RunLoop loop;
    quit_closure_ = loop.QuitClosure();
    loop.Run();
  }

  void OnUkmAllowedStateChanged(ukm::UkmConsentState allowed_state) override {
    if (allowed_state == expected_allowed_) {
      std::move(quit_closure_).Run();
    }
  }

 private:
  ukm::UkmConsentState expected_allowed_;
  base::OnceClosure quit_closure_;
  raw_ptr<ukm::UkmRecorderImpl> ukm_recorder_;
};

}  // namespace

// Test fixture that provides access to some UKM internals.
class UkmBrowserTestBase : public SyncTest {
 public:
  UkmBrowserTestBase() : SyncTest(SINGLE_CLIENT) {
    // Explicitly enable UKM and disable metrics reporting. Disabling metrics
    // reporting should affect only UMA--not UKM.
    scoped_feature_list_.InitWithFeatures({ukm::kUkmFeature},
                                          {internal::kMetricsReportingFeature});
  }

  UkmBrowserTestBase(const UkmBrowserTestBase&) = delete;
  UkmBrowserTestBase& operator=(const UkmBrowserTestBase&) = delete;

#if !BUILDFLAG(IS_ANDROID)
  ukm::UkmSource* NavigateAndGetSource(const GURL& url,
                                       Browser* browser,
                                       ukm::UkmTestHelper* ukm_test_helper) {
    content::NavigationHandleObserver observer(
        browser->tab_strip_model()->GetActiveWebContents(), url);
    EXPECT_TRUE(ui_test_utils::NavigateToURL(browser, url));
    const ukm::SourceId source_id = ukm::ConvertToSourceId(
        observer.navigation_id(), ukm::SourceIdType::NAVIGATION_ID);
    return ukm_test_helper->GetSource(source_id);
  }
#endif  // !BUILDFLAG(IS_ANDROID)

 protected:
  // Creates and returns a platform-appropriate browser for |profile|.
  PlatformBrowser CreatePlatformBrowser(Profile* profile) {
#if !BUILDFLAG(IS_ANDROID)
    return CreateBrowser(profile);
#else
    std::unique_ptr<TestTabModel> tab_model =
        std::make_unique<TestTabModel>(profile);
    tab_model->SetWebContentsList(
        {content::WebContents::Create(
             content::WebContents::CreateParams(profile))
             .release()});
    TabModelList::AddTabModel(tab_model.get());
    EXPECT_TRUE(content::NavigateToURL(tab_model->GetActiveWebContents(),
                                       GURL("about:blank")));
    return tab_model;
#endif  // !BUILDFLAG(IS_ANDROID)
  }

  // Creates a platform-appropriate incognito browser for |profile|.
  PlatformBrowser CreateIncognitoPlatformBrowser(Profile* profile) {
    EXPECT_TRUE(profile->IsOffTheRecord());
#if !BUILDFLAG(IS_ANDROID)
    return CreateIncognitoBrowser(profile);
#else
    return CreatePlatformBrowser(profile);
#endif  // !BUILDFLAG(IS_ANDROID)
  }

  // Closes |browser| in a way that is appropriate for the platform.
  void ClosePlatformBrowser(PlatformBrowser& browser) {
#if !BUILDFLAG(IS_ANDROID)
    CloseBrowserSynchronously(browser);
#else
    TabModelList::RemoveTabModel(browser.get());
    browser.reset();
#endif  // !BUILDFLAG(IS_ANDROID)
  }

  std::unique_ptr<SyncServiceImplHarness> EnableSyncForProfile(
      Profile* profile) {
    std::unique_ptr<SyncServiceImplHarness> harness =
        test::InitializeProfileForSync(profile, GetFakeServer()->AsWeakPtr());

    EXPECT_TRUE(harness->SetupSync());

    // If unified consent is enabled, then enable url-keyed-anonymized data
    // collection through the consent service.
    // Note: If unfied consent is not enabled, then UKM will be enabled based on
    // the history sync state.
    unified_consent::UnifiedConsentService* consent_service =
        UnifiedConsentServiceFactory::GetForProfile(profile);
    if (consent_service) {
      consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(true);
    }

    return harness;
  }

#if !BUILDFLAG(IS_ANDROID)
  Profile* CreateNonSyncProfile() {
    ProfileManager* profile_manager = g_browser_process->profile_manager();
    base::FilePath new_path =
        profile_manager->GenerateNextProfileDirectoryPath();
    Profile& profile =
        profiles::testing::CreateProfileSync(profile_manager, new_path);
    return &profile;
  }
#endif  // !BUILDFLAG(IS_ANDROID)

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

class UkmBrowserTest : public UkmBrowserTestBase {
 public:
  UkmBrowserTest() = default;

  UkmBrowserTest(const UkmBrowserTest&) = delete;
  UkmBrowserTest& operator=(const UkmBrowserTest&) = delete;

#if BUILDFLAG(IS_ANDROID)
  void PreRunTestOnMainThread() override {
    UkmBrowserTestBase::PreRunTestOnMainThread();
    // At some point during set-up, Android's TabModelList is populated with a
    // TabModel. However, it is desirable to begin the tests with an empty
    // TabModelList to avoid complicated logic in CreatePlatformBrowser.
    //
    // For example, if the pre-existing TabModel is not deleted and if the first
    // tab created in a test is an incognito tab, then CreatePlatformBrowser
    // would need to remove the pre-existing TabModel and add a new one.
    // Having an empty TabModelList allows us to simply add the appropriate
    // TabModel.
    EXPECT_EQ(1U, TabModelList::models().size());
    TabModelList::RemoveTabModel(TabModelList::models()[0]);
    EXPECT_EQ(0U, TabModelList::models().size());
  }
#endif  // BUILDFLAG(IS_ANDROID)
};

class UkmBrowserTestWithSyncTransport : public UkmBrowserTestBase {
 public:
  UkmBrowserTestWithSyncTransport() = default;

  UkmBrowserTestWithSyncTransport(const UkmBrowserTestWithSyncTransport&) =
      delete;
  UkmBrowserTestWithSyncTransport& operator=(
      const UkmBrowserTestWithSyncTransport&) = delete;

  void SetUpInProcessBrowserTestFixture() override {
    // This is required to support (fake) secondary-account-signin (based on
    // cookies) in tests. Without this, the real GaiaCookieManagerService would
    // try talking to Google servers which of course wouldn't work in tests.
    test_signin_client_subscription_ =
        secondary_account_helper::SetUpSigninClient(&test_url_loader_factory_);
    UkmBrowserTestBase::SetUpInProcessBrowserTestFixture();
  }

  void SetUpOnMainThread() override {
#if BUILDFLAG(IS_CHROMEOS)
    secondary_account_helper::InitNetwork();
#endif  // BUILDFLAG(IS_CHROMEOS)
    UkmBrowserTestBase::SetUpOnMainThread();
  }

 private:
  base::CallbackListSubscription test_signin_client_subscription_;
};

// This tests if UKM service is enabled/disabled appropriately based on an
// input bool param. The bool reflects if metrics reporting state is
// enabled/disabled via prefs.
#if !BUILDFLAG(IS_ANDROID)
class UkmConsentParamBrowserTest : public UkmBrowserTestBase,
                                   public testing::WithParamInterface<bool> {
 public:
  UkmConsentParamBrowserTest() = default;

  UkmConsentParamBrowserTest(const UkmConsentParamBrowserTest&) = delete;
  UkmConsentParamBrowserTest& operator=(const UkmConsentParamBrowserTest&) =
      delete;

  static bool IsMetricsAndCrashReportingEnabled() {
    return ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled();
  }

  // InProcessBrowserTest overrides.
  bool SetUpUserDataDirectory() override {
    local_state_path_ = SetUpUserDataDirectoryForTesting(
        is_metrics_reporting_enabled_initial_value());
    return !local_state_path_.empty();
  }

  void CreatedBrowserMainParts(content::BrowserMainParts* parts) override {
    UkmBrowserTestBase::CreatedBrowserMainParts(parts);
    // IsMetricsReportingEnabled() in non-official builds always returns false.
    // Enable the official build checks so that this test can work in both
    // official and non-official builds.
    ChromeMetricsServiceAccessor::SetForceIsMetricsReportingEnabledPrefLookup(
        true);
  }

  bool is_metrics_reporting_enabled_initial_value() const { return GetParam(); }

 private:
  base::FilePath local_state_path_;
};
#endif  // !BUILDFLAG(IS_ANDROID)

// Test the reporting of the synced user's birth year and gender.
class UkmBrowserTestWithDemographics
    : public UkmBrowserTestBase,
      public testing::WithParamInterface<test::DemographicsTestParams> {
 public:
  UkmBrowserTestWithDemographics() : UkmBrowserTestBase() {
    test::DemographicsTestParams param = GetParam();
    if (param.enable_feature) {
      scoped_feature_list_.InitWithFeatures(
          // enabled_features
          {kDemographicMetricsReporting,
           ukm::kReportUserNoisedUserBirthYearAndGender},
          // disabled_features
          {});
    } else {
      scoped_feature_list_.InitWithFeatures(
          // enabled_features
          {},
          // disabled_features
          {kDemographicMetricsReporting,
           ukm::kReportUserNoisedUserBirthYearAndGender});
    }
  }

  UkmBrowserTestWithDemographics(const UkmBrowserTestWithDemographics&) =
      delete;
  UkmBrowserTestWithDemographics& operator=(
      const UkmBrowserTestWithDemographics&) = delete;

  PrefService* local_state() { return g_browser_process->local_state(); }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Make sure that UKM is disabled while an incognito window is open.
// LINT.IfChange(RegularPlusIncognitoCheck)
// Disabled on Android due to flakiness. See crbug.com/355609356.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_RegularPlusIncognitoCheck DISABLED_RegularPlusIncognitoCheck
#else
#define MAYBE_RegularPlusIncognitoCheck RegularPlusIncognitoCheck
#endif
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MAYBE_RegularPlusIncognitoCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  PlatformBrowser browser1 = CreatePlatformBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  Profile* incognito_profile =
      profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  PlatformBrowser incognito_browser1 =
      CreateIncognitoPlatformBrowser(incognito_profile);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  // Opening another regular browser mustn't enable UKM.
  PlatformBrowser browser2 = CreatePlatformBrowser(profile);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  // Opening and closing another Incognito browser mustn't enable UKM.
  PlatformBrowser incognito_browser2 =
      CreateIncognitoPlatformBrowser(incognito_profile);
  ClosePlatformBrowser(incognito_browser2);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  ClosePlatformBrowser(browser2);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  ClosePlatformBrowser(incognito_browser1);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  // Client ID should not have been reset.
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());

  ClosePlatformBrowser(browser1);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/ukm_egtest.mm:RegularPlusIncognitoCheck)

// Make sure opening a real window after Incognito doesn't enable UKM.
// LINT.IfChange(IncognitoPlusRegularCheck)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, IncognitoPlusRegularCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Profile* incognito_profile =
      profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  PlatformBrowser incognito_browser =
      CreateIncognitoPlatformBrowser(incognito_profile);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  PlatformBrowser browser = CreatePlatformBrowser(profile);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  ClosePlatformBrowser(incognito_browser);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());

  ClosePlatformBrowser(browser);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/ukm_egtest.mm:IncognitoPlusRegularCheck)

// Make sure that UKM is disabled while a guest profile's window is open.
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, RegularPlusGuestCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* regular_browser = CreateBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();

  // Create browser for guest profile.
  Browser* guest_browser = InProcessBrowserTest::CreateGuestBrowser();
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  CloseBrowserSynchronously(guest_browser);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  // Client ID should not have been reset.
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());

  CloseBrowserSynchronously(regular_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS)

// ProfilePicker and System profile do not exist on ChromeOS and on Android.
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
// Displaying the ProfilePicker implicitly creates a System Profile.
// System Profile shouldn't have any effect on the UKM Enable Status.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, ProfilePickerCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* regular_browser = CreateBrowser(profile);
  ASSERT_TRUE(ukm_test_helper.IsRecordingEnabled());

  // ProfilePicker creates a SystemProfile.
  ProfilePicker::Show(ProfilePicker::Params::FromEntryPoint(
      ProfilePicker::EntryPoint::kProfileMenuManageProfiles));
  profiles::testing::WaitForPickerWidgetCreated();

  Profile* system_profile =
      g_browser_process->profile_manager()->GetProfileByPath(
          ProfileManager::GetSystemProfilePath());
  ASSERT_TRUE(system_profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());

  ProfilePicker::Hide();
  profiles::testing::WaitForPickerClosed();
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());

  CloseBrowserSynchronously(regular_browser);
}
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)

// Not applicable to Android as it doesn't have multiple profiles.
#if !BUILDFLAG(IS_ANDROID)
// Make sure that UKM is disabled while an non-sync profile's window is open.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, OpenNonSyncCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* sync_browser = CreateBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  Profile* nonsync_profile = CreateNonSyncProfile();
  Browser* nonsync_browser = CreateBrowser(nonsync_profile);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  CloseBrowserSynchronously(nonsync_browser);
  // TODO(crbug.com/40530708): UKM doesn't actually get re-enabled yet.
  // EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
#if !BUILDFLAG(IS_CHROMEOS)
  // Client ID should not have been reset.
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());
#else
  // When flag is enabled, removing MSBB consent will cause the client id to be
  // reset. In this case a new profile with sync turned off is added which also
  // removes consent.
  EXPECT_NE(original_client_id, ukm_test_helper.GetClientId());
#endif

  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Make sure that UKM is disabled when metrics consent is revoked.
// LINT.IfChange(MetricsConsentCheck)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MetricsConsentCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  PlatformBrowser browser = CreatePlatformBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  // Make sure there is a persistent log.
  ukm_test_helper.BuildAndStoreLog();
  EXPECT_TRUE(ukm_test_helper.HasUnsentLogs());

  metrics_consent.Update(false);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_FALSE(ukm_test_helper.HasUnsentLogs());

  metrics_consent.Update(true);

  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  // Client ID should have been reset.
  EXPECT_NE(original_client_id, ukm_test_helper.GetClientId());

  ClosePlatformBrowser(browser);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/ukm_egtest.mm:MetricsConsentCheck)

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, LogProtoData) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* sync_browser = CreateBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  // Make sure there is a persistent log.
  ukm_test_helper.BuildAndStoreLog();
  EXPECT_TRUE(ukm_test_helper.HasUnsentLogs());

  // Check log contents.
  std::unique_ptr<ukm::Report> report = ukm_test_helper.GetUkmReport();
  EXPECT_EQ(original_client_id, report->client_id());
  // Note: The version number reported in the proto may have a suffix, such as
  // "-64-devel", so use use StartsWith() rather than checking for equality.
  EXPECT_TRUE(base::StartsWith(report->system_profile().app_version(),
                               version_info::GetVersionNumber(),
                               base::CompareCase::SENSITIVE));

  EXPECT_EQ(base::SysInfo::HardwareModelName(),
            report->system_profile().hardware().hardware_class());

  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// LINT.IfChange(AddSyncedUserBirthYearAndGenderToProtoData)
IN_PROC_BROWSER_TEST_P(UkmBrowserTestWithDemographics,
                       AddSyncedUserBirthYearAndGenderToProtoData) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::DemographicsTestParams param = GetParam();
  test::MetricsConsentOverride metrics_consent(true);

  base::HistogramTester histogram;

  const base::Time now = base::Time::Now();
  test::UpdateNetworkTime(now, g_browser_process->network_time_tracker());
  const int test_birth_year = test::GetMaximumEligibleBirthYear(now);
  const UserDemographicsProto::Gender test_gender =
      UserDemographicsProto::GENDER_FEMALE;

  // Add the test synced user birth year and gender priority prefs to the sync
  // server data.
  test::AddUserBirthYearAndGenderToSyncServer(GetFakeServer()->AsWeakPtr(),
                                              test_birth_year, test_gender);

  Profile* test_profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(test_profile);

  // Make sure that there is only one Profile to allow reporting the user's
  // birth year and gender.
  ASSERT_EQ(1, num_clients());

  PlatformBrowser browser = CreatePlatformBrowser(test_profile);

  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  // Log UKM metrics report.
  ukm_test_helper.BuildAndStoreLog();
  EXPECT_TRUE(ukm_test_helper.HasUnsentLogs());

  // Check the log's content.
  std::unique_ptr<ukm::Report> report = ukm_test_helper.GetUkmReport();
  if (param.expect_reported_demographics) {
    EXPECT_EQ(test::GetNoisedBirthYear(local_state(), test_birth_year),
              report->user_demographics().birth_year());
    EXPECT_EQ(test_gender, report->user_demographics().gender());
  } else {
    EXPECT_FALSE(report->has_user_demographics());
  }

#if !BUILDFLAG(IS_CHROMEOS)
  // Sign out the user to revoke all refresh tokens. This prevents any posted
  // tasks from successfully fetching an access token during the tear-down
  // phase and crashing on a DCHECK. See crbug/1102746 for more details.
  harness->SignOutPrimaryAccount();
#endif  // !BUILDFLAG(IS_CHROMEOS)
  ClosePlatformBrowser(browser);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/demographics_egtest.mm:AddSyncedUserBirthYearAndGenderToProtoDataEnabled_ukmBrowsertest)
// ThenChange(/ios/chrome/browser/metrics/demographics_egtest.mm:AddSyncedUserBirthYearAndGenderToProtoDataDisabled_ukmBrowsertest)

#if BUILDFLAG(IS_CHROMEOS)
// Cannot test for the enabled feature on Chrome OS because there are always
// multiple profiles.
static const auto kDemographicsTestParams = testing::Values(
    test::DemographicsTestParams{/*enable_feature=*/false,
                                 /*expect_reported_demographics=*/false});
#else
static const auto kDemographicsTestParams = testing::Values(
    test::DemographicsTestParams{/*enable_feature=*/false,
                                 /*expect_reported_demographics=*/false},
    test::DemographicsTestParams{/*enable_feature=*/true,
                                 /*expect_reported_demographics=*/true});
#endif

INSTANTIATE_TEST_SUITE_P(,
                         UkmBrowserTestWithDemographics,
                         kDemographicsTestParams);

// Verifies that network provider attaches effective connection type correctly
// to the UKM report.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, NetworkProviderPopulatesSystemProfile) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  // Override network quality to 2G. This should cause the
  // |max_effective_connection_type| in the system profile to be set to 2G.
  g_browser_process->network_quality_tracker()
      ->ReportEffectiveConnectionTypeForTesting(
          net::EFFECTIVE_CONNECTION_TYPE_2G);

  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* sync_browser = CreateBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  // Override network quality to 4G. This should cause the
  // |max_effective_connection_type| in the system profile to be set to 4G.
  g_browser_process->network_quality_tracker()
      ->ReportEffectiveConnectionTypeForTesting(
          net::EFFECTIVE_CONNECTION_TYPE_4G);

  // Make sure there is a persistent log.
  ukm_test_helper.BuildAndStoreLog();
  EXPECT_TRUE(ukm_test_helper.HasUnsentLogs());
  // Check log contents.
  std::unique_ptr<ukm::Report> report = ukm_test_helper.GetUkmReport();

  EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_2G,
            report->system_profile().network().min_effective_connection_type());
  EXPECT_EQ(SystemProfileProto::Network::EFFECTIVE_CONNECTION_TYPE_4G,
            report->system_profile().network().max_effective_connection_type());

  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Verifies that install date is attached.
// Disabled on Android due to flakiness. See crbug.com/355609356.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_InstallDateProviderPopulatesSystemProfile \
  DISABLED_InstallDateProviderPopulatesSystemProfile
#else
#define MAYBE_InstallDateProviderPopulatesSystemProfile \
  InstallDateProviderPopulatesSystemProfile
#endif
IN_PROC_BROWSER_TEST_F(UkmBrowserTest,
                       MAYBE_InstallDateProviderPopulatesSystemProfile) {
  PrefService* local_state = g_browser_process->local_state();
  local_state->SetInt64(prefs::kInstallDate, 123456);

  ukm::UkmTestHelper ukm_test_helper(GetUkmService());

  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  PlatformBrowser browser = CreatePlatformBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  // Make sure there is a persistent log.
  ukm_test_helper.BuildAndStoreLog();
  EXPECT_TRUE(ukm_test_helper.HasUnsentLogs());
  // Check log contents.
  std::unique_ptr<ukm::Report> report = ukm_test_helper.GetUkmReport();

  // Rounded from the 123456 we set earlier, to nearest hour.
  EXPECT_EQ(122400, report->system_profile().install_date());

  ClosePlatformBrowser(browser);
}

// Make sure that providing consent doesn't enable UKM when sync is disabled.
// Flaky on Android crbug.com/1096400
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_ConsentAddedButNoSyncCheck DISABLED_ConsentAddedButNoSyncCheck
#else
#define MAYBE_ConsentAddedButNoSyncCheck ConsentAddedButNoSyncCheck
#endif
// LINT.IfChange(ConsentAddedButNoSyncCheck)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MAYBE_ConsentAddedButNoSyncCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(false);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  PlatformBrowser browser = CreatePlatformBrowser(profile);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  metrics_consent.Update(true);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());

  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  g_browser_process->GetMetricsServicesManager()->UpdateUploadPermissions();
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());

  ClosePlatformBrowser(browser);
}
// LINT.ThenChange(/chrome/android/javatests/src/org/chromium/chrome/browser/sync/UkmTest.java:ConsentAddedButNoSyncCheck)
// ThenChange(/ios/chrome/browser/metrics/model/ukm_egtest.mm:ConsentAddedButNoSyncCheck)

// Make sure that extension URLs are disabled when an open sync window
// disables it.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, SingleDisableExtensionsSyncCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* sync_browser = CreateBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(ukm_test_helper.IsExtensionRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  ASSERT_TRUE(
      harness->DisableSyncForType(syncer::UserSelectableType::kExtensions));
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_FALSE(ukm_test_helper.IsExtensionRecordingEnabled());

  ASSERT_TRUE(
      harness->EnableSyncForType(syncer::UserSelectableType::kExtensions));
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(ukm_test_helper.IsExtensionRecordingEnabled());
  // Client ID should not be reset.
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());

  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Make sure that extension URLs are disabled when any open sync window
// disables it.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MultiDisableExtensionsSyncCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile1 = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness1 =
      EnableSyncForProfile(profile1);

  Browser* browser1 = CreateBrowser(profile1);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  Profile* profile2 = CreateNonSyncProfile();
  std::unique_ptr<SyncServiceImplHarness> harness2 =
      EnableSyncForProfile(profile2);
  Browser* browser2 = CreateBrowser(profile2);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(ukm_test_helper.IsExtensionRecordingEnabled());
#if !BUILDFLAG(IS_CHROMEOS)
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());
#else
  // If the feature is enabled, then the client id will have been reset. The
  // client id is reset when the second profile is initially created without any
  // sync preferences enabled or MSBB. With the feature, any consent on to off
  // change for MSBB or App Sync will cause the client id to reset.
  EXPECT_NE(original_client_id, ukm_test_helper.GetClientId());
  original_client_id = ukm_test_helper.GetClientId();
#endif

  ASSERT_TRUE(
      harness2->DisableSyncForType(syncer::UserSelectableType::kExtensions));
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_FALSE(ukm_test_helper.IsExtensionRecordingEnabled());

  ASSERT_TRUE(
      harness2->EnableSyncForType(syncer::UserSelectableType::kExtensions));
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(ukm_test_helper.IsExtensionRecordingEnabled());
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());

  CloseBrowserSynchronously(browser2);
  CloseBrowserSynchronously(browser1);
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, LogsTabId) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  ASSERT_TRUE(embedded_test_server()->Start());
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  Browser* sync_browser = CreateBrowser(profile);

  const ukm::UkmSource* first_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title1.html"),
                           sync_browser, &ukm_test_helper);

  // Tab ids are incremented starting from 1. Since we started a new sync
  // browser, this is the second tab.
  EXPECT_EQ(2, first_source->navigation_data().tab_id);

  // Ensure the tab id is constant in a single tab.
  const ukm::UkmSource* second_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title2.html"),
                           sync_browser, &ukm_test_helper);
  EXPECT_EQ(first_source->navigation_data().tab_id,
            second_source->navigation_data().tab_id);

  // Add a new tab, it should get a new tab id.
  chrome::NewTab(sync_browser);
  const ukm::UkmSource* third_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title3.html"),
                           sync_browser, &ukm_test_helper);
  EXPECT_EQ(3, third_source->navigation_data().tab_id);
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, LogsPreviousSourceId) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  ASSERT_TRUE(embedded_test_server()->Start());
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  Browser* sync_browser = CreateBrowser(profile);

  const ukm::UkmSource* first_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title1.html"),
                           sync_browser, &ukm_test_helper);

  const ukm::UkmSource* second_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title2.html"),
                           sync_browser, &ukm_test_helper);
  EXPECT_EQ(first_source->id(),
            second_source->navigation_data().previous_source_id);

  // Open a new tab with window.open.
  content::WebContents* opener =
      sync_browser->tab_strip_model()->GetActiveWebContents();
  GURL new_tab_url = embedded_test_server()->GetURL("/title3.html");
  content::TestNavigationObserver waiter(new_tab_url);
  waiter.StartWatchingNewWebContents();
  EXPECT_TRUE(content::ExecJs(
      opener, content::JsReplace("window.open($1)", new_tab_url)));
  waiter.Wait();
  EXPECT_NE(opener, sync_browser->tab_strip_model()->GetActiveWebContents());
  ukm::SourceId new_id = sync_browser->tab_strip_model()
                             ->GetActiveWebContents()
                             ->GetPrimaryMainFrame()
                             ->GetPageUkmSourceId();
  ukm::UkmSource* new_tab_source = ukm_test_helper.GetSource(new_id);
  EXPECT_NE(nullptr, new_tab_source);
  EXPECT_EQ(ukm::kInvalidSourceId,
            new_tab_source->navigation_data().previous_source_id);

  // Subsequent navigations within the tab should get a previous_source_id field
  // set.
  const ukm::UkmSource* subsequent_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title3.html"),
                           sync_browser, &ukm_test_helper);
  EXPECT_EQ(new_tab_source->id(),
            subsequent_source->navigation_data().previous_source_id);
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, LogsOpenerSource) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  ASSERT_TRUE(embedded_test_server()->Start());
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  Browser* sync_browser = CreateBrowser(profile);

  const ukm::UkmSource* first_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title1.html"),
                           sync_browser, &ukm_test_helper);
  // This tab was not opened by another tab, so it should not have an opener
  // id.
  EXPECT_EQ(ukm::kInvalidSourceId,
            first_source->navigation_data().opener_source_id);

  // Open a new tab with window.open.
  content::WebContents* opener =
      sync_browser->tab_strip_model()->GetActiveWebContents();
  GURL new_tab_url = embedded_test_server()->GetURL("/title2.html");
  content::TestNavigationObserver waiter(new_tab_url);
  waiter.StartWatchingNewWebContents();
  EXPECT_TRUE(content::ExecJs(
      opener, content::JsReplace("window.open($1)", new_tab_url)));
  waiter.Wait();
  EXPECT_NE(opener, sync_browser->tab_strip_model()->GetActiveWebContents());
  ukm::SourceId new_id = sync_browser->tab_strip_model()
                             ->GetActiveWebContents()
                             ->GetPrimaryMainFrame()
                             ->GetPageUkmSourceId();
  ukm::UkmSource* new_tab_source = ukm_test_helper.GetSource(new_id);
  EXPECT_NE(nullptr, new_tab_source);
  EXPECT_EQ(first_source->id(),
            new_tab_source->navigation_data().opener_source_id);

  // Subsequent navigations within the tab should not get an opener set.
  const ukm::UkmSource* subsequent_source =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title3.html"),
                           sync_browser, &ukm_test_helper);
  EXPECT_EQ(ukm::kInvalidSourceId,
            subsequent_source->navigation_data().opener_source_id);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// ChromeOS doesn't have the concept of sign-out so this test doesn't make sense
// there.
//
// Flaky on Android: https://crbug.com/1096047.
//
// Make sure that UKM is disabled when the profile signs out of Sync.
// LINT.IfChange(SingleSyncSignoutCheck)
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, SingleSyncSignoutCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  PlatformBrowser browser = CreatePlatformBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  harness->SignOutPrimaryAccount();
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_NE(original_client_id, ukm_test_helper.GetClientId());

  ClosePlatformBrowser(browser);
}
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
// LINT.ThenChange(/chrome/android/javatests/src/org/chromium/chrome/browser/sync/UkmTest.java:SingleSyncSignoutCheck)
// ThenChange(/ios/chrome/browser/metrics/model/ukm_egtest.mm:SingleSyncSignoutCheck)

// ChromeOS doesn't have the concept of sign-out so this test doesn't make sense
// there. Android doesn't have multiple profiles.
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
// Make sure that UKM is disabled when any profile signs out of Sync.
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MultiSyncSignoutCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile1 = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness1 =
      EnableSyncForProfile(profile1);

  Browser* browser1 = CreateBrowser(profile1);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  Profile* profile2 = CreateNonSyncProfile();
  std::unique_ptr<SyncServiceImplHarness> harness2 =
      EnableSyncForProfile(profile2);
  Browser* browser2 = CreateBrowser(profile2);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());

  harness2->SignOutPrimaryAccount();
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_NE(original_client_id, ukm_test_helper.GetClientId());

  CloseBrowserSynchronously(browser2);
  CloseBrowserSynchronously(browser1);
}
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)

// Make sure that if history/sync services weren't available when we tried to
// attach listeners, UKM is not enabled.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, ServiceListenerInitFailedCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);
  ChromeMetricsServiceClient::SetNotificationListenerSetupFailedForTesting(
      true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* sync_browser = CreateBrowser(profile);
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Make sure that UKM is not affected by MetricsReporting Feature (sampling).
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MetricsReportingCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  // Need to set the Metrics Default to OPT_OUT to trigger MetricsReporting.
  PrefService* local_state = g_browser_process->local_state();
  ForceRecordMetricsReportingDefaultState(local_state,
                                          EnableMetricsDefault::OPT_OUT);
  // Verify that kMetricsReportingFeature is disabled (i.e. other metrics
  // services will be sampled out).
  EXPECT_FALSE(
      base::FeatureList::IsEnabled(internal::kMetricsReportingFeature));

  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* sync_browser = CreateBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());

  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Make sure that pending data is deleted when user deletes history.
// LINT.IfChange(HistoryDeleteCheck)
// Flaky on Android: https://crbug.com/1131541.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_HistoryDeleteCheck DISABLED_HistoryDeleteCheck
#else
#define MAYBE_HistoryDeleteCheck HistoryDeleteCheck
#endif
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, MAYBE_HistoryDeleteCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  PlatformBrowser browser = CreatePlatformBrowser(profile);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  uint64_t original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0U, original_client_id);

  const ukm::SourceId kDummySourceId = 0x54321;
  ukm_test_helper.RecordSourceForTesting(kDummySourceId);
  EXPECT_TRUE(ukm_test_helper.HasSource(kDummySourceId));

  ClearBrowsingData(profile);
  // Other sources may already have been recorded since the data was cleared,
  // but the dummy source should be gone.
  EXPECT_FALSE(ukm_test_helper.HasSource(kDummySourceId));
  // Client ID should NOT be reset.
  EXPECT_EQ(original_client_id, ukm_test_helper.GetClientId());
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());

  ClosePlatformBrowser(browser);
}
// LINT.ThenChange(/chrome/android/javatests/src/org/chromium/chrome/browser/metrics/UkmTest.java:HistoryDeleteCheck)
// ThenChange(/ios/chrome/browser/metrics/model/ukm_egtest.mm:HistoryDeleteCheck)

// On ChromeOS, the test profile starts with a primary account already set, so
// this test doesn't apply.
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTestWithSyncTransport,
                       NotEnabledForSecondaryAccountSync) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);

  // Signing in (without granting sync consent or explicitly setting up Sync)
  // should trigger starting the Sync machinery in standalone transport mode.
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      test::InitializeProfileForSync(profile, GetFakeServer()->AsWeakPtr());
  syncer::SyncService* sync_service =
      SyncServiceFactory::GetForProfile(profile);

  secondary_account_helper::SignInUnconsentedAccount(
      profile, &test_url_loader_factory_, "secondary_user@email.com");
  ASSERT_NE(syncer::SyncService::TransportState::DISABLED,
            sync_service->GetTransportState());
  ASSERT_TRUE(harness->AwaitSyncTransportActive());
  ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
            sync_service->GetTransportState());
  ASSERT_FALSE(sync_service->IsSyncFeatureEnabled());

  // History Sync is not active.
  ASSERT_FALSE(sync_service->GetActiveDataTypes().Has(syncer::HISTORY));
  ASSERT_FALSE(sync_service->GetActiveDataTypes().Has(
      syncer::HISTORY_DELETE_DIRECTIVES));

  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
}
#endif  // !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_P(UkmConsentParamBrowserTest, GroupPolicyConsentCheck) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  // Note we are not using the synthetic MetricsConsentOverride since we are
  // testing directly from prefs.

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  Browser* sync_browser = CreateBrowser(profile);

  // The input param controls whether we set the prefs related to group policy
  // enabled or not. Based on its value, we should report the same value for
  // both if reporting is enabled and if UKM service is enabled.
  bool is_enabled = is_metrics_reporting_enabled_initial_value();
  EXPECT_EQ(is_enabled,
            UkmConsentParamBrowserTest::IsMetricsAndCrashReportingEnabled());
  EXPECT_EQ(is_enabled, ukm_test_helper.IsRecordingEnabled());

  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
// Verify UKM is enabled/disabled for both potential settings of group policy.
INSTANTIATE_TEST_SUITE_P(UkmConsentParamBrowserTests,
                         UkmConsentParamBrowserTest,
                         testing::Bool());
#endif  // !BUILDFLAG(IS_ANDROID)

// Verify that sources kept alive in-memory will be discarded by UKM service in
// one reporting cycle after the web contents are destroyed when the tab is
// closed or when the user navigated away in the same tab.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, EvictObsoleteSources) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  Browser* sync_browser = CreateBrowser(profile);
  ASSERT_TRUE(embedded_test_server()->Start());

  ukm::SourceId source_id1 = ukm::kInvalidSourceId;
  ukm::SourceId source_id2 = ukm::kInvalidSourceId;

  const std::vector<GURL> test_urls = {
      embedded_test_server()->GetURL("/title1.html"),
      embedded_test_server()->GetURL("/title2.html"),
      embedded_test_server()->GetURL("/title3.html")};

  // Open a blank new tab.
  ASSERT_TRUE(AddTabAtIndexToBrowser(sync_browser, 1, GURL(url::kAboutBlankURL),
                                     ui::PAGE_TRANSITION_TYPED, true));
  // Gather source id from the NavigationHandle assigned to navigations that
  // start with the expected URL.
  content::NavigationHandleObserver tab_1_observer(
      sync_browser->tab_strip_model()->GetActiveWebContents(), test_urls[0]);
  // Navigate to a test URL in this new tab.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(sync_browser, test_urls[0]));
  // Get the source id associated to the last committed navigation, which could
  // differ from the id from WebContents for example if the site executes a
  // same-document navigation (e.g. history.pushState/replaceState). This
  // navigation source id is the one marked as obsolete by UKM recorder.
  source_id1 = ukm::ConvertToSourceId(tab_1_observer.navigation_id(),
                                      ukm::SourceIdType::NAVIGATION_ID);

  // The UKM report contains this newly-created source.
  ukm_test_helper.BuildAndStoreLog();
  std::unique_ptr<ukm::Report> report = ukm_test_helper.GetUkmReport();
  bool has_source_id1 = false;
  bool has_source_id2 = false;
  for (const auto& s : report->sources()) {
    has_source_id1 |= s.id() == source_id1;
    has_source_id2 |= s.id() == source_id2;
  }
  EXPECT_TRUE(has_source_id1);
  EXPECT_FALSE(has_source_id2);

  // Navigate to another URL in a new tab.
  ASSERT_TRUE(AddTabAtIndexToBrowser(sync_browser, 2, GURL(url::kAboutBlankURL),
                                     ui::PAGE_TRANSITION_TYPED, true));
  content::NavigationHandleObserver tab_2_observer(
      sync_browser->tab_strip_model()->GetActiveWebContents(), test_urls[1]);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(sync_browser, test_urls[1]));
  source_id2 = ukm::ConvertToSourceId(tab_2_observer.navigation_id(),
                                      ukm::SourceIdType::NAVIGATION_ID);

  // The next report should again contain source 1 because the tab is still
  // alive, and also source 2 associated to the new tab that has just been
  // opened.
  ukm_test_helper.BuildAndStoreLog();
  report = ukm_test_helper.GetUkmReport();
  has_source_id1 = false;
  has_source_id2 = false;
  for (const auto& s : report->sources()) {
    has_source_id1 |= s.id() == source_id1;
    has_source_id2 |= s.id() == source_id2;
  }
  EXPECT_TRUE(has_source_id1);
  EXPECT_TRUE(has_source_id2);

  // Close the tab corresponding to source 1, this should mark source 1 as
  // obsolete. Next report will still contain source 1 because we might have
  // associated entries before it was closed.
  sync_browser->tab_strip_model()->CloseWebContentsAt(
      1, TabCloseTypes::CLOSE_NONE);

  ukm_test_helper.BuildAndStoreLog();
  report = ukm_test_helper.GetUkmReport();
  has_source_id1 = false;
  has_source_id2 = false;
  for (const auto& s : report->sources()) {
    has_source_id1 |= s.id() == source_id1;
    has_source_id2 |= s.id() == source_id2;
  }
  EXPECT_TRUE(has_source_id1);
  EXPECT_TRUE(has_source_id2);

  // Navigate to a new URL in the current tab, this will mark source 2 that was
  // in the current tab as obsolete.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(sync_browser, test_urls[2]));

  // The previous report was the last one that could potentially contain entries
  // for source 1. Source 1 is thus no longer included in future reports. This
  // report will still contain source 2 because we might have associated entries
  // since the last report.
  ukm_test_helper.BuildAndStoreLog();
  report = ukm_test_helper.GetUkmReport();
  has_source_id1 = false;
  has_source_id2 = false;
  for (const auto& s : report->sources()) {
    has_source_id1 |= s.id() == source_id1;
    has_source_id2 |= s.id() == source_id2;
  }
  EXPECT_FALSE(has_source_id1);
  EXPECT_TRUE(has_source_id2);

  // Neither source 1 or source 2 is alive anymore.
  ukm_test_helper.BuildAndStoreLog();
  report = ukm_test_helper.GetUkmReport();
  has_source_id1 = false;
  has_source_id2 = false;
  for (const auto& s : report->sources()) {
    has_source_id1 |= s.id() == source_id1;
    has_source_id2 |= s.id() == source_id2;
  }
  EXPECT_FALSE(has_source_id1);
  EXPECT_FALSE(has_source_id2);

  CloseBrowserSynchronously(sync_browser);
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Verify that correct sources are marked as obsolete when same-document
// navigation happens.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest,
                       MarkObsoleteSourcesSameDocumentNavigation) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  Browser* sync_browser = CreateBrowser(profile);
  ASSERT_TRUE(embedded_test_server()->Start());

  // First navigation.
  const ukm::SourceId source_id1 =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title1.html"),
                           sync_browser, &ukm_test_helper)
          ->id();

  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id1));

  // Cross-document navigation where the previous navigation is cross-document.
  const ukm::SourceId source_id2 =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title2.html"),
                           sync_browser, &ukm_test_helper)
          ->id();
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id1));
  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id2));

  // Same-document navigation where the previous navigation is cross-document.
  const ukm::SourceId source_id3 =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title2.html#a"),
                           sync_browser, &ukm_test_helper)
          ->id();
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id1));
  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id2));
  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id3));

  // Same-document navigation where the previous navigation is same-document.
  const ukm::SourceId source_id4 =
      NavigateAndGetSource(embedded_test_server()->GetURL("/title2.html#b"),
                           sync_browser, &ukm_test_helper)
          ->id();
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id1));
  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id2));
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id3));
  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id4));

  // Cross-document navigation where the previous navigation is same-document.
  NavigateAndGetSource(embedded_test_server()->GetURL("/title1.html"),
                       sync_browser, &ukm_test_helper)
      ->id();
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id1));
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id2));
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id3));
  EXPECT_TRUE(ukm_test_helper.IsSourceObsolete(source_id4));
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Verify that sources are not marked as obsolete by a new navigation that does
// not commit.
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, NotMarkSourcesIfNavigationNotCommitted) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  Browser* sync_browser = CreateBrowser(profile);
  ASSERT_TRUE(embedded_test_server()->Start());

  // An example navigation that commits.
  const GURL test_url_with_commit =
      embedded_test_server()->GetURL("/title1.html");

  // An example of a navigation returning "204 No Content" which does not commit
  // (i.e. the WebContents stays at the existing URL).
  const GURL test_url_no_commit =
      embedded_test_server()->GetURL("/page204.html");

  // Get the source id from the committed navigation.
  const ukm::SourceId source_id =
      NavigateAndGetSource(test_url_with_commit, sync_browser, &ukm_test_helper)
          ->id();

  // Initial default state.
  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id));

  // New navigation did not commit, thus the source should still be kept alive.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(sync_browser, test_url_no_commit));
  EXPECT_FALSE(ukm_test_helper.IsSourceObsolete(source_id));
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, DebugUiRenders) {
  test::MetricsConsentOverride metrics_consent(true);

  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  PlatformBrowser browser = CreatePlatformBrowser(profile);

  ukm::UkmService* ukm_service(GetUkmService());
  EXPECT_TRUE(ukm_service->IsSamplingConfigured());

  // chrome://ukm
  g_browser_process->local_state()->SetBoolean(
      chrome_urls::kInternalOnlyUisEnabled, true);
  const GURL debug_url(content::GetWebUIURLString(content::kChromeUIUkmHost));

  content::TestNavigationObserver waiter(debug_url);
  waiter.WatchExistingWebContents();
  EXPECT_TRUE(ui_test_utils::NavigateToURL(browser, debug_url));
  waiter.WaitForNavigationFinished();
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(UkmBrowserTest, AllowedStateChanged) {
  ukm::UkmTestHelper ukm_test_helper(GetUkmService());
  test::MetricsConsentOverride metrics_consent(true);
  Profile* test_profile = ProfileManager::GetLastUsedProfileIfLoaded();
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(test_profile);

  CreatePlatformBrowser(test_profile);

  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(g_browser_process->GetMetricsServicesManager()
                  ->IsUkmAllowedForAllProfiles());

  TestUkmRecorderObserver observer(GetUkmService());
  unified_consent::UnifiedConsentService* consent_service =
      UnifiedConsentServiceFactory::GetForProfile(test_profile);
  consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(false);
#if !BUILDFLAG(IS_CHROMEOS)
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_FALSE(g_browser_process->GetMetricsServicesManager()
                   ->IsUkmAllowedForAllProfiles());
  // Expect nothing to be consented to.
  observer.ExpectAllowedStateChanged(ukm::UkmConsentState());
#else
  // ChromeOS has a different behavior compared to other platforms.
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(g_browser_process->GetMetricsServicesManager()
                  ->IsUkmAllowedForAllProfiles());
  observer.ExpectAllowedStateChanged(ukm::UkmConsentState({ukm::APPS}));
#endif

  consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(true);
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(g_browser_process->GetMetricsServicesManager()
                  ->IsUkmAllowedForAllProfiles());
  observer.ExpectAllowedStateChanged(ukm::UkmConsentState::All());
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(IS_CHROMEOS)
class UkmBrowserTestForAppConsent : public UkmBrowserTestBase {
 public:
  UkmBrowserTestForAppConsent() = default;
};

IN_PROC_BROWSER_TEST_F(UkmBrowserTestForAppConsent, MetricsClientEnablement) {
  ukm::UkmService* ukm_service = GetUkmService();
  ukm::UkmTestHelper ukm_test_helper(ukm_service);
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  unified_consent::UnifiedConsentService* consent_service =
      UnifiedConsentServiceFactory::GetForProfile(profile);
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);

  // All consents are on.
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::MSBB));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::EXTENSIONS));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::APPS));

  // Turn off MSBB consent.
  consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(false);

  // Still have AppKM consent.
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_FALSE(ukm_service->recording_enabled(ukm::MSBB));
  EXPECT_FALSE(ukm_service->recording_enabled(ukm::EXTENSIONS));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::APPS));

  // Turn off App-sync.
  auto* user_settings = harness->service()->GetUserSettings();
  auto registered_os_sync_types =
      user_settings->GetRegisteredSelectableOsTypes();
  registered_os_sync_types.Remove(syncer::UserSelectableOsType::kOsApps);
  user_settings->SetSelectedOsTypes(false, registered_os_sync_types);

  // UKM recording is now disabled since MSBB and App-sync consent
  // has been removed.
  EXPECT_FALSE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_FALSE(ukm_service->recording_enabled(ukm::MSBB));
  EXPECT_FALSE(ukm_service->recording_enabled(ukm::EXTENSIONS));
  EXPECT_FALSE(ukm_service->recording_enabled(ukm::APPS));
}

// LINT.IfChange(ClientIdResetWhenConsentRemoved)
IN_PROC_BROWSER_TEST_F(UkmBrowserTestForAppConsent,
                       ClientIdResetWhenConsentRemoved) {
  ukm::UkmService* ukm_service = GetUkmService();
  ukm::UkmTestHelper ukm_test_helper(ukm_service);
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  unified_consent::UnifiedConsentService* consent_service =
      UnifiedConsentServiceFactory::GetForProfile(profile);
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  const auto original_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(0ul, original_client_id);

  // All consents are on.
  EXPECT_TRUE(ukm_test_helper.IsRecordingEnabled());
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::MSBB));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::EXTENSIONS));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::APPS));

  // Turn off MSBB consent.
  consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(false);

  EXPECT_FALSE(ukm_service->recording_enabled(ukm::MSBB));

  // Client ID should reset when MSBB is disabled.
  const auto app_sync_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(original_client_id, app_sync_client_id);

  // Turn off app sync.
  auto* user_settings = harness->service()->GetUserSettings();
  auto registered_os_sync_types =
      user_settings->GetRegisteredSelectableOsTypes();
  registered_os_sync_types.Remove(syncer::UserSelectableOsType::kOsApps);
  user_settings->SetSelectedOsTypes(false, registered_os_sync_types);

  EXPECT_FALSE(ukm_service->recording_enabled(ukm::APPS));

  // Client ID should reset when app sync is disable.
  const auto final_client_id = ukm_test_helper.GetClientId();
  EXPECT_NE(app_sync_client_id, final_client_id);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/model/ukm_egtest.mm:ClientIdResetWhenConsentRemoved)

IN_PROC_BROWSER_TEST_F(UkmBrowserTestForAppConsent,
                       EnsurePurgeOnConsentChange) {
  ukm::UkmService* ukm_service = GetUkmService();
  ukm::UkmTestHelper ukm_test_helper(ukm_service);
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  unified_consent::UnifiedConsentService* consent_service =
      UnifiedConsentServiceFactory::GetForProfile(profile);
  std::unique_ptr<SyncServiceImplHarness> harness =
      EnableSyncForProfile(profile);
  Browser* sync_browser = CreateBrowser(profile);
  ASSERT_TRUE(embedded_test_server()->Start());

  const std::vector<GURL> test_urls = {
      embedded_test_server()->GetURL("/title1.html"),
      embedded_test_server()->GetURL("/title2.html"),
      embedded_test_server()->GetURL("/title3.html")};

  // Verify all consents are enabled.
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::MSBB));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::EXTENSIONS));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::APPS));

  int tab_index = 1;
  // Generate MSBB ukm entries by navigating to some test webpages.
  for (const auto& url : test_urls) {
    ASSERT_TRUE(AddTabAtIndexToBrowser(sync_browser, tab_index++,
                                       GURL(url::kAboutBlankURL),
                                       ui::PAGE_TRANSITION_TYPED, true));
    NavigateAndGetSource(url, sync_browser, &ukm_test_helper);
  }

  // Revoke MSBB consent.
  consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(false);

  // Verify that MSBB consent was revoked.
  EXPECT_FALSE(ukm_service->recording_enabled(ukm::MSBB));
  EXPECT_FALSE(ukm_service->recording_enabled(ukm::EXTENSIONS));
  EXPECT_TRUE(ukm_service->recording_enabled(ukm::APPS));

  ukm_test_helper.BuildAndStoreLog();
  const std::unique_ptr<ukm::Report> report = ukm_test_helper.GetUkmReport();

  // Verify that the only sources in the report are APP_ID.
  // NOTE(crbug/1395143): It was noticed that there was an APP_ID source
  // generated despite not being explicitly created. No entries are associated
  // with it though.
  for (int i = 0; i < report->sources_size(); ++i) {
    const auto id = report->sources(i).id();
    EXPECT_EQ(ukm::GetSourceIdType(id), ukm::SourceIdType::APP_ID);
  }
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace metrics