/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from "deccjsunit/index" import featureAbility from '@ohos.ability.featureAbility' import ohosDataAbility from '@ohos.data.dataAbility' describe('ActsDataAbilityJsTest', function () { let dataAbilityUri = ("dataability:///com.example.myapplication.DataAbility"); let DAHelper; let gSetTimeout = 500; var valueBucket = { "name": "ActsDataAbilityHelperTest", "age": 24, "salary": 2024.20, } beforeAll(async (done) => { console.debug('= ACTS_AbeforeAll 1425 ====: ' + DAHelper + " ,JSON. " + JSON.stringify(DAHelper)); if(DAHelper == null){ console.debug('ACTS_AbeforeAll DAHelper ====>: DAHelper == null'); } } catch (err) { console.error('=ACTS_AbeforeAll acquireDataAbilityHelper catch(err)====>:' + err); } console.debug('= ACTS_AbeforeAll ==== { console.debug('= ACTS_AbeforeEach ==== { console.debug('= ACTS_AafterEach ==== { console.debug('= ACTS_AafterAll ====: ' + DAHelper) var currentAlertTimeout; try { function onAsyncCallbackInsert0800(err) { clearTimeout(currentAlertTimeout); expect(err.code).assertEqual(0); console.log('ACTS_AInsert_0800==== { console.debug("=ACTS_AInsert_0800 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(data).assertEqual(1); var mSetTimeout = 1000 currentAlertTimeout = setTimeout(mySetTimeout, mSetTimeout); function mySetTimeout() { console.debug('ACTS_AInsert_0800====< mySetTimeout'); done(); } } ); } catch (err) { console.error('=ACTS_AInsert_0800 catch(err)====>:' + err); console.log('ACTS_AInsert_0800====: ' + DAHelper) var valueBucketArray = new Array({ "name": "ActsDataAbilityHelperTest", "age": 24, "salary": 2024.20, }, { "name": "ActsDataAbilityHelperTest", "age": 24, "salary": 2024.20, }, { "name": "ActsDataAbilityHelperTest", "age": 24, "salary": 2024.20, }) try { await DAHelper.batchInsert( dataAbilityUri, valueBucketArray, (err, data) => { console.debug("=ACTS_ABatchInsert_0600 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(data).assertEqual(3); console.log('ACTS_ABatchInsert_0600====:' + err); console.log('ACTS_ABatchInsert_0600==== { console.debug("=ACTS_AQuery_0400 query err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;") + " , " + data); expect(typeof (data)).assertEqual("object"); console.log('ACTS_AQuery_0400====:' + err); console.log('ACTS_AQuery_0400====: ' + DAHelper) try { let predicates = new ohosDataAbility.DataAbilityPredicates(); predicates.equalTo('contact_id', 1); predicates.limitAs(10); predicates.orderByAsc("order_by_class"); predicates.offsetAs(1); console.debug("=ACTS_AUpdate_0400 predicates====>" + ("json predicates 【") + JSON.stringify(predicates) + (" 】") + " , " + predicates); await DAHelper.update( dataAbilityUri, valueBucket, predicates, (err, data) => { console.debug("=ACTS_AUpdate_0400 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(data).assertEqual(1); console.log('ACTS_AUpdate_0400====:' + err); console.log('ACTS_AUpdate_0400====: ' + DAHelper) try { let predicates = new ohosDataAbility.DataAbilityPredicates(); predicates.equalTo('contact_id', 1); predicates.limitAs(10); predicates.orderByAsc("order_by_class"); predicates.offsetAs(1); console.debug("=ACTS_ADelete_0400 predicates====>" + ("json predicates 【") + JSON.stringify(predicates) + (" 】") + " , " + predicates); await DAHelper.delete( dataAbilityUri, predicates, (err, data) => { console.debug("=ACTS_ADelete_0400 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(data).assertEqual(0); console.log('ACTS_ADelete_0400====:' + err); console.log('ACTS_ADelete_0400====: ' + DAHelper) let mimeTypeFilter = '*/*' try { await DAHelper.getFileTypes( dataAbilityUri, mimeTypeFilter, (err, data) => { console.debug("=ACTS_AGetFileTypes_0200 getFileTypes err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); console.log('=ACTS_AGetFileTypes_0200 data.length ====>: ' + data.length); for (var i = 0; i < data.length; i++) { expect(typeof (data[i])).assertEqual("string"); console.log('=ACTS_AGetFileTypes_0200 for data ====>: ' + err.code + " data[" + i + "]: " + data[i]); if (i == 0) { expect(data[i]).assertEqual("hap"); } else if (i == 1) { expect(data[i]).assertEqual("jpg"); } else if (i == 2) { expect(data[i]).assertEqual("image/png"); } } console.log('ACTS_AGetFileTypes_0200====:' + err); console.log('ACTS_AGetFileTypes_0200====: ' + DAHelper) try { await DAHelper.getType( dataAbilityUri, (err, data) => { console.debug("=ACTS_AGetType_0200 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(data).assertEqual("hap"); console.log('ACTS_AGetType_0200====:' + err); console.log('ACTS_AGetType_0200====: ' + DAHelper) var mode = "r"; try { DAHelper.openFile( dataAbilityUri, mode, (err, data) => { console.debug("=ACTS_AOpenFile_0700 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(typeof (data)).assertEqual("number"); console.log('DataAbilityHelper ACTS_AOpenFile_0700 OpenFile asyncCallback errCode ====>: ' + err.code + " data: " + data); ret = true done(); }, ); } catch (err) { console.error('=ACTS_AOpenFile_0700 getType AsyncCallback catch(err)====>:' + err); ret = false done(); } setTimeout(function () { console.log('setTimeout function====<'); }, gSetTimeout); console.log('ACTS_AOpenFile_0700====: ' + DAHelper) try { DAHelper.normalizeUri( dataAbilityUri, (err, data) => { console.debug("=ACTS_ANormalizeUri_0200 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(data).assertEqual(dataAbilityUri); console.log('ACTS_ANormalizeUri_0200====:' + err); console.log('ACTS_ANormalizeUri_0200====: ' + DAHelper) try { DAHelper.denormalizeUri( dataAbilityUri, (err, data) => { console.debug("=ACTS_ADenormalizeUri_0200 err,data=======>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;")); expect(data).assertEqual(dataAbilityUri); console.log('ACTS_ADenormalizeUri_0200====:' + err); console.log('ACTS_ADenormalizeUri_0200==== { console.debug("=ACTS_AOnOff_0100 err=======>" + ("err【") + JSON.stringify(err) + (" 】") + " , " + err); expect(err.code).assertEqual(0); } ); } } catch (err) { console.error('=ACTS_AOnOff_0100 catch(err)====>:' + err); console.log('ACTS_AOnOff_0100===="); expect(false).assertTrue(); clearTimeout(currentAlertTimeout); console.log('ACTS_AOnOff_0200==== { if (err.code != 0) { console.debug("=ACTS_AOnOff_0200 err=======>" + ("err【") + JSON.stringify(err) + (" 】") + " , " + err); expect(false).assertTrue(); console.log('ACTS_AOnOff_0200==== { expect(err.code).assertEqual(0); console.log('ACTS_AOnOff_0200====:' + err); console.log('ACTS_AOnOff_0200==== { console.debug("=ACTS_AExecuteBatch_Insert_0400 executeBatch err,data====>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;") + data.length); expect(err.code).assertEqual(0); expect(data.length).assertEqual(0); console.debug('ACTS_AExecuteBatch_Insert_0400====:' + ("json err 【") + JSON.stringify(err) + (" 】 ,") + err); console.debug('ACTS_AExecuteBatch_Insert_0400==== { console.debug("=ACTS_AExecuteBatch_Update_0400 executeBatch err,data====>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(DataAbilityResult) + (" 】;") + DataAbilityResult.length); for (var i = 0; i < DataAbilityResult.length; i++) { console.debug('=ACTS_AExecuteBatch_Update_0400 for data[' + i + '].uri ====>: ' + DataAbilityResult[i].uri) expect(DataAbilityResult[i].uri).assertEqual(dataAbilityUri); console.debug('=ACTS_AExecuteBatch_Update_0400 for data[' + i + '].count ====>: ' + DataAbilityResult[i].count) expect(DataAbilityResult[i].count).assertEqual(1); } console.debug('ACTS_AExecuteBatch_Update_0400====:' + ("json err 【") + JSON.stringify(err) + (" 】 ,") + err); console.debug('ACTS_AExecuteBatch_Update_0400==== { console.debug("=ACTS_AExecuteBatch_Assert_0400 executeBatch err,data====>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(data) + (" 】;") + data.length); expect(data.length).assertEqual(0); console.debug('ACTS_AExecuteBatch_Assert_0400====:' + ("json err 【") + JSON.stringify(err) + (" 】 ,") + err); console.debug('ACTS_AExecuteBatch_Assert_0400==== { console.debug("=ACTS_AExecuteBatch_Delete_0400 executeBatch err,data====>" + ("json err【") + JSON.stringify(err) + (" 】json data【") + JSON.stringify(DataAbilityResult) + (" 】;") + DataAbilityResult.length); for (var i = 0; i < DataAbilityResult.length; i++) { console.debug('=ACTS_AExecuteBatch_Delete_0400 for data[' + i + '].uri ====>: ' + DataAbilityResult[i].uri) expect(DataAbilityResult[i].uri).assertEqual(dataAbilityUri); console.debug('=ACTS_AExecuteBatch_Delete_0400 for data[' + i + '].count ====>: ' + DataAbilityResult[i].count) expect(DataAbilityResult[i].count).assertEqual(1); } console.debug('ACTS_AExecuteBatch_Delete_0400====:' + ("json err 【") + JSON.stringify(err) + (" 】 ,") + err); console.debug('ACTS_AExecuteBatch_Delete_0400====: ' + rDAHelper + " ,JSON. " + JSON.stringify(rDAHelper)); expect(typeof (rDAHelper)).assertEqual("object"); rDAHelper.release().then((data) => { console.debug("=ACTS_ARelease_0100 then data====>" + ("json data 【") + JSON.stringify(data) + (" 】") + " , " + data); expect(data).assertEqual(true); console.log('ACTS_ARelease_0100==== { console.debug("=ACTS_ARelease_0100 catch err ====>" + ("json err 【") + JSON.stringify(err) + (" 】 ")); expect(false).assertTrue(); console.log('ACTS_ARelease_0100====:' + err); console.log('ACTS_ARelease_0100====: ' + rDAHelper + " ,JSON. " + JSON.stringify(rDAHelper)); expect(typeof (rDAHelper)).assertEqual("object"); pacMap.group_name = 'test2'; rDAHelper.call(dataAbilityUri, 'insert', '', pacMap).then((data) => { console.info('ACTS_Call_0100 succeeded data: ' + JSON.stringify(data)); }).catch((error) => { console.error('ACTS_Call_0100 error: ' + JSON.stringify(error)); }); done(); } catch (err) { console.error('ACTS_Call_0100 catch(err)====>:' + err); console.log('ACTS_Call_0100====: ' + rDAHelper + " ,JSON. " + JSON.stringify(rDAHelper)); rDAHelper.call(dataAbilityUri, 'query', arg, pacMap, (err, data) => { if (err) { console.error('ACTS_Call_0200 error: ' + JSON.stringify(err)); } else { console.info('ACTS_Call_0200 succeeded: ' + JSON.stringify(data)); } }); } catch (err) { console.error('ACTS_Call_0200 catch(err)====>:' + err); console.log('ACTS_Call_0200====: ' + rDAHelper + " ,JSON. " + JSON.stringify(rDAHelper)); rDAHelper.call(dataAbilityUri, 'update', arg, pacMap, (err, data) => { if (err) { console.error('ACTS_Call_0300 error: ' + JSON.stringify(err)); } else { console.info('ACTS_Call_0300 succeeded: ' + JSON.stringify(data)); } }); } catch (err) { console.error('ACTS_Call_0300 catch(err)====>:' + err); console.log('ACTS_Call_0300====: ' + rDAHelper + " ,JSON. " + JSON.stringify(rDAHelper)); rDAHelper.call(dataAbilityUri, 'delete', arg, pacMap, (err, data) => { if (err) { console.error('ACTS_Call_0400 error: ' + JSON.stringify(err)); } else { console.info('ACTS_Call_0400 succeeded: ' + JSON.stringify(data)); } }); done(); } catch (err) { console.error('ACTS_Call_0400 catch(err)====>:' + err); console.log('ACTS_Call_0400====