1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#define LOG_TAG "PasteboardCapiTest"
17bc03f14fSopenharmony_ci
18bc03f14fSopenharmony_ci#include <gtest/gtest.h>
19bc03f14fSopenharmony_ci#include <unistd.h>
20bc03f14fSopenharmony_ci
21bc03f14fSopenharmony_ci#include "token_setproc.h"
22bc03f14fSopenharmony_ci#include "accesstoken_kit.h"
23bc03f14fSopenharmony_ci#include "nativetoken_kit.h"
24bc03f14fSopenharmony_ci
25bc03f14fSopenharmony_ci#include "oh_pasteboard.h"
26bc03f14fSopenharmony_ci#include "oh_pasteboard_err_code.h"
27bc03f14fSopenharmony_ci#include "oh_pasteboard_observer_impl.h"
28bc03f14fSopenharmony_ci#include "udmf.h"
29bc03f14fSopenharmony_ci#include "uds.h"
30bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
31bc03f14fSopenharmony_ci#include "os_account_manager.h"
32bc03f14fSopenharmony_ci
33bc03f14fSopenharmony_ciusing namespace testing::ext;
34bc03f14fSopenharmony_ciusing namespace OHOS::Security::AccessToken;
35bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices;
36bc03f14fSopenharmony_ci
37bc03f14fSopenharmony_cinamespace OHOS::Test {
38bc03f14fSopenharmony_ciclass PasteboardCapiTest : public testing::Test {
39bc03f14fSopenharmony_cipublic:
40bc03f14fSopenharmony_ci    static void SetUpTestCase(void);
41bc03f14fSopenharmony_ci    static void TearDownTestCase(void);
42bc03f14fSopenharmony_ci    void SetUp();
43bc03f14fSopenharmony_ci    void TearDown();
44bc03f14fSopenharmony_ci    static void CallbackFunc(void* context, Pasteboard_NotifyType type);
45bc03f14fSopenharmony_ci    static void RemoveCallbackSideEffects();
46bc03f14fSopenharmony_ci    static void ContextFinalizeFunc(void* context);
47bc03f14fSopenharmony_ci    static int callbackValue;
48bc03f14fSopenharmony_ci    static void AllocTestTokenId();
49bc03f14fSopenharmony_ci    static void DeleteTestTokenId();
50bc03f14fSopenharmony_ci    static void SetTestTokenId();
51bc03f14fSopenharmony_ci    static void RestoreSelfTokenId();
52bc03f14fSopenharmony_ci    static void* GetDataCallback(void* context, const char* type);
53bc03f14fSopenharmony_ci    static constexpr int INIT_VALUE = 0;
54bc03f14fSopenharmony_ci    static constexpr int UPDATE_VALUE = 1;
55bc03f14fSopenharmony_ci    static uint64_t selfTokenId_;
56bc03f14fSopenharmony_ci    static AccessTokenID testTokenId_;
57bc03f14fSopenharmony_ci    static constexpr char PLAINTEXT_CONTENT[] = "PLAINTEXT_CONTENT";
58bc03f14fSopenharmony_ci    static constexpr char HYPERLINK_URL[] = "file://data/image.png";
59bc03f14fSopenharmony_ci};
60bc03f14fSopenharmony_ciuint64_t PasteboardCapiTest::selfTokenId_ = 0;
61bc03f14fSopenharmony_ciAccessTokenID PasteboardCapiTest::testTokenId_ = 0;
62bc03f14fSopenharmony_ciint PasteboardCapiTest::callbackValue = 0;
63bc03f14fSopenharmony_ci
64bc03f14fSopenharmony_civoid PasteboardCapiTest::SetUpTestCase(void)
65bc03f14fSopenharmony_ci{
66bc03f14fSopenharmony_ci    callbackValue = INIT_VALUE;
67bc03f14fSopenharmony_ci    selfTokenId_ = GetSelfTokenID();
68bc03f14fSopenharmony_ci    AllocTestTokenId();
69bc03f14fSopenharmony_ci}
70bc03f14fSopenharmony_ci
71bc03f14fSopenharmony_civoid PasteboardCapiTest::TearDownTestCase(void)
72bc03f14fSopenharmony_ci{
73bc03f14fSopenharmony_ci    RemoveCallbackSideEffects();
74bc03f14fSopenharmony_ci    DeleteTestTokenId();
75bc03f14fSopenharmony_ci}
76bc03f14fSopenharmony_ci
77bc03f14fSopenharmony_civoid PasteboardCapiTest::SetUp(void)
78bc03f14fSopenharmony_ci{
79bc03f14fSopenharmony_ci}
80bc03f14fSopenharmony_ci
81bc03f14fSopenharmony_civoid PasteboardCapiTest::TearDown(void)
82bc03f14fSopenharmony_ci{
83bc03f14fSopenharmony_ci}
84bc03f14fSopenharmony_ci
85bc03f14fSopenharmony_civoid PasteboardCapiTest::AllocTestTokenId()
86bc03f14fSopenharmony_ci{
87bc03f14fSopenharmony_ci    std::vector<int32_t> ids;
88bc03f14fSopenharmony_ci    auto ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
89bc03f14fSopenharmony_ci    if (ret != ERR_OK || ids.empty()) {
90bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "query active user failed errCode = %{public}d", ret);
91bc03f14fSopenharmony_ci        return;
92bc03f14fSopenharmony_ci    }
93bc03f14fSopenharmony_ci    HapInfoParams infoParams = {
94bc03f14fSopenharmony_ci        .userID = ids[0],
95bc03f14fSopenharmony_ci        .bundleName = "ohos.privacy_test.pasteboard",
96bc03f14fSopenharmony_ci        .instIndex = 0,
97bc03f14fSopenharmony_ci        .appIDDesc = "privacy_test.pasteboard"
98bc03f14fSopenharmony_ci    };
99bc03f14fSopenharmony_ci    PermissionStateFull testState = {
100bc03f14fSopenharmony_ci        .permissionName = "ohos.permission.DUMP",
101bc03f14fSopenharmony_ci        .isGeneral = true,
102bc03f14fSopenharmony_ci        .resDeviceID = { "local" },
103bc03f14fSopenharmony_ci        .grantStatus = { PermissionState::PERMISSION_GRANTED },
104bc03f14fSopenharmony_ci        .grantFlags = { 1 }
105bc03f14fSopenharmony_ci    };
106bc03f14fSopenharmony_ci    HapPolicyParams policyParams = {
107bc03f14fSopenharmony_ci        .apl = APL_NORMAL,
108bc03f14fSopenharmony_ci        .domain = "test.domain.pasteboard",
109bc03f14fSopenharmony_ci        .permList = {},
110bc03f14fSopenharmony_ci        .permStateList = { testState }
111bc03f14fSopenharmony_ci    };
112bc03f14fSopenharmony_ci
113bc03f14fSopenharmony_ci    AccessTokenKit::AllocHapToken(infoParams, policyParams);
114bc03f14fSopenharmony_ci    testTokenId_ = Security::AccessToken::AccessTokenKit::GetHapTokenID(
115bc03f14fSopenharmony_ci        infoParams.userID, infoParams.bundleName, infoParams.instIndex);
116bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "userID = %{public}d, testTokenId = 0x%{public}x.", infoParams.userID,
117bc03f14fSopenharmony_ci        testTokenId_);
118bc03f14fSopenharmony_ci}
119bc03f14fSopenharmony_ci
120bc03f14fSopenharmony_civoid PasteboardCapiTest::DeleteTestTokenId()
121bc03f14fSopenharmony_ci{
122bc03f14fSopenharmony_ci    AccessTokenKit::DeleteToken(testTokenId_);
123bc03f14fSopenharmony_ci}
124bc03f14fSopenharmony_ci
125bc03f14fSopenharmony_ci
126bc03f14fSopenharmony_civoid PasteboardCapiTest::SetTestTokenId()
127bc03f14fSopenharmony_ci{
128bc03f14fSopenharmony_ci    auto ret = SetSelfTokenID(testTokenId_);
129bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "testTokenId = 0x%{public}x, ret = %{public}d!", testTokenId_, ret);
130bc03f14fSopenharmony_ci}
131bc03f14fSopenharmony_ci
132bc03f14fSopenharmony_civoid PasteboardCapiTest::RestoreSelfTokenId()
133bc03f14fSopenharmony_ci{
134bc03f14fSopenharmony_ci    auto ret = SetSelfTokenID(selfTokenId_);
135bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "ret = %{public}d!", ret);
136bc03f14fSopenharmony_ci}
137bc03f14fSopenharmony_ci
138bc03f14fSopenharmony_civoid PasteboardCapiTest::CallbackFunc(void* context, Pasteboard_NotifyType type)
139bc03f14fSopenharmony_ci{
140bc03f14fSopenharmony_ci    callbackValue = UPDATE_VALUE;
141bc03f14fSopenharmony_ci}
142bc03f14fSopenharmony_ci
143bc03f14fSopenharmony_civoid PasteboardCapiTest::RemoveCallbackSideEffects()
144bc03f14fSopenharmony_ci{
145bc03f14fSopenharmony_ci    callbackValue = INIT_VALUE;
146bc03f14fSopenharmony_ci}
147bc03f14fSopenharmony_ci
148bc03f14fSopenharmony_civoid PasteboardCapiTest::ContextFinalizeFunc(void* context) {}
149bc03f14fSopenharmony_ci
150bc03f14fSopenharmony_civoid* PasteboardCapiTest::GetDataCallback(void* context, const char* type)
151bc03f14fSopenharmony_ci{
152bc03f14fSopenharmony_ci    if (std::string(type) == "general.plain-text") {
153bc03f14fSopenharmony_ci        OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
154bc03f14fSopenharmony_ci        OH_UdsPlainText_SetContent(plainText, PLAINTEXT_CONTENT);
155bc03f14fSopenharmony_ci        return plainText;
156bc03f14fSopenharmony_ci    } else if (std::string(type) == "general.hyperlink") {
157bc03f14fSopenharmony_ci        OH_UdsHyperlink* link = OH_UdsHyperlink_Create();
158bc03f14fSopenharmony_ci        OH_UdsHyperlink_SetUrl(link, HYPERLINK_URL);
159bc03f14fSopenharmony_ci        return link;
160bc03f14fSopenharmony_ci    }
161bc03f14fSopenharmony_ci    return nullptr;
162bc03f14fSopenharmony_ci}
163bc03f14fSopenharmony_ci
164bc03f14fSopenharmony_ci/**
165bc03f14fSopenharmony_ci * @tc.name: OH_PasteboardSubscriber_Create001
166bc03f14fSopenharmony_ci * @tc.desc: OH_PasteboardObserver_Create test
167bc03f14fSopenharmony_ci * @tc.type: FUNC
168bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
169bc03f14fSopenharmony_ci */
170bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_PasteboardSubscriber_Create001, TestSize.Level1)
171bc03f14fSopenharmony_ci{
172bc03f14fSopenharmony_ci    OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
173bc03f14fSopenharmony_ci    EXPECT_NE(observer, nullptr);
174bc03f14fSopenharmony_ci
175bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(observer);
176bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(nullptr);
177bc03f14fSopenharmony_ci}
178bc03f14fSopenharmony_ci
179bc03f14fSopenharmony_ci/**
180bc03f14fSopenharmony_ci * @tc.name: OH_PasteboardObserver_SetData001
181bc03f14fSopenharmony_ci * @tc.desc: OH_PasteboardObserver_SetData test valid
182bc03f14fSopenharmony_ci * @tc.type: FUNC
183bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
184bc03f14fSopenharmony_ci */
185bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData001, TestSize.Level1)
186bc03f14fSopenharmony_ci{
187bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
188bc03f14fSopenharmony_ci    void* context = static_cast<void*>(pasteboard);
189bc03f14fSopenharmony_ci    OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
190bc03f14fSopenharmony_ci
191bc03f14fSopenharmony_ci    int setRes1 = OH_PasteboardObserver_SetData(observer, context, CallbackFunc, ContextFinalizeFunc);
192bc03f14fSopenharmony_ci    EXPECT_EQ(setRes1, ERR_OK);
193bc03f14fSopenharmony_ci
194bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(observer);
195bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
196bc03f14fSopenharmony_ci}
197bc03f14fSopenharmony_ci
198bc03f14fSopenharmony_ci/**
199bc03f14fSopenharmony_ci * @tc.name: OH_PasteboardObserver_SetData002
200bc03f14fSopenharmony_ci * @tc.desc: OH_PasteboardObserver_SetData test invalid
201bc03f14fSopenharmony_ci * @tc.type: FUNC
202bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
203bc03f14fSopenharmony_ci */
204bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData002, TestSize.Level1)
205bc03f14fSopenharmony_ci{
206bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
207bc03f14fSopenharmony_ci    void* context = static_cast<void*>(pasteboard);
208bc03f14fSopenharmony_ci    OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
209bc03f14fSopenharmony_ci
210bc03f14fSopenharmony_ci    int setRes1 = OH_PasteboardObserver_SetData(nullptr, context, CallbackFunc, ContextFinalizeFunc);
211bc03f14fSopenharmony_ci    EXPECT_EQ(setRes1, ERR_INVALID_PARAMETER);
212bc03f14fSopenharmony_ci
213bc03f14fSopenharmony_ci    int setRes2 = OH_PasteboardObserver_SetData(observer, context, nullptr, ContextFinalizeFunc);
214bc03f14fSopenharmony_ci    EXPECT_EQ(setRes2, ERR_INVALID_PARAMETER);
215bc03f14fSopenharmony_ci
216bc03f14fSopenharmony_ci    int setRes3 = OH_PasteboardObserver_SetData(observer, context, CallbackFunc, nullptr);
217bc03f14fSopenharmony_ci    EXPECT_EQ(setRes3, ERR_INVALID_PARAMETER);
218bc03f14fSopenharmony_ci
219bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(observer);
220bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
221bc03f14fSopenharmony_ci}
222bc03f14fSopenharmony_ci
223bc03f14fSopenharmony_ci/**
224bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_Create001
225bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_Create test valid
226bc03f14fSopenharmony_ci * @tc.type: FUNC
227bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
228bc03f14fSopenharmony_ci */
229bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_Create001, TestSize.Level1)
230bc03f14fSopenharmony_ci{
231bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
232bc03f14fSopenharmony_ci    EXPECT_NE(pasteboard, nullptr);
233bc03f14fSopenharmony_ci
234bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
235bc03f14fSopenharmony_ci}
236bc03f14fSopenharmony_ci
237bc03f14fSopenharmony_ci/**
238bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_Subscribe001
239bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_Subscribe test valid
240bc03f14fSopenharmony_ci * @tc.type: FUNC
241bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
242bc03f14fSopenharmony_ci */
243bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe001, TestSize.Level1)
244bc03f14fSopenharmony_ci{
245bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
246bc03f14fSopenharmony_ci    OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
247bc03f14fSopenharmony_ci    OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
248bc03f14fSopenharmony_ci
249bc03f14fSopenharmony_ci    int res = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
250bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
251bc03f14fSopenharmony_ci
252bc03f14fSopenharmony_ci    int resRepeat = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
253bc03f14fSopenharmony_ci    EXPECT_EQ(resRepeat, ERR_OK);
254bc03f14fSopenharmony_ci
255bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
256bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(observer);
257bc03f14fSopenharmony_ci}
258bc03f14fSopenharmony_ci
259bc03f14fSopenharmony_ci/**
260bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_Subscribe002
261bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_Subscribe test invalid
262bc03f14fSopenharmony_ci * @tc.type: FUNC
263bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
264bc03f14fSopenharmony_ci */
265bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe002, TestSize.Level1)
266bc03f14fSopenharmony_ci{
267bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
268bc03f14fSopenharmony_ci    OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
269bc03f14fSopenharmony_ci    OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
270bc03f14fSopenharmony_ci
271bc03f14fSopenharmony_ci    int res1 = OH_Pasteboard_Subscribe(nullptr, NOTIFY_LOCAL_DATA_CHANGE, observer);
272bc03f14fSopenharmony_ci    EXPECT_EQ(res1, ERR_INVALID_PARAMETER);
273bc03f14fSopenharmony_ci
274bc03f14fSopenharmony_ci    int res2 = OH_Pasteboard_Subscribe(pasteboard, 10, observer);
275bc03f14fSopenharmony_ci    EXPECT_EQ(res2, ERR_INVALID_PARAMETER);
276bc03f14fSopenharmony_ci
277bc03f14fSopenharmony_ci    int res3 = OH_Pasteboard_Subscribe(pasteboard, -1, observer);
278bc03f14fSopenharmony_ci    EXPECT_EQ(res3, ERR_INVALID_PARAMETER);
279bc03f14fSopenharmony_ci
280bc03f14fSopenharmony_ci    int res4 = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, nullptr);
281bc03f14fSopenharmony_ci    EXPECT_EQ(res4, ERR_INVALID_PARAMETER);
282bc03f14fSopenharmony_ci
283bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
284bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(observer);
285bc03f14fSopenharmony_ci}
286bc03f14fSopenharmony_ci
287bc03f14fSopenharmony_ci/**
288bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_Unsubcribe001
289bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_Unsubcribe test valid
290bc03f14fSopenharmony_ci * @tc.type: FUNC
291bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
292bc03f14fSopenharmony_ci */
293bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe001, TestSize.Level1)
294bc03f14fSopenharmony_ci{
295bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
296bc03f14fSopenharmony_ci    OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
297bc03f14fSopenharmony_ci    OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
298bc03f14fSopenharmony_ci
299bc03f14fSopenharmony_ci    OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
300bc03f14fSopenharmony_ci
301bc03f14fSopenharmony_ci    int res = OH_Pasteboard_Unsubscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
302bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
303bc03f14fSopenharmony_ci
304bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
305bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(observer);
306bc03f14fSopenharmony_ci}
307bc03f14fSopenharmony_ci
308bc03f14fSopenharmony_ci/**
309bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_Unsubscribe002
310bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_Unsubscribe test invalid
311bc03f14fSopenharmony_ci * @tc.type: FUNC
312bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
313bc03f14fSopenharmony_ci */
314bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe002, TestSize.Level1)
315bc03f14fSopenharmony_ci{
316bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
317bc03f14fSopenharmony_ci    OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
318bc03f14fSopenharmony_ci    OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
319bc03f14fSopenharmony_ci
320bc03f14fSopenharmony_ci    int res1 = OH_Pasteboard_Unsubscribe(nullptr, NOTIFY_LOCAL_DATA_CHANGE, observer);
321bc03f14fSopenharmony_ci    EXPECT_EQ(res1, ERR_INVALID_PARAMETER);
322bc03f14fSopenharmony_ci
323bc03f14fSopenharmony_ci    int res2 = OH_Pasteboard_Unsubscribe(pasteboard, 10, observer);
324bc03f14fSopenharmony_ci    EXPECT_EQ(res2, ERR_INVALID_PARAMETER);
325bc03f14fSopenharmony_ci
326bc03f14fSopenharmony_ci    int res3 = OH_Pasteboard_Unsubscribe(pasteboard, -1, observer);
327bc03f14fSopenharmony_ci    EXPECT_EQ(res3, ERR_INVALID_PARAMETER);
328bc03f14fSopenharmony_ci
329bc03f14fSopenharmony_ci    int res4 = OH_Pasteboard_Unsubscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, nullptr);
330bc03f14fSopenharmony_ci    EXPECT_EQ(res4, ERR_INVALID_PARAMETER);
331bc03f14fSopenharmony_ci
332bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
333bc03f14fSopenharmony_ci    OH_PasteboardObserver_Destroy(observer);
334bc03f14fSopenharmony_ci}
335bc03f14fSopenharmony_ci
336bc03f14fSopenharmony_ci/**
337bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_IsRemoteData001
338bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_IsRemoteData test valid
339bc03f14fSopenharmony_ci * @tc.type: FUNC
340bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
341bc03f14fSopenharmony_ci */
342bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_IsRemoteData001, TestSize.Level1)
343bc03f14fSopenharmony_ci{
344bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
345bc03f14fSopenharmony_ci
346bc03f14fSopenharmony_ci    bool res = OH_Pasteboard_IsRemoteData(pasteboard);
347bc03f14fSopenharmony_ci    EXPECT_FALSE(res);
348bc03f14fSopenharmony_ci
349bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
350bc03f14fSopenharmony_ci}
351bc03f14fSopenharmony_ci
352bc03f14fSopenharmony_ci/**
353bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_GetDataSrouce001
354bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_GetDataSrouce test valid
355bc03f14fSopenharmony_ci * @tc.type: FUNC
356bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
357bc03f14fSopenharmony_ci */
358bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetDataSrouce001, TestSize.Level1)
359bc03f14fSopenharmony_ci{
360bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
361bc03f14fSopenharmony_ci    OH_UdmfData* setData = OH_UdmfData_Create();
362bc03f14fSopenharmony_ci    OH_UdmfRecord* record = OH_UdmfRecord_Create();
363bc03f14fSopenharmony_ci    OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
364bc03f14fSopenharmony_ci    char content[] = "hello world";
365bc03f14fSopenharmony_ci    OH_UdsPlainText_SetContent(plainText, content);
366bc03f14fSopenharmony_ci    OH_UdmfRecord_AddPlainText(record, plainText);
367bc03f14fSopenharmony_ci    OH_UdmfData_AddRecord(setData, record);
368bc03f14fSopenharmony_ci
369bc03f14fSopenharmony_ci    OH_Pasteboard_SetData(pasteboard, setData);
370bc03f14fSopenharmony_ci
371bc03f14fSopenharmony_ci    int len = 100;
372bc03f14fSopenharmony_ci    char source[100];
373bc03f14fSopenharmony_ci    int res = OH_Pasteboard_GetDataSource(pasteboard, source, len);
374bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
375bc03f14fSopenharmony_ci
376bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
377bc03f14fSopenharmony_ci}
378bc03f14fSopenharmony_ci
379bc03f14fSopenharmony_ci/**
380bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_HasType001
381bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_HasType test valid
382bc03f14fSopenharmony_ci * @tc.type: FUNC
383bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
384bc03f14fSopenharmony_ci */
385bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasType001, TestSize.Level1)
386bc03f14fSopenharmony_ci{
387bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
388bc03f14fSopenharmony_ci    OH_UdmfData* setData = OH_UdmfData_Create();
389bc03f14fSopenharmony_ci    OH_UdmfRecord* record = OH_UdmfRecord_Create();
390bc03f14fSopenharmony_ci    OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
391bc03f14fSopenharmony_ci    char content[] = "hello world";
392bc03f14fSopenharmony_ci    OH_UdsPlainText_SetContent(plainText, content);
393bc03f14fSopenharmony_ci    OH_UdmfRecord_AddPlainText(record, plainText);
394bc03f14fSopenharmony_ci    OH_UdmfData_AddRecord(setData, record);
395bc03f14fSopenharmony_ci
396bc03f14fSopenharmony_ci    OH_Pasteboard_SetData(pasteboard, setData);
397bc03f14fSopenharmony_ci    char type[] = "general.plain-text";
398bc03f14fSopenharmony_ci    bool res = OH_Pasteboard_HasType(pasteboard, type);
399bc03f14fSopenharmony_ci    EXPECT_FALSE(res);
400bc03f14fSopenharmony_ci
401bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
402bc03f14fSopenharmony_ci}
403bc03f14fSopenharmony_ci
404bc03f14fSopenharmony_ci/**
405bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_HasData001
406bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_HasData test valid
407bc03f14fSopenharmony_ci * @tc.type: FUNC
408bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
409bc03f14fSopenharmony_ci */
410bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasData001, TestSize.Level1)
411bc03f14fSopenharmony_ci{
412bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
413bc03f14fSopenharmony_ci    OH_Pasteboard_ClearData(pasteboard);
414bc03f14fSopenharmony_ci    bool res = OH_Pasteboard_HasData(pasteboard);
415bc03f14fSopenharmony_ci    EXPECT_FALSE(res);
416bc03f14fSopenharmony_ci
417bc03f14fSopenharmony_ci    OH_UdmfData* setData = OH_UdmfData_Create();
418bc03f14fSopenharmony_ci    OH_UdmfRecord* record = OH_UdmfRecord_Create();
419bc03f14fSopenharmony_ci    OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
420bc03f14fSopenharmony_ci    char content[] = "hello world";
421bc03f14fSopenharmony_ci    OH_UdsPlainText_SetContent(plainText, content);
422bc03f14fSopenharmony_ci    OH_UdmfRecord_AddPlainText(record, plainText);
423bc03f14fSopenharmony_ci    OH_UdmfData_AddRecord(setData, record);
424bc03f14fSopenharmony_ci
425bc03f14fSopenharmony_ci    OH_Pasteboard_SetData(pasteboard, setData);
426bc03f14fSopenharmony_ci
427bc03f14fSopenharmony_ci    res = OH_Pasteboard_HasData(pasteboard);
428bc03f14fSopenharmony_ci    EXPECT_TRUE(res);
429bc03f14fSopenharmony_ci
430bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
431bc03f14fSopenharmony_ci}
432bc03f14fSopenharmony_ci
433bc03f14fSopenharmony_ci/**
434bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_ClearData001
435bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_ClearData test valid
436bc03f14fSopenharmony_ci * @tc.type: FUNC
437bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
438bc03f14fSopenharmony_ci */
439bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_ClearData001, TestSize.Level1)
440bc03f14fSopenharmony_ci{
441bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
442bc03f14fSopenharmony_ci    bool res = OH_Pasteboard_ClearData(pasteboard);
443bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
444bc03f14fSopenharmony_ci
445bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
446bc03f14fSopenharmony_ci}
447bc03f14fSopenharmony_ci
448bc03f14fSopenharmony_ci/**
449bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_SetData001
450bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_SetData test valid
451bc03f14fSopenharmony_ci * @tc.type: FUNC
452bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
453bc03f14fSopenharmony_ci */
454bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_SetData001, TestSize.Level1)
455bc03f14fSopenharmony_ci{
456bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
457bc03f14fSopenharmony_ci    OH_UdmfData *setData = OH_UdmfData_Create();
458bc03f14fSopenharmony_ci    OH_UdmfRecord *record = OH_UdmfRecord_Create();
459bc03f14fSopenharmony_ci    OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
460bc03f14fSopenharmony_ci    char content[] = "hello world";
461bc03f14fSopenharmony_ci    OH_UdsPlainText_SetContent(plainText, content);
462bc03f14fSopenharmony_ci    OH_UdmfRecord_AddPlainText(record, plainText);
463bc03f14fSopenharmony_ci    OH_UdmfData_AddRecord(setData, record);
464bc03f14fSopenharmony_ci
465bc03f14fSopenharmony_ci    bool res = OH_Pasteboard_SetData(pasteboard, setData);
466bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
467bc03f14fSopenharmony_ci
468bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
469bc03f14fSopenharmony_ci    OH_UdsPlainText_Destroy(plainText);
470bc03f14fSopenharmony_ci    OH_UdmfRecord_Destroy(record);
471bc03f14fSopenharmony_ci    OH_UdmfData_Destroy(setData);
472bc03f14fSopenharmony_ci}
473bc03f14fSopenharmony_ci
474bc03f14fSopenharmony_ci/**
475bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_GetData001
476bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_GetData test valid
477bc03f14fSopenharmony_ci * @tc.type: FUNC
478bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
479bc03f14fSopenharmony_ci */
480bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData001, TestSize.Level1)
481bc03f14fSopenharmony_ci{
482bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
483bc03f14fSopenharmony_ci    OH_UdmfData* setData = OH_UdmfData_Create();
484bc03f14fSopenharmony_ci    OH_UdmfRecord* record = OH_UdmfRecord_Create();
485bc03f14fSopenharmony_ci    OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
486bc03f14fSopenharmony_ci    char content[] = "hello world";
487bc03f14fSopenharmony_ci    OH_UdsPlainText_SetContent(plainText, content);
488bc03f14fSopenharmony_ci    OH_UdmfRecord_AddPlainText(record, plainText);
489bc03f14fSopenharmony_ci    OH_UdmfData_AddRecord(setData, record);
490bc03f14fSopenharmony_ci
491bc03f14fSopenharmony_ci    int res = OH_Pasteboard_SetData(pasteboard, setData);
492bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
493bc03f14fSopenharmony_ci
494bc03f14fSopenharmony_ci    int status = -1;
495bc03f14fSopenharmony_ci    OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
496bc03f14fSopenharmony_ci    EXPECT_EQ(status, ERR_OK);
497bc03f14fSopenharmony_ci    EXPECT_NE(getData, nullptr);
498bc03f14fSopenharmony_ci
499bc03f14fSopenharmony_ci    unsigned int count = 0;
500bc03f14fSopenharmony_ci    OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &count);
501bc03f14fSopenharmony_ci    EXPECT_EQ(count, 1);
502bc03f14fSopenharmony_ci    OH_UdsPlainText *getPlainText = OH_UdsPlainText_Create();
503bc03f14fSopenharmony_ci    OH_UdmfRecord_GetPlainText(getRecords[0], getPlainText);
504bc03f14fSopenharmony_ci    const char *getContent = OH_UdsPlainText_GetContent(getPlainText);
505bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "content is %{public}s", getContent);
506bc03f14fSopenharmony_ci    EXPECT_EQ(strcmp(getContent, content), 0);
507bc03f14fSopenharmony_ci
508bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
509bc03f14fSopenharmony_ci    OH_UdsPlainText_Destroy(plainText);
510bc03f14fSopenharmony_ci    OH_UdsPlainText_Destroy(getPlainText);
511bc03f14fSopenharmony_ci    OH_UdmfRecord_Destroy(record);
512bc03f14fSopenharmony_ci    OH_UdmfData_Destroy(setData);
513bc03f14fSopenharmony_ci    OH_UdmfData_Destroy(getData);
514bc03f14fSopenharmony_ci}
515bc03f14fSopenharmony_ci
516bc03f14fSopenharmony_ci/**
517bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_GetData002
518bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_GetData test valid
519bc03f14fSopenharmony_ci * @tc.type: FUNC
520bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
521bc03f14fSopenharmony_ci */
522bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData002, TestSize.Level1)
523bc03f14fSopenharmony_ci{
524bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
525bc03f14fSopenharmony_ci    OH_UdmfData* setData = OH_UdmfData_Create();
526bc03f14fSopenharmony_ci    OH_UdmfRecord* record = OH_UdmfRecord_Create();
527bc03f14fSopenharmony_ci    OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
528bc03f14fSopenharmony_ci    EXPECT_NE(provider, nullptr);
529bc03f14fSopenharmony_ci    OH_UdmfRecordProvider_SetData(provider, static_cast<void *>(record), GetDataCallback, ContextFinalizeFunc);
530bc03f14fSopenharmony_ci    OH_UdmfData_AddRecord(setData, record);
531bc03f14fSopenharmony_ci
532bc03f14fSopenharmony_ci    const char* types[3] = { "general.plain-text", "general.hyperlink", "general.html" };
533bc03f14fSopenharmony_ci    OH_UdmfRecord_SetProvider(record, types, 3, provider);
534bc03f14fSopenharmony_ci    int res = OH_Pasteboard_SetData(pasteboard, setData);
535bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
536bc03f14fSopenharmony_ci
537bc03f14fSopenharmony_ci    int status = -1;
538bc03f14fSopenharmony_ci    OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
539bc03f14fSopenharmony_ci    EXPECT_EQ(status, ERR_OK);
540bc03f14fSopenharmony_ci    EXPECT_NE(getData, nullptr);
541bc03f14fSopenharmony_ci
542bc03f14fSopenharmony_ci    unsigned int count = 0;
543bc03f14fSopenharmony_ci    OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &count);
544bc03f14fSopenharmony_ci    EXPECT_EQ(count, 1);
545bc03f14fSopenharmony_ci    OH_UdsPlainText *getPlainText = OH_UdsPlainText_Create();
546bc03f14fSopenharmony_ci    OH_UdmfRecord_GetPlainText(getRecords[0], getPlainText);
547bc03f14fSopenharmony_ci    const char *getContent = OH_UdsPlainText_GetContent(getPlainText);
548bc03f14fSopenharmony_ci    EXPECT_EQ(strcmp(getContent, PLAINTEXT_CONTENT), 0);
549bc03f14fSopenharmony_ci
550bc03f14fSopenharmony_ci    OH_UdsHyperlink *getHyperLink = OH_UdsHyperlink_Create();
551bc03f14fSopenharmony_ci    OH_UdmfRecord_GetHyperlink(getRecords[0], getHyperLink);
552bc03f14fSopenharmony_ci    const char *getUrl = OH_UdsHyperlink_GetUrl(getHyperLink);
553bc03f14fSopenharmony_ci    EXPECT_EQ(strcmp(getUrl, HYPERLINK_URL), 0);
554bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
555bc03f14fSopenharmony_ci    OH_UdsPlainText_Destroy(getPlainText);
556bc03f14fSopenharmony_ci    OH_UdsHyperlink_Destroy(getHyperLink);
557bc03f14fSopenharmony_ci    OH_UdmfRecord_Destroy(record);
558bc03f14fSopenharmony_ci    OH_UdmfData_Destroy(setData);
559bc03f14fSopenharmony_ci    OH_UdmfData_Destroy(getData);
560bc03f14fSopenharmony_ci}
561bc03f14fSopenharmony_ci
562bc03f14fSopenharmony_ci/**
563bc03f14fSopenharmony_ci * @tc.name: OH_Pasteboard_GetData003
564bc03f14fSopenharmony_ci * @tc.desc: OH_Pasteboard_GetData test valid
565bc03f14fSopenharmony_ci * @tc.type: FUNC
566bc03f14fSopenharmony_ci * @tc.require: AROOOH5R5G
567bc03f14fSopenharmony_ci */
568bc03f14fSopenharmony_ciHWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData003, TestSize.Level1)
569bc03f14fSopenharmony_ci{
570bc03f14fSopenharmony_ci    char typeId[] = "ApplicationDefined-myType";
571bc03f14fSopenharmony_ci    unsigned char entry[] = "CreateGeneralRecord1";
572bc03f14fSopenharmony_ci    unsigned int count = sizeof(entry);
573bc03f14fSopenharmony_ci    OH_UdmfRecord *record = OH_UdmfRecord_Create();
574bc03f14fSopenharmony_ci    int addRes1 = OH_UdmfRecord_AddGeneralEntry(record, typeId, entry, count);
575bc03f14fSopenharmony_ci    EXPECT_EQ(addRes1, ERR_OK);
576bc03f14fSopenharmony_ci
577bc03f14fSopenharmony_ci    OH_UdmfData* setData = OH_UdmfData_Create();
578bc03f14fSopenharmony_ci    OH_UdmfData_AddRecord(setData, record);
579bc03f14fSopenharmony_ci    OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
580bc03f14fSopenharmony_ci    int res = OH_Pasteboard_SetData(pasteboard, setData);
581bc03f14fSopenharmony_ci    EXPECT_EQ(res, ERR_OK);
582bc03f14fSopenharmony_ci
583bc03f14fSopenharmony_ci    int status = -1;
584bc03f14fSopenharmony_ci    OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
585bc03f14fSopenharmony_ci    EXPECT_EQ(status, ERR_OK);
586bc03f14fSopenharmony_ci    EXPECT_NE(getData, nullptr);
587bc03f14fSopenharmony_ci
588bc03f14fSopenharmony_ci    unsigned int getrecordCount = 0;
589bc03f14fSopenharmony_ci    OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &getrecordCount);
590bc03f14fSopenharmony_ci    EXPECT_EQ(getrecordCount, 1);
591bc03f14fSopenharmony_ci
592bc03f14fSopenharmony_ci    unsigned int getCount = 0;
593bc03f14fSopenharmony_ci    unsigned char *getEntry;
594bc03f14fSopenharmony_ci    int getRes = OH_UdmfRecord_GetGeneralEntry(getRecords[0], typeId, &getEntry, &getCount);
595bc03f14fSopenharmony_ci    EXPECT_EQ(getRes, ERR_OK);
596bc03f14fSopenharmony_ci    EXPECT_EQ(getCount, count);
597bc03f14fSopenharmony_ci    EXPECT_EQ(memcmp(entry, getEntry, getCount), 0);
598bc03f14fSopenharmony_ci
599bc03f14fSopenharmony_ci    OH_Pasteboard_Destroy(pasteboard);
600bc03f14fSopenharmony_ci    OH_UdmfRecord_Destroy(record);
601bc03f14fSopenharmony_ci    OH_UdmfData_Destroy(setData);
602bc03f14fSopenharmony_ci    OH_UdmfData_Destroy(getData);
603bc03f14fSopenharmony_ci}
604bc03f14fSopenharmony_ci}