systemTimeTest.ets 15.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// @ts-nocheck
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
18 19 20 21
import systemTime from "@ohos.systemTime";

export default function systemTimeJsunit() {
  describe('systemTimeTest', function () {
22
    console.info('====>---------------systemTimeTest start-----------------------');
23 24 25 26 27 28 29 30 31
    /**
     * @tc.number    SUB_systemTime_getCurrentTime_JS_API_0001
     * @tc.name      Test systemTime.getCurrentTime
     * @tc.desc      Obtains the number of milliseconds that have elapsed since the Unix epoch.
     * @tc.size      : MEDIUM
     * @tc.type      : Function
     * @tc.level     : Level 1
     */
    it("SUB_systemTime_getCurrentTime_JS_API_0001", 0, async function (done) {
32
      console.info("====>----------UB_systemTime_getCurrentTime_JS_API_0001 start----------------");
33 34 35 36 37 38 39 40 41
      systemTime.getCurrentTime(true, (error, data) => {
        if (error) {
          console.error(`failed to systemTime.getCurrentTime because ` + JSON.stringify(error));
          expect().assertFail();
        };
        console.info(`systemTime.getCurrentTime success data : ` + JSON.stringify(data));
        expect(data != null).assertEqual(true);
      });

42
      console.info("====>----------SUB_systemTime_getRealActiveTime_JS_API_0100 end-----------------");
43 44 45 46 47 48 49 50 51 52 53 54
      done();
    });

    /**
     * @tc.number    SUB_systemTime_getCurrentTime_JS_API_0002
     * @tc.name      Test systemTime.getCurrentTime
     * @tc.desc      Obtains the number of milliseconds that have elapsed since the Unix epoch.
     * @tc.size      : MEDIUM
     * @tc.type      : Function
     * @tc.level     : Level 1
     */
    it("SUB_systemTime_getCurrentTime_JS_API_0002", 0, async function (done) {
55
      console.info("====>-----SUB_systemTime_getCurrentTime_JS_API_0002 start----------------");
56
      systemTime.getCurrentTime(true).then((data) => {
57
        console.info(`SUB_systemTime_getCurrentTime_JS_API_0002 data : ` + JSON.stringify(data));
58 59
        expect(data != null).assertEqual(true);
      }).catch(err => {
60
        console.error(`SUB_systemTime_getCurrentTime_JS_API_0002 err: ` + JSON.stringify(error));
61 62
        expect().assertFail()
      });
63
      console.info("====>-----SUB_systemTime_getCurrentTime_JS_API_0002 end------------");
64 65 66 67 68 69 70 71 72 73 74 75
      done();
    });

    /**
     * @tc.number    SUB_systemTime_getRealActiveTime_JS_API_0001
     * @tc.name      Test systemTime.getCurrentTime
     * @tc.desc      Obtains the number of milliseconds elapsed since the system was booted, not including deep sleep time.
     * @tc.size      : MEDIUM
     * @tc.type      : Function
     * @tc.level     : Level 1
     */
    it("SUB_systemTime_getRealActiveTime_JS_API_0001", 0, async function (done) {
76
      console.info("====>----------SUB_systemTime_getRealActiveTime_JS_API_0001 start----------------");
77 78
      systemTime.getRealActiveTime(true, (error, data) => {
        if (error) {
79
          console.error(`SUB_systemTime_getRealActiveTime_JS_API_0001 err: ` + JSON.stringify(error));
80 81
          expect().assertFail()
        };
82
        console.info(`SUB_systemTime_getRealActiveTime_JS_API_0001 data : ` + JSON.stringify(data));
83 84 85
        expect(data != null).assertEqual(true);
      });

86
      console.info("====>----------SUB_systemTime_getRealActiveTime_JS_API_0001 end-----------------");
87 88 89 90 91 92 93 94 95 96 97 98
      done();
    });

    /**
     * @tc.number    SUB_systemTime_getRealActiveTime_JS_API_0002
     * @tc.name      Test systemTime.getCurrentTime
     * @tc.desc      Obtains the number of milliseconds elapsed since the system was booted, not including deep sleep time.
     * @tc.size      : MEDIUM
     * @tc.type      : Function
     * @tc.level     : Level 1
     */
    it("SUB_systemTime_getRealActiveTime_JS_API_0002", 0, async function (done) {
99
      console.info("====>-----SUB_systemTime_getRealActiveTime_JS_API_0002 start----------------");
100
      systemTime.getRealActiveTime(true).then((data) => {
101
        onsole.log(`SUB_systemTime_getRealActiveTime_JS_API_0002 data : ` + JSON.stringify(data));
102 103
        expect(data != null).assertEqual(true);
      }).catch(err => {
104
        console.error(`SUB_systemTime_getRealActiveTime_JS_API_0002 err: ` + JSON.stringify(error));
105 106
        expect().assertFail()
      });
107
      console.info("====>-----SUB_systemTime_getRealActiveTime_JS_API_0002 end------------");
108 109 110 111 112 113 114 115 116 117 118 119
      done();
    });

    /**
     * @tc.number    SUB_systemTime_getRealTime_JS_API_0001
     * @tc.name      Test systemTime.getCurrentTime
     * @tc.desc      Obtains the number of milliseconds elapsed since the system was booted, including deep sleep time.
     * @tc.size      : MEDIUM
     * @tc.type      : Function
     * @tc.level     : Level 1
     */
    it("SUB_systemTime_getRealTime_JS_API_0001", 0, async function (done) {
120
      console.info("====>----------SUB_systemTime_getRealTime_JS_API_0001 start----------------");
121 122
      systemTime.getRealTime(true, (error, data) => {
        if (error) {
123
          console.error(`SUB_systemTime_getRealTime_JS_API_0001 err: ` + JSON.stringify(error));
124 125
          expect().assertFail()
        };
126
        console.info(`SUB_systemTime_getRealTime_JS_API_0001 data : ` + JSON.stringify(data));
127 128 129
        expect(data != null).assertEqual(true);
      });

130
      console.info("====>----------SUB_systemTime_getRealTime_JS_API_0001 end-----------------");
131 132 133 134 135 136 137 138 139 140 141 142
      done();
    });

    /**
     * @tc.number    SUB_systemTime_getRealTime_JS_API_0002
     * @tc.name      Test systemTime.getCurrentTime
     * @tc.desc      Obtains the number of milliseconds elapsed since the system was booted, not including deep sleep time.
     * @tc.size      : MEDIUM
     * @tc.type      : Function
     * @tc.level     : Level 1
     */
    it("SUB_systemTime_getRealTime_JS_API_0002", 0, async function (done) {
143
      console.info("====>-----SUB_systemTime_getRealTime_JS_API_0002 start----------------");
144
      systemTime.getRealTime(true).then((data) => {
145
        console.info(`SUB_systemTime_getRealTime_JS_API_0002 data : ` + JSON.stringify(data));
146 147
        expect(data != null).assertEqual(true);
      }).catch(err => {
148
        console.error(`SUB_systemTime_getRealTime_JS_API_0002 err: ` + JSON.stringify(error));
149 150
        expect().assertFail();
      });
151
      console.info("====>-----SUB_systemTime_getRealTime_JS_API_0002 end------------");
152 153 154 155 156 157 158 159 160 161 162 163
      done();
    });

    /**
   * @tc.number    SUB_systemTime_setTime_JS_API_0100
   * @tc.name      Test systemTime.setTime
   * @tc.desc      Test systemTime_setTime API functionality.
   * @tc.size      : MEDIUM
   * @tc.type      : Function
   * @tc.level     : Level 0
   */
    it('SUB_systemTime_setTime_JS_API_0100', 0, async function (done) {
164 165 166 167 168 169 170 171 172 173 174
      console.info("====>SUB_systemTime_setTime_JS_API_0100 start");
      let currentTime = new Date().getTime() + 2000
      try{
        await systemTime.setTime(currentTime)
        expect(true).assertTrue();
        done();
      }catch(err){
        console.info("====>SUB_systemTime_setTime_JS_API_0100 error " + err);
        expect().assertFail();
        done();
      }
175 176 177 178 179 180 181 182 183 184 185
    });

    /**
       * @tc.number    SUB_systemTime_setTime_JS_API_0200
       * @tc.name      Test systemTime.setTime Invalid value
       * @tc.desc      Test systemTime_setTime API functionality.
       * @tc.size      : MEDIUM
       * @tc.type      : Function
       * @tc.level     : Level 0
       */
    it('SUB_systemTime_setTime_JS_API_0200', 0, async function (done) {
186 187 188 189 190 191 192 193
      console.info("====>SUB_systemTime_setTime_JS_API_0200 start");
      try{
        systemTime.setTime(15222).then(() => {   
          console.debug('SUB_systemTime_setTime_JS_API_0200 setTime fail')         
          expect().assertFail();
          done();
        }).catch((err)=>{
          console.debug('SUB_systemTime_setTime_JS_API_0200 setTime err:' + JSON.stringify(err))
194
          expect(true).assertTrue();
195 196 197 198 199 200 201 202
          console.debug('SUB_systemTime_setTime_JS_API_0200 end');
          done();
        })
      }catch(err){
          console.debug('SUB_systemTime_setTime_JS_API_0200 setTime throw_err' + JSON.stringify(err))
          expect(err.code).assertEqual('401');
          done();
      }
203 204 205 206 207 208 209 210 211 212 213
    });

    /**
       * @tc.number    SUB_systemTime_setTime_JS_API_0300
       * @tc.name      Test systemTime.setTime3
       * @tc.desc      Test systemTime_setTime API functionality.
       * @tc.size      : MEDIUM
       * @tc.type      : Function
       * @tc.level     : Level 0
       */
    it('SUB_systemTime_setTime_JS_API_0300', 0, async function (done) {
214 215 216 217 218 219 220 221 222 223 224 225
      console.info("====>SUB_systemTime_setTime_JS_API_0300 start");
      let currentTime = new Date().getTime() + 2000
      systemTime.setTime(currentTime, (err) => {
        try{
          expect(true).assertTrue();          
          console.info('SUB_systemTime_setTime_JS_API_0300 end');
          done();
        }catch(err){
          console.info("====>SUB_systemTime_setTime_JS_API_0300 error: " + err);
          expect().assertFail();
          done();
        }
226 227 228 229 230 231 232 233 234 235 236 237
      });
    });

    /**
       * @tc.number    SUB_systemTime_setTime_JS_API_0400
       * @tc.name      Test systemTime.setTime4  Invalid value
       * @tc.desc      Test systemTime_setTime API functionality.
       * @tc.size      : MEDIUM
       * @tc.type      : Function
       * @tc.level     : Level 0
       */
    it('SUB_systemTime_setTime_JS_API_0400', 0, async function (done) {
238
      console.info("====>SUB_systemTime_setTime_JS_API_0400 start");
239 240
      try{
        systemTime.setTime(18, (err, data) => {
241 242
          console.info("====>SUB_systemTime_setTime_JS_API_0400 data: " + data);
          console.info("====>SUB_systemTime_setTime_JS_API_0400 error: " + err);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
          expect(true).assertTrue();
        })}catch(error) {error => {
        expect(1).assertLarger(0);
      };
      };
      console.info('SUB_systemTime_setTime_JS_API_0400 end');
      done();
    });

    /**
      * @tc.number    SUB_systemTime_setDate_JS_API_0100
      * @tc.name      Test systemTime.setDate Invalid value
      * @tc.desc      Test systemTime_setDate API functionality.
      * @tc.size      : MEDIUM
      * @tc.type      : Function
      * @tc.level     : Level 0
      */
    it('SUB_systemTime_setDate_JS_API_0100', 0, async function (done) {
261
      console.info("====>SUB_systemTime_setDate_JS_API_0100 start");
262 263
      var data = new Date("October 13, 2020 11:13:00");
      systemTime.setDate(1).then(data => {
264
        console.info("====>SUB_systemTime_setDate_JS_API_0100 data " + data);
265 266
        expect(true).assertTrue();
      }).catch(error => {
267
        console.info("====>SUB_systemTime_setDate_JS_API_0100 error " + error);
268 269 270 271 272 273 274 275 276 277 278 279 280 281
        expect().assertFail();
      });
      done();
    });

    /**
      * @tc.number    SUB_systemTime_setDate_JS_API_0200
      * @tc.name      Test systemTime.setDate Invalid value
      * @tc.desc      Test systemTime_setDate API functionality.
      * @tc.size      : MEDIUM
      * @tc.type      : Function
      * @tc.level     : Level 0
      */
    it('SUB_systemTime_setDate_JS_API_0200', 0, async function (done) {
282
      console.info("====>SUB_systemTime_setDate_JS_API_0200 start");
283
      systemTime.setDate(0).then(data => {
284
        console.info("====>SUB_systemTime_setDate_JS_API_0200 data " + data);
285 286
        expect(true).assertTrue();
      }).catch(error => {
287
        console.info("====>SUB_systemTime_setDate_JS_API_0200 error " + error);
288 289 290 291 292 293 294 295 296 297 298 299 300 301
        expect().assertFail();
      });
      done();
    });

    /**
      * @tc.number    SUB_systemTime_setDate_JS_API_0300
      * @tc.name      Test systemTime.setDate Invalid value
      * @tc.desc      Test systemTime_setDate API functionality.
      * @tc.size      : MEDIUM
      * @tc.type      : Function
      * @tc.level     : Level 0
      */
    it('SUB_systemTime_setDate_JS_API_0300', 0, async function (done) {
302
      console.info("====>SUB_systemTime_setDate_JS_API_0300 start");
303 304 305
      var data = new Date("October 13, 2020 11:13:00");
      systemTime.setDate(data, (error, data) => {
        if(error){
306
          console.info("====>SUB_systemTime_setDate_JS_API_0300 error " + error);
307 308
          expect().assertFail();
        }else{
309
          console.info("====>SUB_systemTime_setDate_JS_API_0300 data " + data);
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
          expect(true).assertTrue();
        };
      });
      done();
    });

    /**
      * @tc.number    SUB_systemTime_setTimezone_JS_API_0100
      * @tc.name      Test systemTime.setTimezone Invalid value
      * @tc.desc      Test systemTime_setTimezone API functionality.
      * @tc.size      : MEDIUM
      * @tc.type      : Function
      * @tc.level     : Level 0
      */
    it('SUB_systemTime_setTimezone_JS_API_0100', 0, async function (done) {
325
      console.info("====>SUB_systemTime_setTimezone_JS_API_0100 start");
326
      systemTime.setTimezone('Asia, Shanghai').then(data => {
327
        console.info("====>SUB_systemTime_setTimezone_JS_API_0100 data " + data);
328 329
        expect().assertFail();
      }).catch(error => {
330
        console.info("====>SUB_systemTime_setTimezone_JS_API_0100 error " + error);
331 332 333 334 335 336 337 338 339 340 341 342 343 344
        expect(true).assertTrue();
      });
      done();
    });

    /**
      * @tc.number    SUB_systemTime_setTimezone_JS_API_0200
      * @tc.name      Test systemTime.setTimezone Invalid value
      * @tc.desc      Test systemTime_setTimezone API functionality.
      * @tc.size      : MEDIUM
      * @tc.type      : Function
      * @tc.level     : Level 0
      */
    it('SUB_systemTime_setTimezone_JS_API_0200', 0, async function (done) {
345
      console.info("====>SUB_systemTime_setTimezone_JS_API_0200 start");
346
      systemTime.setTimezone('Beijing,China').then(data => {
347
        console.info("====>SUB_systemTime_setTimezone_JS_API_0200 data " + data);
348 349
        expect().assertFail();
      }).catch(error => {
350
        console.info("====>SUB_systemTime_setTimezone_JS_API_0200 error " + error);
351 352 353 354 355 356 357 358 359 360 361 362 363 364
        expect(true).assertTrue();
      });
      done();
    });

    /**
      * @tc.number    SUB_systemTime_setTimezone_JS_API_0300
      * @tc.name      Test systemTime.setTimezone Invalid value
      * @tc.desc      Test systemTime_setTimezone API functionality.
      * @tc.size      : MEDIUM
      * @tc.type      : Function
      * @tc.level     : Level 0
      */
    it('SUB_systemTime_setTimezone_JS_API_0300', 0, async function (done) {
365
      console.info("====>SUB_systemTime_setTimezone_JS_API_0300 start");
366
      systemTime.setTimezone('Baker Island, U.S.A.').then(data => {
367
        console.info("====>SUB_systemTime_setTimezone_JS_API_0300 data " + data);
368 369
        expect().assertFail();
      }).catch(error => {
370
        console.info("====>SUB_systemTime_setTimezone_JS_API_0300 error " + error);
371 372 373 374 375 376
        expect(true).assertTrue();
      });
      done();
    });
  })
}