chromium/chrome/browser/ui/web_applications/web_app_link_capturing_browsertest.cc

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <tuple>

#include "base/location.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/types/expected.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/apps/app_service/app_registry_cache_waiter.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/intent_helper/preferred_apps_test_util.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "chrome/browser/apps/link_capturing/link_capturing_navigation_throttle.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/test/web_app_navigation_browsertest.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/test/web_app_test_observers.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/hit_test_region_observer.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"

RenderFrameHost;
WebContents;
PrerenderHostObserver;
PrerenderHostRegistryObserver;
PrerenderTestHelper;
BrowserChangeObserver;

namespace web_app {
namespace {
ClientMode;

// Tests that links are captured correctly into an installed WebApp using the
// 'tabbed' display mode, which allows the webapp window to have multiple tabs.
class WebAppLinkCapturingBrowserTest
    : public WebAppNavigationBrowserTest,
      public testing::WithParamInterface<bool> {};

// Link capturing with navigate_existing_client: always should navigate existing
// app windows.
IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       NavigateExistingClientFromBrowser) {}

// Link captures from about:blank cleans up the about:blank page.
IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       AboutBlankNavigationCleanUp) {}

// JavaScript initiated link captures from about:blank cleans up the about:blank
// page.
// TODO(crbug.com/40938945): Flaky on Linux and Mac.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_JavascriptAboutBlankNavigationCleanUp
#else
#define MAYBE_JavascriptAboutBlankNavigationCleanUp
#endif
IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       MAYBE_JavascriptAboutBlankNavigationCleanUp) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       DifferentPortConsideredDifferent) {}

// Tests that links to apps from sandboxed iframes can be captured.
IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       HandleClickFromSandboxedIframe) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       ParentAppWithChildLinks) {}

// https://crbug.com/1476011: ChromeOS currently capturing nested app links into
// the parent app, treating them as overlapping apps. Other platforms split the
// URL space and fully respect the child app's user setting.
// Thus, on non-CrOS platforms both apps can capture links.
#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       ParentAppAndChildAppCapture) {}
#endif  // !BUILDFLAG(IS_CHROMEOS)

// Tests that link capturing works while inside a web app window.
// TODO(crbug.com/330148482): Flaky on Linux Debug bots.
#if BUILDFLAG(IS_LINUX) && !defined(NDEBUG)
#define MAYBE_LinkCaptureInWebAppWindow
#else
#define MAYBE_LinkCaptureInWebAppWindow
#endif
IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       MAYBE_LinkCaptureInWebAppWindow) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       NoLinkCaptureOutOfScopeInAppWindow) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       NoLinkCapturePrerenderNavigation) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingBrowserTest,
                       NoLinkCapturePopupNavigation) {}

INSTANTIATE_TEST_SUITE_P();

// TODO: Run these tests on Chrome OS with both Ash and Lacros processes active.
class WebAppTabStripLinkCapturingBrowserTest
    : public WebAppLinkCapturingBrowserTest {};

// First in scope navigation from out of scope gets captured and reparented into
// the app window.
IN_PROC_BROWSER_TEST_P(WebAppTabStripLinkCapturingBrowserTest,
                       InScopeNavigationsCaptured) {}

INSTANTIATE_TEST_SUITE_P();

}  // namespace
}  // namespace web_app