File: dwa_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 (572 lines) | stat: -rw-r--r-- 22,264 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// Copyright 2025 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 "base/test/scoped_feature_list.h"
#include "chrome/browser/browser_process.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/sync_metrics_test_utils.h"
#include "chrome/browser/profiles/profile_manager.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/dwa/dwa_entry_builder.h"
#include "components/metrics/dwa/dwa_recorder.h"
#include "components/metrics/dwa/dwa_service.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/unified_consent/unified_consent_service.h"
#include "content/public/test/browser_test.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser.h"
#else
#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 "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#endif

namespace metrics::dwa {

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

DwaService* GetDwaService() {
  return g_browser_process->GetMetricsServicesManager()->GetDwaService();
}

bool IsDwaAllowedForAllProfiles() {
  return g_browser_process->GetMetricsServicesManager()
      ->IsDwaAllowedForAllProfiles();
}

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

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

#if BUILDFLAG(IS_ANDROID)
  void PreRunTestOnMainThread() override {
    // 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)

  void AssertDwaIsEnabledAndAllowed() const {
    ASSERT_TRUE(metrics::dwa::DwaRecorder::Get()->IsEnabled());
    ASSERT_TRUE(IsDwaAllowedForAllProfiles());
  }

  void AssertDwaRecorderHasMetrics() const {
    ASSERT_TRUE(metrics::dwa::DwaRecorder::Get()->HasEntries());
    ASSERT_TRUE(metrics::dwa::DwaRecorder::Get()->HasPageLoadEvents());
  }

  void ExpectDwaIsDisabledAndDisallowed() const {
    EXPECT_FALSE(metrics::dwa::DwaRecorder::Get()->IsEnabled());
    EXPECT_FALSE(IsDwaAllowedForAllProfiles());
  }

  void ExpectDwaIsEnabledAndAllowed() const {
    EXPECT_TRUE(metrics::dwa::DwaRecorder::Get()->IsEnabled());
    EXPECT_TRUE(IsDwaAllowedForAllProfiles());
  }

  void ExpectDwaRecorderIsEmpty() const {
    EXPECT_FALSE(metrics::dwa::DwaRecorder::Get()->HasEntries());
    EXPECT_FALSE(metrics::dwa::DwaRecorder::Get()->HasPageLoadEvents());
  }

  void RecordTestDwaEntryMetric() {
    ::dwa::DwaEntryBuilder builder("Kangaroo.Jumped");
    builder.SetContent("https://adtech.com");
    builder.SetMetric("Length", 5);
    builder.Record(dwa::DwaRecorder::Get());
  }

  void RecordTestDwaEntryMetricAndPageLoadEvent() {
    RecordTestDwaEntryMetric();
    metrics::dwa::DwaRecorder::Get()->OnPageLoad();
    RecordTestDwaEntryMetric();
  }

  void RecordTestMetricsAndAssertMetricsRecorded() {
    RecordTestDwaEntryMetricAndPageLoadEvent();
    AssertDwaRecorderHasMetrics();
  }

  void SetMsbbConsentState(Profile* profile, bool consent_state) {
    unified_consent::UnifiedConsentService* consent_service =
        UnifiedConsentServiceFactory::GetForProfile(profile);
    ASSERT_NE(consent_service, nullptr);

    if (consent_service) {
      consent_service->SetUrlKeyedAnonymizedDataCollectionEnabled(
          consent_state);
    }
  }

  void SetExtensionsConsentState(Profile* profile, bool consent_state) {
    unified_consent::UnifiedConsentService* consent_service =
        UnifiedConsentServiceFactory::GetForProfile(profile);
    ASSERT_NE(consent_service, nullptr);

    std::unique_ptr<SyncServiceImplHarness> harness =
        test::InitializeProfileForSync(profile, GetFakeServer()->AsWeakPtr());
    EXPECT_TRUE(harness->SetupSync());

    if (consent_state) {
      ASSERT_TRUE(
          harness->EnableSyncForType(syncer::UserSelectableType::kExtensions));
    } else {
      ASSERT_TRUE(
          harness->DisableSyncForType(syncer::UserSelectableType::kExtensions));
    }
  }

#if !BUILDFLAG(IS_CHROMEOS)
  void SetAppsConsentState(Profile* profile, bool consent_state) {
    unified_consent::UnifiedConsentService* consent_service =
        UnifiedConsentServiceFactory::GetForProfile(profile);
    ASSERT_NE(consent_service, nullptr);

    std::unique_ptr<SyncServiceImplHarness> harness =
        test::InitializeProfileForSync(profile, GetFakeServer()->AsWeakPtr());
    EXPECT_TRUE(harness->SetupSync());

    if (consent_state) {
      ASSERT_TRUE(
          harness->EnableSyncForType(syncer::UserSelectableType::kApps));
    } else {
      ASSERT_TRUE(
          harness->DisableSyncForType(syncer::UserSelectableType::kApps));
    }
  }
#endif  // !BUILDFLAG(IS_CHROMEOS)

 protected:
  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 unified consent is not enabled, then DWA will be enabled based
    // on the history sync state.
    SetMsbbConsentState(profile, true);

    return harness;
  }

  // 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
  }

  // Creates a platform-appropriate incognito browser for |profile|.
  PlatformBrowser CreateIncognitoPlatformBrowser(Profile* profile) {
    EXPECT_TRUE(profile->IsOffTheRecord());
#if !BUILDFLAG(IS_ANDROID)
    return CreateIncognitoBrowser(profile);
#else
    // On Android, an incognito platform is the same as a regular platform
    // browser but with an incognito profile. The incognito profile is validated
    // with profile->IsOffTheRecord().
    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)
  }

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

// LINT.IfChange(DwaServiceCheck)
IN_PROC_BROWSER_TEST_F(DwaBrowserTest, DwaServiceCheck) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  dwa::DwaService* dwa_service = GetDwaService();
  dwa::DwaRecorder* dwa_recorder = metrics::dwa::DwaRecorder::Get();

  PlatformBrowser browser = CreatePlatformBrowser(profile);
  ASSERT_TRUE(dwa_recorder->IsEnabled());

  // Records a DWA entry metric.
  RecordTestDwaEntryMetric();
  EXPECT_TRUE(dwa_recorder->HasEntries());
  EXPECT_FALSE(dwa_recorder->HasPageLoadEvents());
  EXPECT_FALSE(dwa_service->unsent_log_store()->has_unsent_logs());

  dwa_recorder->OnPageLoad();
  EXPECT_FALSE(dwa_recorder->HasEntries());
  EXPECT_TRUE(dwa_recorder->HasPageLoadEvents());
  EXPECT_FALSE(dwa_service->unsent_log_store()->has_unsent_logs());

  GetDwaService()->Flush(
      metrics::MetricsLogsEventManager::CreateReason::kPeriodic);
  ExpectDwaRecorderIsEmpty();
  EXPECT_TRUE(dwa_service->unsent_log_store()->has_unsent_logs());

  ClosePlatformBrowser(browser);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/model/dwa_egtest.mm:DwaServiceCheck)

// Make sure that DWA is disabled and purged while an incognito window is open.
// LINT.IfChange(RegularBrowserPlusIncognitoCheck)
IN_PROC_BROWSER_TEST_F(DwaBrowserTest, RegularBrowserPlusIncognitoCheck) {
  dwa::DwaRecorder* dwa_recorder = metrics::dwa::DwaRecorder::Get();
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  // DWA should be enabled and capable of recording metrics when opening the
  // first regular browser.
  PlatformBrowser browser1 = CreatePlatformBrowser(profile);
  ASSERT_TRUE(dwa_recorder->IsEnabled());
  RecordTestDwaEntryMetric();
  EXPECT_TRUE(dwa_recorder->HasEntries());

  // Opening an incognito browser should disable DwaRecorder and metrics should
  // be purged.
  Profile* incognito_profile =
      profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  PlatformBrowser incognito_browser1 =
      CreateIncognitoPlatformBrowser(incognito_profile);
  ASSERT_FALSE(dwa_recorder->IsEnabled());
  EXPECT_FALSE(dwa_recorder->HasEntries());
  RecordTestDwaEntryMetric();
  EXPECT_FALSE(dwa_recorder->HasEntries());

  // Opening another regular browser should not enable DWA.
  PlatformBrowser browser2 = CreatePlatformBrowser(profile);
  ASSERT_FALSE(dwa_recorder->IsEnabled());
  RecordTestDwaEntryMetric();
  EXPECT_FALSE(dwa_recorder->HasEntries());

  // Opening and closing another Incognito browser must not enable DWA.
  PlatformBrowser incognito_browser2 =
      CreateIncognitoPlatformBrowser(incognito_profile);
  ClosePlatformBrowser(incognito_browser2);
  ASSERT_FALSE(dwa_recorder->IsEnabled());
  RecordTestDwaEntryMetric();
  EXPECT_FALSE(dwa_recorder->HasEntries());

  ClosePlatformBrowser(browser2);
  ASSERT_FALSE(dwa_recorder->IsEnabled());
  RecordTestDwaEntryMetric();
  EXPECT_FALSE(dwa_recorder->HasEntries());

  // Closing all incognito browsers should enable DwaRecorder and we should be
  // able to log metrics again.
  ClosePlatformBrowser(incognito_browser1);
  ASSERT_TRUE(dwa_recorder->IsEnabled());
  EXPECT_FALSE(dwa_recorder->HasEntries());
  RecordTestDwaEntryMetric();
  EXPECT_TRUE(dwa_recorder->HasEntries());

  ClosePlatformBrowser(browser1);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/model/dwa_egtest.mm:RegularBrowserPlusIncognitoCheck)

// Make sure opening a regular browser after Incognito doesn't enable DWA.
// LINT.IfChange(IncognitoPlusRegularBrowserCheck)
IN_PROC_BROWSER_TEST_F(DwaBrowserTest, IncognitoPlusRegularBrowserCheck) {
  dwa::DwaRecorder* dwa_recorder = metrics::dwa::DwaRecorder::Get();
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  Profile* incognito_profile =
      profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  PlatformBrowser incognito_browser =
      CreateIncognitoPlatformBrowser(incognito_profile);
  ASSERT_FALSE(dwa_recorder->IsEnabled());

  PlatformBrowser browser = CreatePlatformBrowser(profile);
  ASSERT_FALSE(dwa_recorder->IsEnabled());

  ClosePlatformBrowser(incognito_browser);
  ASSERT_TRUE(dwa_recorder->IsEnabled());

  ClosePlatformBrowser(browser);
}
// LINT.ThenChange(/ios/chrome/browser/metrics/model/dwa_egtest.mm:IncognitoPlusRegularBrowserCheck)

// This test ensures that disabling MSBB UKM consent disables and purges DWA.
// Additionally ensures that DWA is disabled until all UKM consents are enabled.
// LINT.IfChange(UkmMsbbConsentChangeCheck)
IN_PROC_BROWSER_TEST_F(DwaBrowserTest, UkmConsentChangeCheck_Msbb) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  RecordTestDwaEntryMetricAndPageLoadEvent();
  AssertDwaIsEnabledAndAllowed();
  AssertDwaRecorderHasMetrics();

  // Turn off MSBB consent.
  SetMsbbConsentState(profile, /*consent_state=*/false);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turn on MSBB consent.
  SetMsbbConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsEnabledAndAllowed();
  ExpectDwaRecorderIsEmpty();

  // Validate DWA entries and page load events are able to be recorded when all
  // consents are enabled.
  RecordTestMetricsAndAssertMetricsRecorded();
}
// LINT.ThenChange(/ios/chrome/browser/metrics/model/dwa_egtest.mm:UkmMsbbConsentChangeCheck)

// Not enabled on Android because on Android, kApps and kExtensions is not
// registered through UserSelectableType.
#if !BUILDFLAG(IS_ANDROID)
// This test ensures that disabling Extensions UKM consent disables and purges
// DWA. Additionally ensures that DWA is disabled until all UKM consents are
// enabled.
IN_PROC_BROWSER_TEST_F(DwaBrowserTest, UkmConsentChangeCheck_Extensions) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  RecordTestDwaEntryMetricAndPageLoadEvent();
  AssertDwaIsEnabledAndAllowed();
  AssertDwaRecorderHasMetrics();

  // Turn off Extension consent.
  SetExtensionsConsentState(profile, /*consent_state=*/false);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turn on Extension consent.
  SetExtensionsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsEnabledAndAllowed();
  ExpectDwaRecorderIsEmpty();

  // Validate DWA entries and page load events are able to be recorded when all
  // consents are enabled.
  RecordTestMetricsAndAssertMetricsRecorded();
}

// Not enabled on ChromeOS because on ChromeOS, kApps is not registered through
// UserSelectableType but rather through OS settings. This test ensures that
// disabling Apps UKM consent disables and purges DWA. Additionally ensures that
// DWA is disabled until all UKM consents are enabled.
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(DwaBrowserTest, UkmConsentChangeCheck_Apps) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  RecordTestDwaEntryMetricAndPageLoadEvent();
  AssertDwaIsEnabledAndAllowed();
  AssertDwaRecorderHasMetrics();

  // Turn off Apps consent.
  SetAppsConsentState(profile, /*consent_state=*/false);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turn on Apps consent.
  SetAppsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsEnabledAndAllowed();
  ExpectDwaRecorderIsEmpty();

  // Validate DWA entries and page load events are able to be recorded when all
  // consents are enabled.
  RecordTestMetricsAndAssertMetricsRecorded();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

// This test ensures that disabling MSBB and Extensions UKM consents disables
// and purges DWA. Additionally ensures that DWA is disabled until all UKM
// consents are enabled.
IN_PROC_BROWSER_TEST_F(DwaBrowserTest,
                       UkmConsentChangeCheck_MsbbAndExtensions) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  RecordTestDwaEntryMetricAndPageLoadEvent();
  AssertDwaIsEnabledAndAllowed();
  AssertDwaRecorderHasMetrics();

  // Turn off MSBB and Extension consent.
  SetMsbbConsentState(profile, /*consent_state=*/false);
  SetExtensionsConsentState(profile, /*consent_state=*/false);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turning on MSBB should not enable DWA because Extensions consent is still
  // disabled.
  SetMsbbConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turn on Extensions consent.
  SetExtensionsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsEnabledAndAllowed();
  ExpectDwaRecorderIsEmpty();

  // Validate DWA entries and page load events are able to be recorded when all
  // consents are enabled.
  RecordTestMetricsAndAssertMetricsRecorded();
}

// Not enabled on ChromeOS because on ChromeOS, kApps is not registered through
// UserSelectableType but rather through OS settings. This test ensures that
// disabling MSBB and Apps UKM consents disables and purges DWA. Additionally
// ensures that DWA is disabled until all UKM consents are enabled.
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(DwaBrowserTest, UkmConsentChangeCheck_MsbbAndApps) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  RecordTestDwaEntryMetricAndPageLoadEvent();
  AssertDwaIsEnabledAndAllowed();
  AssertDwaRecorderHasMetrics();

  // Turn off MSBB and Apps consent.
  SetMsbbConsentState(profile, /*consent_state=*/false);
  SetAppsConsentState(profile, /*consent_state=*/false);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turning on MSBB should not enable DWA because Apps consent is still
  // disabled.
  SetMsbbConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turn on Apps consent.
  SetAppsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsEnabledAndAllowed();
  ExpectDwaRecorderIsEmpty();

  // Validate DWA entries and page load events are able to be recorded when all
  // consents are enabled.
  RecordTestMetricsAndAssertMetricsRecorded();
}

// This test ensures that disabling Extensions and Apps UKM consents disables
// and purges DWA. Additionally ensures that DWA is disabled until all UKM
// consents are enabled.
IN_PROC_BROWSER_TEST_F(DwaBrowserTest,
                       UkmConsentChangeCheck_ExtensionsAndApps) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  RecordTestDwaEntryMetricAndPageLoadEvent();
  AssertDwaIsEnabledAndAllowed();
  AssertDwaRecorderHasMetrics();

  // Turn off Extensions and Apps consent.
  SetExtensionsConsentState(profile, /*consent_state=*/false);
  SetAppsConsentState(profile, /*consent_state=*/false);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turning on Extensions should not enable DWA because Apps consent is still
  // disabled.
  SetExtensionsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turn on Apps consent.
  SetAppsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsEnabledAndAllowed();
  ExpectDwaRecorderIsEmpty();

  // Validate DWA entries and page load events are able to be recorded when all
  // consents are enabled.
  RecordTestMetricsAndAssertMetricsRecorded();
}

// This test ensures that disabling MSBB, Extensions, and Apps UKM consents
// disables and purges DWA. Additionally ensures that DWA is disabled until all
// UKM consents are enabled.
IN_PROC_BROWSER_TEST_F(DwaBrowserTest,
                       UkmConsentChangeCheck_MsbbAndExtensionsAndApps) {
  test::MetricsConsentOverride metrics_consent(true);
  Profile* profile = ProfileManager::GetLastUsedProfileIfLoaded();
  EnableSyncForProfile(profile);

  RecordTestDwaEntryMetricAndPageLoadEvent();
  AssertDwaIsEnabledAndAllowed();
  AssertDwaRecorderHasMetrics();

  // Turn off MSBB, Apps, and Extensions consent.
  SetMsbbConsentState(profile, /*consent_state=*/false);
  SetExtensionsConsentState(profile, /*consent_state=*/false);
  SetAppsConsentState(profile, /*consent_state=*/false);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turning on Apps consent should not enable DWA because MSBB and Extensions
  // consent are still disabled.
  SetAppsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turning on Extensions should not enable DWA because MSBB is still disabled.
  SetExtensionsConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsDisabledAndDisallowed();
  ExpectDwaRecorderIsEmpty();
  // Turning on MSBB consent should enable DWA.
  SetMsbbConsentState(profile, /*consent_state=*/true);
  ExpectDwaIsEnabledAndAllowed();
  ExpectDwaRecorderIsEmpty();

  // Validate DWA entries and page load events are able to be recorded when all
  // consents are enabled.
  RecordTestMetricsAndAssertMetricsRecorded();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace metrics::dwa