-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Do not show copy clean link for plain text (#17541) * Do not show copy clean link for plain text * Added tests for clean link item * Block failed test
- Loading branch information
1 parent
66bb14b
commit 1b0b2ec
Showing
7 changed files
with
208 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (c) 2023 The Brave Authors. All rights reserved. | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
# You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
source_set("unit_tests") { | ||
testonly = true | ||
assert(is_win || is_linux || is_mac) | ||
sources = [ "render_view_context_menu_unittest.cc" ] | ||
deps = [ | ||
"//base", | ||
"//brave/app:command_ids", | ||
"//chrome/browser", | ||
"//chrome/test:test_support", | ||
"//components/custom_handlers", | ||
"//components/custom_handlers:test_support", | ||
"//components/search_engines", | ||
"//content/test:test_support", | ||
"//testing/gmock", | ||
"//testing/gtest", | ||
"//third_party/abseil-cpp:absl", | ||
] | ||
} |
135 changes: 135 additions & 0 deletions
135
browser/renderer_context_menu/test/render_view_context_menu_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* Copyright (c) 2023 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | ||
|
||
#include "brave/app/brave_command_ids.h" | ||
#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | ||
#include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" | ||
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" | ||
#include "chrome/browser/search_engines/template_url_service_factory.h" | ||
#include "chrome/test/base/scoped_testing_local_state.h" | ||
#include "chrome/test/base/testing_browser_process.h" | ||
#include "chrome/test/base/testing_profile.h" | ||
#include "components/custom_handlers/protocol_handler_registry.h" | ||
#include "components/custom_handlers/test_protocol_handler_registry_delegate.h" | ||
#include "components/search_engines/template_url_service.h" | ||
#include "content/public/browser/context_menu_params.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "content/public/test/browser_task_environment.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace { | ||
|
||
content::ContextMenuParams CreateSelectedTextParams( | ||
const std::u16string& selected_text) { | ||
content::ContextMenuParams rv; | ||
rv.is_editable = false; | ||
rv.page_url = GURL("http://test.page/"); | ||
rv.selection_text = selected_text; | ||
return rv; | ||
} | ||
|
||
content::ContextMenuParams CreateLinkParams(const GURL& selected_link) { | ||
content::ContextMenuParams rv; | ||
rv.is_editable = false; | ||
rv.unfiltered_link_url = selected_link; | ||
rv.page_url = GURL("http://test.page/"); | ||
rv.link_url = selected_link; | ||
return rv; | ||
} | ||
|
||
std::unique_ptr<KeyedService> BuildProtocolHandlerRegistry( | ||
content::BrowserContext* context) { | ||
Profile* profile = Profile::FromBrowserContext(context); | ||
return std::make_unique<custom_handlers::ProtocolHandlerRegistry>( | ||
profile->GetPrefs(), | ||
std::make_unique<custom_handlers::TestProtocolHandlerRegistryDelegate>()); | ||
} | ||
|
||
} // namespace | ||
|
||
class BraveRenderViewContextMenuMock : public BraveRenderViewContextMenu { | ||
using BraveRenderViewContextMenu::BraveRenderViewContextMenu; | ||
void Show() override {} | ||
}; | ||
|
||
class BraveRenderViewContextMenuTest : public testing::Test { | ||
protected: | ||
BraveRenderViewContextMenuTest() | ||
: testing_local_state_(TestingBrowserProcess::GetGlobal()) {} | ||
content::WebContents* GetWebContents() { return web_contents_.get(); } | ||
|
||
// Returns a test context menu. | ||
std::unique_ptr<BraveRenderViewContextMenuMock> CreateContextMenu( | ||
content::WebContents* web_contents, | ||
content::ContextMenuParams params) { | ||
auto menu = std::make_unique<BraveRenderViewContextMenuMock>( | ||
*web_contents->GetPrimaryMainFrame(), params); | ||
menu->Init(); | ||
return menu; | ||
} | ||
|
||
void SetUp() override { | ||
TestingProfile::Builder builder; | ||
builder.AddTestingFactory( | ||
TemplateURLServiceFactory::GetInstance(), | ||
base::BindRepeating(&TemplateURLServiceFactory::BuildInstanceFor)); | ||
profile_ = builder.Build(); | ||
web_contents_ = content::WebContents::Create( | ||
content::WebContents::CreateParams(profile_.get())); | ||
auto* service = TemplateURLServiceFactory::GetForProfile(profile_.get()); | ||
EXPECT_TRUE(service); | ||
client_ = | ||
std::make_unique<ChromeAutocompleteProviderClient>(profile_.get()); | ||
registry_ = std::make_unique<custom_handlers::ProtocolHandlerRegistry>( | ||
profile_.get()->GetPrefs(), nullptr); | ||
AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( | ||
profile_.get(), | ||
base::BindRepeating(&AutocompleteClassifierFactory::BuildInstanceFor)); | ||
ProtocolHandlerRegistryFactory::GetInstance()->SetTestingFactory( | ||
profile_.get(), base::BindRepeating(&BuildProtocolHandlerRegistry)); | ||
} | ||
void TearDown() override { registry_.reset(); } | ||
|
||
private: | ||
content::BrowserTaskEnvironment browser_task_environment; | ||
std::unique_ptr<TestingProfile> profile_; | ||
std::unique_ptr<custom_handlers::ProtocolHandlerRegistry> registry_; | ||
ScopedTestingLocalState testing_local_state_; | ||
std::unique_ptr<ChromeAutocompleteProviderClient> client_; | ||
std::unique_ptr<content::WebContents> web_contents_; | ||
}; | ||
|
||
TEST_F(BraveRenderViewContextMenuTest, MenuForPlainText) { | ||
content::ContextMenuParams params = CreateSelectedTextParams(u"plain text"); | ||
auto context_menu = CreateContextMenu(GetWebContents(), params); | ||
EXPECT_TRUE(context_menu); | ||
absl::optional<size_t> clean_link_index = | ||
context_menu->menu_model().GetIndexOfCommandId(IDC_COPY_CLEAN_LINK); | ||
EXPECT_FALSE(clean_link_index.has_value()); | ||
} | ||
|
||
TEST_F(BraveRenderViewContextMenuTest, MenuForSelectedUrl) { | ||
content::ContextMenuParams params = CreateSelectedTextParams(u"brave.com"); | ||
auto context_menu = CreateContextMenu(GetWebContents(), params); | ||
EXPECT_TRUE(context_menu); | ||
absl::optional<size_t> clean_link_index = | ||
context_menu->menu_model().GetIndexOfCommandId(IDC_COPY_CLEAN_LINK); | ||
EXPECT_TRUE(clean_link_index.has_value()); | ||
EXPECT_TRUE(context_menu->IsCommandIdEnabled(IDC_COPY_CLEAN_LINK)); | ||
} | ||
|
||
TEST_F(BraveRenderViewContextMenuTest, MenuForLink) { | ||
content::ContextMenuParams params = | ||
CreateLinkParams(GURL("https://brave.com")); | ||
auto context_menu = CreateContextMenu(GetWebContents(), params); | ||
EXPECT_TRUE(context_menu); | ||
absl::optional<size_t> clean_link_index = | ||
context_menu->menu_model().GetIndexOfCommandId(IDC_COPY_CLEAN_LINK); | ||
EXPECT_TRUE(clean_link_index.has_value()); | ||
EXPECT_TRUE(context_menu->IsCommandIdEnabled(IDC_COPY_CLEAN_LINK)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters