• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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
16// @ts-nocheck
17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
18import systemTime from '@ohos.systemTime'
19
20describe('SystemTimeGetTest', function () {
21    const MILLI_TO_BASE = 1000;
22    const NANO_TO_BASE = 1000000000;
23    const NANO_TO_MILLI = NANO_TO_BASE / MILLI_TO_BASE;
24
25    /**
26     * @tc.name: TestGetCurrentTimeMs001
27     * @tc.desc: test getCurrentTime ms for promise.
28     * @tc.type: Function
29     * @tc.require:
30     */
31    it('testGetCurrentTimeMs001', 0, async function (done) {
32        console.log("testGetCurrentTimeMs001 start");
33        const nowTime = new Date().getTime();
34        const milliTime = await systemTime.getCurrentTime();
35        console.log('Get current time is ' + milliTime);
36        expect(typeof (milliTime) === 'number' && milliTime >= nowTime).assertTrue();
37        console.log('testGetCurrentTimeMs001 end');
38        done();
39    })
40
41    /**
42     * @tc.name: TestGetCurrentTimeMs002
43     * @tc.desc: test getCurrentTime ms for callback.
44     * @tc.type: Function
45     * @tc.require:
46     */
47    it('testGetCurrentTimeMs002', 0, async function (done) {
48        console.log("testGetCurrentTimeMs002 start");
49        const nowTime = new Date().getTime();
50        systemTime.getCurrentTime((err, data) => {
51            if (err) {
52                expect(false).assertTrue();
53            }
54            expect(typeof (data) === 'number' && data >= nowTime).assertTrue();
55            done();
56        })
57        console.log('testGetCurrentTimeMs002 end');
58    })
59
60    /**
61     * @tc.name: TestGetCurrentTimeMs003
62     * @tc.desc: test getCurrentTime ms for promise when isNano is false.
63     * @tc.type: Function
64     * @tc.require:
65     */
66    it('testGetCurrentTimeMs003', 0, async function (done) {
67        console.log("testGetCurrentTimeMs003 start");
68        const nowTime = new Date().getTime();
69        const milliTime = await systemTime.getCurrentTime(false);
70        console.log('Get current time is ' + milliTime);
71        expect(milliTime >= nowTime && typeof (milliTime) === 'number').assertTrue();
72        console.log('testGetCurrentTimeMs003 end');
73        done();
74    })
75
76    /**
77     * @tc.name: TestGetCurrentTimeMs004
78     * @tc.desc: test getCurrentTime ms for callback when isNano is false.
79     * @tc.type: Function
80     * @tc.require:
81     */
82    it('testGetCurrentTimeMs004', 0, async function (done) {
83        console.log("testGetCurrentTimeMs004 start");
84        const nowTime = new Date().getTime();
85        systemTime.getCurrentTime(false, (err, data) => {
86            if (err) {
87                expect(false).assertTrue();
88            }
89            expect(typeof (data) === 'number' && data >= nowTime).assertTrue();
90            done();
91        });
92        console.log('testGetCurrentTimeMs004 end');
93    })
94
95    /**
96     * @tc.name: TestGetCurrentTimeNs001
97     * @tc.desc: test getCurrentTime ns for promise when inNano is true
98     * @tc.type: Function
99     * @tc.require:
100     */
101    it('testGetCurrentTimeNs001', 0, async function (done) {
102        console.log("testGetCurrentTimeNs001 start");
103        const nowTime = new Date().getTime();
104        const nanoTime = await systemTime.getCurrentTime(true);
105        console.log('Get current nano time is ' + nanoTime);
106        const milliTime = nanoTime / NANO_TO_MILLI;
107        expect(milliTime >= nowTime).assertTrue();
108        console.log('testGetCurrentTimeNs001 end');
109        done();
110    })
111
112    /**
113     * @tc.name: TestGetCurrentTimeNs002
114     * @tc.desc: test getCurrentTime ns for promise when inNano is true
115     * @tc.type: Function
116     * @tc.require:
117     */
118    it('testGetCurrentTimeNs002', 0, async function (done) {
119        console.log("testGetCurrentTimeNs002 start");
120        const nowTime = new Date().getTime();
121        systemTime.getCurrentTime(true, (err, data) => {
122            if (err) {
123                expect(false).assertTrue();
124            }
125            expect((data / NANO_TO_MILLI) >= nowTime).assertTrue();
126            done();
127        })
128        console.log('testGetCurrentTimeNs002 end');
129    })
130
131    /**
132     * @tc.name: TestGetRealActiveTimeMs001
133     * @tc.desc: test getRealActiveTime ms for promise.
134     * @tc.type: Function
135     * @tc.require:
136     */
137    it('testGetRealActiveTimeMs001', 0, async function (done) {
138        console.log("testGetRealActiveTimeMs001 start");
139        const milliTime = await systemTime.getRealActiveTime();
140        console.log('Get real active time is ' + milliTime);
141        expect(typeof (milliTime) === 'number' && milliTime / MILLI_TO_BASE > 0).assertTrue();
142        console.log('testGetRealActiveTimeMs001 end');
143        done();
144    })
145
146    /**
147     * @tc.name: TestGetRealActiveTimeMs002
148     * @tc.desc: test getRealActiveTime ms for callback.
149     * @tc.type: Function
150     * @tc.require:
151     */
152    it('testGetRealActiveTimeMs002', 0, async function (done) {
153        console.log("testGetRealActiveTimeMs002 start");
154        systemTime.getRealActiveTime((err, data) => {
155            if (err) {
156                expect(false).assertTrue();
157            }
158            expect(typeof (data) === 'number' && (data / MILLI_TO_BASE) > 0).assertTrue();
159            done();
160        })
161        console.log('testGetRealActiveTimeMs002 end');
162    })
163
164    /**
165     * @tc.name: TestGetRealActiveTimeMs003
166     * @tc.desc: test getRealActiveTime ms for promise when isNano is false.
167     * @tc.type: Function
168     * @tc.require:
169     */
170    it('testGetRealActiveTimeMs003', 0, async function (done) {
171        console.log("testGetRealActiveTimeMs003 start");
172        const milliTime = await systemTime.getRealActiveTime(false);
173        expect(typeof (milliTime) === 'number' && milliTime / MILLI_TO_BASE > 0).assertTrue();
174        console.log('testGetRealActiveTimeMs003 end');
175        done();
176    })
177
178    /**
179     * @tc.name: TestGetRealActiveTimeMs004
180     * @tc.desc: test getRealActiveTime ms for callback when isNano is false.
181     * @tc.type: Function
182     * @tc.require:
183     */
184    it('testGetRealActiveTimeMs004', 0, async function (done) {
185        console.log("testGetRealActiveTimeMs004 start");
186        systemTime.getRealActiveTime(false, (err, data) => {
187            if (err) {
188                expect(false).assertTrue();
189            }
190            expect(typeof (data) === 'number' && (data / MILLI_TO_BASE) > 0).assertTrue();
191            done();
192        })
193        console.log('testGetRealActiveTimeMs004 end');
194    })
195
196    /**
197     * @tc.name: TestGetRealActiveTimeNs001
198     * @tc.desc: test getRealActiveTime ns for promise when isNano is true.
199     * @tc.type: Function
200     * @tc.require:
201     */
202    it('testGetRealActiveTimeNs001', 0, async function (done) {
203        console.log("testGetRealActiveTimeNs001 start");
204        const nanoTime = await systemTime.getRealActiveTime(true);
205        console.log('Get real active nano time is ' + nanoTime);
206        expect(nanoTime / NANO_TO_BASE > 0).assertTrue();
207        console.log('testGetRealActiveTimeNs001 end');
208        done();
209    })
210
211    /**
212     * @tc.name: TestGetRealActiveTimeNs002
213     * @tc.desc: test getRealActiveTime ns for callback when isNano is true.
214     * @tc.type: Function
215     * @tc.require:
216     */
217    it('testGetRealActiveTimeNs002', 0, async function (done) {
218        console.log("testGetRealActiveTimeNs002 start");
219        systemTime.getRealActiveTime(true, (err, data) => {
220            if (err) {
221                expect(false).assertTrue();
222            }
223            expect((data / MILLI_TO_BASE) > 0).assertTrue();
224            done();
225        })
226        console.log('testGetRealActiveTimeNs002 end');
227    })
228
229    /**
230     * @tc.name: TestGetRealTimeMs001
231     * @tc.desc: test getRealTime ms for promise.
232     * @tc.type: Function
233     * @tc.require:
234     */
235    it('testGetRealTimeMs001', 0, async function (done) {
236        console.log("testGetRealTimeMs001 start");
237        const milliTime = await systemTime.getRealTime();
238        console.log('Get real time is ' + milliTime);
239        expect(typeof (milliTime) === 'number' && milliTime / MILLI_TO_BASE >= 0).assertTrue();
240        console.log('testGetRealTimeMs001 end');
241        done();
242    })
243
244    /**
245     * @tc.name: TestGetRealTimeMs002
246     * @tc.desc: test getRealTime ms for callback.
247     * @tc.type: Function
248     * @tc.require:
249     */
250    it('testGetRealTimeMs002', 0, async function (done) {
251        console.log("testGetRealTimeMs002 start");
252        systemTime.getRealTime((err, data) => {
253            if (err) {
254                expect(false).assertTrue();
255            }
256            expect(typeof (data) === 'number' && (data / MILLI_TO_BASE) > 0).assertTrue();
257            done();
258        })
259        console.log('testGetRealTimeMs002 end');
260    })
261
262    /**
263     * @tc.name: TestGetRealTimeMs003
264     * @tc.desc: test getRealTime ms for promise when isNano is false.
265     * @tc.type: Function
266     * @tc.require:
267     */
268    it('testGetRealTimeMs003', 0, async function (done) {
269        console.log("testGetRealTimeMs003 start");
270        const milliTime = await systemTime.getRealTime(false);
271        console.log('Get real time is ' + milliTime);
272        expect(typeof (milliTime) === 'number' && milliTime / MILLI_TO_BASE > 0).assertTrue();
273        console.log('testGetRealTimeMs003 end');
274        done();
275    })
276
277    /**
278     * @tc.name: TestGetRealTimeMs004
279     * @tc.desc: test getRealTime ms for callback when isNano is false.
280     * @tc.type: Function
281     * @tc.require:
282     */
283    it('testGetRealTimeMs004', 0, async function (done) {
284        console.log("testGetRealTimeMs004 start");
285        systemTime.getRealTime(false, (err, data) => {
286            if (err) {
287                expect(false).assertTrue();
288            }
289            expect(typeof (data) === 'number' && (data / MILLI_TO_BASE) > 0).assertTrue();
290            done();
291        })
292        console.log('testGetRealTimeMs004 end');
293    })
294
295    /**
296     * @tc.name: TestGetRealTimeNs001
297     * @tc.desc: test getRealTime ns for promise when isNano is true.
298     * @tc.type: Function
299     * @tc.require:
300     */
301    it('testGetRealTimeNs001', 0, async function (done) {
302        console.log("testGetRealTimeNs001 start");
303        const nanoTime = await systemTime.getRealTime(true);
304        console.log('Get real nano time is ' + nanoTime);
305        expect(nanoTime / NANO_TO_BASE > 0).assertTrue();
306        console.log('testGetRealTimeNs001 end');
307        done();
308    })
309
310    /**
311     * @tc.name: TestGetRealTimeNs002
312     * @tc.desc: test getRealTime ns for callback when isNano is true.
313     * @tc.type: Function
314     * @tc.require:
315     */
316    it('testGetRealTimeNs002', 0, async function (done) {
317        console.log("testGetRealTimeNs002 start");
318        systemTime.getRealTime(true, (err, data) => {
319            if (err) {
320                expect(false).assertTrue();
321            }
322            expect((data / MILLI_TO_BASE) > 0).assertTrue();
323            done();
324        })
325        console.log('testGetRealTimeNs002 end');
326    })
327
328    /**
329     * @tc.name: TestGetDate001
330     * @tc.desc: test getDate for promise.
331     * @tc.type: Function
332     * @tc.require:
333     */
334    it('testGetDate001', 0, async function (done) {
335        console.log("testGetDate001 start");
336        let date = new Date(2022, 1, 1);
337        await systemTime.setDate(date);
338        const currentDate = await systemTime.getDate();
339        expect(currentDate instanceof Date && currentDate.toDateString() === date.toDateString()).assertTrue();
340        done();
341        console.log('testGetDate001 end');
342    })
343
344    /**
345     * @tc.name: TestGetDate002
346     * @tc.desc: test getDate for callback.
347     * @tc.type: Function
348     * @tc.require:
349     */
350    it('testGetDate002', 0, async function (done) {
351        console.log("testGetDate002 start");
352        let date = new Date(2022, 1, 1);
353        await systemTime.setDate(date);
354        systemTime.getDate((err, data) => {
355            if (err) {
356                expect(false).assertTrue();
357            }
358            expect(data instanceof Date && data.toDateString() === date.toDateString()).assertTrue();
359            done();
360        })
361        console.log('testGetDate002 end');
362    })
363
364    /**
365     * @tc.name: TestGetCurrentTimeInvalidParam001
366     * @tc.desc: test getCurrentTime for promise with invalid param.
367     * @tc.type: Function
368     * @tc.require:
369     */
370    it('testGetCurrentTimeInvalidParam001', 0, async function (done) {
371        console.log("testGetCurrentTimeInvalidParam001 start");
372        try {
373            systemTime.getCurrentTime("true").then((time) => {
374                expect(true).assertTrue();
375                done();
376            });
377        } catch (err) {
378            expect(false).assertTrue();
379            done();
380        }
381        console.log('testGetCurrentTimeInvalidParam001 end');
382    })
383
384    /**
385     * @tc.name: TestGetCurrentTimeInvalidParam002
386     * @tc.desc: test getCurrentTime for callback with invalid param.
387     * @tc.type: Function
388     * @tc.require:
389     */
390    it('testGetCurrentTimeInvalidParam002', 0, async function (done) {
391        console.log("testGetCurrentTimeInvalidParam002 start");
392        try {
393            systemTime.getCurrentTime("true", function (err) {
394                if (err) {
395                    expect(false).assertTrue();
396                }
397                expect(true).assertTrue();
398                done();
399            });
400        } catch (err) {
401            expect(false).assertTrue();
402            done();
403        }
404        console.log('testGetCurrentTimeInvalidParam002 end');
405    })
406
407    /**
408     * @tc.name: TestGetCurrentTimeInvalidParam002
409     * @tc.desc: test getRealActiveTime for callback with invalid param.
410     * @tc.type: Function
411     * @tc.require:
412     */
413    it('testGetCurrentTimeInvalidParam002', 0, async function (done) {
414        console.log("testGetCurrentTimeInvalidParam002 start");
415        try {
416            systemTime.getRealActiveTime("true").then((time) => {
417                expect(true).assertTrue();
418                done();
419            });
420        } catch (err) {
421            expect(false).assertTrue();
422            done();
423        }
424        console.log('testGetCurrentTimeInvalidParam002 end');
425    })
426
427    /**
428     * @tc.name: TestGetRealActiveTimeInvalidParam001
429     * @tc.desc: test getRealActiveTime for promise with invalid param.
430     * @tc.type: Function
431     * @tc.require:
432     */
433    it('testGetRealActiveTimeInvalidParam001', 0, async function (done) {
434        console.log("testGetRealActiveTimeInvalidParam001 start");
435        try {
436            systemTime.getRealActiveTime("true", function (err) {
437                if (err) {
438                    expect(false).assertTrue();
439                }
440                expect(true).assertTrue();
441                done();
442            });
443        } catch (err) {
444            expect(false).assertTrue();
445            done();
446        }
447        console.log('testGetRealActiveTimeInvalidParam001 end');
448    })
449
450    /**
451     * @tc.name: TestGetRealActiveTimeInvalidParam001
452     * @tc.desc: test getRealTime for promise with invalid param.
453     * @tc.type: Function
454     * @tc.require:
455     */
456    it('testGetRealTimeInvalidParam001', 0, async function (done) {
457        console.log("testGetRealTimeInvalidParam001 start");
458        try {
459            systemTime.getRealTime("true").then((time) => {
460                expect(true).assertTrue();
461                done();
462            });
463        } catch (err) {
464            expect(false).assertTrue();
465            done();
466        }
467        console.log('testGetRealTimeInvalidParam001 end');
468    })
469
470    /**
471     * @tc.name: TestGetRealTimeInvalidParam002
472     * @tc.desc: test getRealTime for callback with invalid param.
473     * @tc.type: Function
474     * @tc.require:
475     */
476    it('testGetRealTimeInvalidParam002', 0, async function (done) {
477        console.log("testGetRealTimeInvalidParam002 start");
478        try {
479            systemTime.getRealTime("true", function (err) {
480                if (err) {
481                    expect(false).assertTrue();
482                }
483                expect(true).assertTrue();
484                done();
485            });
486        } catch (err) {
487            expect(false).assertTrue();
488            done();
489        }
490        console.log('testGetRealTimeInvalidParam002 end');
491    })
492
493    /**
494     * @tc.name: TestGetTimezone001
495     * @tc.desc: test getTimezone for promise.
496     * @tc.type: Function
497     * @tc.require:
498     */
499    it('testGetTimezone001', 0, async function (done) {
500        console.log("testGetTimezone001 start");
501        let timezone = "Pacific/Majuro";
502        await systemTime.setTimezone(timezone);
503        const currentTimezone = await systemTime.getTimezone();
504        expect(typeof (currentTimezone) == 'string' && timezone === currentTimezone).assertTrue();
505        await systemTime.setTimezone('Asia/Shanghai');
506        done();
507        console.log('testGetTimezone001 end');
508    })
509
510    /**
511     * @tc.name: TestGetTimezone002
512     * @tc.desc: test getTimezone for callback.
513     * @tc.type: Function
514     * @tc.require:
515     */
516    it('testGetTimezone002', 0, async function (done) {
517        console.log("testGetTimezone002 start");
518        let timezone = "Pacific/Majuro";
519        await systemTime.setTimezone(timezone);
520        systemTime.getTimezone((err, data) => {
521            if (err) {
522                expect(false).assertTrue();
523                done();
524            }
525            expect(typeof(data) == 'string' && data === timezone).assertTrue();
526            done();
527            systemTime.setTimezone('Asia/Shanghai');
528        })
529        console.log('testGetTimezone002 end');
530    })
531})