chromium/chrome/browser/dips/dips_test_utils.h

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

#ifndef CHROME_BROWSER_DIPS_DIPS_TEST_UTILS_H_
#define CHROME_BROWSER_DIPS_DIPS_TEST_UTILS_H_

#include <iosfwd>
#include <string>
#include <string_view>
#include <vector>

#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/scoped_feature_list.h"
#include "base/types/expected.h"
#include "chrome/browser/dips/dips_redirect_info.h"
#include "chrome/browser/dips/dips_service.h"
#include "chrome/browser/dips/dips_utils.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/cookie_access_details.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test_utils.h"
#include "url/gurl.h"

namespace testing {
class MatchResultListener;
}

constexpr char kStorageAccessScript[] =;

StateForURLCallback;

// Helper function to close (and waits for closure of) a `web_contents` tab.
void CloseTab(content::WebContents* web_contents);

// Helper function to open a link to the given URL in a new tab and return the
// new tab's WebContents.
base::expected<content::WebContents*, std::string> OpenInNewTab(
    content::WebContents* original_tab,
    const GURL& url);

// Helper function for performing client side cookie access via JS.
void AccessCookieViaJSIn(content::WebContents* web_contents,
                         content::RenderFrameHost* frame);

// Helper function to navigate to /set-cookie on `host` and wait for
// OnCookiesAccessed() to be called.
bool NavigateToSetCookie(content::WebContents* web_contents,
                         const net::EmbeddedTestServer* server,
                         std::string_view host,
                         bool is_secure_cookie_set,
                         bool is_ad_tagged);

// Helper function for creating an image with a cookie access on the provided
// WebContents.
void CreateImageAndWaitForCookieAccess(content::WebContents* web_contents,
                                       const GURL& image_url);

// Helper function to block until all DIPS storage requests are complete.
inline void WaitOnStorage(DIPSServiceImpl* dips_service) {}

// Helper function to query the `url` state from DIPS storage.
std::optional<StateValue> GetDIPSState(DIPSServiceImpl* dips_service,
                                       const GURL& url);

inline DIPSServiceImpl* GetDipsService(content::WebContents* web_contents) {}

class URLCookieAccessObserver : public content::WebContentsObserver {};

class FrameCookieAccessObserver : public content::WebContentsObserver {};

class RedirectChainObserver : public DIPSService::Observer {};

class UserActivationObserver : public content::WebContentsObserver {};

// Checks that the URLs associated with the UKM entries with the given name are
// as expected. Sorts the URLs so order doesn't matter.
//
// Example usage:
//
// EXPECT_THAT(ukm_recorder, EntryUrlsAre(entry_name, {url1, url2, url3}));
class EntryUrlsAre {};

// Enables or disables a base::Feature.
class ScopedInitFeature {};

// Enables/disables the DIPS Feature.
class ScopedInitDIPSFeature {};

// Waits for a window to open.
class OpenedWindowObserver : public content::WebContentsObserver {};

// Simulate a mouse click and wait for the main frame to receive user
// activation.
void SimulateMouseClickAndWait(content::WebContents*);

// Make a UrlAndSourceId with a randomly-generated UKM source id.
UrlAndSourceId MakeUrlAndId(std::string_view url);

// Cause DIPS to record a stateful client bounce on `bounce_url` to `final_url`.
// The redirect chain will be started by performing a browser-initiated
// navigation to `initial_url`, and terminated by another such navigation to
// `next_url`.
testing::AssertionResult SimulateDipsBounce(content::WebContents*,
                                            const GURL& initial_url,
                                            const GURL& bounce_url,
                                            const GURL& final_url,
                                            const GURL& next_url);

#endif  // CHROME_BROWSER_DIPS_DIPS_TEST_UTILS_H_