#include "ui/base/clipboard/clipboard_non_backed.h"
#include <memory>
#include <string>
#include <vector>
#include "base/pickle.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/clipboard_data.h"
#include "ui/base/clipboard/custom_data_helper.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/skia_util.h"
#if BUILDFLAG(IS_OZONE)
#include "base/command_line.h"
#include "ui/gfx/switches.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/ozone_switches.h"
#endif
namespace ui {
namespace {
std::vector<std::string> UTF8Types(std::vector<std::u16string> types) { … }
}
class ClipboardNonBackedTestBase : public testing::Test { … };
class ClipboardNonBackedTest : public ClipboardNonBackedTestBase { … };
TEST_F(ClipboardNonBackedTest, WriteAndGetClipboardData) { … }
TEST_F(ClipboardNonBackedTest, WriteClipboardData) { … }
TEST_F(ClipboardNonBackedTest, AdminWriteDoesNotRecordHistograms) { … }
TEST_F(ClipboardNonBackedTest, PlainText) { … }
TEST_F(ClipboardNonBackedTest, BookmarkURL) { … }
TEST_F(ClipboardNonBackedTest, TextURIList) { … }
TEST_F(ClipboardNonBackedTest, ImageEncoding) { … }
TEST_F(ClipboardNonBackedTest, EncodeImageOnce) { … }
TEST_F(ClipboardNonBackedTest, EncodeMultipleImages) { … }
TEST_F(ClipboardNonBackedTest, ClipboardBufferTypes) { … }
#if BUILDFLAG(IS_CHROMEOS)
class ClipboardNonBackedMockTimeTest : public ClipboardNonBackedTestBase {
public:
ClipboardNonBackedMockTimeTest()
: ClipboardNonBackedTestBase(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
};
TEST_F(ClipboardNonBackedMockTimeTest,
RecordsTimeIntervalBetweenCommitAndRead) {
auto* clipboard = ClipboardNonBacked::GetForCurrentThread();
ASSERT_TRUE(clipboard);
ScopedClipboardWriter(ClipboardBuffer::kCopyPaste).WriteText(u"");
auto* clipboard_data = clipboard->GetClipboardData(nullptr);
ASSERT_TRUE(clipboard_data);
ASSERT_TRUE(clipboard_data->commit_time().has_value());
EXPECT_EQ(clipboard_data->commit_time().value(), base::Time::Now());
std::vector<base::RepeatingClosure> test_cases = {{
base::BindLambdaForTesting([&]() {
clipboard->ReadAsciiText(
ClipboardBuffer::kCopyPaste, nullptr,
std::make_unique<std::string>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadBookmark(
nullptr,
std::make_unique<std::u16string>().get(),
std::make_unique<std::string>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadDataTransferCustomData(
ClipboardBuffer::kCopyPaste,
std::u16string(), nullptr,
std::make_unique<std::u16string>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadData(ui::ClipboardFormatType(),
nullptr,
std::make_unique<std::string>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadFilenames(
ClipboardBuffer::kCopyPaste, nullptr,
std::make_unique<std::vector<ui::FileInfo>>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadHTML(
ClipboardBuffer::kCopyPaste, nullptr,
std::make_unique<std::u16string>().get(),
std::make_unique<std::string>().get(),
std::make_unique<uint32_t>().get(),
std::make_unique<uint32_t>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadPng(ClipboardBuffer::kCopyPaste,
nullptr,
base::DoNothing());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadRTF(ClipboardBuffer::kCopyPaste,
nullptr,
std::make_unique<std::string>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadSvg(ClipboardBuffer::kCopyPaste, nullptr,
std::make_unique<std::u16string>().get());
}),
base::BindLambdaForTesting([&]() {
clipboard->ReadText(
ClipboardBuffer::kCopyPaste, nullptr,
std::make_unique<std::u16string>().get());
}),
}};
constexpr base::TimeDelta kTimeDelta = base::Seconds(10);
constexpr char kHistogram[] = "Clipboard.TimeIntervalBetweenCommitAndRead";
for (size_t i = 1u; i <= test_cases.size(); ++i) {
base::HistogramTester histogram_tester;
histogram_tester.ExpectUniqueTimeSample(kHistogram, i * kTimeDelta, 0u);
task_environment_.FastForwardBy(kTimeDelta);
test_cases.at(i - 1u).Run();
histogram_tester.ExpectUniqueTimeSample(kHistogram, i * kTimeDelta, 1u);
}
}
#endif
}