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*/ 15import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 16import storage from '@ohos.data.storage' 17 18const PATH = "/data/storage/el2/database/test_storage"; 19const KEY_TEST_INT_ELEMENT = 'key_test_int'; 20const KEY_TEST_LONG_ELEMENT = 'key_test_long'; 21const KEY_TEST_FLOAT_ELEMENT = 'key_test_float'; 22const KEY_TEST_BOOLEAN_ELEMENT = 'key_test_boolean'; 23const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 24var mPref; 25 26describe('storageTest', function () { 27 beforeAll(function () { 28 console.info('beforeAll') 29 mPref = storage.getStorageSync(PATH); 30 }) 31 32 afterAll(function () { 33 console.info('afterAll') 34 storage.deleteStorageSync(PATH); 35 }) 36 37 /** 38 * @tc.name clear promise interface test 39 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Promise_0010 40 * @tc.desc clear promise interface test 41 */ 42 it('testClear0011', 0, async function (done) { 43 mPref.putSync(KEY_TEST_STRING_ELEMENT, "test"); 44 mPref.flushSync(); 45 const promise = mPref.clear(); 46 promise.then((ret) => { 47 expect("defaultvalue").assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue")); 48 }).catch((err) => { 49 expect(null).assertFail(); 50 }); 51 await promise; 52 done(); 53 }) 54 55 /** 56 * @tc.name has string interface test 57 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0020 58 * @tc.desc has string interface test 59 */ 60 it('testHasKey0031', 0, async function (done) { 61 mPref.putSync(KEY_TEST_STRING_ELEMENT, "test"); 62 const promise = mPref.has(KEY_TEST_STRING_ELEMENT); 63 promise.then((ret) => { 64 expect(true).assertEqual(ret); 65 }).catch((err) => { 66 expect(null).assertFail(); 67 }); 68 await promise; 69 done(); 70 }) 71 72 /** 73 * @tc.name has int interface test 74 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0140 75 * @tc.desc has int interface test 76 */ 77 it('testHasKey0032', 0, async function (done) { 78 mPref.putSync(KEY_TEST_INT_ELEMENT, 1); 79 const promise = mPref.has(KEY_TEST_INT_ELEMENT); 80 promise.then((ret) => { 81 expect(true).assertEqual(ret); 82 }).catch((err) => { 83 expect(null).assertFail(); 84 }); 85 await promise; 86 done(); 87 }) 88 89 /** 90 * @tc.name has float interface test 91 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0150 92 * @tc.desc has float interface test 93 */ 94 it('testHasKey0033', 0, async function (done) { 95 mPref.putSync(KEY_TEST_FLOAT_ELEMENT, 2.0); 96 const promise = mPref.has(KEY_TEST_FLOAT_ELEMENT); 97 promise.then((ret) => { 98 expect(true).assertEqual(ret); 99 }).catch((err) => { 100 expect(null).assertFail(); 101 }); 102 await promise; 103 done(); 104 }) 105 106 /** 107 * @tc.name has boolean interface test 108 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0160 109 * @tc.desc has boolean interface test 110 */ 111 it('testHasKey0034', 0, async function (done) { 112 mPref.putSync(KEY_TEST_BOOLEAN_ELEMENT, false); 113 const promise = mPref.has(KEY_TEST_BOOLEAN_ELEMENT); 114 promise.then((ret) => { 115 expect(true).assertEqual(ret); 116 }).catch((err) => { 117 expect(null).assertFail(); 118 }); 119 await promise; 120 done(); 121 }) 122 123 /** 124 * @tc.name has long interface test 125 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0170 126 * @tc.desc has long interface test 127 */ 128 it('testHasKey0035', 0, async function (done) { 129 mPref.putSync(KEY_TEST_LONG_ELEMENT, 0); 130 const promise = mPref.has(KEY_TEST_LONG_ELEMENT); 131 promise.then((ret) => { 132 expect(true).assertEqual(ret); 133 }).catch((err) => { 134 expect(null).assertFail(); 135 }); 136 await promise; 137 done(); 138 }) 139 140 /** 141 * @tc.name get string promise interface test 142 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0030 143 * @tc.desc get string promise interface test 144 */ 145 it('testGetDefValue0061', 0, async function (done) { 146 mPref.clearSync(); 147 const promise = mPref.get(KEY_TEST_STRING_ELEMENT, "defaultValue"); 148 promise.then((ret) => { 149 expect('defaultValue').assertEqual(ret); 150 }).catch((err) => { 151 expect(null).assertFail(); 152 }); 153 await promise; 154 done(); 155 }) 156 157 /** 158 * @tc.name get float promise interface test 159 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0040 160 * @tc.desc get float promise interface test 161 */ 162 it('testGetFloat0071', 0, async function (done) { 163 mPref.clearSync(); 164 mPref.putSync(KEY_TEST_FLOAT_ELEMENT, 3.0); 165 const promise = mPref.get(KEY_TEST_FLOAT_ELEMENT, 0.0); 166 promise.then((ret) => { 167 expect(3.0).assertEqual(ret); 168 }).catch((err) => { 169 expect(null).assertFail(); 170 }); 171 await promise; 172 done(); 173 }) 174 175 /** 176 * @tc.name get int promise interface test 177 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0050 178 * @tc.desc get int promise interface test 179 */ 180 it('testGetInt0081', 0, async function (done) { 181 mPref.clearSync(); 182 mPref.putSync(KEY_TEST_INT_ELEMENT, 3); 183 const promise = mPref.get(KEY_TEST_INT_ELEMENT, 0.0); 184 promise.then((ret) => { 185 expect(3).assertEqual(ret); 186 }).catch((err) => { 187 expect(null).assertFail(); 188 }); 189 await promise; 190 done(); 191 }) 192 193 /** 194 * @tc.name get long promise interface test 195 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0060 196 * @tc.desc get long promise interface test 197 */ 198 it('testGetLong0091', 0, async function (done) { 199 mPref.clearSync(); 200 mPref.putSync(KEY_TEST_LONG_ELEMENT, 3); 201 const promise = mPref.get(KEY_TEST_LONG_ELEMENT, 0); 202 promise.then((ret) => { 203 expect(3).assertEqual(ret); 204 }).catch((err) => { 205 expect(null).assertFail(); 206 }); 207 await promise; 208 done(); 209 }) 210 211 /** 212 * @tc.name get String promise interface test 213 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0070 214 * @tc.desc get String promise interface test 215 */ 216 it('testGetString101', 0, async function (done) { 217 mPref.clearSync(); 218 mPref.putSync(KEY_TEST_STRING_ELEMENT, "test"); 219 mPref.flushSync(); 220 const promise = mPref.get(KEY_TEST_STRING_ELEMENT, "defaultvalue"); 221 promise.then((ret) => { 222 expect('test').assertEqual(ret); 223 }).catch((err) => { 224 expect(null).assertFail(); 225 }); 226 await promise; 227 done(); 228 }) 229 230 /** 231 * @tc.name put boolean promise interface test 232 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0090 233 * @tc.desc put boolean promise interface test 234 */ 235 it('testPutBoolean0121', 0, async function (done) { 236 mPref.clearSync(); 237 const promise = mPref.put(KEY_TEST_BOOLEAN_ELEMENT, true); 238 promise.then((ret) => { 239 expect(true).assertEqual(mPref.getSync(KEY_TEST_BOOLEAN_ELEMENT, false)); 240 mPref.flushSync(); 241 expect(true).assertEqual(mPref.getSync(KEY_TEST_BOOLEAN_ELEMENT, false)); 242 }).catch((err) => { 243 expect(null).assertFail(); 244 }); 245 await promise; 246 done(); 247 }) 248 249 /** 250 * @tc.name put float promise interface test 251 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0100 252 * @tc.desc put float promise interface test 253 */ 254 it('testPutFloat0131', 0, async function (done) { 255 mPref.clearSync(); 256 const promise = mPref.put(KEY_TEST_FLOAT_ELEMENT, 4.0); 257 promise.then((ret) => { 258 expect(4.0).assertEqual(mPref.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0)); 259 mPref.flushSync(); 260 expect(4.0).assertEqual(mPref.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0)); 261 }).catch((err) => { 262 expect(null).assertFail(); 263 }); 264 await promise; 265 done(); 266 }) 267 268 /** 269 * @tc.name put int promise interface test 270 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0110 271 * @tc.desc put int promise interface test 272 */ 273 it('testPutInt0141', 0, async function (done) { 274 mPref.clearSync(); 275 const promise = mPref.put(KEY_TEST_INT_ELEMENT, 4); 276 promise.then((ret) => { 277 expect(4).assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, 0)); 278 mPref.flushSync(); 279 expect(4).assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, 0)); 280 }).catch((err) => { 281 expect(null).assertFail(); 282 }); 283 await promise; 284 done(); 285 }) 286 287 /** 288 * @tc.name put long promise interface test 289 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0120 290 * @tc.desc put long promise interface test 291 */ 292 it('testPutLong0151', 0, async function (done) { 293 mPref.clearSync(); 294 mPref.putSync(KEY_TEST_LONG_ELEMENT, 4); 295 const promise = mPref.put(KEY_TEST_LONG_ELEMENT, 4); 296 promise.then((ret) => { 297 expect(4).assertEqual(mPref.getSync(KEY_TEST_LONG_ELEMENT, 0)); 298 mPref.flushSync(); 299 expect(4).assertEqual(mPref.getSync(KEY_TEST_LONG_ELEMENT, 0)); 300 }).catch((err) => { 301 expect(null).assertFail(); 302 }); 303 await promise; 304 done(); 305 }) 306 307 /** 308 * @tc.name put String promise interface test 309 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Storage_0130 310 * @tc.desc put String promise interface test 311 */ 312 it('testPutString0161', 0, async function (done) { 313 mPref.clearSync(); 314 mPref.putSync(KEY_TEST_STRING_ELEMENT, "abc"); 315 const promise = mPref.put(KEY_TEST_STRING_ELEMENT, ''); 316 promise.then((ret) => { 317 expect('').assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue")); 318 mPref.flushSync(); 319 expect('').assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue")); 320 }).catch((err) => { 321 expect(null).assertFail(); 322 }); 323 await promise; 324 done(); 325 }) 326})