chromium/chrome/browser/permissions/chrome_permission_request_manager_unittest.cc

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <stddef.h>

#include <optional>
#include <string>

#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/json/values_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/permissions/adaptive_quiet_notification_permission_ui_enabler.h"
#include "chrome/browser/permissions/permission_actions_history_factory.h"
#include "chrome/browser/permissions/quiet_notification_permission_ui_config.h"
#include "chrome/browser/permissions/quiet_notification_permission_ui_state.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_actions_history.h"
#include "components/permissions/permission_decision_auto_blocker.h"
#include "components/permissions/permission_request.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/permission_ui_selector.h"
#include "components/permissions/permission_uma_util.h"
#include "components/permissions/permissions_client.h"
#include "components/permissions/pref_names.h"
#include "components/permissions/request_type.h"
#include "components/permissions/test/mock_permission_prompt_factory.h"
#include "components/permissions/test/mock_permission_request.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/site_engagement/content/site_engagement_service.h"
#include "components/ukm/content/source_url_recorder.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/user_manager/scoped_user_manager.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/app_mode/web_app/web_kiosk_app_manager.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#endif

const double kTestEngagementScore =;

class ChromePermissionRequestManagerTest
    : public ChromeRenderViewHostTestHarness,
      public testing::WithParamInterface<std::pair<std::string, bool>> {};

TEST_F(ChromePermissionRequestManagerTest, UMAForSimpleAcceptedGestureBubble) {}

TEST_F(ChromePermissionRequestManagerTest, UMAForSimpleDeniedNoGestureBubble) {}

TEST_F(ChromePermissionRequestManagerTest, UMAForMergedAcceptedBubble) {}

TEST_F(ChromePermissionRequestManagerTest, UMAForMergedDeniedBubble) {}

TEST_F(ChromePermissionRequestManagerTest, UMAForIgnores) {}

TEST_F(ChromePermissionRequestManagerTest,
       NotificationsAdaptiveActivationQuietUIDryRunUKM) {}

TEST_F(ChromePermissionRequestManagerTest,
       NotificationsAdaptiveActivationQuietUIWindowSize) {}

TEST_F(ChromePermissionRequestManagerTest,
       NotificationsUnderClientSideEmbargoAfterSeveralDenies) {}

TEST_F(ChromePermissionRequestManagerTest,
       TestEmbargoForEmbeddedPermissionRequest) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(ChromePermissionRequestManagerTest, TestWebKioskModeSameOrigin) {
  auto request =
      MakeRequestInWebKioskMode(/*url*/ GURL("https://google.com/page"),
                                /*app_url*/ GURL("https://google.com/launch"));

  WaitForBubbleToBeShown();
  // It should be granted by default.
  EXPECT_TRUE(request->granted());
}

TEST_F(ChromePermissionRequestManagerTest, TestWebKioskModeDifferentOrigin) {
  auto request =
      MakeRequestInWebKioskMode(/*url*/ GURL("https://example.com/page"),
                                /*app_url*/ GURL("https://google.com/launch"));

  WaitForBubbleToBeShown();
  // It should not be granted by default.
  EXPECT_FALSE(request->granted());
  EXPECT_TRUE(request->finished());
}

TEST_F(ChromePermissionRequestManagerTest,
       TestWebKioskModeDifferentOriginWhenFeatureIsDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndDisableFeature(
      permissions::features::kAllowMultipleOriginsForWebKioskPermissions);
  SetKioskBrowserPermissionsAllowedForOrigins("https://example.com/page");

  auto request =
      MakeRequestInWebKioskMode(/*url*/ GURL("https://example.com/page"),
                                /*app_url*/ GURL("https://google.com/launch"));

  WaitForBubbleToBeShown();

  // It should not be granted as the origin is allowlisted.
  EXPECT_EQ(request->granted(), false);
  EXPECT_TRUE(request->finished());
}

TEST_P(ChromePermissionRequestManagerTest,
       TestWebKioskModeDifferentOriginWhenAllowedByFeature) {
  base::test::ScopedFeatureList feature_list;
  base::FieldTrialParams feature_params;
  feature_params
      [permissions::feature_params::kWebKioskBrowserPermissionsAllowlist.name] =
          GetParam().first;
  feature_list.InitAndEnableFeatureWithParameters(
      permissions::features::kAllowMultipleOriginsForWebKioskPermissions,
      feature_params);

  auto request =
      MakeRequestInWebKioskMode(/*url*/ GURL("https://example.com/page"),
                                /*app_url*/ GURL("https://google.com/launch"));

  WaitForBubbleToBeShown();

  // It should be granted as the origin is allowlisted.
  EXPECT_EQ(request->granted(), GetParam().second);
  EXPECT_TRUE(request->finished());
}

TEST_P(ChromePermissionRequestManagerTest,
       TestWebKioskModeDifferentOriginAllowedByKioskBrowserPref) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(
      permissions::features::kAllowMultipleOriginsForWebKioskPermissions);
  SetKioskBrowserPermissionsAllowedForOrigins(GetParam().first);

  auto request =
      MakeRequestInWebKioskMode(/*url*/ GURL("https://example.com/page"),
                                /*app_url*/ GURL("https://google.com/launch"));

  WaitForBubbleToBeShown();

  // It should be granted as the origin is allowlisted.
  EXPECT_EQ(request->granted(), GetParam().second);
  EXPECT_TRUE(request->finished());
}

INSTANTIATE_TEST_SUITE_P(
    TestWebKioskModeDifferentOriginWhenAllowedByFeature,
    ChromePermissionRequestManagerTest,
    testing::ValuesIn(
        {std::pair<std::string, bool>("*", false),
         std::pair<std::string, bool>(".example.com", false),
         std::pair<std::string, bool>("example.", false),
         std::pair<std::string, bool>("file://example*", false),
         std::pair<std::string, bool>("invalid-example.com", false),
         std::pair<std::string, bool>("https://example.com", true),
         std::pair<std::string, bool>("https://example.com/sample", true),
         std::pair<std::string, bool>("example.com", true),
         std::pair<std::string, bool>("*://example.com:*/", true),
         std::pair<std::string, bool>("[*.]example.com", true)}));

#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

class ChromePermissionRequestManagerAdaptiveQuietUiActivationTest
    : public ChromePermissionRequestManagerTest {};

TEST_F(ChromePermissionRequestManagerAdaptiveQuietUiActivationTest,
       EnableDisabledQuietNotificationToggleUponThreeConsecutiveBlocks) {}