Copy file results to clipboard correctly.
When a local file result cannot be inserted, it needs to be copied to clipboard. Bug: b/328655564 Change-Id: Ibe288601852956201d28021fead1041b2ec50c4c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5373693 Commit-Queue: Darren Shen <shend@chromium.org> Reviewed-by: Michael Cui <mlcui@google.com> Cr-Commit-Position: refs/heads/main@{#1274008}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
b01c13e74e
commit
26695b51db
@ -20,6 +20,7 @@
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "ui/base/clipboard/file_info.h"
|
||||
#include "ui/base/clipboard/scoped_clipboard_writer.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
#include "url/gurl.h"
|
||||
@ -78,25 +79,25 @@ void CopyMediaToClipboard(const PickerRichMedia& media) {
|
||||
ui::ClipboardBuffer::kCopyPaste);
|
||||
|
||||
// Overwrite the clipboard data.
|
||||
std::visit(base::Overloaded{
|
||||
[&clipboard](const PickerTextMedia& media) {
|
||||
clipboard->WriteText(std::move(media.text));
|
||||
},
|
||||
[&clipboard](const PickerImageMedia& media) {
|
||||
clipboard->WriteHTML(
|
||||
base::UTF8ToUTF16(BuildImageHtml(media)),
|
||||
/*document_url=*/"");
|
||||
},
|
||||
[&clipboard](const PickerLinkMedia& media) {
|
||||
// TODO(b/322729192): Copy a real hyperlink.
|
||||
clipboard->WriteText(base::UTF8ToUTF16(media.url.spec()));
|
||||
},
|
||||
[&clipboard](const PickerLocalFileMedia& media) {
|
||||
// TODO(b/325872346): Copy the file or its contents.
|
||||
clipboard->WriteText(media.path.AsUTF16Unsafe());
|
||||
},
|
||||
},
|
||||
media);
|
||||
std::visit(
|
||||
base::Overloaded{
|
||||
[&clipboard](const PickerTextMedia& media) {
|
||||
clipboard->WriteText(std::move(media.text));
|
||||
},
|
||||
[&clipboard](const PickerImageMedia& media) {
|
||||
clipboard->WriteHTML(base::UTF8ToUTF16(BuildImageHtml(media)),
|
||||
/*document_url=*/"");
|
||||
},
|
||||
[&clipboard](const PickerLinkMedia& media) {
|
||||
// TODO(b/322729192): Copy a real hyperlink.
|
||||
clipboard->WriteText(base::UTF8ToUTF16(media.url.spec()));
|
||||
},
|
||||
[&clipboard](const PickerLocalFileMedia& media) {
|
||||
clipboard->WriteFilenames(ui::FileInfosToURIList(
|
||||
/*filenames=*/{ui::FileInfo(media.path, /*display_name=*/{})}));
|
||||
},
|
||||
},
|
||||
media);
|
||||
|
||||
// Show a toast to inform the user about the copy.
|
||||
// TODO: b/322928125 - Use dedicated toast catalog name.
|
||||
|
@ -24,13 +24,6 @@ TEST_F(PickerCopyMediaTest, CopiesText) {
|
||||
u"hello");
|
||||
}
|
||||
|
||||
TEST_F(PickerCopyMediaTest, ShowsToastAfterCopyingText) {
|
||||
CopyMediaToClipboard(PickerTextMedia(u"hello"));
|
||||
|
||||
EXPECT_TRUE(
|
||||
ash::ToastManager::Get()->IsToastShown("picker_copy_to_clipboard"));
|
||||
}
|
||||
|
||||
TEST_F(PickerCopyMediaTest, CopiesImageWithKnownDimensionsAsHtml) {
|
||||
CopyMediaToClipboard(
|
||||
PickerImageMedia(GURL("https://foo.com"), gfx::Size(30, 20)));
|
||||
@ -68,14 +61,6 @@ TEST_F(PickerCopyMediaTest, EscapesAltTextForImages) {
|
||||
uR"html(<img src="https://foo.com/" referrerpolicy="no-referrer" alt=""img""/>)html");
|
||||
}
|
||||
|
||||
TEST_F(PickerCopyMediaTest, ShowsToastAfterCopyingImage) {
|
||||
CopyMediaToClipboard(
|
||||
PickerImageMedia(GURL("https://foo.com"), gfx::Size(30, 20)));
|
||||
|
||||
EXPECT_TRUE(
|
||||
ash::ToastManager::Get()->IsToastShown("picker_copy_to_clipboard"));
|
||||
}
|
||||
|
||||
TEST_F(PickerCopyMediaTest, CopiesLinks) {
|
||||
CopyMediaToClipboard(PickerLinkMedia(GURL("https://foo.com")));
|
||||
|
||||
@ -83,8 +68,28 @@ TEST_F(PickerCopyMediaTest, CopiesLinks) {
|
||||
u"https://foo.com/");
|
||||
}
|
||||
|
||||
TEST_F(PickerCopyMediaTest, ShowsToastAfterCopyingLink) {
|
||||
CopyMediaToClipboard(PickerLinkMedia(GURL("https://foo.com")));
|
||||
TEST_F(PickerCopyMediaTest, CopiesFiles) {
|
||||
CopyMediaToClipboard(PickerLocalFileMedia(base::FilePath("/foo.txt")));
|
||||
|
||||
EXPECT_EQ(ReadFilenameFromClipboard(ui::Clipboard::GetForCurrentThread()),
|
||||
base::FilePath("/foo.txt"));
|
||||
}
|
||||
|
||||
class PickerCopyMediaToastTest
|
||||
: public AshTestBase,
|
||||
public testing::WithParamInterface<PickerRichMedia> {};
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
,
|
||||
PickerCopyMediaToastTest,
|
||||
::testing::Values(PickerTextMedia(u"hello"),
|
||||
PickerImageMedia(GURL("https://foo.com"),
|
||||
gfx::Size(30, 20)),
|
||||
PickerLinkMedia(GURL("https://foo.com")),
|
||||
PickerLocalFileMedia(base::FilePath("/foo.txt"))));
|
||||
|
||||
TEST_P(PickerCopyMediaToastTest, ShowsToastAfterCopyingLink) {
|
||||
CopyMediaToClipboard(GetParam());
|
||||
|
||||
EXPECT_TRUE(
|
||||
ash::ToastManager::Get()->IsToastShown("picker_copy_to_clipboard"));
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "ui/base/clipboard/clipboard.h"
|
||||
#include "ui/base/clipboard/clipboard_buffer.h"
|
||||
#include "ui/base/clipboard/file_info.h"
|
||||
#include "ui/events/test/event_generator.h"
|
||||
#include "ui/views/view.h"
|
||||
#include "ui/views/widget/widget_utils.h"
|
||||
@ -31,6 +32,12 @@ std::u16string ReadHtmlFromClipboard(ui::Clipboard* clipboard) {
|
||||
return data;
|
||||
}
|
||||
|
||||
base::FilePath ReadFilenameFromClipboard(ui::Clipboard* clipboard) {
|
||||
std::vector<ui::FileInfo> result;
|
||||
clipboard->ReadFilenames(ui::ClipboardBuffer::kCopyPaste, nullptr, &result);
|
||||
return result.empty() ? base::FilePath() : result.front().path;
|
||||
}
|
||||
|
||||
void LeftClickOn(views::View& view) {
|
||||
ui::test::EventGenerator event_generator(GetRootWindow(view.GetWidget()));
|
||||
event_generator.MoveMouseTo(view.GetBoundsInScreen().CenterPoint());
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "ash/ash_export.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "ui/events/event_constants.h"
|
||||
#include "ui/events/keycodes/keyboard_codes.h"
|
||||
|
||||
@ -28,6 +29,9 @@ ASH_EXPORT std::u16string ReadTextFromClipboard(ui::Clipboard* clipboard);
|
||||
// Returns the HTML contents of `clipboard`.
|
||||
ASH_EXPORT std::u16string ReadHtmlFromClipboard(ui::Clipboard* clipboard);
|
||||
|
||||
// Returns the filename contents of `clipboard`.
|
||||
ASH_EXPORT base::FilePath ReadFilenameFromClipboard(ui::Clipboard* clipboard);
|
||||
|
||||
// Clicks on `view` with the left mouse button.
|
||||
void ASH_EXPORT LeftClickOn(views::View& view);
|
||||
|
||||
|
Reference in New Issue
Block a user