#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include <optional>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/companion/core/features.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/extensions/menu_manager.h"
#include "chrome/browser/extensions/menu_manager_factory.h"
#include "chrome/browser/extensions/test_extension_environment.h"
#include "chrome/browser/extensions/test_extension_prefs.h"
#include "chrome/browser/feed/web_feed_tab_helper.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service_factory.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/password_manager/profile_password_store_factory.h"
#include "chrome/browser/predictors/loading_predictor.h"
#include "chrome/browser/predictors/loading_predictor_factory.h"
#include "chrome/browser/predictors/preconnect_manager.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/passwords/ui_utils.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/search_test_utils.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/content/browser/test_autofill_client_injector.h"
#include "components/autofill/content/browser/test_autofill_driver_injector.h"
#include "components/autofill/content/browser/test_content_autofill_client.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/test_autofill_driver.h"
#include "components/autofill/core/browser/ui/suggestion_hiding_reason.h"
#include "components/autofill/core/browser/ui/suggestion_type.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/custom_handlers/protocol_handler_registry.h"
#include "components/lens/buildflags.h"
#include "components/lens/lens_features.h"
#include "components/password_manager/core/browser/password_manager_test_utils.h"
#include "components/password_manager/core/browser/password_store/mock_password_store_interface.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/search_engines/template_url_service.h"
#include "components/sync/test/test_sync_service.h"
#include "components/translate/core/browser/language_state.h"
#include "components/translate/core/browser/translate_manager.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/common/url_pattern.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/navigation/impression.h"
#include "third_party/blink/public/mojom/context_menu/context_menu.mojom.h"
#include "ui/base/clipboard/clipboard.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/chromeos/policy/dlp/dlp_policy_constants.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager_impl.h"
#include "chrome/browser/chromeos/policy/dlp/test/dlp_rules_manager_test_utils.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "base/test/scoped_command_line.h"
#include "chrome/browser/ash/crosapi/browser_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/standalone_browser/feature_refs.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#endif
Extension;
MenuItem;
MenuManager;
MenuManagerFactory;
URLPatternSet;
namespace {
static content::ContextMenuParams CreateParams(int contexts) { … }
std::unique_ptr<TestRenderViewContextMenu> CreateContextMenu(
content::WebContents* web_contents,
custom_handlers::ProtocolHandlerRegistry* registry) { … }
class TestNavigationDelegate : public content::WebContentsDelegate { … };
#if BUILDFLAG(IS_CHROMEOS)
class MockDlpRulesManager : public policy::DlpRulesManagerImpl {
public:
explicit MockDlpRulesManager(PrefService* local_state, Profile* profile)
: DlpRulesManagerImpl(local_state, profile) {}
};
#endif
}
class RenderViewContextMenuTest : public testing::Test { … };
static URLPatternSet CreatePatternSet(const std::string& pattern) { … }
TEST_F(RenderViewContextMenuTest, TargetIgnoredForPage) { … }
TEST_F(RenderViewContextMenuTest, TargetCheckedForLink) { … }
TEST_F(RenderViewContextMenuTest, TargetCheckedForImage) { … }
TEST_F(RenderViewContextMenuTest, TargetCheckedForVideo) { … }
TEST_F(RenderViewContextMenuTest, TargetCheckedForAudio) { … }
TEST_F(RenderViewContextMenuTest, MatchWhenLinkedImageMatchesTarget) { … }
TEST_F(RenderViewContextMenuTest, MatchWhenLinkedImageMatchesSource) { … }
TEST_F(RenderViewContextMenuTest, NoMatchWhenLinkedImageMatchesNeither) { … }
TEST_F(RenderViewContextMenuTest, TargetIgnoredForFrame) { … }
TEST_F(RenderViewContextMenuTest, TargetIgnoredForEditable) { … }
TEST_F(RenderViewContextMenuTest, TargetIgnoredForSelection) { … }
TEST_F(RenderViewContextMenuTest, TargetIgnoredForSelectionOnLink) { … }
TEST_F(RenderViewContextMenuTest, TargetIgnoredForSelectionOnImage) { … }
class RenderViewContextMenuExtensionsTest : public RenderViewContextMenuTest { … };
TEST_F(RenderViewContextMenuExtensionsTest,
ItemWithSameTitleFromTwoExtensions) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
using RenderViewContextMenuDeveloperItemsTest = ChromeRenderViewHostTestHarness;
TEST_F(RenderViewContextMenuDeveloperItemsTest,
DeveloperItemsAreNotPresentByDefaultIfAshBrowserIsDisabled) {
base::test::ScopedFeatureList features;
features.InitWithFeatures(ash::standalone_browser::GetFeatureRefs(), {});
base::test::ScopedCommandLine scoped_command_line;
scoped_command_line.GetProcessCommandLine()->AppendSwitch(
ash::switches::kEnableLacrosForTesting);
auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
auto* primary_user =
fake_user_manager->AddUser(AccountId::FromUserEmail("test@test"));
fake_user_manager->UserLoggedIn(primary_user->GetAccountId(),
primary_user->username_hash(),
false,
false);
auto scoped_user_manager = std::make_unique<user_manager::ScopedUserManager>(
std::move(fake_user_manager));
ASSERT_FALSE(crosapi::browser_util::IsAshDevToolEnabled());
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->Init();
EXPECT_FALSE(menu->IsItemPresent(IDC_VIEW_SOURCE));
EXPECT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
}
TEST_F(RenderViewContextMenuDeveloperItemsTest,
DeveloperItemsArePresentIfAshBrowserIsDisabledAndFlagIsEnabled) {
base::test::ScopedFeatureList features;
std::vector<base::test::FeatureRef> enabled =
ash::standalone_browser::GetFeatureRefs();
enabled.push_back(ash::features::kAllowDevtoolsInSystemUI);
features.InitWithFeatures(enabled, {});
base::test::ScopedCommandLine scoped_command_line;
scoped_command_line.GetProcessCommandLine()->AppendSwitch(
ash::switches::kEnableLacrosForTesting);
auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
auto* primary_user =
fake_user_manager->AddUser(AccountId::FromUserEmail("test@test"));
fake_user_manager->UserLoggedIn(primary_user->GetAccountId(),
primary_user->username_hash(),
false,
false);
auto scoped_user_manager = std::make_unique<user_manager::ScopedUserManager>(
std::move(fake_user_manager));
ASSERT_TRUE(crosapi::browser_util::IsAshDevToolEnabled());
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->Init();
EXPECT_TRUE(menu->IsItemPresent(IDC_VIEW_SOURCE));
EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_INSPECTELEMENT));
}
#endif
class RenderViewContextMenuPrefsTest
: public ChromeRenderViewHostTestHarness,
public predictors::PreconnectManager::Observer { … };
TEST_F(RenderViewContextMenuPrefsTest,
DisableOpenInIncognitoWindowWhenIncognitoIsDisabled) { … }
#if BUILDFLAG(IS_CHROMEOS)
class RenderViewContextMenuDlpPrefsTest
: public RenderViewContextMenuPrefsTest {
public:
RenderViewContextMenuDlpPrefsTest() = default;
RenderViewContextMenuDlpPrefsTest(const RenderViewContextMenuDlpPrefsTest&) =
delete;
RenderViewContextMenuDlpPrefsTest& operator=(
const RenderViewContextMenuDlpPrefsTest&) = delete;
void SetDlpClipboardRestriction() {
policy::dlp_test_util::DlpRule rule("Rule #1", "Block", "testid1");
rule.AddSrcUrl(PAGE_URL)
.AddDstUrl(RESTRICTED_URL)
.AddRestriction(data_controls::kRestrictionClipboard,
data_controls::kLevelBlock);
base::Value::List rules;
rules.Append(rule.Create());
local_state()->SetList(policy::policy_prefs::kDlpRulesList,
std::move(rules));
}
static constexpr char PAGE_URL[] = "http://www.foo.com/";
static constexpr char RESTRICTED_URL[] = "http://www.bar.com/";
static constexpr char NOT_RESTRICTED_URL[] = "http://www.site.com/";
};
TEST_F(RenderViewContextMenuDlpPrefsTest,
DisableOpenLinkInNewTabWhenClipboardIsBlocked) {
content::ContextMenuParams params = CreateParams(MenuItem::LINK);
params.page_url = GURL(PAGE_URL);
params.link_url = GURL(RESTRICTED_URL);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(nullptr);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
TestingProfile profile;
MockDlpRulesManager mock_dlp_rules_manager(local_state(), &profile);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
SetDlpClipboardRestriction();
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
params.link_url = GURL(NOT_RESTRICTED_URL);
menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
}
TEST_F(RenderViewContextMenuDlpPrefsTest,
DisableOpenLinkInNewWindowWhenClipboardIsBlocked) {
content::ContextMenuParams params = CreateParams(MenuItem::LINK);
params.page_url = GURL(PAGE_URL);
params.link_url = GURL(RESTRICTED_URL);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(nullptr);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
TestingProfile profile;
MockDlpRulesManager mock_dlp_rules_manager(local_state(), &profile);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
SetDlpClipboardRestriction();
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
params.link_url = GURL(NOT_RESTRICTED_URL);
menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
}
TEST_F(RenderViewContextMenuDlpPrefsTest,
DisableOpenLinkInProfileTabWhenClipboardIsBlocked) {
content::ContextMenuParams params = CreateParams(MenuItem::LINK);
params.page_url = GURL(PAGE_URL);
params.link_url = GURL(RESTRICTED_URL);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(nullptr);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE));
TestingProfile profile;
MockDlpRulesManager mock_dlp_rules_manager(local_state(), &profile);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE));
SetDlpClipboardRestriction();
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE));
params.link_url = GURL(NOT_RESTRICTED_URL);
menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKINPROFILE));
}
TEST_F(RenderViewContextMenuDlpPrefsTest,
DisableOpenLinkInWebAppWhenClipboardIsBlocked) {
content::ContextMenuParams params = CreateParams(MenuItem::LINK);
params.page_url = GURL(PAGE_URL);
params.link_url = GURL(RESTRICTED_URL);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(nullptr);
EXPECT_TRUE(
menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
TestingProfile profile;
MockDlpRulesManager mock_dlp_rules_manager(local_state(), &profile);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(
menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
SetDlpClipboardRestriction();
EXPECT_FALSE(
menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
params.link_url = GURL(NOT_RESTRICTED_URL);
menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(
menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_OPENLINKBOOKMARKAPP));
}
TEST_F(RenderViewContextMenuDlpPrefsTest,
DisableGoToURLWhenClipboardIsBlocked) {
content::ContextMenuParams params = CreateParams(MenuItem::SELECTION);
params.page_url = GURL(PAGE_URL);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(nullptr);
menu->set_selection_navigation_url(GURL(RESTRICTED_URL));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_GOTOURL));
TestingProfile profile;
MockDlpRulesManager mock_dlp_rules_manager(local_state(), &profile);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_GOTOURL));
SetDlpClipboardRestriction();
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_GOTOURL));
menu->set_selection_navigation_url(GURL(NOT_RESTRICTED_URL));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_GOTOURL));
}
TEST_F(RenderViewContextMenuDlpPrefsTest,
DisableSearchWebForWhenClipboardIsBlocked) {
content::ContextMenuParams params = CreateParams(MenuItem::SELECTION);
params.page_url = GURL(PAGE_URL);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(nullptr);
menu->set_selection_navigation_url(GURL(RESTRICTED_URL));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFOR));
TestingProfile profile;
MockDlpRulesManager mock_dlp_rules_manager(local_state(), &profile);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFOR));
SetDlpClipboardRestriction();
EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFOR));
menu->set_selection_navigation_url(GURL(NOT_RESTRICTED_URL));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFOR));
}
TEST_F(RenderViewContextMenuDlpPrefsTest,
DisableSearchWebForNewTabWhenClipboardIsBlocked) {
content::ContextMenuParams params = CreateParams(MenuItem::SELECTION);
params.page_url = GURL(PAGE_URL);
auto menu = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), params);
menu->set_dlp_rules_manager(nullptr);
menu->set_selection_navigation_url(GURL(RESTRICTED_URL));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFORNEWTAB));
TestingProfile profile;
MockDlpRulesManager mock_dlp_rules_manager(local_state(), &profile);
menu->set_dlp_rules_manager(&mock_dlp_rules_manager);
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFORNEWTAB));
SetDlpClipboardRestriction();
EXPECT_FALSE(
menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFORNEWTAB));
menu->set_selection_navigation_url(GURL(NOT_RESTRICTED_URL));
EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_SEARCHWEBFORNEWTAB));
}
#endif
TEST_F(RenderViewContextMenuPrefsTest,
DisableOpenInIncognitoWindowForDisallowedUrls) { … }
TEST_F(RenderViewContextMenuPrefsTest, IsCustomCommandIdEnabled) { … }
TEST_F(RenderViewContextMenuPrefsTest, LoadBrokenImage) { … }
TEST_F(RenderViewContextMenuPrefsTest, SaveMediaSuggestedFileName) { … }
TEST_F(RenderViewContextMenuPrefsTest, OpenLinkNavigationParamsSet) { … }
TEST_F(RenderViewContextMenuPrefsTest, OpenLinkNavigationInitiatorSet) { … }
TEST_F(RenderViewContextMenuPrefsTest, ShowAllPasswords) { … }
TEST_F(RenderViewContextMenuPrefsTest, ShowAllPasswordsIncognito) { … }
TEST_F(RenderViewContextMenuPrefsTest,
SaveAsDisabledByDownloadRestrictionsPolicy) { … }
TEST_F(RenderViewContextMenuPrefsTest,
SaveAsDisabledByAllowFileSelectionDialogsPolicy) { … }
TEST_F(RenderViewContextMenuPrefsTest,
SearchWebForOptionOnPasswordsManagerSubPageIsDisabled) { … }
TEST_F(RenderViewContextMenuPrefsTest,
SearchWebForOptionOnPasswordManagerCheckIsDisabled) { … }
TEST_F(RenderViewContextMenuPrefsTest,
SearchWebForOptionOnPasswordManagerSettingsIsDisabled) { … }
class RenderViewContextMenuHideAutofillSuggestionsTest
: public RenderViewContextMenuPrefsTest { … };
TEST_F(RenderViewContextMenuHideAutofillSuggestionsTest,
HideAutofillSuggestions) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensImageSearchNonImage) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensImageSearchNoBrowser) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensImageSearchEnabled) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(RenderViewContextMenuPrefsTest,
LensImageSearchDisabledIfAshBrowserIsDisabled) {
base::test::ScopedFeatureList features;
std::vector<base::test::FeatureRef> enabled =
ash::standalone_browser::GetFeatureRefs();
enabled.push_back(lens::features::kLensStandalone);
enabled.push_back(lens::features::kEnableImageTranslate);
features.InitWithFeatures(enabled, {lens::features::kLensOverlay});
base::test::ScopedCommandLine scoped_command_line;
scoped_command_line.GetProcessCommandLine()->AppendSwitch(
ash::switches::kEnableLacrosForTesting);
auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
auto* primary_user =
fake_user_manager->AddUser(AccountId::FromUserEmail("test@test"));
fake_user_manager->UserLoggedIn(primary_user->GetAccountId(),
primary_user->username_hash(),
false,
false);
auto scoped_user_manager = std::make_unique<user_manager::ScopedUserManager>(
std::move(fake_user_manager));
ASSERT_FALSE(crosapi::browser_util::IsAshWebBrowserEnabled());
ash::ProfileHelper::Get();
TestingProfileManager testing_profile_manager(
TestingBrowserProcess::GetGlobal(), testing_local_state());
ASSERT_TRUE(testing_profile_manager.SetUp());
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE));
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_SEARCHLENSFORIMAGE));
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATEIMAGEWITHWEB));
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATEIMAGEWITHLENS));
}
#endif
TEST_F(RenderViewContextMenuPrefsTest, LensImageSearchForProgressiveWebApp) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensImageSearchEnabledFor3pDse) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensImageSearchDisabledFor3pDse) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensTranslateImageEnabled) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensTranslateImageEnabledFor3pDse) { … }
TEST_F(RenderViewContextMenuPrefsTest, LensTranslateImageDisabledFor3pDse) { … }
#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
TEST_F(RenderViewContextMenuPrefsTest, LensRegionSearch) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_TRUE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(RenderViewContextMenuPrefsTest,
LensRegionSearchDisabledIfAshBrowserIsDisabled) {
base::test::ScopedFeatureList features;
std::vector<base::test::FeatureRef> enabled =
ash::standalone_browser::GetFeatureRefs();
enabled.push_back(lens::features::kLensStandalone);
features.InitWithFeatures(enabled, {lens::features::kLensOverlay});
base::test::ScopedCommandLine scoped_command_line;
scoped_command_line.GetProcessCommandLine()->AppendSwitch(
ash::switches::kEnableLacrosForTesting);
auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
auto* primary_user =
fake_user_manager->AddUser(AccountId::FromUserEmail("test@test"));
fake_user_manager->UserLoggedIn(primary_user->GetAccountId(),
primary_user->username_hash(),
false,
false);
auto scoped_user_manager = std::make_unique<user_manager::ScopedUserManager>(
std::move(fake_user_manager));
ASSERT_FALSE(crosapi::browser_util::IsAshWebBrowserEnabled());
ash::ProfileHelper::Get();
TestingProfileManager testing_profile_manager(
TestingBrowserProcess::GetGlobal(), testing_local_state());
ASSERT_TRUE(testing_profile_manager.SetUp());
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
#endif
TEST_F(RenderViewContextMenuPrefsTest, LensRegionSearchPdfEnabled) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::RenderFrameHost* render_frame_host =
web_contents()->GetPrimaryMainFrame();
OverrideLastCommittedOrigin(
render_frame_host,
url::Origin::Create(
GURL("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai")));
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*render_frame_host, params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_TRUE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
TEST_F(RenderViewContextMenuPrefsTest,
LensRegionSearchEnterprisePoicyDisabled) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
profile()->GetPrefs()->SetBoolean(prefs::kLensRegionSearchEnabled, false);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
TEST_F(RenderViewContextMenuPrefsTest, LensRegionSearchDisabledOnImage) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
AppendImageItems(&menu);
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_WEB_REGION_SEARCH));
}
TEST_F(RenderViewContextMenuPrefsTest, LensRegionSearchPdfDisabledNoBrowser) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::RenderFrameHost* render_frame_host =
web_contents()->GetPrimaryMainFrame();
OverrideLastCommittedOrigin(
render_frame_host,
url::Origin::Create(
GURL("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai")));
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*render_frame_host, params);
menu.Init();
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_WEB_REGION_SEARCH));
}
TEST_F(RenderViewContextMenuPrefsTest,
LensRegionSearchNonGoogleDefaultSearchEngineSupportsImageSearch) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.search.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_TRUE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_WEB_REGION_SEARCH));
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
TEST_F(RenderViewContextMenuPrefsTest,
LensRegionSearchDefaultSearchEngineDoesNotSupportImageSearch) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.search.com",
false);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_WEB_REGION_SEARCH));
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
TEST_F(RenderViewContextMenuPrefsTest, LensRegionSearchChromeUIScheme) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
params.page_url = GURL(chrome::kChromeUISettingsURL);
params.frame_url = params.page_url;
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
EXPECT_FALSE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
TEST_F(RenderViewContextMenuPrefsTest,
CompanionImageSearchIssuesGoogleLensPreconnect) {
BeginPreresolveListening();
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{companion::features::internal::kSidePanelCompanion,
{{"open-companion-for-image-search", "true"}}}},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_SEARCHLENSFORIMAGE, &model, &index));
base::RunLoop run_loop;
preresolved_finished_closure() = run_loop.QuitClosure();
run_loop.Run();
ASSERT_EQ(last_preresolved_url().spec(), "https://lens.google.com/");
}
TEST_F(RenderViewContextMenuPrefsTest,
CompanionRegionSearchIssuesGoogleLensPreconnect) {
BeginPreresolveListening();
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{companion::features::internal::kSidePanelCompanion,
{{"open-companion-for-image-search", "true"}}}},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH, &model, &index));
EXPECT_TRUE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
base::RunLoop run_loop;
preresolved_finished_closure() = run_loop.QuitClosure();
run_loop.Run();
ASSERT_EQ(last_preresolved_url().spec(), "https://lens.google.com/");
}
TEST_F(RenderViewContextMenuPrefsTest,
LensImageSearchIssuesGoogleLensPreconnect) {
BeginPreresolveListening();
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_SEARCHLENSFORIMAGE, &model, &index));
base::RunLoop run_loop;
preresolved_finished_closure() = run_loop.QuitClosure();
run_loop.Run();
ASSERT_EQ(last_preresolved_url().spec(), "https://lens.google.com/");
}
TEST_F(RenderViewContextMenuPrefsTest,
LensRegionSearchIssuesGoogleLensPreconnect) {
BeginPreresolveListening();
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH, &model, &index));
EXPECT_TRUE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
base::RunLoop run_loop;
preresolved_finished_closure() = run_loop.QuitClosure();
run_loop.Run();
ASSERT_EQ(last_preresolved_url().spec(), "https://lens.google.com/");
}
TEST_F(RenderViewContextMenuPrefsTest,
CompanionImageSearchIssuesProcessPrewarming) {
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{companion::features::internal::kSidePanelCompanion,
{{"open-companion-for-image-search", "true"}}}},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
unsigned int initial_num_processes =
mock_rph_factory().GetProcesses()->size();
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_SEARCHLENSFORIMAGE, &model, &index));
ASSERT_EQ(initial_num_processes + 1,
mock_rph_factory().GetProcesses()->size());
}
TEST_F(RenderViewContextMenuPrefsTest,
CompanionRegionSearchIssuesProcessPrewarming) {
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{companion::features::internal::kSidePanelCompanion,
{{"open-companion-for-image-search", "true"}}}},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
unsigned int initial_num_processes =
mock_rph_factory().GetProcesses()->size();
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH, &model, &index));
ASSERT_EQ(initial_num_processes + 1,
mock_rph_factory().GetProcesses()->size());
}
TEST_F(RenderViewContextMenuPrefsTest, LensImageSearchIssuesProcessPrewarming) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
unsigned int initial_num_processes =
mock_rph_factory().GetProcesses()->size();
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_SEARCHLENSFORIMAGE, &model, &index));
ASSERT_EQ(initial_num_processes + 1,
mock_rph_factory().GetProcesses()->size());
}
TEST_F(RenderViewContextMenuPrefsTest,
LensRegionSearchIssuesProcessPrewarming) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
unsigned int initial_num_processes =
mock_rph_factory().GetProcesses()->size();
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH, &model, &index));
ASSERT_EQ(initial_num_processes + 1,
mock_rph_factory().GetProcesses()->size());
}
TEST_F(RenderViewContextMenuPrefsTest,
WithoutLensOrCompanionDoesNotIssueProcessPrewarming) {
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
unsigned int initial_num_processes =
mock_rph_factory().GetProcesses()->size();
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
ASSERT_EQ(initial_num_processes, mock_rph_factory().GetProcesses()->size());
}
TEST_F(RenderViewContextMenuPrefsTest,
CompanionPrewarmingFlagDisablesProcessPrewarming) {
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{companion::features::internal::kSidePanelCompanion,
{{"open-companion-for-image-search", "true"},
{"companion-issue-process-prewarming", "false"}}}},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
unsigned int initial_num_processes =
mock_rph_factory().GetProcesses()->size();
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_SEARCHLENSFORIMAGE, &model, &index));
#if BUILDFLAG(IS_CHROMEOS)
ASSERT_EQ(initial_num_processes + 1,
mock_rph_factory().GetProcesses()->size());
#else
ASSERT_EQ(initial_num_processes, mock_rph_factory().GetProcesses()->size());
#endif
}
TEST_F(RenderViewContextMenuPrefsTest,
LensPrewarmingFlagDisablesProcessPrewarming) {
base::test::ScopedFeatureList features;
features.InitWithFeaturesAndParameters(
{{lens::features::kLensStandalone,
{{"lens-issue-process-prewarming", "false"}}}},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::IMAGE);
params.has_image_contents = true;
unsigned int initial_num_processes =
mock_rph_factory().GetProcesses()->size();
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetBrowser());
menu.Init();
size_t index = 0;
raw_ptr<ui::MenuModel> model = nullptr;
ASSERT_TRUE(menu.GetMenuModelAndItemIndex(
IDC_CONTENT_CONTEXT_SEARCHLENSFORIMAGE, &model, &index));
ASSERT_EQ(initial_num_processes, mock_rph_factory().GetProcesses()->size());
}
#if BUILDFLAG(IS_MAC)
#define MAYBE_LensRegionSearchProgressiveWebApp …
#else
#define MAYBE_LensRegionSearchProgressiveWebApp …
#endif
TEST_F(RenderViewContextMenuPrefsTest,
MAYBE_LensRegionSearchProgressiveWebApp) {
base::test::ScopedFeatureList features;
features.InitWithFeatures({lens::features::kLensStandalone},
{lens::features::kLensOverlay});
SetUserSelectedDefaultSearchProvider("https://www.google.com",
true);
content::ContextMenuParams params = CreateParams(MenuItem::PAGE);
TestRenderViewContextMenu menu(*web_contents()->GetPrimaryMainFrame(),
params);
menu.SetBrowser(GetPwaBrowser());
menu.Init();
EXPECT_TRUE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_LENS_REGION_SEARCH));
}
#endif
struct FormatUrlForClipboardTestData { … };
class FormatUrlForClipboardTest
: public testing::TestWithParam<FormatUrlForClipboardTestData> { … };
const FormatUrlForClipboardTestData kFormatUrlForClipboardTestData[]{ … };
INSTANTIATE_TEST_SUITE_P(…);
TEST_P(FormatUrlForClipboardTest, FormatUrlForClipboard) { … }