chromium/chrome/browser/devtools/protocol/devtools_pwa_browsertest.cc

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

#include <climits>
#include <initializer_list>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>

#include "base/base_paths.h"
#include "base/check.h"
#include "base/functional/function_ref.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/badging/badge_manager.h"
#include "chrome/browser/badging/badge_manager_factory.h"
#include "chrome/browser/devtools/protocol/devtools_protocol_test_support.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/web_applications/proto/web_app_os_integration_state.pb.h"
#include "chrome/browser/web_applications/proto/web_app_proto_package.pb.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/services/app_service/public/cpp/file_handler.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h"

namespace {

WebAppInstallInfo;
WebAppProvider;
ManifestId;

class PWAProtocolTestWithoutApp : public DevToolsProtocolTestBase {};

class PWAProtocolTest : public PWAProtocolTestWithoutApp {};

IN_PROC_BROWSER_TEST_F(PWAProtocolTestWithoutApp, GetOsAppState_CannotFindApp) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetOsAppState) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetOsAppState_WithBadge) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetOsAppState_WithZeroBadge) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetOsAppState_WithBadgeOverInt) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetOsAppState_WithFileHandler) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetProcessedManifest_CannotFindApp) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       GetProcessedManifest_CannotFindApp_WithoutManfiestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       GetProcessedManifest_WithoutManifestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetProcessedManifest_WithManifestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetProcessedManifest_IconWithNoSizes) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetProcessedManifest_MismatchId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       GetProcessedManifest_NotOnPage_WithManifestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, GetProcessedManifest_NotInstallable) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Uninstall) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Uninstall_CannotFindApp) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Uninstall_MissingManifestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromManifest) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromManifest_Twice) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromManifest_NoWebContents) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromManifest_InvalidStartUrl) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       Install_FromManifest_InconsistentAppId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromManifest_HasManifestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       Install_FromManifest_HasManifestId_UrlAsManifestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_Twice) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromManifest_FromUrl) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_FromManifest) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_UpperCase) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_Unreachable) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_UnmatchManifestId) {}

// TODO(crbug.com/331214986): May want a test to trigger the installation
// failure when installing from the url.

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       Install_FromUrl_ValidManifestId_DifferentInstallUrl) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_InconsistentAppId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_NoScheme) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_UnsupportedScheme) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_FromUrl_HasManifestId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Install_Uninstall) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_ReturnsAttachableTargetId) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_AutoAttach) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_NoApp) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_InFullScreenMode) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_FromUrl) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_FromUrl_InScope) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_FromUrl_NoApp) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_FromUrl_InvalidUrl) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, Launch_FromUrl_OutOfScopeUrl) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_MultipleFiles) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_RepeatedFiles) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_MultipleTypes) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       LaunchFilesInApp_OneTypeMultipleFilesPerRequest) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       LaunchFilesInApp_MultipleTypesMultipleFiles) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_PartiallyUnsupported) {}

// TODO(crbug.com/331214986): May want a test to trigger the LaunchFilesInApp
// failure in LaunchApp callback.

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_PartiallyFailed) {}

// The existence of the files should be checked somewhere, but now the launch
// app command still succeed. So this test does not check the return value of
// the command, but only ensure it won't crash the browser.
IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_NonexistentFiles) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_NoFileHandlers) {}

// This scenario does not reach the handler itself, but it's worth ensuring that
// running PWA.launchFilesInApp without "files" parameter would always fail.
IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_NoFilesField) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_NoFile) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, LaunchFilesInApp_UnsupportedFile) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, OpenCurrentPageInApp) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, OpenCurrentPageInApp_NoWebContents) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, OpenCurrentPageInApp_NotInstalled) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       OpenCurrentPageInApp_DifferentManifestIdUrl) {}

// This test should fail since web apps with browser display mode shouldn't be
// reparentable to match the behavior of post-installation reparenting. But now
// the check only applies in WebAppInstallFinalizer::CanReparentTab.
// TODO(crbug.com/339453521): Find a proper way to check the display mode.
IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       DISABLED_OpenCurrentPageInApp_NotReparentable) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       OpenCurrentPageInApp_StillAttachedInNewAppWindow) {}

#if BUILDFLAG(IS_MAC)
// Only macos needs a shortcut to open the page in app.
IN_PROC_BROWSER_TEST_F(PWAProtocolTest, OpenCurrentPageInApp_NoShortcut) {
  // Unlike the PWA.install, web_app::test::InstallWebApp won't create
  // shortcuts.
  InstallWebApp();
  ASSERT_FALSE(
      SendCommandSync("PWA.openCurrentPageInApp",
                      base::Value::Dict{}.Set(
                          "manifestId", InstallableWebAppManifestId().spec())));
  ASSERT_TRUE(ErrorMessageContains({InstallableWebAppManifestId().spec()}));
}
#endif

IN_PROC_BROWSER_TEST_F(PWAProtocolTest, ChangeAppUserSettings_ChangeNothing) {}

#if BUILDFLAG(IS_CHROMEOS)
// Setting linkCapturing on ChromeOS is not supported yet.
// TODO(crbug.com/339453269): Implement setting linkCapturing on ChromeOS.
#define DISABLE_ON_CHROMEOS
#else
#define DISABLE_ON_CHROMEOS(x)
#endif

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       DISABLE_ON_CHROMEOS(ChangeAppUserSettings_NoApp)) {}

// Unlike the NoApp test above, even without changes, the API should check the
// existence of the app and returns an error.
IN_PROC_BROWSER_TEST_F(PWAProtocolTest, ChangeAppUserSettings_NoAppNoChange) {}

IN_PROC_BROWSER_TEST_F(
    PWAProtocolTest,
    DISABLE_ON_CHROMEOS(
        ChangeAppUserSettings_ChangeLinkCapturingAndDisplayMode)) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       ChangeAppUserSettings_ChangeToStandaloneDisplayMode) {}

// Even though supporting on ChromeOS hasn't been implemented, it should not
// crash.
IN_PROC_BROWSER_TEST_F(PWAProtocolTest, ChangeAppUserSettings_NotCrash) {}

IN_PROC_BROWSER_TEST_F(PWAProtocolTest,
                       ChangeAppUserSettings_UnknownDisplayMode) {}

IN_PROC_BROWSER_TEST_F(
    PWAProtocolTest,
    DISABLE_ON_CHROMEOS(ChangeAppUserSettings_DoNotCapture)) {}

// This scenario does not reach the handler itself, but it's worth ensuring that
// running PWA.changeAppUserSettings without manifestId would always fail.
// The same concept applies to other APIs, but since they are using same
// implementation, the test won't be repeated.
IN_PROC_BROWSER_TEST_F(PWAProtocolTest, ChangeAppUserSettings_NoManifestId) {}

}  // namespace