David Black | d78ea3f | 2020-08-24 18:29:07 | [diff] [blame] | 1 | // 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 | |
| 16 | namespace ash { |
| 17 | namespace ClipboardHistoryUtil { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | constexpr std::array<ui::ClipboardInternalFormat, 7> kAllFormats = { |
Alex Newcomer | 4d601ca | 2020-09-10 20:30:14 | [diff] [blame] | 22 | ui::ClipboardInternalFormat::kBitmap, |
David Black | d78ea3f | 2020-08-24 18:29:07 | [diff] [blame] | 23 | ui::ClipboardInternalFormat::kHtml, |
Alex Newcomer | 4d601ca | 2020-09-10 20:30:14 | [diff] [blame] | 24 | ui::ClipboardInternalFormat::kText, |
David Black | d78ea3f | 2020-08-24 18:29:07 | [diff] [blame] | 25 | ui::ClipboardInternalFormat::kRtf, |
| 26 | ui::ClipboardInternalFormat::kBookmark, |
David Black | d78ea3f | 2020-08-24 18:29:07 | [diff] [blame] | 27 | ui::ClipboardInternalFormat::kCustom, |
| 28 | ui::ClipboardInternalFormat::kWeb}; |
| 29 | |
| 30 | // Helpers --------------------------------------------------------------------- |
| 31 | |
| 32 | // Sets hardcoded data for all formats on `builder`. |
| 33 | void SetAllFormats(ClipboardHistoryItemBuilder* builder) { |
| 34 | for (const auto& format : kAllFormats) |
| 35 | builder->SetFormat(format); |
| 36 | } |
| 37 | |
| 38 | } // namespace |
| 39 | |
| 40 | // Tests ----------------------------------------------------------------------- |
| 41 | |
| 42 | using ClipboardHistoryUtilTest = testing::Test; |
| 43 | |
| 44 | TEST_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 Black | d78ea3f | 2020-08-24 18:29:07 | [diff] [blame] | 51 | ui::ClipboardInternalFormat::kHtml, |
Alex Newcomer | 4d601ca | 2020-09-10 20:30:14 | [diff] [blame] | 52 | ui::ClipboardInternalFormat::kText, |
David Black | d78ea3f | 2020-08-24 18:29:07 | [diff] [blame] | 53 | 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 | |
| 71 | TEST_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 | |
| 81 | TEST_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 | |
| 93 | TEST_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 Xu | be3d7ea | 2021-01-14 21:48:17 | [diff] [blame] | 106 | TEST_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örrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 110 | std::u16string sources; |
Andrew Xu | be3d7ea | 2021-01-14 21:48:17 | [diff] [blame] | 111 | 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 | |
| 117 | TEST_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 Black | d78ea3f | 2020-08-24 18:29:07 | [diff] [blame] | 124 | TEST_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 |