override drawImage
[chromium-blink-merge.git] / components / clipboard / clipboard_apptest.cc
blobda45d2e8b0a3867aa1a9e02d38bcc93dc840e5d3
1 // Copyright 2014 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.
5 #include "base/bind.h"
6 #include "base/run_loop.h"
7 #include "components/clipboard/public/interfaces/clipboard.mojom.h"
8 #include "mojo/application/application_test_base_chromium.h"
9 #include "mojo/common/common_type_converters.h"
10 #include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h"
12 using mojo::Array;
13 using mojo::Clipboard;
14 using mojo::Map;
15 using mojo::String;
17 namespace {
19 void CopyUint64AndEndRunloop(uint64_t* output,
20 base::RunLoop* run_loop,
21 uint64_t input) {
22 *output = input;
23 run_loop->Quit();
26 void CopyStringAndEndRunloop(std::string* output,
27 bool* string_is_null,
28 base::RunLoop* run_loop,
29 const Array<uint8_t>& input) {
30 *string_is_null = input.is_null();
31 *output = input.is_null() ? "" : input.To<std::string>();
32 run_loop->Quit();
35 void CopyVectorStringAndEndRunloop(std::vector<std::string>* output,
36 base::RunLoop* run_loop,
37 const Array<String>& input) {
38 *output = input.To<std::vector<std::string> >();
39 run_loop->Quit();
42 const char* kUninitialized = "Uninitialized data";
43 const char* kPlainTextData = "Some plain data";
44 const char* kHtmlData = "<html>data</html>";
46 } // namespace
48 namespace clipboard {
50 class ClipboardAppTest : public mojo::test::ApplicationTestBase {
51 public:
52 ClipboardAppTest() : ApplicationTestBase() {}
53 ~ClipboardAppTest() override {}
55 void SetUp() override {
56 mojo::test::ApplicationTestBase::SetUp();
57 application_impl()->ConnectToService("mojo:clipboard", &clipboard_);
60 uint64_t GetSequenceNumber() {
61 base::RunLoop run_loop;
62 uint64_t sequence_num = 999999;
63 clipboard_->GetSequenceNumber(
64 Clipboard::TYPE_COPY_PASTE,
65 base::Bind(&CopyUint64AndEndRunloop, &sequence_num, &run_loop));
66 run_loop.Run();
67 return sequence_num;
70 std::vector<std::string> GetAvailableFormatMimeTypes() {
71 base::RunLoop run_loop;
72 std::vector<std::string> types;
73 types.push_back(kUninitialized);
74 clipboard_->GetAvailableMimeTypes(
75 Clipboard::TYPE_COPY_PASTE,
76 base::Bind(&CopyVectorStringAndEndRunloop, &types, &run_loop));
77 run_loop.Run();
78 return types;
81 bool GetDataOfType(const std::string& mime_type, std::string* data) {
82 base::RunLoop run_loop;
83 bool is_null = false;
84 clipboard_->ReadMimeType(
85 Clipboard::TYPE_COPY_PASTE, mime_type,
86 base::Bind(&CopyStringAndEndRunloop, data, &is_null, &run_loop));
87 run_loop.Run();
88 return !is_null;
91 void SetStringText(const std::string& data) {
92 Map<String, Array<uint8_t>> mime_data;
93 mime_data[Clipboard::MIME_TYPE_TEXT] = Array<uint8_t>::From(data);
94 clipboard_->WriteClipboardData(Clipboard::TYPE_COPY_PASTE,
95 mime_data.Pass());
98 protected:
99 mojo::ClipboardPtr clipboard_;
101 DISALLOW_COPY_AND_ASSIGN(ClipboardAppTest);
104 TEST_F(ClipboardAppTest, EmptyClipboardOK) {
105 EXPECT_EQ(0ul, GetSequenceNumber());
106 EXPECT_TRUE(GetAvailableFormatMimeTypes().empty());
107 std::string data;
108 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT, &data));
111 TEST_F(ClipboardAppTest, CanReadBackText) {
112 std::string data;
113 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT, &data));
114 EXPECT_EQ(0ul, GetSequenceNumber());
116 SetStringText(kPlainTextData);
117 EXPECT_EQ(1ul, GetSequenceNumber());
119 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_TEXT, &data));
120 EXPECT_EQ(kPlainTextData, data);
123 TEST_F(ClipboardAppTest, CanSetMultipleDataTypesAtOnce) {
124 Map<String, Array<uint8_t>> mime_data;
125 mime_data[Clipboard::MIME_TYPE_TEXT] =
126 Array<uint8_t>::From(std::string(kPlainTextData));
127 mime_data[Clipboard::MIME_TYPE_HTML] =
128 Array<uint8_t>::From(std::string(kHtmlData));
130 clipboard_->WriteClipboardData(Clipboard::TYPE_COPY_PASTE, mime_data.Pass());
132 EXPECT_EQ(1ul, GetSequenceNumber());
134 std::string data;
135 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_TEXT, &data));
136 EXPECT_EQ(kPlainTextData, data);
137 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_HTML, &data));
138 EXPECT_EQ(kHtmlData, data);
141 TEST_F(ClipboardAppTest, CanClearClipboardWithZeroArray) {
142 std::string data;
143 SetStringText(kPlainTextData);
144 EXPECT_EQ(1ul, GetSequenceNumber());
146 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_TEXT, &data));
147 EXPECT_EQ(kPlainTextData, data);
149 Map<String, Array<uint8_t>> mime_data;
150 clipboard_->WriteClipboardData(Clipboard::TYPE_COPY_PASTE, mime_data.Pass());
152 EXPECT_EQ(2ul, GetSequenceNumber());
153 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT, &data));
156 } // namespace clipboard