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/public/cpp/application_impl.h"
9 #include "mojo/application/public/cpp/application_test_base.h"
10 #include "mojo/common/common_type_converters.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 mojo::URLRequestPtr
request(mojo::URLRequest::New());
58 request
->url
= mojo::String::From("mojo:clipboard");
59 application_impl()->ConnectToService(request
.Pass(), &clipboard_
);
62 uint64_t GetSequenceNumber() {
63 base::RunLoop run_loop
;
64 uint64_t sequence_num
= 999999;
65 clipboard_
->GetSequenceNumber(
66 Clipboard::TYPE_COPY_PASTE
,
67 base::Bind(&CopyUint64AndEndRunloop
, &sequence_num
, &run_loop
));
72 std::vector
<std::string
> GetAvailableFormatMimeTypes() {
73 base::RunLoop run_loop
;
74 std::vector
<std::string
> types
;
75 types
.push_back(kUninitialized
);
76 clipboard_
->GetAvailableMimeTypes(
77 Clipboard::TYPE_COPY_PASTE
,
78 base::Bind(&CopyVectorStringAndEndRunloop
, &types
, &run_loop
));
83 bool GetDataOfType(const std::string
& mime_type
, std::string
* data
) {
84 base::RunLoop run_loop
;
86 clipboard_
->ReadMimeType(
87 Clipboard::TYPE_COPY_PASTE
, mime_type
,
88 base::Bind(&CopyStringAndEndRunloop
, data
, &is_null
, &run_loop
));
93 void SetStringText(const std::string
& data
) {
94 Map
<String
, Array
<uint8_t>> mime_data
;
95 mime_data
[Clipboard::MIME_TYPE_TEXT
] = Array
<uint8_t>::From(data
);
96 clipboard_
->WriteClipboardData(Clipboard::TYPE_COPY_PASTE
,
101 mojo::ClipboardPtr clipboard_
;
103 DISALLOW_COPY_AND_ASSIGN(ClipboardAppTest
);
106 TEST_F(ClipboardAppTest
, EmptyClipboardOK
) {
107 EXPECT_EQ(0ul, GetSequenceNumber());
108 EXPECT_TRUE(GetAvailableFormatMimeTypes().empty());
110 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT
, &data
));
113 TEST_F(ClipboardAppTest
, CanReadBackText
) {
115 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT
, &data
));
116 EXPECT_EQ(0ul, GetSequenceNumber());
118 SetStringText(kPlainTextData
);
119 EXPECT_EQ(1ul, GetSequenceNumber());
121 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_TEXT
, &data
));
122 EXPECT_EQ(kPlainTextData
, data
);
125 TEST_F(ClipboardAppTest
, CanSetMultipleDataTypesAtOnce
) {
126 Map
<String
, Array
<uint8_t>> mime_data
;
127 mime_data
[Clipboard::MIME_TYPE_TEXT
] =
128 Array
<uint8_t>::From(std::string(kPlainTextData
));
129 mime_data
[Clipboard::MIME_TYPE_HTML
] =
130 Array
<uint8_t>::From(std::string(kHtmlData
));
132 clipboard_
->WriteClipboardData(Clipboard::TYPE_COPY_PASTE
, mime_data
.Pass());
134 EXPECT_EQ(1ul, GetSequenceNumber());
137 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_TEXT
, &data
));
138 EXPECT_EQ(kPlainTextData
, data
);
139 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_HTML
, &data
));
140 EXPECT_EQ(kHtmlData
, data
);
143 TEST_F(ClipboardAppTest
, CanClearClipboardWithZeroArray
) {
145 SetStringText(kPlainTextData
);
146 EXPECT_EQ(1ul, GetSequenceNumber());
148 EXPECT_TRUE(GetDataOfType(Clipboard::MIME_TYPE_TEXT
, &data
));
149 EXPECT_EQ(kPlainTextData
, data
);
151 Map
<String
, Array
<uint8_t>> mime_data
;
152 clipboard_
->WriteClipboardData(Clipboard::TYPE_COPY_PASTE
, mime_data
.Pass());
154 EXPECT_EQ(2ul, GetSequenceNumber());
155 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT
, &data
));
158 } // namespace clipboard