1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
ash / quick_insert / quick_insert_copy_media_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/quick_insert/quick_insert_copy_media.h"
#include "ash/public/cpp/system/toast_manager.h"
#include "ash/quick_insert/quick_insert_rich_media.h"
#include "ash/quick_insert/quick_insert_test_util.h"
#include "ash/test/ash_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/gfx/geometry/size.h"
namespace ash {
namespace {
class QuickInsertCopyMediaTest : public AshTestBase {};
TEST_F(QuickInsertCopyMediaTest, CopiesText) {
CopyMediaToClipboard(QuickInsertTextMedia(u"hello"));
EXPECT_EQ(ReadTextFromClipboard(ui::Clipboard::GetForCurrentThread()),
u"hello");
}
TEST_F(QuickInsertCopyMediaTest, CopiesImageWithKnownDimensionsAsHtml) {
CopyMediaToClipboard(
QuickInsertImageMedia(GURL("https://foo.com"), gfx::Size(30, 20)));
EXPECT_EQ(
ReadHtmlFromClipboard(ui::Clipboard::GetForCurrentThread()),
uR"html(<img src="https://foo.com/" referrerpolicy="no-referrer" width="30" height="20"/>)html");
}
TEST_F(QuickInsertCopyMediaTest, CopiesImageWithUnknownDimensionsAsHtml) {
CopyMediaToClipboard(QuickInsertImageMedia(GURL("https://foo.com")));
EXPECT_EQ(
ReadHtmlFromClipboard(ui::Clipboard::GetForCurrentThread()),
uR"html(<img src="https://foo.com/" referrerpolicy="no-referrer"/>)html");
}
TEST_F(QuickInsertCopyMediaTest,
CopiesImagesWithBothAltTextAndDimensionsAsHtml) {
CopyMediaToClipboard(QuickInsertImageMedia(GURL("https://foo.com"),
gfx::Size(30, 20),
/*content_description=*/u"img"));
EXPECT_EQ(
ReadHtmlFromClipboard(ui::Clipboard::GetForCurrentThread()),
uR"html(<img src="https://foo.com/" referrerpolicy="no-referrer" alt="img" width="30" height="20"/>)html");
}
TEST_F(QuickInsertCopyMediaTest, EscapesAltTextForImages) {
CopyMediaToClipboard(
QuickInsertImageMedia(GURL("https://foo.com"),
/*dimensions=*/std::nullopt,
/*content_description=*/u"\"img\""));
EXPECT_EQ(
ReadHtmlFromClipboard(ui::Clipboard::GetForCurrentThread()),
uR"html(<img src="https://foo.com/" referrerpolicy="no-referrer" alt=""img""/>)html");
}
TEST_F(QuickInsertCopyMediaTest, CopiesLinks) {
CopyMediaToClipboard(QuickInsertLinkMedia(GURL("https://foo.com"), "Foo"));
EXPECT_EQ(ReadTextFromClipboard(ui::Clipboard::GetForCurrentThread()),
u"https://foo.com/");
// We include the title as the `title` attribute for maximum compatibility.
// See `ShouldUseLinkTitle` in quick_insert_insert_media.cc for more details.
EXPECT_EQ(ReadHtmlFromClipboard(ui::Clipboard::GetForCurrentThread()),
u"<a title=\"Foo\" href=\"https://foo.com/\">https://foo.com/</a>");
}
TEST_F(QuickInsertCopyMediaTest, LinksAreEscaped) {
CopyMediaToClipboard(QuickInsertLinkMedia(
GURL("https://foo.com/?\"><svg onload=\"alert(1)\"><a href=\""),
"<svg onload=\"alert(1)\">"));
EXPECT_EQ(ReadTextFromClipboard(ui::Clipboard::GetForCurrentThread()),
u"https://foo.com/"
u"?%22%3E%3Csvg%20onload=%22alert(1)%22%3E%3Ca%20href=%22");
EXPECT_EQ(
ReadHtmlFromClipboard(ui::Clipboard::GetForCurrentThread()),
u"<a"
u" title=\"<svg onload="alert(1)">\""
u" href=\"https://foo.com/"
u"?%22%3E%3Csvg%20onload=%22alert(1)%22%3E%3Ca%20href=%22\">"
u"https://foo.com/?%22%3E%3Csvg%20onload=%22alert(1)%22%3E%3Ca%20href=%22"
u"</a>");
}
TEST_F(QuickInsertCopyMediaTest, CopiesFiles) {
CopyMediaToClipboard(QuickInsertLocalFileMedia(base::FilePath("/foo.txt")));
EXPECT_EQ(ReadFilenameFromClipboard(ui::Clipboard::GetForCurrentThread()),
base::FilePath("/foo.txt"));
}
class QuickInsertCopyMediaToastTest
: public AshTestBase,
public testing::WithParamInterface<QuickInsertRichMedia> {};
INSTANTIATE_TEST_SUITE_P(
,
QuickInsertCopyMediaToastTest,
::testing::Values(QuickInsertTextMedia(u"hello"),
QuickInsertImageMedia(GURL("https://foo.com"),
gfx::Size(30, 20)),
QuickInsertLinkMedia(GURL("https://foo.com"), "Foo"),
QuickInsertLocalFileMedia(base::FilePath("/foo.txt"))));
TEST_P(QuickInsertCopyMediaToastTest, ShowsToastAfterCopyingLink) {
CopyMediaToClipboard(GetParam());
EXPECT_TRUE(
ToastManager::Get()->IsToastShown("quick_insert_copy_to_clipboard"));
}
} // namespace
} // namespace ash