chromium/chrome/browser/ui/views/permissions/permission_prompt_bubble_base_view_browsertest.cc

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

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

#include "chrome/browser/ui/views/permissions/permission_prompt_bubble_base_view.h"

#include "base/feature_list.h"
#include "base/functional/callback_helpers.h"
#include "base/ranges/algorithm.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/download/download_permission_request.h"
#include "chrome/browser/permissions/quiet_notification_permission_ui_config.h"
#include "chrome/browser/permissions/quiet_notification_permission_ui_state.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/permissions/chip/chip_controller.h"
#include "chrome/browser/ui/views/permissions/permission_prompt_bubble_base_view.h"
#include "chrome/browser/ui/views/permissions/permission_prompt_chip.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/permissions/permission_request_manager_test_api.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/custom_handlers/protocol_handler_registry.h"
#include "components/custom_handlers/register_protocol_handler_permission_request.h"
#include "components/permissions/constants.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_request.h"
#include "components/permissions/permission_ui_selector.h"
#include "components/permissions/permission_util.h"
#include "components/permissions/request_type.h"
#include "components/permissions/test/mock_permission_request.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/cursor_utils.h"
#include "content/public/test/test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/animation/animation.h"
#include "ui/gfx/animation/animation_test_api.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/test/button_test_api.h"
#include "url/gurl.h"

// To run the pixel tests of this file run: browser_tests
// --gtest_filter=BrowserUiTest.Invoke --test-launcher-interactive
// --enable-pixel-output-in-tests --ui=<test name e.g.
// PermissionPromptBubbleBaseViewBrowserTest>.*
//
// Check go/brapp-desktop-pixel-tests for more info.

namespace {
// Test implementation of PermissionUiSelector that always returns a canned
// decision.
class TestQuietNotificationPermissionUiSelector
    : public permissions::PermissionUiSelector {};
}  // namespace

class PermissionPromptBubbleBaseViewBrowserTest : public DialogBrowserTest {};

// Flaky on Mac: http://crbug.com/1502621
#if BUILDFLAG(IS_MAC)
#define MAYBE_AlertAccessibleEvent
#else
#define MAYBE_AlertAccessibleEvent
#endif
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       MAYBE_AlertAccessibleEvent) {}

// Test switching between PermissionChip and PermissionPromptBubbleBaseView and
// make sure no crashes.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       SwitchBetweenChipAndBubble) {}

// crbug.com/989858
#if BUILDFLAG(IS_WIN)
#define MAYBE_ActiveTabClosedAfterRendererCrashesWithPendingPermissionRequest
#else
#define MAYBE_ActiveTabClosedAfterRendererCrashesWithPendingPermissionRequest
#endif
// Regression test for https://crbug.com/933321.
IN_PROC_BROWSER_TEST_F(
    PermissionPromptBubbleBaseViewBrowserTest,
    MAYBE_ActiveTabClosedAfterRendererCrashesWithPendingPermissionRequest) {}

// Host wants to know your location.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_geolocation) {}

// Host wants to show notifications.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_notifications) {}

// Host wants to use your microphone.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_mic) {}

// Host wants to use your camera.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_camera) {}

// Host wants to open email links.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_protocol_handlers) {}

// Host wants to use your MIDI devices.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_midi) {}

// Host wants to access storage from the site in which it's embedded.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_storage_access) {}

// Host wants to trigger multiple downloads.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_downloads) {}

// Shows a permissions bubble with multiple requests.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_multiple) {}

IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       OpenHelpCenterLinkInNewTab) {}

// ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER is ChromeOS only.
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_protected_media) {
  ShowAndVerifyUi();
}
#endif

// Test that the quiet prompt disposition returns the same value when permission
// is not considered abusive (currently only applicable for Notifications) vs.
// when permission is not considered abusive.
IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       DispositionNoAbusiveTest) {}

IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       AcceptedOnceDispositionNoAbusiveTest) {}

IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       PermissionPromptBubbleDisallowsCustomCursors) {}

IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       LoudChipOrAnchoredBubbleIsShownForNonAbusiveRequests) {}

IN_PROC_BROWSER_TEST_F(PermissionPromptBubbleBaseViewBrowserTest,
                       QuietChipIsShownForAbusiveRequests) {}

class PermissionPromptBubbleBaseViewAllowAlwaysFirstBrowserTest
    : public PermissionPromptBubbleBaseViewBrowserTest {};

IN_PROC_BROWSER_TEST_F(
    PermissionPromptBubbleBaseViewAllowAlwaysFirstBrowserTest,
    InvokeUi_geolocation) {}

class PermissionPromptBubbleBaseViewAllowWhileVisitingFirstBrowserTest
    : public PermissionPromptBubbleBaseViewBrowserTest {};

IN_PROC_BROWSER_TEST_F(
    PermissionPromptBubbleBaseViewAllowWhileVisitingFirstBrowserTest,
    InvokeUi_geolocation) {}

class LongOriginPermissionPromptBubbleBaseViewBrowserTest
    : public PermissionPromptBubbleBaseViewBrowserTest {};

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_geolocation) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_notifications) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_mic) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_camera) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_protocol_handlers) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_midi) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_storage_access) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_downloads) {}

IN_PROC_BROWSER_TEST_F(LongOriginPermissionPromptBubbleBaseViewBrowserTest,
                       InvokeUi_multiple) {}