Skip to content

Commit

Permalink
Merge a581871 into 0c24c8e
Browse files Browse the repository at this point in the history
  • Loading branch information
vrunoa committed May 7, 2017
2 parents 0c24c8e + a581871 commit 0aa902a
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 1 deletion.
63 changes: 63 additions & 0 deletions lib/tools/adb-emu-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@ import log from '../logger.js';
import _ from 'lodash';

const PHONE_NUMBER_PATTERN = /^[\+]?[(]?[0-9]*[)]?[-\s\.]?[0-9]*[-\s\.]?[0-9]{2,}$/im;
const GSM_CALL = 'call';
const GSM_ACCEPT = 'accept';
const GSM_CANCEL = 'cancel';
const GSM_HOLD = 'hold';
const GSM_CALL_ACTIONS = [
GSM_CALL, GSM_ACCEPT,
GSM_CANCEL, GSM_HOLD
];
const GSM_VOICE_UNREGISTERED = 'unregistered';
const GSM_VOICE_HOME = 'home';
const GSM_VOICE_ROAMING = 'roaming';
const GSM_VOICE_SEARCHING = 'searching';
const GSM_VOICE_DENIED = 'denied';
const GSM_VOICE_OFF = 'off';
const GSM_VOICE_ON = 'on';
const GSM_VOICE_STATES = [
GSM_VOICE_UNREGISTERED, GSM_VOICE_HOME,
GSM_VOICE_ROAMING, GSM_VOICE_SEARCHING,
GSM_VOICE_DENIED, GSM_VOICE_OFF,
GSM_VOICE_ON
];
const GSM_SIGNAL_STRENGTH = [0, 1, 2, 3, 4];
let emuMethods = {};

emuMethods.isEmulatorConnected = async function () {
Expand Down Expand Up @@ -43,4 +65,45 @@ emuMethods.sendSMS = async function (phoneNumber, message = '') {
await this.adbExecEmu(['sms', 'send', phoneNumber, message]);
};

emuMethods.gsmCall = async function (phoneNumber, action = '') {
if (GSM_CALL_ACTIONS.indexOf(action) === -1) {
log.errorAndThrow(`Invalid gsm action param ${action}`);
}
phoneNumber = `${phoneNumber}`.replace(/\s*/, "");
if (!PHONE_NUMBER_PATTERN.test(phoneNumber)) {
log.errorAndThrow(`Invalid phoneNumber param ${phoneNumber}`);
}
await this.adbExecEmu(['gsm', action, phoneNumber]);
};

emuMethods.gsmSignal = async function (strength = 4) {
if (!(strength in GSM_SIGNAL_STRENGTH)) {
log.errorAndThrow(`Invalid signal strength param ${strength}`);
}
log.info('gsm signal-profile <strength> changes the reported strength on next (15s) update.');
await this.adbExecEmu(['gsm', 'signal-profile', strength]);
};

emuMethods.gsmVoice = async function (state = 'on') {
// gsm voice <state> allows you to change the state of your GPRS connection
if (GSM_VOICE_STATES.indexOf(state) === -1) {
log.errorAndThrow(`Invalid gsm voice state param ${state}`);
}
await this.adbExecEmu(['gsm', 'voice', state]);
};

emuMethods.GSM_CALL = GSM_CALL;
emuMethods.GSM_ACCEPT = GSM_ACCEPT;
emuMethods.GSM_CANCEL = GSM_CANCEL;
emuMethods.GSM_HOLD = GSM_HOLD;
emuMethods.GSM_CALL_ACTIONS = GSM_CALL_ACTIONS;
emuMethods.GSM_VOICE_UNREGISTERED = GSM_VOICE_UNREGISTERED;
emuMethods.GSM_VOICE_HOME = GSM_VOICE_HOME;
emuMethods.GSM_VOICE_ROAMING = GSM_VOICE_ROAMING;
emuMethods.GSM_VOICE_SEARCHING = GSM_VOICE_SEARCHING;
emuMethods.GSM_VOICE_DENIED = GSM_VOICE_DENIED;
emuMethods.GSM_VOICE_OFF = GSM_VOICE_OFF;
emuMethods.GSM_VOICE_ON = GSM_VOICE_ON;
emuMethods.GSM_VOICE_STATES = GSM_VOICE_STATES;

export default emuMethods;
185 changes: 184 additions & 1 deletion test/unit/adb-emu-commands-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('adb emulator commands', () => {
});
}));
describe("sendSMS", withMocks({adb}, (mocks) => {
it("should throw exception on invalid phoneNumber", async () => {
it("should throw exception on invalid message", async () => {
await adb.sendSMS("+549341312345678").should.eventually.be.rejectedWith("Sending an SMS requires a message");
mocks.adb.verify();
});
Expand All @@ -109,5 +109,188 @@ describe('adb emulator commands', () => {
mocks.adb.verify();
});
}));
describe("gsm signal method", withMocks({adb}, (mocks) => {
it("should throw exception on invalid strength", async () => {
await adb.gsmSignal(5).should.eventually.be.rejectedWith("Invalid signal strength");
mocks.adb.verify();
});
it("should call adbExecEmu with the correct args", async () => {
let signalStrength = 0;
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", 'signal-profile', signalStrength])
.returns();
await adb.gsmSignal(signalStrength);
mocks.adb.verify();
});
}));
describe("gsm call methods", withMocks({adb}, (mocks) => {
it("should throw exception on invalid action", async () => {
await adb.gsmCall("+549341312345678").should.eventually.be.rejectedWith("Invalid gsm action");
mocks.adb.verify();
});
it("should throw exception on invalid phoneNumber", async () => {
await adb.gsmCall("+5493413a12345678", "call").should.eventually.be.rejectedWith("Invalid phoneNumber");
mocks.adb.verify();
});
it("should set the correct method for making gsm call", async () => {
let phoneNumber = 4509;
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", adb.GSM_CALL, "4509"])
.returns();
await adb.gsmCall(phoneNumber, "call");
mocks.adb.verify();
});
it("should set the correct method for accepting gsm call", async () => {
let phoneNumber = 4509;
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", adb.GSM_ACCEPT, "4509"])
.returns();
await adb.gsmCall(phoneNumber, "accept");
mocks.adb.verify();
});
it("should set the correct method for refusing gsm call", async () => {
let phoneNumber = 4509;
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", adb.GSM_CANCEL, "4509"])
.returns();
await adb.gsmCall(phoneNumber, "cancel");
mocks.adb.verify();
});
it("should set the correct method for holding gsm call", async () => {
let phoneNumber = 4509;
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", adb.GSM_HOLD, "4509"])
.returns();
await adb.gsmCall(phoneNumber, "hold");
mocks.adb.verify();
});
}));
describe("gsm voice method", withMocks({adb}, (mocks) => {
it("should throw exception on invalid strength", async () => {
await adb.gsmVoice('weird').should.eventually.be.rejectedWith("Invalid gsm voice state");
mocks.adb.verify();
});
it("should set gsm voice to unregistered", async () => {
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", "voice", adb.GSM_VOICE_UNREGISTERED])
.returns();
await adb.gsmVoice("unregistered");
mocks.adb.verify();
});
it("should set gsm voice to home", async () => {
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", "voice", adb.GSM_VOICE_HOME])
.returns();
await adb.gsmVoice("home");
mocks.adb.verify();
});
it("should set gsm voice to roaming", async () => {
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", "voice", adb.GSM_VOICE_ROAMING])
.returns();
await adb.gsmVoice("roaming");
mocks.adb.verify();
});
it("should set gsm voice to searching", async () => {
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", "voice", adb.GSM_VOICE_SEARCHING])
.returns();
await adb.gsmVoice("searching");
mocks.adb.verify();
});
it("should set gsm voice to denied", async () => {
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", "voice", adb.GSM_VOICE_DENIED])
.returns();
await adb.gsmVoice("denied");
mocks.adb.verify();
});
it("should set gsm voice to off", async () => {
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", "voice", adb.GSM_VOICE_OFF])
.returns();
await adb.gsmVoice("off");
mocks.adb.verify();
});
it("should set gsm voice to on", async () => {
mocks.adb.expects("isEmulatorConnected")
.once().withExactArgs()
.returns(true);
mocks.adb.expects("resetTelnetAuthToken")
.once().withExactArgs()
.returns();
mocks.adb.expects("adbExec")
.once().withExactArgs(["emu", "gsm", "voice", adb.GSM_VOICE_ON])
.returns();
await adb.gsmVoice("on");
mocks.adb.verify();
});
}));
});
});

0 comments on commit 0aa902a

Please sign in to comment.