1/* 2 * Copyright (c) 2023 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 16import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'; 17import UDC from '@ohos.data.unifiedDataChannel'; 18import UTD from '@ohos.data.uniformTypeDescriptor'; 19 20const TEXT_CONTEXT_01 = 'TextContent01'; 21const TEXT_CONTEXT_02 = 'TextContent02'; 22const ERROR_PARAMETER = '401'; 23 24describe('UdmfPromiseJSTest', function () { 25 26 let gPlainText1 = new UDC.PlainText(); 27 gPlainText1.textContent = TEXT_CONTEXT_01; 28 let gPlainText2 = new UDC.PlainText(); 29 gPlainText2.textContent = TEXT_CONTEXT_02; 30 31 const optionsValid = { intention: 'DataHub', }; 32 const optionsInValidIntention = { intention: 'Drag', }; 33 const optionsInValidKey = { key: 'udmf://drag/com.test.demo/123456789', }; 34 const optionsInValidAll = { intention: 'DataHub', key: 'udmf://drag/com.test.demo/123456789' }; 35 const unifiedData01 = new UDC.UnifiedData(gPlainText1); 36 const unifiedData02 = new UDC.UnifiedData(gPlainText2); 37 const unifiedDataInvalid = new UDC.UnifiedData(); 38 39 /** 40 * @tc.name UdmfInsertPromiseInvalidOptionsTest 41 * @tc.desc Test Js Api insertData with invalid options 42 * @tc.type: FUNC 43 * @tc.require: issueNumber 44 */ 45 it('UdmfInsertPromiseInvalidOptionsTest', 0, async function (done) { 46 const TAG = 'UdmfInsertPromiseInvalidOptionsTest:'; 47 console.info(TAG, 'start'); 48 try { 49 UDC.insertData(optionsInValidIntention, unifiedData01).then(() => { 50 console.info(TAG, 'Unreachable code!'); 51 expect(null).assertFail(); 52 done(); 53 }).catch(() => { 54 console.info(TAG, 'Unreachable code!'); 55 expect(null).assertFail(); 56 done(); 57 }); 58 } catch (e) { 59 console.error(TAG, `get e. code is ${e.code},message is ${e.message} `); 60 expect(e.code === ERROR_PARAMETER).assertTrue(); 61 done(); 62 } 63 console.info(TAG, 'end'); 64 }); 65 66 /** 67 * @tc.name UdmfInsertPromiseInvalidDataTest 68 * @tc.desc Test Js Api insertData with invalid UnifiedData 69 * @tc.type: FUNC 70 * @tc.require: issueNumber 71 */ 72 it('UdmfInsertPromiseInvalidDataTest', 0, async function (done) { 73 const TAG = 'UdmfInsertPromiseInvalidDataTest:'; 74 console.info(TAG, 'start'); 75 try { 76 UDC.insertData(optionsValid, unifiedDataInvalid).then(() => { 77 console.info(TAG, 'Unreachable code!'); 78 expect(null).assertFail(); 79 done(); 80 }).catch((err) => { 81 console.error(TAG, `get fail. code is ${err.code},message is ${err.message} `); 82 expect(err.code === ERROR_PARAMETER).assertTrue(); 83 done(); 84 }); 85 } catch (e) { 86 console.error(TAG, 'Unreachable code!'); 87 expect(null).assertFail(); 88 done(); 89 } 90 console.info(TAG, 'end'); 91 }); 92 93 /** 94 * @tc.name UdmfInsertPromiseSucTest 95 * @tc.desc Test Js Api insertData successful 96 * @tc.type: FUNC 97 * @tc.require: issueNumber 98 */ 99 it('UdmfInsertPromiseSucTest', 0, async function (done) { 100 const TAG = 'UdmfInsertPromiseSucTest:'; 101 console.info(TAG, 'start'); 102 try { 103 UDC.insertData(optionsValid, unifiedData01).then((data) => { 104 console.info(TAG, `insert success. The key: ${data}`); 105 let options = { key: data }; 106 console.info(TAG, `query start. The options: ${JSON.stringify(options)}`); 107 UDC.queryData(options).then((data) => { 108 console.info(TAG, 'query success.'); 109 console.info(TAG, `data.length = ${data.length}.`); 110 expect(data.length).assertEqual(1); 111 let records = data[0].getRecords(); 112 console.info(TAG, `records.length = ${records.length}.`); 113 console.info(TAG, `records[0].getType() = ${records[0].getType()}.`); 114 console.info(TAG, `records[0].textContent = ${records[0].textContent}.`); 115 expect(records.length).assertEqual(1); 116 expect(records[0].getType()).assertEqual(UTD.UniformDataType.PLAIN_TEXT); 117 expect(records[0].textContent).assertEqual(TEXT_CONTEXT_01); 118 done(); 119 }).catch(() => { 120 console.info(TAG, 'Unreachable code!'); 121 expect(null).assertFail(); 122 done(); 123 }); 124 }).catch(() => { 125 console.info(TAG, 'Unreachable code!'); 126 expect(null).assertFail(); 127 done(); 128 }); 129 } catch (e) { 130 console.error(TAG, 'Unreachable code!'); 131 expect(null).assertFail(); 132 done(); 133 } 134 console.info(TAG, 'end'); 135 }); 136 137 /** 138 * @tc.name UdmfUpdatePromiseInvalidOptionsTest 139 * @tc.desc Test Js Api updateData with invalid options 140 * @tc.type: FUNC 141 * @tc.require: issueNumber 142 */ 143 it('UdmfUpdatePromiseInvalidOptionsTest', 0, async function (done) { 144 const TAG = 'UdmfUpdatePromiseInvalidOptionsTest:'; 145 console.info(TAG, 'start'); 146 try { 147 UDC.updateData(optionsInValidKey, unifiedData01).then(() => { 148 console.info(TAG, 'Unreachable code!'); 149 expect(null).assertFail(); 150 done(); 151 }).catch(() => { 152 console.info(TAG, 'Unreachable code!'); 153 expect(null).assertFail(); 154 done(); 155 }); 156 } catch (e) { 157 console.error(TAG, `get e. code is ${e.code},message is ${e.message} `); 158 expect(e.code === ERROR_PARAMETER).assertTrue(); 159 done(); 160 } 161 console.info(TAG, 'end'); 162 }); 163 164 /** 165 * @tc.name UdmfUpdatePromiseInvalidDataTest 166 * @tc.desc Test Js Api updateData with invalid UnifiedData 167 * @tc.type: FUNC 168 * @tc.require: issueNumber 169 */ 170 it('UdmfUpdatePromiseInvalidDataTest', 0, async function (done) { 171 const TAG = 'UdmfUpdatePromiseInvalidDataTest:'; 172 console.info(TAG, 'start'); 173 try { 174 UDC.insertData(optionsValid, unifiedData01).then((data) => { 175 console.info(TAG, `insert success. The key: ${data}`); 176 let options = { key: data }; 177 console.info(TAG, `query start. The options: ${JSON.stringify(options)}`); 178 UDC.updateData(options, unifiedDataInvalid).then(() => { 179 console.info(TAG, 'Unreachable code!'); 180 expect(null).assertFail(); 181 done(); 182 }).catch((err) => { 183 console.error(TAG, `get fail. code is ${err.code},message is ${err.message} `); 184 expect(err.code === ERROR_PARAMETER).assertTrue(); 185 done(); 186 }); 187 }).catch(() => { 188 console.info(TAG, 'Unreachable code!'); 189 expect(null).assertFail(); 190 done(); 191 }); 192 } catch (e) { 193 console.error(TAG, 'Unreachable code!'); 194 expect(null).assertFail(); 195 done(); 196 } 197 console.info(TAG, 'end'); 198 }); 199 200 /** 201 * @tc.name UdmfUpdatePromiseSucTest 202 * @tc.desc Test Js Api updateData successful 203 * @tc.type: FUNC 204 * @tc.require: issueNumber 205 */ 206 it('UdmfUpdatePromiseSucTest', 0, async function (done) { 207 const TAG = 'UdmfUpdatePromiseSucTest:'; 208 console.info(TAG, 'start'); 209 try { 210 UDC.insertData(optionsValid, unifiedData01).then((data) => { 211 console.info(TAG, `insert success. The key: ${data}`); 212 let options = { key: data }; 213 console.info(TAG, `query start. The options: ${JSON.stringify(options)}`); 214 UDC.updateData(options, unifiedData02).then((data) => { 215 console.info(TAG, 'update success.'); 216 UDC.queryData(options).then((data) => { 217 console.info(TAG, 'query success.'); 218 expect(data.length).assertEqual(1); 219 let records = data[0].getRecords(); 220 expect(records.length).assertEqual(1); 221 expect(records[0].getType()).assertEqual(UTD.UniformDataType.PLAIN_TEXT); 222 expect(records[0].textContent).assertEqual(TEXT_CONTEXT_02); 223 done(); 224 }).catch(() => { 225 console.info(TAG, 'Unreachable code!'); 226 expect(null).assertFail(); 227 done(); 228 }); 229 }).catch(() => { 230 console.info(TAG, 'Unreachable code!'); 231 expect(null).assertFail(); 232 done(); 233 }); 234 }).catch(() => { 235 console.info(TAG, 'Unreachable code!'); 236 expect(null).assertFail(); 237 done(); 238 }); 239 } catch (e) { 240 console.error(TAG, 'Unreachable code!'); 241 expect(null).assertFail(); 242 done(); 243 } 244 console.info(TAG, 'end'); 245 }); 246 247 248 /** 249 * @tc.name UdmfQueryPromiseInvalidOptionsTest 250 * @tc.desc Test Js Api queryData with invalid options 251 * @tc.type: FUNC 252 * @tc.require: issueNumber 253 */ 254 it('UdmfQueryPromiseInvalidOptionsTest', 0, async function (done) { 255 const TAG = 'UdmfQueryPromiseInvalidOptionsTest:'; 256 console.info(TAG, 'start'); 257 try { 258 UDC.queryData(optionsInValidAll).then(() => { 259 console.error(TAG, 'Unreachable code!'); 260 expect(null).assertFail(); 261 done(); 262 }).catch(() => { 263 console.error(TAG, 'Unreachable code!'); 264 expect(null).assertFail(); 265 done(); 266 }); 267 } catch (e) { 268 console.error(TAG, `get e. code is ${e.code},message is ${e.message} `); 269 expect(e.code === ERROR_PARAMETER).assertTrue(); 270 done(); 271 } 272 console.info(TAG, 'end'); 273 }); 274 275 /** 276 * @tc.name UdmfQueryPromiseSucTest 277 * @tc.desc Test Js Api queryData successful 278 * @tc.type: FUNC 279 * @tc.require: issueNumber 280 */ 281 it('UdmfQueryPromiseSucTest', 0, async function (done) { 282 const TAG = 'UdmfQueryPromiseSucTest:'; 283 console.info(TAG, 'start'); 284 try { 285 UDC.deleteData(optionsValid).then(() => { 286 console.info(TAG, 'delete success.'); 287 UDC.queryData(optionsValid).then(() => { 288 console.error(TAG, 'Unreachable code!'); 289 expect(null).assertFail(); 290 done(); 291 }).catch((err) => { 292 console.info(TAG, 'query has no data.'); 293 UDC.insertData(optionsValid, unifiedData01).then((data) => { 294 console.info(TAG, `insert success. The key: ${data}`); 295 UDC.insertData(optionsValid, unifiedData01).then((data) => { 296 console.info(TAG, `insert success. The key: ${data}`); 297 UDC.queryData(optionsValid).then((data) => { 298 console.info(TAG, 'query success.'); 299 expect(data.length).assertEqual(2); 300 done(); 301 }).catch(() => { 302 console.error(TAG, 'Unreachable code!'); 303 expect(null).assertFail(); 304 done(); 305 }); 306 }).catch(() => { 307 console.error(TAG, 'Unreachable code!'); 308 expect(null).assertFail(); 309 done(); 310 }); 311 }).catch(() => { 312 console.error(TAG, 'Unreachable code!'); 313 expect(null).assertFail(); 314 done(); 315 }); 316 }); 317 }).catch(() => { 318 console.error(TAG, 'Unreachable code!'); 319 expect(null).assertFail(); 320 done(); 321 }); 322 } catch (e) { 323 console.error(TAG, 'Unreachable code!'); 324 expect(null).assertFail(); 325 done(); 326 } 327 console.info(TAG, 'end'); 328 }); 329 330 331 /** 332 * @tc.name UdmfDeletePromiseInvalidOptionsTest 333 * @tc.desc Test Js Api deleteData with invalid options 334 * @tc.type: FUNC 335 * @tc.require: issueNumber 336 */ 337 it('UdmfDeletePromiseInvalidOptionsTest', 0, async function (done) { 338 const TAG = 'UdmfDeletePromiseInvalidOptionsTest:'; 339 console.info(TAG, 'start'); 340 try { 341 UDC.deleteData(optionsInValidAll).then(() => { 342 console.error(TAG, 'Unreachable code!'); 343 expect(null).assertFail(); 344 done(); 345 }).catch(() => { 346 console.error(TAG, 'Unreachable code!'); 347 expect(null).assertFail(); 348 done(); 349 }); 350 } catch (e) { 351 console.error(TAG, `get e. code is ${e.code},message is ${e.message} `); 352 expect(e.code === ERROR_PARAMETER).assertTrue(); 353 done(); 354 } 355 console.info(TAG, 'end'); 356 }); 357 358 /** 359 * @tc.name UdmfDeletePromiseSucTest 360 * @tc.desc Test Js Api deleteData successful 361 * @tc.type: FUNC 362 * @tc.require: issueNumber 363 */ 364 it('UdmfDeletePromiseSucTest', 0, async function (done) { 365 const TAG = 'UdmfDeletePromiseSucTest:'; 366 console.info(TAG, 'start'); 367 try { 368 UDC.insertData(optionsValid, unifiedData01).then((data) => { 369 console.info(TAG, `insert success. The key: ${data}`); 370 let options = { key: data }; 371 console.info(TAG, `query start. The options: ${JSON.stringify(options)}`); 372 UDC.queryData(options).then((data) => { 373 console.info(TAG, 'query success.'); 374 expect(data.length).assertEqual(1); 375 UDC.deleteData(options).then((data) => { 376 console.info(TAG, 'delete success.'); 377 expect(data.length).assertEqual(1); 378 let records = data[0].getRecords(); 379 expect(records.length).assertEqual(1); 380 expect(records[0].getType()).assertEqual(UTD.UniformDataType.PLAIN_TEXT); 381 expect(records[0].textContent).assertEqual(TEXT_CONTEXT_01); 382 UDC.queryData(options).then(() => { 383 console.error(TAG, 'Unreachable code!'); 384 expect(null).assertFail(); 385 done(); 386 }).catch(() => { 387 console.info(TAG, 'query has no data.'); 388 done(); 389 }); 390 }).catch(() => { 391 console.error(TAG, 'Unreachable code!'); 392 expect(null).assertFail(); 393 done(); 394 }); 395 }).catch(() => { 396 console.error(TAG, 'Unreachable code!'); 397 expect(null).assertFail(); 398 done(); 399 }); 400 }).catch(() => { 401 console.error(TAG, 'Unreachable code!'); 402 expect(null).assertFail(); 403 done(); 404 }); 405 } catch (e) { 406 console.error(TAG, 'Unreachable code!'); 407 expect(null).assertFail(); 408 done(); 409 } 410 console.info(TAG, 'end'); 411 }); 412});