chromium/chrome/browser/extensions/api/context_menus/extension_context_menu_browsertest.cc

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

#include <stddef.h>

#include <memory>
#include <set>
#include <string_view>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/browsertest_util.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/menu_manager_test_observer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/channel_info.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/version_info/channel.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "extensions/browser/extension_host_test_helper.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/state_store.h"
#include "extensions/browser/state_store_test_observer.h"
#include "extensions/browser/test_management_policy.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/common/mojom/view_type.mojom.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/models/menu_model.h"

WebContents;
ContextMenuMatcher;
ContextType;
MenuItem;
ResultCatcher;
MenuModel;

namespace {

constexpr char kPersistentExtensionId[] =;

}  // namespace

class ExtensionContextMenuBrowserTest
    : public extensions::ExtensionBrowserTest {};

class ExtensionContextMenuLazyTest
    : public ExtensionContextMenuBrowserTest,
      public testing::WithParamInterface<ContextType> {};

class ExtensionContextMenuPersistentTest
    : public ExtensionContextMenuBrowserTest {};

// Tests adding a simple context menu item.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, Simple) {}

// Tests that context menus for event page and Service Worker-based
// extensions are stored properly.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, PRE_Persistent) {}

IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, Persistent) {}

// Tests that previous onclick is not fired after updating the menu's onclick,
// and whether setting onclick to null removes the handler.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuPersistentTest, UpdateOnclick) {}

// Tests that updating the first radio item in a radio list from checked to
// unchecked should not work. The radio button should remain checked because
// context menu radio lists should always have one item selected.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest,
                       UpdateCheckedStateOfFirstRadioItem) {}

// Tests that updating a checked radio button (that is not the first item) to be
// unchecked should not work. The radio button should remain checked because
// context menu radio lists should always have one item selected.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest,
                       UpdateCheckedStateOfNonfirstRadioItem) {}

// Tests that setting "documentUrlPatterns" for an item properly restricts
// those items to matching pages.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, Patterns) {}

// Tests registering an item with a very long title that should get truncated in
// the actual menu displayed.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, LongTitle) {}

// Checks that Context Menus are ordered alphabetically by their name when
// extensions have only one single Context Menu item and by the extension name
// when multiples Context Menu items are created.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, TopLevel) {}

// Checks that in |menu|, the item at |index| has type |expected_type| and a
// label of |expected_label|.
static void ExpectLabelAndType(const char* expected_label,
                               MenuModel::ItemType expected_type,
                               const MenuModel& menu,
                               size_t index) {}

// In the separators test we build a submenu with items and separators in two
// different ways - this is used to verify the results in both cases. Separators
// are not included on OS_CHROMEOS.
static void VerifyMenuForSeparatorsTest(const MenuModel& menu) {}

// Tests a number of cases for auto-generated and explicitly added separators.
IN_PROC_BROWSER_TEST_F(ExtensionContextMenuPersistentTest, Separators) {}

// Tests that targetUrlPattern keeps items from appearing when there is no
// target url.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, TargetURLs) {}

// TODO(http://crbug.com/1035062): This test is flaky only with the event
// page-based version. Remove this once that's fixed.
ExtensionContextMenuSWTest;

INSTANTIATE_TEST_SUITE_P();

// Tests adding of context menus in incognito mode.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuSWTest, IncognitoSplit) {}

// Tests that items with a context of frames only appear when the menu is
// invoked in a frame.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, Frames) {}

// Tests that info.frameId is correctly set when the context menu is invoked.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, ClickInFrame) {}

// Tests enabling and disabling a context menu item. The item starts
// enabled.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, StartEnabled) {}

// Tests enabling and disabling a context menu item. The item starts
// disabled.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, StartDisabled) {}

IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, EventPage) {}

// Flaky on Mac and Windows. https://crbug.com/1035062
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_IncognitoSplitContextMenuCount
#else
#define MAYBE_IncognitoSplitContextMenuCount
#endif
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest,
                       MAYBE_IncognitoSplitContextMenuCount) {}

// Tests updating checkboxes' checked state to true and false.
IN_PROC_BROWSER_TEST_P(ExtensionContextMenuLazyTest, UpdateCheckboxes) {}

INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();