Skip to content

Commit

Permalink
add unit tests for device extraction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jlipps committed Mar 4, 2014
1 parent e6e4788 commit 7c2593c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
14 changes: 4 additions & 10 deletions lib/appium.js
Expand Up @@ -19,7 +19,6 @@ var DT_IOS = "ios"
, DT_ANDROID = "android"
, DT_CHROME = "chrome"
, DT_SELENDROID = "selendroid"
, DT_MOCK_IOS = "mock_ios"
, DT_FIREFOX_OS = "firefoxos";

var Appium = function (args) {
Expand Down Expand Up @@ -89,11 +88,10 @@ Appium.prototype.start = function (desiredCaps, cb) {

Appium.prototype.getDeviceType = function (args, caps) {
var type = this.getDeviceTypeFromArgs(args) ||
this.getDeviceTypeFromDeviceCap(caps.device) ||
this.getDeviceTypeFromApp(args.app || caps.app) ||
this.getDeviceTypeFromPackage(args.androidPackage ||
caps['app-package']) ||
this.getDeviceTypeFromDeviceCap(caps.device) ||
(args.avd ? DT_ANDROID : false);
caps['app-package']);
if (type) return type;
throw new Error("Could not determine your device from Appium arguments " +
"or desired capabilities. Please make sure to specify the " +
Expand All @@ -113,17 +111,13 @@ Appium.prototype.getDeviceTypeFromDeviceCap = function (device) {
return DT_FIREFOX_OS;
} else if (device.toLowerCase().indexOf('android') !== -1) {
return DT_ANDROID;
} else if (device === DT_MOCK_IOS) {
return DT_MOCK_IOS;
} else {
throw new Error("A valid device type is required in the capabilities list");
}
};

Appium.prototype.getDeviceTypeFromApp = function (app) {
if (/\.app/.test(app)) {
if (/\.app$/.test(app) || /\.app\.zip$/.test(app)) {
return DT_IOS;
} else if (/\.apk/.test(app)) {
} else if (/\.apk$/.test(app) || /\.apk\.zip$/.test(app)) {
return DT_ANDROID;
} else if ((app && app.toLowerCase() === "safari")) {
return DT_SAFARI;
Expand Down
62 changes: 62 additions & 0 deletions test/unit/configuration-specs.js
@@ -0,0 +1,62 @@
"use strict";

var getAppium = require('../../lib/appium')
, chai = require('chai')
, should = chai.should()
, _ = require('underscore')
, IOS = require('../../lib/devices/ios/ios.js')
, Safari = require('../../lib/devices/ios/safari.js')
, Android = require('../../lib/devices/android/android.js')
, Chrome = require('../../lib/devices/android/chrome.js')
, Selendroid = require('../../lib/devices/android/selendroid.js');


describe('Appium', function () {
describe('#getDeviceType', function () {
// test is [args, caps, device]
var appium = getAppium({});
var happyTests = [
[{ipa: '/path/to/my.ipa'}, {}, 'ios']
, [{ipa: '/path/to/my.ipa', safari: true}, {}, 'ios']
, [{safari: true}, {}, 'safari']
, [{safari: true}, {app: '/path/to/my.apk'}, 'safari']
, [{safari: false}, {app: 'safari'}, 'safari']
, [{}, {app: 'settings'}, 'ios']
, [{}, {app: 'chrome'}, 'chrome']
, [{}, {app: 'chromium'}, 'chrome']
, [{}, {app: 'browser'}, 'chrome']
, [{}, {app: 'http://www.site.com/my.app.zip'}, 'ios']
, [{}, {app: 'http://www.site.com/my.apk.zip'}, 'android']
, [{}, {app: 'http://www.site.com/my.apk'}, 'android']
, [{}, {app: '/path/to/my.app'}, 'ios']
, [{}, {app: '/path/to/my.apk'}, 'android']
, [{}, {app: '/path/to/my.apk.app'}, 'ios']
, [{}, {app: '/path/to/my.app.apk'}, 'android']
, [{}, {app: '/path/to/my.app', device: 'Android'}, 'android']
, [{app: '/path/to/my.app'}, {device: 'Android'}, 'android']
, [{}, {device: 'iPhone Simulator'}, 'ios']
, [{}, {device: 'iPhone'}, 'ios']
, [{}, {device: 'ipad'}, 'ios']
, [{}, {device: 'iPad Simulator'}, 'ios']
, [{}, {device: 'Selendroid'}, 'selendroid']
, [{}, {device: 'Android'}, 'android']
, [{}, {device: 'FirefoxOS'}, 'firefoxos']
, [{}, {device: 'firefox'}, 'firefoxos']
, [{}, {device: 'firefox', 'app-package': 'com.android.chrome'}, 'firefoxos']
, [{}, {'app-package': 'com.android.chrome'}, 'chrome']
, [{}, {device: 'iphone', 'app-package': 'lol'}, 'ios']
, [{}, {'app-package': 'lol'}, 'android']
, [{androidPackage: 'com.foo'}, {}, 'android']
, [{androidPackage: 'com.android.browser'}, {}, 'chrome']
];
_.each(happyTests, function (test) {
it('should turn ' + JSON.stringify(test[0]) + ' args and ' + JSON.stringify(test[1]) + ' caps into ' + test[2] + ' device', function () {
appium.getDeviceType(test[0], test[1]).should.equal(test[2]);
});
});
});
});

describe('IOS', function () {
});

2 changes: 0 additions & 2 deletions test/unit/queue-specs.js
Expand Up @@ -67,7 +67,6 @@ describe('Appium', function () {
var loop = function (num) {
if (num > 9)
return;

appium.start({app: "/path/to/fake.app", device: "iPhone"}, function (err) {
var n = num;
if (n === 0) {
Expand All @@ -84,7 +83,6 @@ describe('Appium', function () {
loop(++num);
});
};

loop(0);
});
});
Expand Down

0 comments on commit 7c2593c

Please sign in to comment.