Skip to content

Commit

Permalink
Bug 1003788 - [marionette-apps] switchToApp method doesn't wait for t…
Browse files Browse the repository at this point in the history
…he app is rendered on screen

- Wait for the transition-state of app's frame as opened.
- Rmove the async drvier support.
  • Loading branch information
evanxd committed May 15, 2014
1 parent bfeafcf commit 6d7e702
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 53 deletions.
13 changes: 3 additions & 10 deletions Makefile
Expand Up @@ -13,15 +13,8 @@ b2g:
lint:
gjslint --recurse . \
--disable "210,217,220,225,0212" \
--exclude_directories "b2g,examples,node_modules"

.PHONY: test-sync
test-sync:
SYNC=true ./node_modules/.bin/marionette-mocha $(MOCHA_OPTS)

.PHONY: test-async
test-async:
./node_modules/.bin/marionette-mocha $(MOCHA_OPTS)
--exclude_directories "b2g,examples,node_modules"

.PHONY: test
test: b2g test-sync test-async
test: b2g
SYNC=true ./node_modules/.bin/marionette-mocha $(MOCHA_OPTS)
24 changes: 14 additions & 10 deletions lib/launch.js
Expand Up @@ -4,14 +4,6 @@ var getApp = require('./getapp').getApp;
var url = require('url');
var fsPath = require('path');


/**
* Origin for the homescreen app.
* TODO: pull this from settings rather than hardcode.
* @const {string}
*/
var HOMESCREEN_ORIGIN = 'homescreen.gaiamobile.org';

/**
* Launch an application based on its origin and optionally entrypoint.
* Will wait until app's iframe is visible before firing callback.
Expand All @@ -33,8 +25,20 @@ function launch(apps, origin, entrypoint, callback) {

callback = callback || apps._client.defaultCallback;

// wait for homescreen before launching
waitForApp(apps, HOMESCREEN_ORIGIN);
// Wait for Homescreen app is rendered.
var client = apps._client.scope({ searchTimeout: 10000 });
client.waitFor(function() {
var el = client.findElement('iframe[src*="homescreen.gaiamobile.org"]');
var frameClass = el.scriptWith(function(el) {
return el.parentNode.getAttribute('class');
});

if (frameClass !== null) {
return frameClass.indexOf('render') !== -1;
} else {
return el.displayed();
}
});

// launch the given app
return getApp(apps, origin, entrypoint, function(err, app) {
Expand Down
55 changes: 23 additions & 32 deletions lib/waitforapp.js
Expand Up @@ -3,13 +3,11 @@
*/
var SEARCH_TIMEOUT = 10000;


/**
* @const {number}
*/
var WAIT_BETWEEN_CHECKS = 250;


/**
* Wait until the app is visible.
* @param {Apps} apps state.
Expand All @@ -22,7 +20,7 @@ function waitForApp(apps, source, callback) {
if (client.isSync) {
waitForAppSync(client, source, callback);
} else {
waitForAppAsync(client, source, callback);
throw Error('No longer support async driver. Please use sync driver.');
}
}

Expand All @@ -34,42 +32,35 @@ function waitForApp(apps, source, callback) {
* @param {Function} callback [Err error, Marionette.Element el].
*/
function waitForAppSync(client, source, callback) {
var selector = 'iframe[src*="' + source + '"]';
var el = client.findElement('iframe[src*="' + source + '"]');

// find iframe
var el = client.findElement(selector);
// Wait for the iframe is rendered.
client.waitFor(function() {
return el.displayed;
});

callback && callback(null, el);
}


/**
* Wait until the app is visible using an async driver.
* @param {Marionette.Client} client with async driver.
* @param {String} source to wait for.
* @param {Function} callback [Err error, Marionette.Element el].
*/
function waitForAppAsync(client, source, callback) {
var selector = 'iframe[src*="' + source + '"]';

// find iframe
client.findElement(selector, function(err, element) {
if (err) {
return callback && callback(err);
}
var frameClass = el.scriptWith(function(el) {
return el.parentNode.getAttribute('class');
});

function displayed(done) {
element.displayed(done);
if (frameClass !== null) {
return frameClass.indexOf('render') !== -1;
} else {
return true;
}
});

client.waitFor(displayed, function(err) {
callback && callback(err, element);
// Wait for the iframe is displayed on screen.
client.waitFor(function() {
var transitionState = el.scriptWith(function(el) {
return el.parentNode.getAttribute('transition-state');
});

if (transitionState !== null) {
return transitionState === 'opened';
} else {
return el.displayed();
}
});
}

callback && callback(null, el);
}

module.exports.waitForApp = waitForApp;
29 changes: 28 additions & 1 deletion test/waitforapp_test.js
Expand Up @@ -28,8 +28,35 @@ suite('waitforapp', function() {
});
});

test('iframe is with the render class', function(done) {
client.findElement('iframe[src*="' + domain + '"]', function(err, el) {
el.scriptWith(
function(el) {
return el.parentNode.getAttribute('class');
},
function(err, value) {
assert.ok(value.indexOf('render') !== -1);
done();
}
);
});
});

test('the transition-state of the iframe is opened', function(done) {
client.findElement('iframe[src*="' + domain + '"]', function(err, el) {
el.scriptWith(
function(el) {
return el.parentNode.getAttribute('transition-state');
},
function(err, value) {
assert.equal(value, 'opened');
done();
}
);
});
});

test('iframe is visible', function(done) {
var iframe;
client.findElement('iframe[src*="' + domain + '"]', function(err, el) {
el.displayed(function(err, displayed) {
assert.ok(displayed);
Expand Down

0 comments on commit 6d7e702

Please sign in to comment.