|
| 1 | +import { expect } from 'chai' |
| 2 | +import sinon from 'sinon' |
| 3 | +import Appium from '../../../lib/helper/Appium.js' |
| 4 | + |
| 5 | +function createApp() { |
| 6 | + const app = new Appium({ |
| 7 | + platform: 'Android', |
| 8 | + desiredCapabilities: { |
| 9 | + platformName: 'Android', |
| 10 | + }, |
| 11 | + }) |
| 12 | + app.browser = { execute: sinon.stub() } |
| 13 | + return app |
| 14 | +} |
| 15 | + |
| 16 | +describe('Appium #setNetworkConnection, #grabNetworkConnection', () => { |
| 17 | + it('should call mobile: setConnectivity with airplane mode only for value 1', async () => { |
| 18 | + const app = createApp() |
| 19 | + await app.setNetworkConnection(1) |
| 20 | + expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: true, wifi: false, data: false })).to.be.true |
| 21 | + }) |
| 22 | + |
| 23 | + it('should call mobile: setConnectivity with wifi only for value 2', async () => { |
| 24 | + const app = createApp() |
| 25 | + await app.setNetworkConnection(2) |
| 26 | + expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: true, data: false })).to.be.true |
| 27 | + }) |
| 28 | + |
| 29 | + it('should call mobile: setConnectivity with data only for value 4', async () => { |
| 30 | + const app = createApp() |
| 31 | + await app.setNetworkConnection(4) |
| 32 | + expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: false, data: true })).to.be.true |
| 33 | + }) |
| 34 | + |
| 35 | + it('should call mobile: setConnectivity with wifi and data for value 6', async () => { |
| 36 | + const app = createApp() |
| 37 | + await app.setNetworkConnection(6) |
| 38 | + expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: true, data: true })).to.be.true |
| 39 | + }) |
| 40 | + |
| 41 | + it('should call mobile: setConnectivity with everything off for value 0', async () => { |
| 42 | + const app = createApp() |
| 43 | + await app.setNetworkConnection(0) |
| 44 | + expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: false, data: false })).to.be.true |
| 45 | + }) |
| 46 | + |
| 47 | + it('should grab network connection using mobile: getConnectivity and map to legacy bitmask shape', async () => { |
| 48 | + const app = createApp() |
| 49 | + app.browser.execute.resolves({ airplaneMode: false, wifi: false, data: true }) |
| 50 | + const val = await app.grabNetworkConnection() |
| 51 | + expect(app.browser.execute.calledWith('mobile: getConnectivity')).to.be.true |
| 52 | + expect(val.value).to.equal(4) |
| 53 | + expect(val.inAirplaneMode).to.equal(false) |
| 54 | + expect(val.hasWifi).to.equal(false) |
| 55 | + expect(val.hasData).to.equal(true) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should grab network connection with wifi and airplane mode combined', async () => { |
| 59 | + const app = createApp() |
| 60 | + app.browser.execute.resolves({ airplaneMode: true, wifi: true, data: false }) |
| 61 | + const val = await app.grabNetworkConnection() |
| 62 | + expect(val.value).to.equal(3) |
| 63 | + expect(val.inAirplaneMode).to.equal(true) |
| 64 | + expect(val.hasWifi).to.equal(true) |
| 65 | + expect(val.hasData).to.equal(false) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should throw when used outside android platform', async () => { |
| 69 | + const app = new Appium({ |
| 70 | + platform: 'iOS', |
| 71 | + desiredCapabilities: { |
| 72 | + platformName: 'iOS', |
| 73 | + }, |
| 74 | + }) |
| 75 | + app.browser = { execute: sinon.stub() } |
| 76 | + let error |
| 77 | + try { |
| 78 | + await app.setNetworkConnection(1) |
| 79 | + } catch (err) { |
| 80 | + error = err |
| 81 | + } |
| 82 | + expect(error).to.be.instanceOf(Error) |
| 83 | + }) |
| 84 | +}) |
0 commit comments