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'; 24 25var mPref; 26 27describe('storageTest', function () { 28 beforeAll(function() { 29 console.info('beforeAll') 30 mPref = storage.getStorageSync(PATH); 31 }) 32 33 afterAll(function () { 34 console.info('afterAll') 35 storage.deleteStorageSync(PATH); 36 }) 37 38 it('testClear001', 0, function () { 39 mPref.putSync(KEY_TEST_STRING_ELEMENT, "test"); 40 mPref.putSync(KEY_TEST_INT_ELEMENT, 3); 41 mPref.flushSync(); 42 mPref.clearSync(); 43 expect("defaultvalue").assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue")); 44 expect(0).assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, 0)); 45 }) 46 47 /** 48 * @tc.name Constructor test 49 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0010 50 * @tc.desc Constructor test 51 */ 52 it('testConstructor002', 0, function () { 53 expect('to:' + mPref).assertEqual('to:[object Object]'); 54 }) 55 56 /** 57 * @tc.name put string sync interface test 58 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0020 59 * @tc.desc put string sync interface test 60 */ 61 it('testHasKey003', 0, function () { 62 mPref.putSync(KEY_TEST_STRING_ELEMENT, "test"); 63 expect(true).assertEqual(mPref.hasSync(KEY_TEST_STRING_ELEMENT)); 64 }) 65 66 /** 67 * @tc.name put int sync interface test 68 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0170 69 * @tc.desc put int sync interface test 70 */ 71 it('testHasKey004', 0, function () { 72 mPref.putSync(KEY_TEST_INT_ELEMENT, 1); 73 expect(true).assertEqual(mPref.hasSync(KEY_TEST_INT_ELEMENT)); 74 }) 75 76 /** 77 * @tc.name put boolean sync interface test 78 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0180 79 * @tc.desc put boolean sync interface test 80 */ 81 it('testHasKey005', 0, function () { 82 mPref.putSync(KEY_TEST_BOOLEAN_ELEMENT, false); 83 expect(true).assertEqual(mPref.hasSync(KEY_TEST_BOOLEAN_ELEMENT)); 84 }) 85 86 /** 87 * @tc.name put long sync interface test 88 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0190 89 * @tc.desc put long sync interface test 90 */ 91 it('testHasKey006', 0, function () { 92 mPref.putSync(KEY_TEST_LONG_ELEMENT, 0); 93 expect(true).assertEqual(mPref.hasSync(KEY_TEST_LONG_ELEMENT)); 94 }) 95 96 /** 97 * @tc.name put float sync interface test 98 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0200 99 * @tc.desc put float sync interface test 100 */ 101 it('testHasKey007', 0, function () { 102 mPref.putSync(KEY_TEST_FLOAT_ELEMENT, 1.1); 103 expect(true).assertEqual(mPref.hasSync(KEY_TEST_FLOAT_ELEMENT)); 104 }) 105 106 /** 107 * @tc.name put boolean sync interface test 108 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0030 109 * @tc.desc put boolean sync interface test 110 */ 111 it('testGetBoolean005', 0, function () { 112 mPref.putSync(KEY_TEST_LONG_ELEMENT, true); 113 expect(true).assertEqual(mPref.hasSync(KEY_TEST_LONG_ELEMENT)); 114 }) 115 116 /** 117 * @tc.name get defaultValue sync interface test 118 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0040 119 * @tc.desc get defaultValue sync interface test 120 */ 121 it('testGetDefValue006', 0, function () { 122 mPref.clearSync(); 123 expect(-1).assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, -1)); 124 expect(1.0).assertEqual(mPref.getSync(KEY_TEST_FLOAT_ELEMENT, 1.0)); 125 expect(10000).assertEqual(mPref.getSync(KEY_TEST_LONG_ELEMENT, 10000)); 126 expect(true).assertEqual(mPref.getSync(KEY_TEST_BOOLEAN_ELEMENT, true)); 127 expect('defaultValue').assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultValue")); 128 }) 129 130 /** 131 * @tc.name put float sync interface test 132 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0050 133 * @tc.desc put float sync interface test 134 */ 135 it('testGetFloat007', 0, function () { 136 mPref.clearSync(); 137 mPref.putSync(KEY_TEST_FLOAT_ELEMENT, 3.0); 138 expect(3.0).assertEqual(mPref.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0)); 139 expect(0.0).assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, 0.0)); 140 }) 141 142 /** 143 * @tc.name put int sync interface test 144 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0060 145 * @tc.desc put int sync interface test 146 */ 147 it('testGetInt008', 0, function () { 148 mPref.clearSync(); 149 mPref.putSync(KEY_TEST_INT_ELEMENT, 3); 150 expect(3).assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, 0.0)); 151 }) 152 153 /** 154 * @tc.name put long sync interface test 155 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0070 156 * @tc.desc put long sync interface test 157 */ 158 it('testGetLong009', 0, function () { 159 mPref.clearSync(); 160 mPref.putSync(KEY_TEST_LONG_ELEMENT, 3); 161 expect(3).assertEqual(mPref.getSync(KEY_TEST_LONG_ELEMENT, 0)); 162 expect(0).assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, 0)); 163 }) 164 165 /** 166 * @tc.name put String & int sync interface test 167 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0080 168 * @tc.desc put String & int sync interface test 169 */ 170 it('testGetString10', 0, function () { 171 mPref.clearSync(); 172 mPref.putSync(KEY_TEST_STRING_ELEMENT, "test"); 173 mPref.putSync(KEY_TEST_INT_ELEMENT, 3); 174 mPref.flushSync(); 175 expect('test').assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue")); 176 expect('defaultvalue').assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, "defaultvalue")); 177 }) 178 179 /** 180 * @tc.name put boolean sync interface test 181 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0090 182 * @tc.desc put boolean sync interface test 183 */ 184 it('testPutBoolean012', 0, function () { 185 mPref.clearSync(); 186 mPref.putSync(KEY_TEST_BOOLEAN_ELEMENT, true); 187 expect(true).assertEqual(mPref.getSync(KEY_TEST_BOOLEAN_ELEMENT, false)); 188 mPref.flushSync(); 189 expect(true).assertEqual(mPref.getSync(KEY_TEST_BOOLEAN_ELEMENT, false)); 190 }) 191 192 /** 193 * @tc.name put float sync interface test 194 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0100 195 * @tc.desc put float sync interface test 196 */ 197 it('testPutFloat013', 0, function () { 198 mPref.clearSync(); 199 mPref.putSync(KEY_TEST_FLOAT_ELEMENT, 4.0); 200 expect(4.0).assertEqual(mPref.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0)); 201 mPref.flushSync(); 202 expect(4.0).assertEqual(mPref.getSync(KEY_TEST_FLOAT_ELEMENT, 0.0)); 203 }) 204 205 /** 206 * @tc.name put int sync interface test 207 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0110 208 * @tc.desc put int sync interface test 209 */ 210 it('testPutInt014', 0, function () { 211 mPref.clearSync(); 212 mPref.putSync(KEY_TEST_INT_ELEMENT, 4); 213 expect(4).assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, 0)); 214 mPref.flushSync(); 215 expect(4).assertEqual(mPref.getSync(KEY_TEST_INT_ELEMENT, 0)); 216 }) 217 218 /** 219 * @tc.name put long sync interface test 220 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0120 221 * @tc.desc put long sync interface test 222 */ 223 it('testPutLong015', 0, function () { 224 mPref.clearSync(); 225 mPref.putSync(KEY_TEST_LONG_ELEMENT, 4); 226 expect(4).assertEqual(mPref.getSync(KEY_TEST_LONG_ELEMENT, 0)); 227 mPref.flushSync(); 228 expect(4).assertEqual(mPref.getSync(KEY_TEST_LONG_ELEMENT, 0)); 229 }) 230 231 /** 232 * @tc.name put String sync interface test 233 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0130 234 * @tc.desc put String sync interface test 235 */ 236 it('testPutString016', 0, function () { 237 mPref.clearSync(); 238 mPref.putSync(KEY_TEST_STRING_ELEMENT, "abc"); 239 mPref.putSync(KEY_TEST_STRING_ELEMENT, ''); 240 expect('').assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue")); 241 mPref.flushSync(); 242 expect('').assertEqual(mPref.getSync(KEY_TEST_STRING_ELEMENT, "defaultvalue")); 243 }) 244 245 /** 246 * @tc.name put interface test 247 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0140 248 * @tc.desc put interface test 249 */ 250 it('testRegisterObserver001', 0, function () { 251 mPref.clearSync(); 252 var observer = function (key) { 253 expect('abcd').assertEqual(key); 254 }; 255 mPref.on('change', observer); 256 mPref.putSync(KEY_TEST_STRING_ELEMENT, "abcd"); 257 }) 258 259 /** 260 * @tc.name repeat on interface test 261 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0150 262 * @tc.desc repeat on interface test 263 */ 264 it('testRegisterObserver002', 0, function () { 265 mPref.clearSync(); 266 var observer = function (key) { 267 console.info('testRegisterObserver001 key' + key); 268 expect('abc').assertEqual(key); 269 }; 270 mPref.on('change', observer); 271 mPref.on('change', observer); 272 mPref.putSync(KEY_TEST_STRING_ELEMENT, "abc"); 273 }) 274 275 /** 276 * @tc.name off interface test 277 * @tc.number SUB_DDM_AppDataFWK_JSPreferences_Sync_0160 278 * @tc.desc off interface test 279 */ 280 it('testUnRegisterObserver001', 0, function () { 281 var observer = function (key) { 282 console.info('testUnRegisterObserver001 key' + key); 283 expect('').assertEqual(key); 284 }; 285 mPref.on('change', observer); 286 mPref.off('change', observer); 287 mPref.putSync(KEY_TEST_STRING_ELEMENT, "abc"); 288 }) 289}) 290