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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
ash / clipboard / clipboard_history_util_unittest.cc [blame]
// Copyright 2020 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/clipboard/clipboard_history_util.h"
#include <array>
#include <deque>
#include <string_view>
#include "ash/clipboard/test_support/clipboard_history_item_builder.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/clipboard_data.h"
namespace ash::clipboard_history_util {
namespace {
constexpr std::array<ui::ClipboardInternalFormat, 8> kAllFormats = {
ui::ClipboardInternalFormat::kPng,
ui::ClipboardInternalFormat::kHtml,
ui::ClipboardInternalFormat::kText,
ui::ClipboardInternalFormat::kRtf,
ui::ClipboardInternalFormat::kBookmark,
ui::ClipboardInternalFormat::kCustom,
ui::ClipboardInternalFormat::kWeb,
ui::ClipboardInternalFormat::kFilenames};
// Helpers ---------------------------------------------------------------------
// Sets hardcoded data for all formats on `builder`.
void SetAllFormats(ClipboardHistoryItemBuilder* builder) {
for (const auto& format : kAllFormats)
builder->SetFormat(format);
}
} // namespace
// Tests -----------------------------------------------------------------------
using ClipboardHistoryUtilTest = testing::Test;
TEST_F(ClipboardHistoryUtilTest, CalculateMainFormat) {
ClipboardHistoryItemBuilder builder;
SetAllFormats(&builder);
// We will cycle through all formats in prioritized order.
std::deque<ui::ClipboardInternalFormat> prioritized_formats = {
ui::ClipboardInternalFormat::kPng,
ui::ClipboardInternalFormat::kHtml,
ui::ClipboardInternalFormat::kText,
ui::ClipboardInternalFormat::kRtf,
ui::ClipboardInternalFormat::kFilenames,
ui::ClipboardInternalFormat::kBookmark,
ui::ClipboardInternalFormat::kWeb,
ui::ClipboardInternalFormat::kCustom,
};
while (!prioritized_formats.empty()) {
ui::ClipboardInternalFormat format = prioritized_formats.front();
EXPECT_EQ(CalculateMainFormat(builder.BuildData()), format);
// Pop a format and resume testing.
builder.ClearFormat(format);
prioritized_formats.pop_front();
}
EXPECT_FALSE(CalculateMainFormat(builder.BuildData()).has_value());
}
TEST_F(ClipboardHistoryUtilTest, ContainsFormat) {
ClipboardHistoryItemBuilder builder;
for (const auto& format : kAllFormats) {
EXPECT_FALSE(ContainsFormat(builder.BuildData(), format));
builder.SetFormat(format);
EXPECT_TRUE(ContainsFormat(builder.BuildData(), format));
}
}
TEST_F(ClipboardHistoryUtilTest, ContainsFileSystemData) {
ClipboardHistoryItemBuilder builder;
EXPECT_FALSE(ContainsFileSystemData(builder.BuildData()));
SetAllFormats(&builder);
builder.ClearFormat(ui::ClipboardInternalFormat::kFilenames);
EXPECT_FALSE(ContainsFileSystemData(builder.BuildData()));
// Outside the Files app, file system sources are written to filenames.
builder.SetFormat(ui::ClipboardInternalFormat::kFilenames);
EXPECT_TRUE(ContainsFileSystemData(builder.BuildData()));
builder.ClearFormat(ui::ClipboardInternalFormat::kFilenames);
// Within the Files app, file system sources are written to custom data.
builder.SetFileSystemData({u"/path/to/My%20File.txt"});
EXPECT_TRUE(ContainsFileSystemData(builder.BuildData()));
}
TEST_F(ClipboardHistoryUtilTest, GetFileSystemSources) {
ClipboardHistoryItemBuilder builder;
EXPECT_TRUE(GetFileSystemSources(builder.BuildData()).empty());
SetAllFormats(&builder);
builder.ClearFormat(ui::ClipboardInternalFormat::kFilenames);
EXPECT_TRUE(GetFileSystemSources(builder.BuildData()).empty());
// Outside the Files app, file system sources are written to filenames.
builder.SetFilenames({ui::FileInfo(base::FilePath("/path/to/My%20File.txt"),
base::FilePath("My%20File.txt"))});
EXPECT_EQ(GetFileSystemSources(builder.BuildData()),
u"/path/to/My%20File.txt");
builder.ClearFilenames();
// Within the Files app, file system sources are written to custom data.
builder.SetFileSystemData({u"/path/to/My%20File.txt"});
EXPECT_EQ(GetFileSystemSources(builder.BuildData()),
u"/path/to/My%20File.txt");
}
TEST_F(ClipboardHistoryUtilTest, GetSplitFileSystemData) {
const std::string file_name1("File1.txt"), file_name2("File2.txt");
const std::u16string file_name1_16(base::UTF8ToUTF16(file_name1)),
file_name2_16(base::UTF8ToUTF16(file_name2));
ClipboardHistoryItemBuilder builder;
std::u16string sources;
std::vector<std::u16string_view> source_list;
// Outside the Files app, file system sources are written to filenames.
builder.SetFilenames(
{ui::FileInfo(base::FilePath(file_name1), base::FilePath(file_name1)),
ui::FileInfo(base::FilePath(file_name2), base::FilePath(file_name2))});
GetSplitFileSystemData(builder.BuildData(), &source_list, &sources);
EXPECT_EQ(file_name1_16, source_list[0]);
EXPECT_EQ(file_name2_16, source_list[1]);
builder.ClearFilenames();
sources.clear();
source_list.clear();
// Within the Files app, file system sources are written to custom data.
builder.SetFileSystemData({file_name1_16, file_name2_16});
GetSplitFileSystemData(builder.BuildData(), &source_list, &sources);
EXPECT_EQ(file_name1_16, source_list[0]);
EXPECT_EQ(file_name2_16, source_list[1]);
}
TEST_F(ClipboardHistoryUtilTest, GetFilesCount) {
ClipboardHistoryItemBuilder builder;
// Outside the Files app, file system sources are written to filenames.
builder.SetFilenames({ui::FileInfo(base::FilePath("/path/to/My%20File1.txt"),
base::FilePath("My%20File1.txt")),
ui::FileInfo(base::FilePath("/path/to/My%20File2.txt"),
base::FilePath("My%20File2.txt"))});
EXPECT_EQ(2u, GetCountOfCopiedFiles(builder.BuildData()));
builder.ClearFilenames();
// Within the Files app, file system sources are written to custom data.
builder.SetFileSystemData(
{u"/path/to/My%20File1.txt", u"/path/to/My%20File2.txt"});
EXPECT_EQ(2u, GetCountOfCopiedFiles(builder.BuildData()));
}
TEST_F(ClipboardHistoryUtilTest, IsSupported) {
ClipboardHistoryItemBuilder builder;
EXPECT_FALSE(IsSupported(builder.BuildData()));
for (const auto& format : kAllFormats) {
if (format != ui::ClipboardInternalFormat::kCustom) {
builder.SetFormat(format);
EXPECT_TRUE(IsSupported(builder.BuildData()));
builder.Clear();
}
}
builder.SetFormat(ui::ClipboardInternalFormat::kCustom);
EXPECT_FALSE(IsSupported(builder.BuildData()));
builder.SetFileSystemData({u"/path/to/My%20File.txt"});
EXPECT_TRUE(IsSupported(builder.BuildData()));
}
} // namespace ash::clipboard_history_util