1 /*
2 * Copyright (c) 2024 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
16 #include "napi/native_api.h"
17 #include "hilog/log.h"
18 #include <string>
19 #include <unistd.h>
20 #include "database/pasteboard/oh_pasteboard.h"
21 #include "database/pasteboard/oh_pasteboard_err_code.h"
22 #include "database/udmf/udmf.h"
23 #include "database/udmf/udmf_meta.h"
24 #include "database/udmf/uds.h"
25 #include "database/udmf/utd.h"
26 #include <iostream>
27 #include <map>
28 #include <ctime>
29 #include "common.h"
30 #include <cctype>
31 #include <condition_variable>
32 #include <cstdint>
33 #include <fcntl.h>
34 #include <functional>
35 #include <iostream>
36 #include <list>
37 #include <map>
38 #include <memory>
39 #include <mutex>
40 #include <string>
41 #include <sys/mman.h>
42 #include <thread>
43 #include <vector>
44 #include <string>
45 #include <random>
46 #include <chrono>
47 const char *TAG = "testLog";
48
OH_Pasteboard_GetMimeTypes0100(napi_env env,napi_callback_info info)49 static napi_value OH_Pasteboard_GetMimeTypes0100(napi_env env, napi_callback_info info)
50 {
51 napi_value result = nullptr;
52 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
53 NAPI_ASSERT(env, pasteboard != nullptr, "OH_Pasteboard_Create is fail.");
54 OH_Pasteboard_ClearData(pasteboard);
55 unsigned int count = 1000;
56 char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
57 NAPI_ASSERT(env, count == 0, "OH_Pasteboard_GetMimeTypes is fail.");
58 NAPI_ASSERT(env, res == nullptr, "OH_Pasteboard_GetMimeTypes is fail.");
59 napi_create_int32(env, res == nullptr, &result);
60 OH_Pasteboard_Destroy(pasteboard);
61 return result;
62 }
63
OH_Pasteboard_GetMimeTypes0200(napi_env env,napi_callback_info info)64 static napi_value OH_Pasteboard_GetMimeTypes0200(napi_env env, napi_callback_info info)
65 {
66 napi_value result = nullptr;
67 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
68 NAPI_ASSERT(env, pasteboard != nullptr, "OH_Pasteboard_Create is fail.");
69 OH_UdmfData* setData = OH_UdmfData_Create();
70 NAPI_ASSERT(env, setData != nullptr, "OH_UdmfData_Create is fail.");
71 OH_UdmfRecord* record = OH_UdmfRecord_Create();
72 NAPI_ASSERT(env, record != nullptr, "OH_UdmfData_Create is fail.");
73 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
74 NAPI_ASSERT(env, plainText != nullptr, "OH_UdmfData_Create is fail.");
75
76 char content[] = "hello world";
77 OH_UdsPlainText_SetContent(plainText, content);
78 OH_UdmfRecord_AddPlainText(record, plainText);
79 OH_UdmfData_AddRecord(setData, record);
80 OH_Pasteboard_SetData(pasteboard, setData);
81
82 unsigned int count = 1000;
83 char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
84 NAPI_ASSERT(env, count == 1, "OH_Pasteboard_GetMimeTypes is fail.");
85 NAPI_ASSERT(env, res != nullptr, "OH_Pasteboard_GetMimeTypes is fail.");
86 NAPI_ASSERT(env, strcmp("text/plain", res[0]) == 0, "OH_Pasteboard_GetMimeTypes is fail.");
87 napi_create_int32(env, strcmp("text/plain", res[0]) == 0, &result);
88 OH_Pasteboard_Destroy(pasteboard);
89 return result;
90 }
91
OH_Pasteboard_GetMimeTypes0300(napi_env env,napi_callback_info info)92 static napi_value OH_Pasteboard_GetMimeTypes0300(napi_env env, napi_callback_info info)
93 {
94 napi_value result = nullptr;
95 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
96 NAPI_ASSERT(env, pasteboard != nullptr, "OH_Pasteboard_Create is fail.");
97 OH_UdmfData* setData = OH_UdmfData_Create();
98 NAPI_ASSERT(env, setData != nullptr, "OH_UdmfData_Create is fail.");
99 OH_UdmfRecord* record = OH_UdmfRecord_Create();
100 NAPI_ASSERT(env, record != nullptr, "OH_UdmfData_Create is fail.");
101 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
102 NAPI_ASSERT(env, plainText != nullptr, "OH_UdmfData_Create is fail.");
103 char content[] = "hello world";
104 OH_UdsPlainText_SetContent(plainText, content);
105 OH_UdmfRecord_AddPlainText(record, plainText);
106 OH_UdmfData_AddRecord(setData, record);
107
108 OH_UdmfRecord* record2 = OH_UdmfRecord_Create();
109 NAPI_ASSERT(env, record2 != nullptr, "OH_UdmfData_Create is fail.");
110 OH_UdsHtml* htmlText = OH_UdsHtml_Create();
111 NAPI_ASSERT(env, htmlText != nullptr, "OH_UdmfData_Create is fail.");
112 char html[] = "<div class='disabled'>hello</div>";
113 OH_UdsHtml_SetContent(htmlText, html);
114 OH_UdmfRecord_AddHtml(record2, htmlText);
115 OH_UdmfData_AddRecord(setData, record2);
116 OH_Pasteboard_SetData(pasteboard, setData);
117
118 unsigned int count = 1000;
119 int num = 2;
120 char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
121 NAPI_ASSERT(env, count == num, "OH_Pasteboard_GetMimeTypes is fail.");
122 NAPI_ASSERT(env, res != nullptr, "OH_Pasteboard_GetMimeTypes is fail.");
123 NAPI_ASSERT(env, (strcmp("text/plain", res[0]) == 0 && strcmp("text/html", res[1]) == 0) ||
124 (strcmp("text/plain", res[1]) == 0 && strcmp("text/html", res[0]) == 0), "OH_Pasteboard_GetMimeTypes is fail.");
125 napi_create_int32(env, (strcmp("text/plain", res[0]) == 0 && strcmp("text/html", res[1]) == 0) ||
126 (strcmp("text/plain", res[1]) == 0 && strcmp("text/html", res[0]) == 0), &result);
127 OH_Pasteboard_Destroy(pasteboard);
128 return result;
129 }
130
OH_Pasteboard_GetDataWithNoProgress001(napi_env env,napi_callback_info info)131 static napi_value OH_Pasteboard_GetDataWithNoProgress001(napi_env env, napi_callback_info info)
132 {
133 const char *uri = "file://com.acts.Pastboard.ndkWithPermissionstest/data/storage/el2/base/files/dstFile.txt";
134 OH_UdmfRecord* record = OH_UdmfRecord_Create();
135 OH_UdmfData* unifiedData = OH_UdmfData_Create();
136 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
137 OH_UdsFileUri* fileUri = OH_UdsFileUri_Create();
138 OH_UdsFileUri_SetFileUri(fileUri, uri);
139 OH_UdmfRecord_AddFileUri(record, fileUri);
140 OH_UdmfData_AddRecord(unifiedData, record);
141 int ret = OH_Pasteboard_SetData(pasteboard, unifiedData);
142 NAPI_ASSERT(env, ret == ERR_OK, "OH_Pasteboard_SetData is fail.");
143
144 const char *uri1 = "file://com.acts.Pastboard.ndkWithPermissionstest/data/storage/el2/base/files/haps/";
145 Pasteboard_GetDataParams* params = OH_Pasteboard_GetDataParams_Create();
146 OH_Pasteboard_GetDataParams_SetDestUri(params, uri1, strlen(uri1));
147 OH_Pasteboard_GetDataParams_SetFileConflictOptions(params, PASTEBOARD_SKIP);
148 OH_Pasteboard_GetDataParams_SetProgressIndicator(params, PASTEBOARD_DEFAULT);
149 int status = -1;
150
151 OH_UdmfData* getData = OH_Pasteboard_GetDataWithProgress(pasteboard, params, &status);
152 NAPI_ASSERT(env, status == ERR_PERMISSION_ERROR, "OH_Pasteboard_GetDataWithProgress is fail.");
153 NAPI_ASSERT(env, getData == nullptr, "OH_Pasteboard_GetDataWithProgress is fail.");
154
155 int recode = 0;
156 napi_value result;
157 napi_create_int32(env, recode, &result);
158 OH_Pasteboard_Destroy(pasteboard);
159 return result;
160 }
161
162
163 EXTERN_C_START
Init(napi_env env,napi_value exports)164 static napi_value Init(napi_env env, napi_value exports)
165 {
166 napi_property_descriptor desc[] = {
167 {"OH_Pasteboard_GetMimeTypes0100", nullptr, OH_Pasteboard_GetMimeTypes0100,
168 nullptr, nullptr, nullptr, napi_default, nullptr},
169 {"OH_Pasteboard_GetMimeTypes0200", nullptr, OH_Pasteboard_GetMimeTypes0200,
170 nullptr, nullptr, nullptr, napi_default, nullptr},
171 {"OH_Pasteboard_GetMimeTypes0300", nullptr, OH_Pasteboard_GetMimeTypes0300,
172 nullptr, nullptr, nullptr, napi_default, nullptr},
173 {"OH_Pasteboard_GetDataWithNoProgress001", nullptr, OH_Pasteboard_GetDataWithNoProgress001,
174 nullptr, nullptr, nullptr, napi_default, nullptr}
175 };
176 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
177 return exports;
178 }
179 EXTERN_C_END
180
181 static napi_module demoModule = {
182 .nm_version = 1,
183 .nm_flags = 0,
184 .nm_filename = nullptr,
185 .nm_register_func = Init,
186 .nm_modname = "entry",
187 .nm_priv = ((void*)0),
188 .reserved = { 0 },
189 };
190
RegisterEntryModule(void)191 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
192 {
193 napi_module_register(&demoModule);
194 }
195