[go: nahoru, domu]

blob: 19cb86bc967fa781a0c01b09da2137801570e74a [file] [log] [blame]
David Blackd78ea3f2020-08-24 18:29:071// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/clipboard/clipboard_history_util.h"
6
7#include <array>
8#include <deque>
9
10#include "ash/clipboard/clipboard_history_item.h"
11#include "ash/clipboard/test_support/clipboard_history_item_builder.h"
12#include "base/stl_util.h"
13#include "base/strings/utf_string_conversions.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace ash {
17namespace ClipboardHistoryUtil {
18
19namespace {
20
21constexpr std::array<ui::ClipboardInternalFormat, 7> kAllFormats = {
Alex Newcomer4d601ca2020-09-10 20:30:1422 ui::ClipboardInternalFormat::kBitmap,
David Blackd78ea3f2020-08-24 18:29:0723 ui::ClipboardInternalFormat::kHtml,
Alex Newcomer4d601ca2020-09-10 20:30:1424 ui::ClipboardInternalFormat::kText,
David Blackd78ea3f2020-08-24 18:29:0725 ui::ClipboardInternalFormat::kRtf,
26 ui::ClipboardInternalFormat::kBookmark,
David Blackd78ea3f2020-08-24 18:29:0727 ui::ClipboardInternalFormat::kCustom,
28 ui::ClipboardInternalFormat::kWeb};
29
30// Helpers ---------------------------------------------------------------------
31
32// Sets hardcoded data for all formats on `builder`.
33void SetAllFormats(ClipboardHistoryItemBuilder* builder) {
34 for (const auto& format : kAllFormats)
35 builder->SetFormat(format);
36}
37
38} // namespace
39
40// Tests -----------------------------------------------------------------------
41
42using ClipboardHistoryUtilTest = testing::Test;
43
44TEST_F(ClipboardHistoryUtilTest, CalculateMainFormat) {
45 ClipboardHistoryItemBuilder builder;
46 SetAllFormats(&builder);
47
48 // We will cycle through all formats in prioritized order.
49 std::deque<ui::ClipboardInternalFormat> prioritized_formats = {
50 ui::ClipboardInternalFormat::kBitmap,
David Blackd78ea3f2020-08-24 18:29:0751 ui::ClipboardInternalFormat::kHtml,
Alex Newcomer4d601ca2020-09-10 20:30:1452 ui::ClipboardInternalFormat::kText,
David Blackd78ea3f2020-08-24 18:29:0753 ui::ClipboardInternalFormat::kRtf,
54 ui::ClipboardInternalFormat::kBookmark,
55 ui::ClipboardInternalFormat::kWeb,
56 ui::ClipboardInternalFormat::kCustom,
57 };
58
59 while (!prioritized_formats.empty()) {
60 ui::ClipboardInternalFormat format = prioritized_formats.front();
61 EXPECT_EQ(CalculateMainFormat(builder.Build().data()), format);
62
63 // Pop a format and resume testing.
64 builder.ClearFormat(format);
65 prioritized_formats.pop_front();
66 }
67
68 EXPECT_FALSE(CalculateMainFormat(builder.Build().data()).has_value());
69}
70
71TEST_F(ClipboardHistoryUtilTest, ContainsFormat) {
72 ClipboardHistoryItemBuilder builder;
73
74 for (const auto& format : kAllFormats) {
75 EXPECT_FALSE(ContainsFormat(builder.Build().data(), format));
76 builder.SetFormat(format);
77 EXPECT_TRUE(ContainsFormat(builder.Build().data(), format));
78 }
79}
80
81TEST_F(ClipboardHistoryUtilTest, ContainsFileSystemData) {
82 ClipboardHistoryItemBuilder builder;
83
84 EXPECT_FALSE(ContainsFileSystemData(builder.Build().data()));
85
86 SetAllFormats(&builder);
87 EXPECT_FALSE(ContainsFileSystemData(builder.Build().data()));
88
89 builder.SetFileSystemData({"/path/to/My%20File.txt"});
90 EXPECT_TRUE(ContainsFileSystemData(builder.Build().data()));
91}
92
93TEST_F(ClipboardHistoryUtilTest, GetFileSystemSources) {
94 ClipboardHistoryItemBuilder builder;
95
96 EXPECT_TRUE(GetFileSystemSources(builder.Build().data()).empty());
97
98 SetAllFormats(&builder);
99 EXPECT_TRUE(GetFileSystemSources(builder.Build().data()).empty());
100
101 builder.SetFileSystemData({"/path/to/My%20File.txt"});
102 EXPECT_EQ(GetFileSystemSources(builder.Build().data()),
103 base::UTF8ToUTF16("/path/to/My%20File.txt"));
104}
105
Andrew Xube3d7ea2021-01-14 21:48:17106TEST_F(ClipboardHistoryUtilTest, GetSplitFileSystemData) {
107 const std::string file_name1("File1.txt"), file_name2("File2.txt");
108 ClipboardHistoryItemBuilder builder;
109 builder.SetFileSystemData({file_name1, file_name2});
Jan Wilken Dörrie85285b02021-03-11 23:38:47110 std::u16string sources;
Andrew Xube3d7ea2021-01-14 21:48:17111 std::vector<base::StringPiece16> source_list;
112 GetSplitFileSystemData(builder.Build().data(), &source_list, &sources);
113 EXPECT_EQ(file_name1, base::UTF16ToUTF8(source_list[0]));
114 EXPECT_EQ(file_name2, base::UTF16ToUTF8(source_list[1]));
115}
116
117TEST_F(ClipboardHistoryUtilTest, GetFilesCount) {
118 ClipboardHistoryItemBuilder builder;
119 builder.SetFileSystemData(
120 {"/path/to/My%20File1.txt", "/path/to/My%20File2.txt"});
121 EXPECT_EQ(2u, GetCountOfCopiedFiles(builder.Build().data()));
122}
123
David Blackd78ea3f2020-08-24 18:29:07124TEST_F(ClipboardHistoryUtilTest, IsSupported) {
125 ClipboardHistoryItemBuilder builder;
126
127 EXPECT_FALSE(IsSupported(builder.Build().data()));
128
129 for (const auto& format : kAllFormats) {
130 if (format != ui::ClipboardInternalFormat::kCustom) {
131 builder.SetFormat(format);
132 EXPECT_TRUE(IsSupported(builder.Build().data()));
133 builder.Clear();
134 }
135 }
136
137 builder.SetFormat(ui::ClipboardInternalFormat::kCustom);
138 EXPECT_FALSE(IsSupported(builder.Build().data()));
139
140 builder.SetFileSystemData({"/path/to/My%20File.txt"});
141 EXPECT_TRUE(IsSupported(builder.Build().data()));
142}
143
144} // namespace ClipboardHistoryUtil
145} // namespace ash