Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / clipboard / clipboard_apptest.cc
blob081fac36782bd47a4eba45e3fcc79e5fbf77a6ee
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/public/cpp/application_impl.h"
9 #include "mojo/application/public/cpp/application_test_base.h"
10 #include "mojo/common/common_type_converters.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 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));
68 run_loop.Run();
69 return sequence_num;
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));
79 run_loop.Run();
80 return types;
83 bool GetDataOfType(const std::string& mime_type, std::string* data) {
84 base::RunLoop run_loop;
85 bool is_null = false;
86 clipboard_->ReadMimeType(
87 Clipboard::TYPE_COPY_PASTE, mime_type,
88 base::Bind(&CopyStringAndEndRunloop, data, &is_null, &run_loop));
89 run_loop.Run();
90 return !is_null;
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,
97 mime_data.Pass());
100 protected:
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());
109 std::string data;
110 EXPECT_FALSE(GetDataOfType(Clipboard::MIME_TYPE_TEXT, &data));
113 TEST_F(ClipboardAppTest, CanReadBackText) {
114 std::string data;
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());
136 std::string data;
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) {
144 std::string data;
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