Skip to content

Commit

Permalink
sped up emulate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
filmaj committed Jan 31, 2013
1 parent 2cfdf55 commit 8296eda
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 211 deletions.
26 changes: 11 additions & 15 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,20 @@ platforms.forEach(function(platform) {
console.error('ERROR! Could not create a native ' + platform + ' project test fixture. See below for error output.');
console.error(output);
} else {
// set permissions on executables
switch (platform) {
case 'android':
break;
case 'ios':
var scripts_path = path.join(fix_path, 'cordova');
var scripts = fs.readdirSync(scripts_path);
scripts.forEach(function(script) {
var script_path = path.join(scripts_path, script);
shell.chmod('+x', script_path);
});
break;
case 'blackberry':
break;
};
// copy over to full cordova project test fixture
var platformDir = path.join(platformsDir, platform);
shell.mkdir('-p', platformDir);
shell.cp('-rf', path.join(fix_path, '*'), platformDir);
// set permissions on executables
var scripts_path = path.join(fix_path, 'cordova');
var other_path = path.join(platformDir, 'cordova');
var scripts = fs.readdirSync(scripts_path);
scripts.forEach(function(script) {
var script_path = path.join(scripts_path, script);
var other_script_path = path.join(other_path, script);
shell.chmod('+x', script_path);
shell.chmod('+x', other_script_path);
});
console.log('SUCCESS: ' + platform + ' ready to rock!');
}
});
Expand Down
244 changes: 63 additions & 181 deletions spec/emulate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ var cordova = require('../cordova'),
ios_parser = require('../src/metadata/ios_parser'),
blackberry_parser = require('../src/metadata/blackberry_parser'),
hooker = require('../src/hooker'),
tempDir = path.join(__dirname, '..', 'temp');
fixtures = path.join(__dirname, 'fixtures'),
hooks = path.join(fixtures, 'hooks'),
tempDir = path.join(__dirname, '..', 'temp'),
cordova_project = path.join(fixtures, 'projects', 'cordova');

var cwd = process.cwd();

xdescribe('emulate command', function() {
describe('emulate command', function() {
beforeEach(function() {
// Make a temp directory
shell.rm('-rf', tempDir);
shell.mkdir('-p', tempDir);
});
Expand All @@ -50,237 +52,109 @@ xdescribe('emulate command', function() {
});

it('should run inside a Cordova-based project with at least one added platform', function() {
shell.mv('-f', path.join(cordova_project, 'platforms', 'blackberry'), path.join(tempDir));
shell.mv('-f', path.join(cordova_project, 'platforms', 'ios'), path.join(tempDir));
this.after(function() {
process.chdir(cwd);
shell.mv('-f', path.join(tempDir, 'blackberry'), path.join(cordova_project, 'platforms', 'blackberry'));
shell.mv('-f', path.join(tempDir, 'ios'), path.join(cordova_project, 'platforms', 'ios'));
});

var cb = jasmine.createSpy();
var cbem = jasmine.createSpy();
var s;
process.chdir(cordova_project);

runs(function() {
cordova.create(tempDir);
process.chdir(tempDir);
cordova.platform('add', 'ios', cb);
});
waitsFor(function() { return cb.wasCalled; }, 'platform add ios');

runs(function() {
s = spyOn(shell, 'exec').andReturn({code:0});
expect(function() {
cordova.emulate(cbem);
}).not.toThrow();
});
waitsFor(function() { return cbem.wasCalled; }, 'ios emulate');

runs(function() {
var s = spyOn(shell, 'exec');
spyOn(android_parser.prototype, 'update_project');
expect(function() {
cordova.emulate();
expect(s).toHaveBeenCalled();
});
}).not.toThrow();
});

it('should not run outside of a Cordova-based project', function() {
this.after(function() {
process.chdir(cwd);
});

shell.mkdir('-p', tempDir);
process.chdir(tempDir);

expect(function() {
cordova.emulate();
}).toThrow();
});

describe('per platform', function() {
beforeEach(function() {
cordova.create(tempDir);
process.chdir(tempDir);
process.chdir(cordova_project);
});

afterEach(function() {
process.chdir(cwd);
});

describe('Android', function() {
var s;
beforeEach(function() {
cordova.platform('add', 'android');
s = spyOn(require('shelljs'), 'exec');
});

it('should shell out to run command on Android', function() {
var s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
cordova.emulate();
expect(s.mostRecentCall.args[0].match(/android\/cordova\/run/)).not.toBeNull();
cordova.emulate('android');
expect(s.mostRecentCall.args[0].match(/\/cordova\/run/)).not.toBeNull();
});
it('should call android_parser\'s update_project', function() {
spyOn(require('shelljs'), 'exec').andReturn({code:0});
var s = spyOn(android_parser.prototype, 'update_project');
cordova.emulate();
expect(s).toHaveBeenCalled();
var spy = spyOn(android_parser.prototype, 'update_project');
cordova.emulate('android');
expect(spy).toHaveBeenCalled();
});
});
describe('iOS', function() {
it('should shell out to emulate command on iOS', function() {
var cb = jasmine.createSpy();
var buildcb = jasmine.createSpy();
var s;

runs(function() {
cordova.platform('add', 'ios', cb);
});
waitsFor(function() { return cb.wasCalled; }, 'platform add ios callback');
runs(function() {
s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
cordova.emulate(buildcb);
});
waitsFor(function() { return buildcb.wasCalled; }, 'emulate ios');
runs(function() {
expect(s).toHaveBeenCalled();
expect(s.mostRecentCall.args[0].match(/ios\/cordova\/emulate/)).not.toBeNull();
});
var s = spyOn(require('shelljs'), 'exec');
var proj_spy = spyOn(ios_parser.prototype, 'update_project');
cordova.emulate('ios');
proj_spy.mostRecentCall.args[1]();
expect(s).toHaveBeenCalled();
expect(s.mostRecentCall.args[0].match(/\/cordova\/emulate/)).not.toBeNull();
});
it('should call ios_parser\'s update_project', function() {
var cb = jasmine.createSpy();
var buildcb = jasmine.createSpy();
var s;

runs(function() {
cordova.platform('add', 'ios', cb);
});
waitsFor(function() { return cb.wasCalled; }, 'platform add ios callback');
runs(function() {
s = spyOn(ios_parser.prototype, 'update_project');
cordova.emulate(buildcb);
expect(s).toHaveBeenCalled();
});
var s = spyOn(ios_parser.prototype, 'update_project');
cordova.emulate('ios');
expect(s).toHaveBeenCalled();
});
});
describe('BlackBerry', function() {
it('should shell out to ant command on blackberry', function() {
var buildcb = jasmine.createSpy();
var cb = jasmine.createSpy();
var s, t = spyOn(require('prompt'), 'get').andReturn(true);

runs(function() {
cordova.platform('add', 'blackberry', cb);
// Fake prompt invoking its callback
t.mostRecentCall.args[1](null, {});
});
waitsFor(function() { return cb.wasCalled; }, 'platform add blackberry');
runs(function() {
s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
cordova.emulate(buildcb);
});
waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000);
runs(function() {
expect(s).toHaveBeenCalled();
expect(s.mostRecentCall.args[0].match(/ant -f .*build\.xml qnx load-simulator/)).not.toBeNull();
});
var proj_spy = spyOn(blackberry_parser.prototype, 'update_project');
var s = spyOn(require('shelljs'), 'exec');
cordova.emulate('blackberry');
proj_spy.mostRecentCall.args[1](); // update_project fake
expect(s).toHaveBeenCalled();
expect(s.mostRecentCall.args[0]).toMatch(/ant -f .*build\.xml" qnx load-simulator/);
});
it('should call blackberry_parser\'s update_project', function() {
var cb = jasmine.createSpy();
var buildcb = jasmine.createSpy();
var s;

runs(function() {
var p = spyOn(require('prompt'), 'get');
cordova.platform('add', 'blackberry', cb);
p.mostRecentCall.args[1](null, {});
});
waitsFor(function() { return cb.wasCalled; }, 'platform add bb');
runs(function() {
s = spyOn(blackberry_parser.prototype, 'update_project');
cordova.emulate(buildcb);
expect(s).toHaveBeenCalled();
});
});
});
});
describe('specifying platforms to emulate', function() {
beforeEach(function() {
cordova.create(tempDir);
process.chdir(tempDir);
cordova.platform('add', 'android');
});

afterEach(function() {
process.chdir(cwd);
shell.rm('-rf', tempDir);
});
it('should only emulate the specified platform (array notation)', function() {
var cb = jasmine.createSpy();
var buildcb = jasmine.createSpy();
var s;
runs(function() {
cordova.platform('add', 'ios', cb);
});
waitsFor(function() { return cb.wasCalled; }, 'platform add ios');
runs(function() {
s = spyOn(shell, 'exec').andReturn({code:0});
cordova.build(['android'], buildcb);
});
waitsFor(function() { return buildcb.wasCalled; }, 'emulate android');
runs(function() {
expect(s.callCount).toEqual(1);
});
});
it('should only emulate the specified platform (string notation)', function() {
var cb = jasmine.createSpy();
var buildcb = jasmine.createSpy();
var s;
runs(function() {
cordova.platform('add', 'ios', cb);
});
waitsFor(function() { return cb.wasCalled; }, 'platform add ios');
runs(function() {
s = spyOn(shell, 'exec').andReturn({code:0});
cordova.build('android', buildcb);
});
waitsFor(function() { return buildcb.wasCalled; }, 'emulate android');
runs(function() {
expect(s.callCount).toEqual(1);
});
});
it('should handle multiple platforms to be emulated', function() {
var cb = jasmine.createSpy();
var bbcb = jasmine.createSpy();
var buildcb = jasmine.createSpy();
var s;
runs(function() {
cordova.platform('add', 'ios', cb);
});
waitsFor(function() { return cb.wasCalled; }, 'platform add ios');
runs(function() {
var g = spyOn(require('prompt'), 'get');
cordova.platform('add', 'blackberry', bbcb);
g.mostRecentCall.args[1](null, {}); // fake out prompt io
});
waitsFor(function() { return bbcb.wasCalled; }, 'platform add bb');
runs(function() {
s = spyOn(shell, 'exec').andReturn({code:0});
cordova.build(['android','ios'], buildcb);
});
waitsFor(function() { return buildcb.wasCalled; }, 'emulate android+ios');
runs(function() {
expect(s.callCount).toEqual(2);
var s = spyOn(blackberry_parser.prototype, 'update_project');
cordova.emulate('blackberry');
expect(s).toHaveBeenCalled();
});
});
});

describe('hooks', function() {
var s;
var s, sh, ap;
beforeEach(function() {
cordova.create(tempDir);
process.chdir(tempDir);
s = spyOn(hooker.prototype, 'fire').andReturn(true);
});
afterEach(function() {
process.chdir(cwd);
shell.rm('-rf', tempDir);
});

describe('when platforms are added', function() {
beforeEach(function() {
cordova.platform('add', 'android');
spyOn(shell, 'exec').andReturn({code:0});
shell.mv('-f', path.join(cordova_project, 'platforms', 'blackberry'), path.join(tempDir));
shell.mv('-f', path.join(cordova_project, 'platforms', 'ios'), path.join(tempDir));
sh = spyOn(shell, 'exec');
ap = spyOn(android_parser.prototype, 'update_project');
process.chdir(cordova_project);
});
afterEach(function() {
shell.mv('-f', path.join(tempDir, 'blackberry'), path.join(cordova_project, 'platforms', 'blackberry'));
shell.mv('-f', path.join(tempDir, 'ios'), path.join(cordova_project, 'platforms', 'ios'));
process.chdir(cwd);
});

it('should fire before hooks through the hooker module', function() {
Expand All @@ -289,13 +163,21 @@ xdescribe('emulate command', function() {
});
it('should fire after hooks through the hooker module', function() {
cordova.emulate();
sh.mostRecentCall.args[2](0); //fake shell call
expect(s).toHaveBeenCalledWith('after_emulate');
});
});

describe('with no platforms added', function() {
beforeEach(function() {
cordova.create(tempDir);
process.chdir(tempDir);
});
afterEach(function() {
process.chdir(cwd);
});
it('should not fire the hooker', function() {
spyOn(shell, 'exec').andReturn({code:0});
spyOn(shell, 'exec');
expect(function() {
cordova.emulate();
}).toThrow();
Expand Down
Loading

0 comments on commit 8296eda

Please sign in to comment.