chromium/chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h

// Copyright 2022 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_UI_WEB_APPLICATIONS_TEST_ISOLATED_WEB_APP_TEST_UTILS_H_
#define CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_ISOLATED_WEB_APP_TEST_UTILS_H_

#include <memory>
#include <string>
#include <string_view>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "base/version.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_source.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_storage_location.h"
#include "chrome/browser/web_applications/web_app.h"
#include "components/version_info/channel.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "extensions/common/features/feature_channel.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/window_open_disposition.h"

class Browser;
class GURL;
class Profile;

namespace content {
class RenderFrameHost;
}  // namespace content

namespace net::test_server {
class EmbeddedTestServer;
}  // namespace net::test_server

namespace url {
class Origin;
}  // namespace url

namespace web_app {

class IsolatedWebAppUrlInfo;

class IsolatedWebAppBrowserTestHarness : public WebAppBrowserTestBase {};

std::unique_ptr<net::EmbeddedTestServer> CreateAndStartDevServer(
    const base::FilePath::StringPieceType& chrome_test_data_relative_root);

IsolatedWebAppUrlInfo InstallDevModeProxyIsolatedWebApp(
    Profile* profile,
    const url::Origin& proxy_origin);

content::RenderFrameHost* OpenIsolatedWebApp(Profile* profile,
                                             const webapps::AppId& app_id,
                                             std::string_view path = "");

void CreateIframe(content::RenderFrameHost* parent_frame,
                  const std::string& iframe_id,
                  const GURL& url,
                  const std::string& permissions_policy);

// Adds an Isolated Web App to the WebAppRegistrar. The IWA will have an empty
// filepath for |IsolatedWebAppLocation|.
webapps::AppId AddDummyIsolatedAppToRegistry(
    Profile* profile,
    const GURL& start_url,
    const std::string& name,
    const WebApp::IsolationData& isolation_data = WebApp::IsolationData(
        IwaStorageOwnedBundle{};

// Simulates navigating `web_contents` main frame to the provided isolated-app:
// URL for unit tests. `TestWebContents::NavigateAndCommit` won't work for IWAs
// because they require COI headers, but the IsolatedWebAppURLLoaderFactory
// that injects them isn't run in RenderViewHostTestHarness-based unit tests.
void SimulateIsolatedWebAppNavigation(content::WebContents* web_contents,
                                      const GURL& url);

// Commits a pending IWA navigation in `web_contents`. This should be called
// instead of `RenderFrameHostTester::CommitPendingLoad` in IWAs because COI
// headers need to be injected.
void CommitPendingIsolatedWebAppNavigation(content::WebContents* web_contents);

// TODO(cmfcmf): Move more test utils into this `test` namespace
namespace test {
namespace {
AllOf;
ExplainMatchResult;
Field;
Optional;
Pointee;
Property;
}  // namespace

MATCHER_P(IsInIwaRandomDir, profile_directory, "") {}

MATCHER(FileExists, "") {}

MATCHER_P(OwnedIwaBundleExists, profile_directory, "") {}

MATCHER_P2(IwaIs, untranslated_name, isolation_data, "") {}

MATCHER_P5(IsolationDataIs,
           location,
           version,
           controlled_frame_partitions,
           pending_update_info,
           integrity_block_data,
           "") {}

MATCHER_P3(PendingUpdateInfoIs, location, version, integrity_block_data, "") {}

}  // namespace test

}  // namespace web_app

#endif  // CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_ISOLATED_WEB_APP_TEST_UTILS_H_