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