1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16 #include <cstdint>
17 #include <vector>
18 #include <ohos/aafwk/content/want.h>
19 #include "pasteboard_client.h"
20 #include "uri.h"
21 #include "pasteboard_observer_callback.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::MiscServices;
26
27 class PasteboardServiceTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase(void)35 void PasteboardServiceTest::SetUpTestCase(void)
36 {}
37
TearDownTestCase(void)38 void PasteboardServiceTest::TearDownTestCase(void)
39 {}
40
SetUp(void)41 void PasteboardServiceTest::SetUp(void)
42 {}
43
TearDown(void)44 void PasteboardServiceTest::TearDown(void)
45 {
46 PasteboardClient::GetInstance()->Clear();
47 }
48
OnPasteboardChanged()49 void PasteboardObserverCallback::OnPasteboardChanged()
50 {
51 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "callback.");
52 }
53
54 namespace {
55 /**
56 * @tc.name: PasteboardTest001
57 * @tc.desc: Create paste board test.
58 * @tc.type: FUNC
59 */
60 HWTEST_F(PasteboardServiceTest, PasteboardTest001, TestSize.Level0)
61 {
62 auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord("paste record1");
63 EXPECT_TRUE(record != nullptr);
64
65 auto data = PasteboardClient::GetInstance()->CreatePlainTextData("paste data1");
66 EXPECT_TRUE(data != nullptr);
67
68 PasteboardClient::GetInstance()->SetPasteData(*data);
69 auto has = PasteboardClient::GetInstance()->HasPasteData();
70 EXPECT_TRUE(has == true);
71 PasteData pasteData;
72 auto ok = PasteboardClient::GetInstance()->GetPasteData(pasteData);
73 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "get.");
74 EXPECT_TRUE(ok == true);
75 auto textPtr = pasteData.GetPrimaryText();
76 }
77
78 /**
79 * @tc.name: PasteRecordTest001
80 * @tc.desc: Create paste board record test.
81 * @tc.type: FUNC
82 */
83 HWTEST_F(PasteboardServiceTest, PasteRecordTest001, TestSize.Level0)
84 {
85 auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord("plain text");
86 EXPECT_TRUE(record != nullptr);
87 }
88
89 /**
90 * @tc.name: PasteRecordTest002
91 * @tc.desc: Create paste board record test.
92 * @tc.type: FUNC
93 */
94 HWTEST_F(PasteboardServiceTest, PasteRecordTest002, TestSize.Level0)
95 {
96 auto record = PasteboardClient::GetInstance()->CreateHtmlTextRecord("html record");
97 EXPECT_TRUE(record != nullptr);
98 }
99
100 /**
101 * @tc.name: PasteRecordTest003
102 * @tc.desc: Create paste board record test.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(PasteboardServiceTest, PasteRecordTest003, TestSize.Level0)
106 {
107 std::shared_ptr<OHOS::AAFwk::Want> want = std::make_shared<OHOS::AAFwk::Want>();
108 auto record = PasteboardClient::GetInstance()->CreateWantRecord(want);
109 EXPECT_TRUE(record != nullptr);
110 }
111
112 /**
113 * @tc.name: PasteRecordTest004
114 * @tc.desc: Create paste board record test.
115 * @tc.type: FUNC
116 */
117 HWTEST_F(PasteboardServiceTest, PasteRecordTest004, TestSize.Level0)
118 {
119 OHOS::Uri uri("uri");
120 auto record = PasteboardClient::GetInstance()->CreateUriRecord(uri);
121 EXPECT_TRUE(record != nullptr);
122 }
123
124
125 /**
126 * @tc.name: PasteDataTest001
127 * @tc.desc: Create paste board data test.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(PasteboardServiceTest, PasteDataTest001, TestSize.Level0)
131 {
132 std::shared_ptr<OHOS::AAFwk::Want> want = std::make_shared<OHOS::AAFwk::Want>();
133 auto data = PasteboardClient::GetInstance()->CreateWantData(want);
134 EXPECT_TRUE(data != nullptr);
135 auto has = PasteboardClient::GetInstance()->HasPasteData();
136 EXPECT_TRUE(has != true);
137 PasteboardClient::GetInstance()->SetPasteData(*data);
138 has = PasteboardClient::GetInstance()->HasPasteData();
139 EXPECT_TRUE(has == true);
140 PasteData pasteData;
141 auto ok = PasteboardClient::GetInstance()->GetPasteData(pasteData);
142 EXPECT_TRUE(ok == true);
143 auto record = pasteData.GetPrimaryWant();
144 EXPECT_TRUE(record != nullptr);
145 }
146
147 /**
148 * @tc.name: PasteDataTest002
149 * @tc.desc: Create paste board data test.
150 * @tc.type: FUNC
151 */
152 HWTEST_F(PasteboardServiceTest, PasteDataTest002, TestSize.Level0)
153 {
154 OHOS::Uri uri("uri");
155 auto data = PasteboardClient::GetInstance()->CreateUriData(uri);
156 EXPECT_TRUE(data != nullptr);
157 PasteboardClient::GetInstance()->Clear();
158 auto has = PasteboardClient::GetInstance()->HasPasteData();
159 EXPECT_TRUE(has != true);
160 PasteboardClient::GetInstance()->SetPasteData(*data);
161 has = PasteboardClient::GetInstance()->HasPasteData();
162 EXPECT_TRUE(has == true);
163 PasteData pasteData;
164 auto ok = PasteboardClient::GetInstance()->GetPasteData(pasteData);
165 EXPECT_TRUE(ok == true);
166 auto record = pasteData.GetPrimaryUri();
167 EXPECT_TRUE(record != nullptr);
168 }
169
170 /**
171 * @tc.name: PasteDataTest003
172 * @tc.desc: Create paste board data test.
173 * @tc.type: FUNC
174 */
175 HWTEST_F(PasteboardServiceTest, PasteDataTest003, TestSize.Level0)
176 {
177 std::string text = "plain text";
178 auto data = PasteboardClient::GetInstance()->CreatePlainTextData(text);
179 EXPECT_TRUE(data != nullptr);
180 PasteboardClient::GetInstance()->Clear();
181 auto has = PasteboardClient::GetInstance()->HasPasteData();
182 EXPECT_TRUE(has != true);
183 PasteboardClient::GetInstance()->SetPasteData(*data);
184 has = PasteboardClient::GetInstance()->HasPasteData();
185 EXPECT_TRUE(has == true);
186 PasteData pasteData;
187 auto ok = PasteboardClient::GetInstance()->GetPasteData(pasteData);
188 EXPECT_TRUE(ok == true);
189 auto record = pasteData.GetPrimaryText();
190 EXPECT_TRUE(record != nullptr);
191 }
192
193 /**
194 * @tc.name: PasteDataTest003
195 * @tc.desc: Create paste board data test.
196 * @tc.type: FUNC
197 */
198 HWTEST_F(PasteboardServiceTest, PasteDataTest004, TestSize.Level0)
199 {
200 std::string html = "plain text";
201 auto data = PasteboardClient::GetInstance()->CreateHtmlData(html);
202 EXPECT_TRUE(data != nullptr);
203 PasteboardClient::GetInstance()->Clear();
204 auto has = PasteboardClient::GetInstance()->HasPasteData();
205 EXPECT_TRUE(has != true);
206 PasteboardClient::GetInstance()->SetPasteData(*data);
207 has = PasteboardClient::GetInstance()->HasPasteData();
208 EXPECT_TRUE(has == true);
209 PasteData pasteData;
210 auto ok = PasteboardClient::GetInstance()->GetPasteData(pasteData);
211 EXPECT_TRUE(ok == true);
212 auto record = pasteData.GetPrimaryHtml();
213 EXPECT_TRUE(record != nullptr);
214 }
215 }
216