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.
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"
13 using mojo::Clipboard
;
19 void CopyUint64AndEndRunloop(uint64_t* output
,
20 base::RunLoop
* run_loop
,
26 void CopyStringAndEndRunloop(std::string
* output
,
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
>();
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
> >();
42 const char* kUninitialized
= "Uninitialized data";
43 const char* kPlainTextData
= "Some plain data";
44 const char* kHtmlData
= "<html>data</html>";
50 class ClipboardAppTest
: public mojo::test::ApplicationTestBase
{
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
));
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
));
81 bool GetDataOfType(const std::string
& mime_type
, std::string
* data
) {
82 base::RunLoop run_loop
;
84 clipboard_
->ReadMimeType(
85 Clipboard::TYPE_COPY_PASTE
, mime_type
,
86 base::Bind(&CopyStringAndEndRunloop
, data
, &is_null
, &run_loop
));
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
,
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());
108 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT
, &data
));
111 TEST_F(ClipboardAppTest
, CanReadBackText
) {
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());
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
) {
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