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

#include "base/files/file_path.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/subresource_filter/subresource_filter_browser_test_harness.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/features.h"
#include "components/prefs/pref_service.h"
#include "components/subresource_filter/core/common/test_ruleset_utils.h"
#include "components/tpcd/metadata/browser/parser.h"
#include "content/public/browser/cookie_access_details.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_devtools_protocol_client.h"
#include "net/base/features.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/mojom/devtools/inspector_issue.mojom.h"

namespace tpcd::metadata {
namespace {

using ::chrome_test_utils::GetActiveWebContents;

class URLCookieAccessObserver : public content::WebContentsObserver {
 public:
  URLCookieAccessObserver(content::WebContents* web_contents,
                          GURL url,
                          content::CookieAccessDetails::Type access_type);

  void Wait();

 private:
  // WebContentsObserver overrides
  void OnCookiesAccessed(content::RenderFrameHost* render_frame_host,
                         const content::CookieAccessDetails& details) override;
  void OnCookiesAccessed(content::NavigationHandle* navigation_handle,
                         const content::CookieAccessDetails& details) override;

  GURL url_;
  content::CookieAccessDetails::Type access_type_;
  base::RunLoop run_loop_;
};

URLCookieAccessObserver::URLCookieAccessObserver(
    content::WebContents* web_contents,
    GURL url,
    content::CookieAccessDetails::Type access_type)
    : WebContentsObserver(web_contents),
      url_(std::move(url)),
      access_type_(access_type) {}

void URLCookieAccessObserver::Wait() {
  run_loop_.Run();
}

void URLCookieAccessObserver::OnCookiesAccessed(
    content::RenderFrameHost* render_frame_host,
    const content::CookieAccessDetails& details) {
  if (details.type == access_type_ && details.url == url_) {
    run_loop_.Quit();
  }
}

void URLCookieAccessObserver::OnCookiesAccessed(
    content::NavigationHandle* navigation_handle,
    const content::CookieAccessDetails& details) {
  if (details.type == access_type_ && details.url == url_) {
    run_loop_.Quit();
  }
}

bool NavigateToSetCookie(content::WebContents* web_contents,
                         const net::EmbeddedTestServer* server,
                         std::string_view host,
                         bool is_secure_cookie_set,
                         bool is_ad_tagged) {
  std::string relative_url = "/set-cookie?name=value";
  if (is_secure_cookie_set) {
    relative_url += ";Secure;SameSite=None";
  }
  if (is_ad_tagged) {
    relative_url += "&isad=1";
  }
  const auto url = server->GetURL(host, relative_url);

  URLCookieAccessObserver observer(web_contents, url,
                                   content::CookieAccessDetails::Type::kChange);
  bool success = content::NavigateToURL(web_contents, url);
  if (success) {
    observer.Wait();
  }
  return success;
}

void CreateImageAndWaitForCookieAccess(content::WebContents* web_contents,
                                       const GURL& image_url) {
  URLCookieAccessObserver observer(web_contents, image_url,
                                   content::CookieAccessDetails::Type::kRead);
  ASSERT_TRUE(content::ExecJs(web_contents,
                              content::JsReplace(
                                  R"(
    let img = document.createElement('img');
    img.src = $1;
    document.body.appendChild(img);)",
                                  image_url),
                              content::EXECUTE_SCRIPT_NO_USER_GESTURE));
  // The image must cause a cookie access, or else this will hang.
  observer.Wait();
}

}  // namespace

class TpcdMetadataDevtoolsObserverBrowserTest
    : public subresource_filter::SubresourceFilterBrowserTest {
 public:
  explicit TpcdMetadataDevtoolsObserverBrowserTest(
      bool enable_tracking_protection = true,
      bool enable_metadata_feature = true,
      bool enable_staged_control = true)
      : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
    enabled_features_.push_back(
        {network::features::kSkipTpcdMitigationsForAds,
         {{"SkipTpcdMitigationsForAdsMetadata", "true"}}});
    // Since Tracking Protection is always enabled via the field trial config
    // for browser tests, we need to manually disable it.
    if (enable_tracking_protection) {
      enabled_features_.push_back(
          {content_settings::features::kTrackingProtection3pcd, {}});
    } else {
      disabled_features_.push_back(
          content_settings::features::kTrackingProtection3pcd);
    }
    if (enable_metadata_feature) {
      enabled_features_.push_back({net::features::kTpcdMetadataGrants, {}});
    } else {
      disabled_features_.push_back(net::features::kTpcdMetadataGrants);
    }

    enable_staged_control_ = enable_staged_control;
    if (!enable_staged_control) {
      disabled_features_.push_back(net::features::kTpcdMetadataStageControl);
    }

    feature_list_.InitWithFeaturesAndParameters(enabled_features_,
                                                disabled_features_);
  }

  void SetUpOnMainThread() override {
    ASSERT_TRUE(embedded_test_server()->Start());
    host_resolver()->AddRule("a.test", "127.0.0.1");
    host_resolver()->AddRule("b.test", "127.0.0.1");
    host_resolver()->AddRule("c.test", "127.0.0.1");

    // Set up HTTPS server with image for cookie.
    https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
    https_server_.AddDefaultHandlers(
        base::FilePath(FILE_PATH_LITERAL("chrome/test/data")));
    ASSERT_TRUE(https_server_.Start());

    // Open and reset DevTools.
    web_contents_devtools_client.AttachToWebContents(
        GetActiveWebContents(this));
    web_contents_devtools_client.SendCommandSync("Audits.enable");
    web_contents_devtools_client.SendCommandSync("Network.enable");
    web_contents_devtools_client.ClearNotifications();

    // Initialize mock 3PCD metadata component.
    const std::string first_party_pattern_spec = "[*.]a.test";
    const std::string third_party_pattern_spec_1 = "[*.]b.test";
    const std::string third_party_pattern_spec_2 = "c.test";

    Metadata metadata;
    // If staged control is enabled, DTRP values must be set to 0 to avoid
    // flakiness if the entry is dropped. If staged control is disabled, the
    // values can be tested.
    std::optional<uint32_t> dtrp =
        enable_staged_control_ ? std::nullopt : std::make_optional(50u);
    std::optional<uint32_t> dtrp_override =
        enable_staged_control_ ? std::nullopt : std::make_optional(20u);
    tpcd::metadata::helpers::AddEntryToMetadata(
        metadata, third_party_pattern_spec_1, first_party_pattern_spec,
        Parser::kSource1pDt, dtrp);
    tpcd::metadata::helpers::AddEntryToMetadata(
        metadata, third_party_pattern_spec_2, first_party_pattern_spec,
        Parser::kSource3pDt, dtrp, dtrp_override);
    tpcd::metadata::Parser::GetInstance()->ParseMetadata(
        metadata.SerializeAsString());

    // Initialize devtools WebContentsObserver.
    devtools_observer_ = TpcdMetadataDevtoolsObserver::FromWebContents(
        GetActiveWebContents(this));

    // These rules apply an ad-tagging param to cookies marked with the `isad=1`
    // param value.
    SetRulesetWithRules(
        {subresource_filter::testing::CreateSuffixRule("isad=1")});
  }

  void TearDownOnMainThread() override {
    web_contents_devtools_client.DetachProtocolClient();
    frame_devtools_client.DetachProtocolClient();
    devtools_observer_ = nullptr;
  }

 protected:
  void AddCookieAccess(const std::string& first_party_site,
                       const std::string& third_party_site,
                       bool is_ad_tagged) {
    ASSERT_TRUE(NavigateToSetCookie(
        GetActiveWebContents(this), &https_server_, third_party_site,
        /*is_secure_cookie_set=*/true, is_ad_tagged));

    ASSERT_TRUE(content::NavigateToURL(
        GetActiveWebContents(this),
        embedded_test_server()->GetURL(first_party_site, "/title1.html")));

    std::string relative_url = "/favicon/icon.png";
    if (is_ad_tagged) {
      relative_url += "?isad=1";
    }
    CreateImageAndWaitForCookieAccess(
        GetActiveWebContents(this),
        https_server_.GetURL(third_party_site, relative_url));
  }

  void WaitForMetadataIssueAndCheck(const std::vector<std::string>& sites,
                                    uint32_t opt_out_percentage,
                                    bool is_opt_out_top_level) {
    auto is_metadata_issue = [](const base::Value::Dict& params) {
      const std::string* issue_code =
          params.FindStringByDottedPath("issue.code");
      return issue_code && *issue_code == "CookieDeprecationMetadataIssue";
    };

    // Wait for notification of a Metadata Issue.
    base::Value::Dict params =
        web_contents_devtools_client.WaitForMatchingNotification(
            "Audits.issueAdded", base::BindRepeating(is_metadata_issue));
    const std::string* issue_code = params.FindStringByDottedPath("issue.code");
    ASSERT_TRUE(issue_code);
    ASSERT_EQ(*issue_code, "CookieDeprecationMetadataIssue");

    base::Value::Dict* metadata_issue_details = params.FindDictByDottedPath(
        "issue.details.cookieDeprecationMetadataIssueDetails");
    ASSERT_TRUE(metadata_issue_details);

    // Verify the reported allowed sites match the expected sites.
    std::vector<std::string> allowed_sites;
    base::Value::List* allowed_sites_list =
        metadata_issue_details->FindList("allowedSites");
    if (allowed_sites_list) {
      for (const auto& val : *allowed_sites_list) {
        allowed_sites.push_back(val.GetString());
      }
    }
    EXPECT_THAT(allowed_sites, testing::ElementsAreArray(sites));

    // Verify the reported DTRP values against the expected values.
    EXPECT_EQ(
        static_cast<uint32_t>(
            metadata_issue_details->FindInt("optOutPercentage").value_or(0)),
        opt_out_percentage);
    EXPECT_EQ(
        metadata_issue_details->FindBool("isOptOutTopLevel").value_or(false),
        is_opt_out_top_level);
    EXPECT_THAT(
        *metadata_issue_details,
        base::test::DictionaryHasValue("operation", base::Value("ReadCookie")));

    // Clear existing notifications so subsequent calls don't fail by checking
    // `sites` against old notifications.
    web_contents_devtools_client.ClearNotifications();
  }

  void WaitForCookieIssueAndCheck(std::string_view third_party_site,
                                  std::string_view warning,
                                  std::string_view exclusion) {
    CHECK(warning.empty() || exclusion.empty())
        << "inclusion reason and exclusion reason should not co-exist";
    auto is_cookie_issue = [](const base::Value::Dict& params) {
      const std::string* issue_code =
          params.FindStringByDottedPath("issue.code");
      return issue_code && *issue_code == "CookieIssue";
    };

    // Wait for notification of a Cookie Issue.
    base::Value::Dict params =
        web_contents_devtools_client.WaitForMatchingNotification(
            "Audits.issueAdded", base::BindRepeating(is_cookie_issue));

    std::string_view reason_name =
        warning.empty() ? "cookieExclusionReasons" : "cookieWarningReasons";
    std::string_view reason_value = warning.empty() ? exclusion : warning;
    std::string partial_expected =
        content::JsReplace(R"({
            "cookie": {
               "domain": $1,
               "name": "name",
               "path": "/"
            },
            $2: [ $3 ],
            "operation": "ReadCookie",
         })",
                           third_party_site, reason_name, reason_value);

    // Find relevant fields from cookieIssueDetails
    ASSERT_THAT(params.FindDictByDottedPath("issue.details.cookieIssueDetails"),
                testing::Pointee(base::test::DictionaryHasValues(
                    base::test::ParseJsonDict(partial_expected))));

    web_contents_devtools_client.ClearNotifications();
  }

  void CheckNoAddedIssue() {
    ReportDummyIssue();

    WaitForMetadataIssueAndCheck({"dummy.test"}, 0u, false);
  }

  void SendSetCookieControls(bool enable_third_party_cookie_restriction,
                             bool disable_third_party_cookie_metadata,
                             bool disable_third_party_cookie_heuristics) {
    base::Value::Dict command_params;
    command_params.Set("enableThirdPartyCookieRestriction",
                       enable_third_party_cookie_restriction);
    command_params.Set("disableThirdPartyCookieMetadata",
                       disable_third_party_cookie_metadata);
    command_params.Set("disableThirdPartyCookieHeuristics",
                       disable_third_party_cookie_heuristics);
    web_contents_devtools_client.SendCommandSync("Network.setCookieControls",
                                                 std::move(command_params));
  }

  content::RenderFrameHost* GetFrame() {
    return ChildFrameAt(GetActiveWebContents(this)->GetPrimaryMainFrame(), 0);
  }

  void NavigateToPageWith3pIFrame(std::string_view host) {
    frame_devtools_client.DetachProtocolClient();
    GURL main_url(https_server().GetURL(host, "/iframe.html"));

    ASSERT_TRUE(content::NavigateToURL(GetActiveWebContents(this), main_url));
    EXPECT_TRUE(
        NavigateIframeToURL(GetActiveWebContents(this), "test",
                            https_server().GetURL("b.test", "/blank.html")));

    frame_devtools_client.AttachToFrameTreeHost(GetFrame());
    frame_devtools_client.SendCommandSync("Network.enable");
  }

  std::string SetCookieFromJS(content::RenderFrameHost* render_frame_host,
                              std::string cookie) {
    content::EvalJsResult result = content::EvalJs(
        render_frame_host,
        "document.cookie = '" + cookie + "; SameSite=None; Secure'",
        content::EXECUTE_SCRIPT_NO_USER_GESTURE);

    return result.ExtractString();
  }

  std::string ReadCookiesFromJS(content::RenderFrameHost* render_frame_host) {
    std::string res = content::EvalJs(render_frame_host, "document.cookie",
                                      content::EXECUTE_SCRIPT_NO_USER_GESTURE)
                          .ExtractString();

    return res;
  }

  net::test_server::EmbeddedTestServer& https_server() { return https_server_; }

  content::TestDevToolsProtocolClient web_contents_devtools_client;
  content::TestDevToolsProtocolClient frame_devtools_client;

 private:
  void ReportDummyIssue() {
    auto details = blink::mojom::InspectorIssueDetails::New();

    auto metadata_issue_details =
        blink::mojom::CookieDeprecationMetadataIssueDetails::New();
    metadata_issue_details->allowed_sites.push_back("dummy.test");
    metadata_issue_details->operation =
        blink::mojom::CookieOperation::kReadCookie;
    details->cookie_deprecation_metadata_issue_details =
        std::move(metadata_issue_details);

    GetActiveWebContents(this)->GetPrimaryMainFrame()->ReportInspectorIssue(
        blink::mojom::InspectorIssueInfo::New(
            blink::mojom::InspectorIssueCode::kCookieDeprecationMetadataIssue,
            std::move(details)));
  }

  bool enable_staged_control_ = true;
  base::test::ScopedFeatureList feature_list_;
  std::vector<base::test::FeatureRefAndParams> enabled_features_;
  std::vector<base::test::FeatureRef> disabled_features_;
  net::EmbeddedTestServer https_server_;
  raw_ptr<TpcdMetadataDevtoolsObserver> devtools_observer_ = nullptr;
};

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverBrowserTest,
                       EmitsDevtoolsIssues) {
  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  WaitForMetadataIssueAndCheck({"b.test"}, 0u, true);

  AddCookieAccess("a.test", "c.test", /*is_ad_tagged=*/false);
  WaitForMetadataIssueAndCheck({"c.test"}, 0u, false);
}

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverBrowserTest,
                       DoesNotEmitMetadataIssueWhenDevToolsDisableMetadata) {
  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  WaitForMetadataIssueAndCheck({"b.test"}, 0u, true);

  SendSetCookieControls(/*enable_third_party_cookie_restriction=*/true,
                        /*disable_third_party_cookie_metadata=*/true,
                        /*disable_third_party_cookie_heuristics=*/false);

  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  CheckNoAddedIssue();
}

class TpcdMetadataDevtoolsObserverTrackingProtectionDisabledBrowserTest
    : public TpcdMetadataDevtoolsObserverBrowserTest {
 public:
  TpcdMetadataDevtoolsObserverTrackingProtectionDisabledBrowserTest()
      : TpcdMetadataDevtoolsObserverBrowserTest(
            /*enable_tracking_protection=*/false) {}
};

IN_PROC_BROWSER_TEST_F(
    TpcdMetadataDevtoolsObserverTrackingProtectionDisabledBrowserTest,
    EmitCookieIssueWhenDevToolsBlockTPC) {
  SendSetCookieControls(/*enable_third_party_cookie_restriction=*/true,
                        /*disable_third_party_cookie_metadata=*/false,
                        /*disable_third_party_cookie_heuristics=*/false);

  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  WaitForCookieIssueAndCheck("b.test", {"WarnDeprecationTrialMetadata"}, {});
}

IN_PROC_BROWSER_TEST_F(
    TpcdMetadataDevtoolsObserverTrackingProtectionDisabledBrowserTest,
    EmitMetadataIssueWhenDevToolsBlockTPC) {
  SendSetCookieControls(/*enable_third_party_cookie_restriction=*/true,
                        /*disable_third_party_cookie_metadata=*/false,
                        /*disable_third_party_cookie_heuristics=*/false);

  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  WaitForMetadataIssueAndCheck({"b.test"}, 0u, true);
}

IN_PROC_BROWSER_TEST_F(
    TpcdMetadataDevtoolsObserverTrackingProtectionDisabledBrowserTest,
    EmitCookieIssueWhenDevToolsDisableMetadata) {
  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  WaitForCookieIssueAndCheck("b.test", {"WarnThirdPartyPhaseout"}, {});

  SendSetCookieControls(/*enable_third_party_cookie_restriction=*/true,
                        /*disable_third_party_cookie_metadata=*/true,
                        /*disable_third_party_cookie_heuristics=*/false);

  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  // Since the cookie is no longer exempted by metadata,
  // ExcludeThirdPartyPhaseout cookie issue should be present.
  WaitForCookieIssueAndCheck("b.test", {}, {"ExcludeThirdPartyPhaseout"});
}

IN_PROC_BROWSER_TEST_F(
    TpcdMetadataDevtoolsObserverTrackingProtectionDisabledBrowserTest,
    DevToolsDisableMetadataJS) {
  SendSetCookieControls(/*enable_third_party_cookie_restriction=*/true,
                        /*disable_third_party_cookie_metadata=*/true,
                        /*disable_third_party_cookie_heuristics=*/false);

  NavigateToPageWith3pIFrame("a.test");

  // Neither of these commands should work.
  SetCookieFromJS(GetFrame(), "nonExistentCookie=value");
  EXPECT_EQ(ReadCookiesFromJS(GetFrame()), "");

  // Reenabling the metadata exemption. Cookie should now get set.
  SendSetCookieControls(/*enable_third_party_cookie_restriction=*/true,
                        /*disable_third_party_cookie_metadata=*/false,
                        /*disable_third_party_cookie_heuristics=*/false);

  // Refreshing so that RCM is re-created with new controls
  NavigateToPageWith3pIFrame("a.test");

  // Should now be unblocked by metadata and return the new cookie we set.
  SetCookieFromJS(GetFrame(), "cookie=false");
  EXPECT_EQ(ReadCookiesFromJS(GetFrame()), "cookie=false");
}

// Setting the DTRP values in the issue needs to be tested with the flag off.
// Otherwise, a non-zero DTRP value might filter the entry and the issue will
// never fire.
class TpcdMetadataDevtoolsObserverDtrpDisabledBrowserTest
    : public TpcdMetadataDevtoolsObserverBrowserTest {
 public:
  TpcdMetadataDevtoolsObserverDtrpDisabledBrowserTest()
      : TpcdMetadataDevtoolsObserverBrowserTest(
            /*enable_tracking_protection=*/true,
            /*enable_metadata_feature=*/true,
            /*enable_staged_control=*/false) {}
};

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverDtrpDisabledBrowserTest,
                       EmitsDevtoolsIssuesWithDtrpValues) {
  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  WaitForMetadataIssueAndCheck({"b.test"}, 50u, true);

  AddCookieAccess("a.test", "c.test", /*is_ad_tagged=*/false);
  WaitForMetadataIssueAndCheck({"c.test"}, 20u, false);
}

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverBrowserTest,
                       EmitsDevtoolsIssuesForExemption) {
  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);
  WaitForCookieIssueAndCheck(
      "b.test", /*warning=*/{"WarnDeprecationTrialMetadata"}, /*exclusion=*/{});

  AddCookieAccess("a.test", "c.test", /*is_ad_tagged=*/false);
  WaitForCookieIssueAndCheck(
      "c.test", /*warning=*/{"WarnDeprecationTrialMetadata"}, /*exclusion=*/{});
}

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverBrowserTest,
                       DoesNotEmitDevtoolsIssueForSiteNotInAllowlist) {
  AddCookieAccess("b.test", "a.test", /*is_ad_tagged=*/false);

  CheckNoAddedIssue();
}

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverBrowserTest,
                       DoesNotEmitDevtoolsIssueWithBlockedCookiesSetting) {
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockAll3pcToggleEnabled,
                                               true);

  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);

  CheckNoAddedIssue();
}

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverBrowserTest,
                       DoesNotEmitDevtoolsIssueForAdTaggedCookie) {
  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/true);

  CheckNoAddedIssue();
}

class TpcdMetadataDevtoolsObserverDisabledBrowserTest
    : public TpcdMetadataDevtoolsObserverBrowserTest {
 public:
  TpcdMetadataDevtoolsObserverDisabledBrowserTest()
      : TpcdMetadataDevtoolsObserverBrowserTest(
            /*enable_tracking_protection=*/true,
            /*enable_metadata_feature=*/false,
            /*enable_staged_control=*/true) {}
};

IN_PROC_BROWSER_TEST_F(TpcdMetadataDevtoolsObserverDisabledBrowserTest,
                       DoesNotEmitDevtoolsIssueWithMissingFeature) {
  AddCookieAccess("a.test", "b.test", /*is_ad_tagged=*/false);

  CheckNoAddedIssue();
}

}  // namespace tpcd::metadata