BtSetLocalName.test.js 16.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * 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.
 */

import bluetooth from '@ohos.bluetooth';
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
let Btname = {
    NUM_TEST :'012345678901234567890123456789012345678901234567890123'+
    '45678901234567890123456789012345678901234567890123456789012345678901234567',
    NUM_TEST1 :'0123456789012345678901234567890123456789012345678901'
    +'23456789012345678901234567890123456789012345678901234567890123456789012345678012'
    +'345678901234567890123456789012345678901234567890123456789012367890123456789012345568'
    +'01234567890123456789012345678912',
    LETTERS_TEST :'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
    CHINESES_TEST :'测试蓝牙名称是否正常测试蓝牙名称是否试蓝牙',
    CHINESES_TEST2 :'测试蓝牙名称正常',
    SYMBOL_TEST:'*^_^* 、。·ˉˇ¨〃々—~‖·‘’“”「『』〖❂【±×'
    +'÷∶∧∨∑∏∪∩∈∷√⊥‖∠⌒⊙∫∮≡≌≈∽∝≠♂♀°℃$¤¢£‰§№☆★○●◎◇□■△※→←↑↓〓',
    MIXES:'测试蓝牙名称是否正试蓝牙\'名称是否[666]aaw',
    MIXES2:'——◎◇◆□■△▲测试蓝牙',
    MIXES3:'78453-、、。。◎◇◆□■△▲',
    MIXES4:'hhhfdf-、、。。◎◇◆□■△▲',
    MIXES5:'#01-5@<?;:!@$%^&*(1[]{【+-;:‘’“”测试=》《\'[6]',
    MIXES6:'#01-567890@<>?/;:5675ASDF012345678!@'
    +'$%^&*()9012378901[]{}【】566~·67890blue',
    MIXES7:'0123456789012345678901234567890123456789012345678901'
    +'23456789012345678901234567890123456789012345678901234567890123456789012345678012'
    +'345678901234567890123456789012345678901234567890123456789012367890123456789012345568'
    +'012345678901234567890123456789123'
}
Q
quanli 已提交
42 43
export default function btLocalNameTest() {
describe('btLocalNameTest', function() {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    function sleep(delay) {
        return new Promise(resovle => setTimeout(resovle, delay))
    }
    async function tryToEnableBt() {
        let sta = bluetooth.getState();
        switch(sta){
            case 0:
                bluetooth.enableBluetooth();
                await sleep(10000);
                let sta1 = bluetooth.getState();
                console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta1));
                break;
            case 1:
                console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta));
                await sleep(3000);
                break;
            case 2:
                console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta));
                break;
            case 3:
                bluetooth.enableBluetooth();
                await sleep(10000);
                let sta2 = bluetooth.getState();
                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta2));
                break;
            default:
                console.info('[bluetooth_js] enable success');
        }
    }
    beforeAll(function () {
        console.info('beforeAll called')
    })
    beforeEach(async function(done) {
        console.info('beforeEach called')
        await tryToEnableBt()
        done()
    })
    afterEach(function () {
        console.info('afterEach called')
    })
    afterAll(function () {
        console.info('afterAll called')
    })

    /**
Q
quanli 已提交
89
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0100
90 91 92 93 94
     * @tc.name setLocalName
     * @tc.desc Test setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 1 
     */
Q
quanli 已提交
95
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0100', 0, async function (done) {
96 97 98 99 100 101 102 103 104 105
        let result = bluetooth.setLocalName(Btname.LETTERS_TEST);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0100 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.LETTERS_TEST == getNewName);
        done();
    })

    /**
Q
quanli 已提交
106
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0200
107 108 109 110 111
     * @tc.name setLocalName
     * @tc.desc Test setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
112
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0200', 0, async function (done) {
113 114 115 116 117 118 119 120 121 122
        let result = bluetooth.setLocalName(Btname.CHINESES_TEST2);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0200 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.CHINESES_TEST2 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
123
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0300
124 125 126 127 128
     * @tc.name setLocalName
     * @tc.desc Test setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
129
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0300', 0, async function (done) {
130 131 132 133 134 135 136 137 138 139
        let result = bluetooth.setLocalName(Btname.NUM_TEST);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0300 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.NUM_TEST == getNewName);
        done();
    })

   /**
Q
quanli 已提交
140
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0400
141 142 143 144 145
     * @tc.name setLocalName
     * @tc.desc Test setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 1
     */
Q
quanli 已提交
146
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0400', 0, async function (done) {
147 148 149 150 151 152 153 154 155 156
        let result = bluetooth.setLocalName(Btname.SYMBOL_TEST);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0400 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.SYMBOL_TEST == getNewName);
        done();
    })

    /**
Q
quanli 已提交
157
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0500
158 159 160 161 162
     * @tc.name setLocalName
     * @tc.desc Test setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
163
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0500', 0, async function (done) {
164 165 166 167 168 169 170 171 172 173 174
        let newName = 'my bluetooth';
        let result = bluetooth.setLocalName(newName);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0500 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(newName == getNewName);
        done();
    })

    /**
Q
quanli 已提交
175
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0600
176 177 178 179 180
     * @tc.name setLocalName
     * @tc.desc Test setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
181
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0600', 0, async function (done) {
182 183 184 185 186 187 188 189 190 191 192
        let newName = 'bluetooth1234ABCDEFGH';
        let result = bluetooth.setLocalName(newName);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0600 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(newName == getNewName);
        done();
    })

    /**
Q
quanli 已提交
193
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0700
194 195 196 197 198
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
199
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0700', 0, async function (done) {
200 201 202 203 204 205 206 207 208 209 210
        let newName = '蓝牙设备bluetooth';
        let result = bluetooth.setLocalName(newName);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0700 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(newName == getNewName);
        done();
    })

    /**
Q
quanli 已提交
211
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0800
212 213 214 215 216
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
217
     it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0800', 0, async function (done) {
218 219 220 221 222 223 224 225 226 227
        let result = bluetooth.setLocalName(Btname.MIXES4);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0800 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.MIXES4 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
228
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0900
229 230 231 232 233
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
234
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_0900', 0, async function (done) {
235 236 237 238 239 240 241 242 243 244
        let result = bluetooth.setLocalName(Btname.MIXES2);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_0900 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.MIXES2 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
245
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1000
246 247 248 249 250
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
251
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1000', 0, async function (done) {
252 253 254 255 256 257 258 259 260 261
        let result = bluetooth.setLocalName(Btname.MIXES3);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1000 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.MIXES3 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
262
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1100
263 264 265 266 267
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
268
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1100', 0, async function (done) {
269 270 271 272 273 274 275 276 277 278 279
        let newName = '蓝牙设备123';
        let result = bluetooth.setLocalName(newName);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1100 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(newName == getNewName);
        done();
    })

    /**
Q
quanli 已提交
280
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1200
281 282 283 284 285
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
286
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1200', 0, async function (done) {
287 288 289 290 291 292 293 294 295 296 297
        let newName = '蓝牙设备bluetooth12';
        let result = bluetooth.setLocalName(newName);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName1200 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(newName == getNewName);
        done();
    })

    /**
Q
quanli 已提交
298
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1300
299 300 301 302 303
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
304
      it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1300', 0, async function (done) {
305 306 307 308 309 310 311 312 313 314
        let result = bluetooth.setLocalName(Btname.MIXES6);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1300 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.MIXES6 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
315
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1400
316 317 318 319 320
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
321
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1400', 0, async function (done) {
322 323 324 325 326 327 328 329 330 331 332
        let result = bluetooth.setLocalName(Btname.MIXES);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1400 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.MIXES == getNewName);
        
        done();
    })

    /**
Q
quanli 已提交
333
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1500
334 335 336 337 338
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
339
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1500', 0, async function (done) {
340 341 342 343 344 345 346 347 348 349
        let result = bluetooth.setLocalName(Btname.MIXES5);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1500 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.MIXES5 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
350
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1600
351 352 353 354 355
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
356
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1600', 0, async function (done) {
357 358 359 360 361 362 363 364 365 366
        let result = bluetooth.setLocalName(Btname.NUM_TEST1);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1600 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(Btname.NUM_TEST1 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
367
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1700
368 369 370 371 372
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
373
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1700', 0, async function (done) {
374 375 376 377 378 379 380 381 382 383
        let result = bluetooth.setLocalName(Btname.MIXES7);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1700 NewName = '+ JSON.stringify(getNewName));
        expect(false).assertEqual(Btname.MIXES7 == getNewName);
        done();
    })

    /**
Q
quanli 已提交
384
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1800
385 386 387 388 389
     * @tc.name TEST setLocalName
     * @tc.desc TEST setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
390
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1800', 0, async function (done) {
391 392 393 394 395 396 397 398 399 400 401
        let name = bluetooth.getLocalName();
        let set = bluetooth.setLocalName('');
        expect(set).assertFalse();
        await sleep(1000);
        let localName = bluetooth.getLocalName();
        expect(true).assertTrue(localName==name);
        console.info('[bluetooth_js] getLocalName1800=' + JSON.stringify(localName));
        done();
    })

    /**
Q
quanli 已提交
402
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1900
403 404 405 406 407
     * @tc.name set LocalName
     * @tc.desc Test setLocalName api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
408
    it('SUB_COMMUNICATION_BLUETOOTH_LOCALNAME_1900', 0, async function (done) {
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
        let localName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1900 localName = '+ JSON.stringify(localName));
        expect(true).assertEqual(localName!=null);
        let newName = 'bluetoothtest';
        let result = bluetooth.setLocalName(newName);
        expect(result).assertTrue();
        await sleep(1000);
        let getNewName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalName_1900 NewName = '+ JSON.stringify(getNewName));
        expect(true).assertEqual(newName == getNewName);
        let result1=bluetooth.setLocalName(localName);
        expect(result1).assertTrue();
        await sleep(1000);
        let getLocalName = bluetooth.getLocalName();
        console.info('[bluetooth_js] LocalNam1900 localName ='+ JSON.stringify(getLocalName));
        expect(true).assertEqual(localName == getLocalName);
        done();
    })

})
Q
quanli 已提交
429
}