Skip to content

Commit

Permalink
Add attribution browsertests for enrollment
Browse files Browse the repository at this point in the history
(cherry picked from commit 7ad8e47)

Bug: 1454840
Change-Id: I4f5b40afb6c3cf52a8744ff0c761857fa81db10d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4627739
Commit-Queue: John Delaney <johnidel@chromium.org>
Reviewed-by: John Delaney <johnidel@chromium.org>
Commit-Queue: Anthony Garant <anthonygarant@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1160855}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4672067
Cr-Commit-Position: refs/branch-heads/5845@{#390}
Cr-Branched-From: 5a5dff6-refs/heads/main@{#1160321}
  • Loading branch information
agarant authored and Chromium LUCI CQ committed Jul 10, 2023
1 parent 91667b1 commit bc7e72d
Showing 1 changed file with 127 additions and 24 deletions.
151 changes: 127 additions & 24 deletions chrome/browser/attribution_reporting/chrome_attribution_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,54 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_settings_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/privacy_sandbox_attestations.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"

// Tests for the Conversion Measurement API that rely on chrome/ layer features.
// UseCounter recording and multiple browser window behavior is not available
// content shell.
class ChromeAttributionBrowserTest : public InProcessBrowserTest {
public:
ChromeAttributionBrowserTest() = default;
ChromeAttributionBrowserTest() {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/
{features::kPrivacySandboxAdsAPIsOverride,
privacy_sandbox::kEnforcePrivacySandboxAttestations},
/*disabled_features=*/{});
}

void SetUpCommandLine(base::CommandLine* command_line) override {
// Sets up the blink runtime feature for ConversionMeasurement.
command_line->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
// Debug mode is needed to skip delays and noise which allows to assert
// that reports are received when expected.
command_line->AppendSwitch(switches::kAttributionReportingDebugMode);
command_line->AppendSwitch(switches::kEnableBlinkTestFeatures);
}

Expand All @@ -40,40 +59,80 @@ class ChromeAttributionBrowserTest : public InProcessBrowserTest {
net::test_server::RegisterDefaultHandlers(&server_);
server_.ServeFilesFromSourceDirectory("content/test/data");
content::SetupCrossSiteRedirector(&server_);
ASSERT_TRUE(server_.Start());
}

protected:
static constexpr char kReportEndpoint[] =
"https://c.test/.well-known/attribution-reporting/"
"report-event-attribution";

content::WebContents* RegisterSourceWithNavigation() {
content::WebContentsAddedObserver window_observer;
EXPECT_TRUE(ui_test_utils::NavigateToURL(
browser(),
server_.GetURL(
"a.test",
"/attribution_reporting/page_with_impression_creator.html")));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();

GURL register_url = server_.GetURL(
"c.test", "/attribution_reporting/register_source_headers.html");
GURL link_url = server_.GetURL(
"d.test", "/attribution_reporting/page_with_conversion_redirect.html");
// Navigate the page using window.open and set an attribution source.
EXPECT_TRUE(
ExecJs(web_contents, content::JsReplace(R"(
window.open($1, "_blank", "attributionsrc="+$2);)",
link_url, register_url)));

content::WebContents* new_contents = window_observer.GetWebContents();
WaitForLoadStop(new_contents);
return new_contents;
}

void RegisterTrigger(content::WebContents* contents) {
GURL register_trigger_url = server_.GetURL(
"c.test", "/attribution_reporting/register_trigger_headers.html");
EXPECT_TRUE(
ExecJs(contents, content::JsReplace("createAttributionSrcImg($1);",
register_trigger_url)));
}

net::EmbeddedTestServer server_{net::EmbeddedTestServer::TYPE_HTTPS};
base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(ChromeAttributionBrowserTest,
WindowOpenWithOnlyAttributionFeatures_LinkOpenedInTab) {
base::HistogramTester histogram_tester;
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
struct ExpectedReportWaiter {
// ControllableHTTPResponses can only wait for relative urls, so only supply
// the path.
ExpectedReportWaiter(GURL report_url, net::EmbeddedTestServer* server)
: expected_url(std::move(report_url)),
response(std::make_unique<net::test_server::ControllableHttpResponse>(
server,
expected_url.path())) {}

EXPECT_TRUE(ui_test_utils::NavigateToURL(
browser(),
server_.GetURL(
"a.test",
"/attribution_reporting/page_with_impression_creator.html")));
GURL expected_url;
std::unique_ptr<net::test_server::ControllableHttpResponse> response;

// Create an observer to catch the opened WebContents.
content::WebContentsAddedObserver window_observer;
// Waits for a report to be received matching the report url.
void WaitForRequest() {
if (!HasRequest()) {
response->WaitForRequest();
}
EXPECT_TRUE(response->http_request()->has_content);
}

GURL register_url = server_.GetURL(
"a.test", "/attribution_reporting/register_source_headers.html");
bool HasRequest() { return response->http_request(); }
};

GURL link_url = server_.GetURL(
"d.test", "/attribution_reporting/page_with_conversion_redirect.html");
// Navigate the page using window.open and set an attribution source.
EXPECT_TRUE(ExecJs(web_contents, content::JsReplace(R"(
window.open($1, "_blank", "attributionsrc="+$2);)",
link_url, register_url)));
IN_PROC_BROWSER_TEST_F(ChromeAttributionBrowserTest,
WindowOpenWithOnlyAttributionFeatures_LinkOpenedInTab) {
ASSERT_TRUE(server_.Start());

content::WebContents* new_contents = window_observer.GetWebContents();
WaitForLoadStop(new_contents);
base::HistogramTester histogram_tester;

content::WebContents* new_contents = RegisterSourceWithNavigation();

// Ensure the window was opened in a new tab. If the window is in a new popup
// the web contents would not belong to the tab strip.
Expand All @@ -82,3 +141,47 @@ IN_PROC_BROWSER_TEST_F(ChromeAttributionBrowserTest,
EXPECT_EQ(BrowserList::GetInstance()->size(), 1u);
EXPECT_EQ(browser()->tab_strip_model()->count(), 2);
}

IN_PROC_BROWSER_TEST_F(ChromeAttributionBrowserTest,
AttestedSiteCanReceiveAttributionReport) {
PrivacySandboxSettingsFactory::GetForProfile(browser()->profile())
->SetAllPrivacySandboxAllowedForTesting();

privacy_sandbox::PrivacySandboxAttestationsMap map;
map.insert_or_assign(net::SchemefulSite(GURL("https://c.test")),
privacy_sandbox::PrivacySandboxAttestationsGatedAPISet{
privacy_sandbox::PrivacySandboxAttestationsGatedAPI::
kAttributionReporting});
privacy_sandbox::PrivacySandboxAttestations::GetInstance()
->SetAttestationsForTesting(map);

ExpectedReportWaiter expected_report(GURL(kReportEndpoint), &server_);
ASSERT_TRUE(server_.Start());

auto* new_contents = RegisterSourceWithNavigation();
RegisterTrigger(new_contents);

expected_report.WaitForRequest();
}

IN_PROC_BROWSER_TEST_F(ChromeAttributionBrowserTest,
UnattestedSiteCannotReceiveAttributionReport) {
PrivacySandboxSettingsFactory::GetForProfile(browser()->profile())
->SetAllPrivacySandboxAllowedForTesting();

// We do not add an attestation for the reporting origin.

ExpectedReportWaiter expected_report(GURL(kReportEndpoint), &server_);
ASSERT_TRUE(server_.Start());

auto* new_contents = RegisterSourceWithNavigation();
RegisterTrigger(new_contents);

// Wait to 2 seconds as reports should be received by then and we want to
// limit the test duration.
base::RunLoop loop_;
base::OneShotTimer timer;
timer.Start(FROM_HERE, base::Seconds(2), loop_.QuitClosure());
loop_.Run();
EXPECT_FALSE(expected_report.HasRequest());
}

0 comments on commit bc7e72d

Please sign in to comment.