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 systemDateTime from '@ohos.systemDateTime'
19
20let timezone = ["Antarctica/McMurdo", "America/Argentina/Buenos_Aires", "Australia/Sydney", "America/Noronha",
21    "America/St_Johns", "Africa/Kinshasa", "America/Santiago", "Europe/Lisbon", "Asia/Nicosia", "Europe/Berlin",
22    "America/Guayaquil", "Europe/Madrid", "Pacific/Pohnpei", "Asia/Jakarta", "Pacific/Tarawa", "Asia/Almaty",
23    "Pacific/Majuro", "Asia/Ulaanbaatar", "America/Mexico_City", "Asia/Kuala_Lumpur", "Pacific/Auckland",
24    "Pacific/Tahiti", "Pacific/Port_Moresby", "Asia/Gaza", "Europe/Moscow", "Europe/Kiev", "Pacific/Wake", "America/New_York",
25    "Asia/Tashkent", "Asia/Shanghai"
26]
27
28describe("SystemDateTimeSetTest", function () {
29
30    /**
31     * @tc.number: TestSetTime001
32     * @tc.name: TestSetTime001
33     * @tc.desc: Test setTime for promise.
34     * @tc.size: MediumTest
35     * @tc.type: Function
36     * @tc.level: Level 1
37     * @tc.require:
38     */
39    it("testSetTime001", 0,function (done) {
40        console.log("testSetTime001 start");
41        systemDateTime.setTime(123235423411).then(() => {
42            expect(true).assertTrue();
43            done();
44        }).catch((err) => {
45            expect(false).assertTrue();
46            done();
47        })
48        console.log("testSetTime001 end");
49    });
50
51    /**
52     * @tc.number: TestSetTime002
53     * @tc.name: TestSetTime002
54     * @tc.desc: Test setTime for callback.
55     * @tc.size: MediumTest
56     * @tc.type: Function
57     * @tc.level: Level 1
58     * @tc.require:
59     */
60    it("testSetTime002", 0, async function (done) {
61        console.log("testSetTime002 start");
62        let time = 123235423411;
63        systemDateTime.setTime(time, (err) => {
64            if (err) {
65                expect(false).assertTrue();
66            }
67            expect(true).assertTrue();
68            done();
69        })
70        console.log("testSetTime002 end");
71    });
72
73    /**
74     * @tc.number: TestSetTimeInvalidValue003
75     * @tc.name: TestSetTimeInvalidValue003
76     * @tc.desc: Test setTime for promise with invalid value.
77     * @tc.size: MediumTest
78     * @tc.type: Function
79     * @tc.level: Level 1
80     * @tc.require:
81     */
82    it("testSetTimeInvalidValue003", 0, async function (done) {
83        console.log("testSetTimeInvalidValue003 start");
84        let time = 10451042204000;
85        systemDateTime.setTime(time).then(() => {
86            expect(false).assertTrue();
87            done();
88        }).catch((err) => {
89            expect(true).assertTrue();
90            done();
91        })
92        console.log("testSetTimeInvalidValue003 end");
93    });
94
95    /**
96     * @tc.number: TestSetTimeInvalidValue004
97     * @tc.name: TestSetTimeInvalidValue004
98     * @tc.desc: Test setTime for callback with invalid value.
99     * @tc.size: MediumTest
100     * @tc.type: Function
101     * @tc.level: Level 1
102     * @tc.require:
103     */
104    it("testSetTimeInvalidValue004", 0, async function (done) {
105        console.log("testSetTimeInvalidValue004 start");
106        let time = 10451042204000;
107        systemDateTime.setTime(time, (err) => {
108            if (err) {
109                expect(true).assertTrue();
110            } else {
111                expect(false).assertTrue();
112            }
113            done();
114        })
115        console.log("testSetTimeInvalidValue004 end");
116    });
117
118    /**
119     * @tc.number: TestSetTimeInvalidParam005
120     * @tc.name: TestSetTimeInvalidParam005
121     * @tc.desc: Test setTime for promise with invalid param.
122     * @tc.size: MediumTest
123     * @tc.type: Function
124     * @tc.level: Level 1
125     * @tc.require:
126     */
127    it("testSetTimeInvalidParam005", 0, async function (done) {
128        console.log("testSetTimeInvalidParam005 start");
129        try {
130            systemDateTime.setTime("time").then(() => {
131                expect(false).assertTrue();
132                done();
133            }).catch((err) => {
134                expect(err.code).assertEqual(401);
135                done();
136            })
137        } catch (err) {
138            expect(err.code).assertEqual(401);
139            done();
140        }
141        console.log("testSetTimeInvalidParam005 end");
142    });
143
144    /**
145     * @tc.number: TestSetTimeInvalidParam006
146     * @tc.name: TestSetTimeInvalidParam006
147     * @tc.desc: Test setTime for callback with invalid param.
148     * @tc.size: MediumTest
149     * @tc.type: Function
150     * @tc.level: Level 1
151     * @tc.require:
152     */
153    it("testSetTimeInvalidParam006", 0, async function (done) {
154        console.log("testSetTimeInvalidParam006 start");
155        try {
156            systemDateTime.setTime("time", (err) => {
157                if (err) {
158                    expect(err.code).assertEqual(401);
159                } else {
160                    expect(false).assertTrue();
161                }
162                done();
163            })
164        } catch (err) {
165            expect(err.code).assertEqual(401);
166            done();
167        }
168        console.log("testSetTimeInvalidParam006 end");
169    });
170
171    /**
172     * @tc.number: TestSetTimeInvalidValue007
173     * @tc.name: TestSetTimeInvalidValue007
174     * @tc.desc: Test setTime for callback with invalid value.
175     * @tc.size: MediumTest
176     * @tc.type: Function
177     * @tc.level: Level 1
178     * @tc.require:
179     */
180    it("testSetTimeInvalidValue007", 0, async function (done) {
181        console.log("testSetTimeInvalidValue007 start");
182        systemDateTime.setTime(-1).then(() => {
183            expect(false).assertTrue();
184            done();
185        }).catch((err) => {
186            expect(err.code).assertEqual(undefined);
187            done();
188        })
189        console.log("testSetTimeInvalidValue007 end");
190    });
191
192    /**
193     * @tc.number: TestSetTimeInvalidValue008
194     * @tc.name: TestSetTimeInvalidValue008
195     * @tc.desc: Test setTime for callback with invalid value.
196     * @tc.size: MediumTest
197     * @tc.type: Function
198     * @tc.level: Level 1
199     * @tc.require:
200     */
201    it("testSetTimeInvalidValue008", 0, async function (done) {
202        console.log("testSetTimeInvalidValue008 start");
203        systemDateTime.setTime(-1, (err) => {
204            if (err) {
205                expect(err.code).assertEqual(undefined);
206            } else {
207                expect(false).assertTrue();
208            }
209            done();
210        })
211        console.log("testSetTimeInvalidValue008 end");
212    });
213
214    /**
215     * @tc.number: TestSetDate001
216     * @tc.name: TestSetDate001
217     * @tc.desc: Test setDate for promise.
218     * @tc.size: MediumTest
219     * @tc.type: Function
220     * @tc.level: Level 1
221     * @tc.require:
222     */
223    it("testSetDate001", 0, async function (done) {
224        let date = new Date();
225        systemDateTime.setDate(date).then(() => {
226            systemDateTime.getDate().then((data) => {
227                expect(date.toDateString() === data.toDateString()).assertTrue();
228                done();
229            }).catch((err) => {
230                expect(false).assertTrue();
231                done();
232            });
233        }).catch((err) => {
234            expect(false).assertTrue();
235            done();
236        })
237    });
238
239    /**
240     * @tc.number: TestSetDate002
241     * @tc.name: TestSetDate002
242     * @tc.desc: Test setDate for callback.
243     * @tc.size: MediumTest
244     * @tc.type: Function
245     * @tc.level: Level 1
246     * @tc.require:
247     */
248    it("testSetDate002", 0, async function (done) {
249        let date = new Date();
250        systemDateTime.setDate(date, (err) => {
251            if (err) {
252                expect(false).assertTrue();
253                done();
254            }
255            systemDateTime.getDate().then((data) => {
256                expect(date.toDateString() === data.toDateString()).assertTrue();
257                done();
258            }).catch((err) => {
259                expect(false).assertTrue();
260                done();
261            });
262        });
263    });
264
265    /**
266     * @tc.number: TestSetDateNull001
267     * @tc.name: TestSetDateNull001
268     * @tc.desc: Test setDate for promise with null.
269     * @tc.size: MediumTest
270     * @tc.type: Function
271     * @tc.level: Level 1
272     * @tc.require:
273     */
274    it("testSetDateNull001", 0, async function (done) {
275        try {
276            systemDateTime.setDate(null).then(() => {
277                expect(false).assertTrue();
278                done();
279            }).catch((err) => {
280                expect(err.code).assertEqual(401);
281                done();
282            })
283        } catch (err) {
284            expect(err.code).assertEqual(401);
285            done();
286        }
287    });
288
289    /**
290     * @tc.number: TestSetDateNull002
291     * @tc.name: TestSetDateNull002
292     * @tc.desc: Test setDate for callback with null.
293     * @tc.size: MediumTest
294     * @tc.type: Function
295     * @tc.level: Level 1
296     * @tc.require:
297     */
298    it("testSetDateNull002", 0, async function (done) {
299        try {
300            systemDateTime.setDate(null, (err) => {
301                if (err) {
302                    expect(true).assertTrue();
303                } else {
304                    expect(false).assertTrue();
305                }
306                done();
307            })
308        } catch (err) {
309            expect(true).assertTrue();
310            done();
311        }
312    });
313
314    /**
315     * @tc.number: TestSetTimezone001
316     * @tc.name: TestSetTimezone001
317     * @tc.desc: Test setDate for promise.
318     * @tc.size: MediumTest
319     * @tc.type: Function
320     * @tc.level: Level 1
321     * @tc.require:
322     */
323    it("testSetTimezone001", 0, async function (done) {
324        function calLoop(index) {
325            if (index >= timezone.length - 1) {
326                expect(true).assertTrue();
327                done();
328            }
329        }
330        for (let i = 0; i < timezone.length; i++) {
331            systemDateTime.setTimezone(timezone[i]).then(() => {
332                calLoop(i)
333            }).catch((err) => {
334                expect(false).assertTrue();
335                done();
336            });
337        }
338
339    });
340
341    /**
342     * @tc.number: TestSetTimezone002
343     * @tc.name: TestSetTimezone002
344     * @tc.desc: Test setDate for callback.
345     * @tc.size: MediumTest
346     * @tc.type: Function
347     * @tc.level: Level 1
348     * @tc.require:
349     */
350    it("testSetTimezone002", 0, async function (done) {
351        for (let i = 0; i < timezone.length; i++) {
352            systemDateTime.setTimezone(timezone[i], (err) => {
353                if (err) {
354                    expect(false).assertTrue();
355                }
356            })
357        }
358        expect(true).assertTrue();
359        done();
360    });
361
362    /**
363     * @tc.number: TestSetTimezoneNull003
364     * @tc.name: TestSetTimezoneNull003
365     * @tc.desc: Test setDate for promise with null.
366     * @tc.size: MediumTest
367     * @tc.type: Function
368     * @tc.level: Level 1
369     * @tc.require:
370     */
371    it("testSetTimezoneNull003", 0, async function (done) {
372        try {
373            systemDateTime.setTimezone(null).then(() => {
374                expect(false).assertTrue();
375                done();
376            }).catch((err) => {
377                expect(err.code).assertEqual(401);
378                done();
379            })
380        } catch (err) {
381            expect(err.code).assertEqual(401);
382            done();
383        }
384    });
385
386    /**
387     * @tc.number: TestSetTimezoneNull004
388     * @tc.name: TestSetTimezoneNull004
389     * @tc.desc: Test setDate for callback with null.
390     * @tc.size: MediumTest
391     * @tc.type: Function
392     * @tc.level: Level 1
393     * @tc.require:
394     */
395    it("testSetTimezoneNull004", 0, async function (done) {
396        try {
397            systemDateTime.setTimezone(null, (err) => {
398                if (err) {
399                    expect(err.code).assertEqual(401);
400                } else {
401                    expect(false).assertTrue();
402                }
403                done();
404            });
405        } catch (err) {
406            expect(err.code).assertEqual(401);
407            done();
408        }
409    });
410})