Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #31359 from Cwiiis/bug1191746-new-homescreen-mario…
Browse files Browse the repository at this point in the history
…nette-hosted-app-tests

Bug 1191746 - Port hosted app tests to new homescreen. r=gmarty
  • Loading branch information
Cwiiis committed Aug 17, 2015
2 parents 34f1426 + b34449b commit 415825f
Show file tree
Hide file tree
Showing 37 changed files with 1,506 additions and 24 deletions.
4 changes: 2 additions & 2 deletions apps/homescreen/bower_components/gaia-app-icon/.bower.json
@@ -1,10 +1,10 @@
{
"name": "gaia-app-icon",
"_release": "d787671ad6",
"_release": "f1b643c6e4",
"_resolution": {
"type": "branch",
"branch": "master",
"commit": "d787671ad6d3bb8109d5ea58f8cd607edd9fbcd5"
"commit": "f1b643c6e4ab2f9153a6a19ce54a25a4497b3140"
},
"_source": "https://gitlab.com/Cwiiis/gaia-app-icon.git",
"_target": "*",
Expand Down
70 changes: 52 additions & 18 deletions apps/homescreen/bower_components/gaia-app-icon/script.js
Expand Up @@ -62,6 +62,7 @@ window.GaiaAppIcon = (function(exports) {
this._app.removeEventListener('downloadapplied', this);
}

delete this.dataset.testIconUrl;
this._cancelIconLoad();
this._removeOldIcon();
this._hasIcon = false;
Expand Down Expand Up @@ -183,8 +184,11 @@ window.GaiaAppIcon = (function(exports) {
this._relayout();
this._prepareIconLoader();
this._hasUserSetIcon = true;
this._pendingIconUrl = 'user-set';
this._image.src = URL.createObjectURL(blob);
} else {
this._pendingIconUrl = null;
delete this.dataset.testIconUrl;
this._cancelIconLoad();
this._removeOldIcon();
}
Expand All @@ -198,6 +202,7 @@ window.GaiaAppIcon = (function(exports) {
return;
}

this._pendingIconUrl = null;
this._image.onload = null;
this._image.src = '';
this._image = null;
Expand Down Expand Up @@ -244,6 +249,31 @@ window.GaiaAppIcon = (function(exports) {
this._image = document.createElement('img');

this._image.onload = () => {
this._image.onload = () => {
// Add new icon
this._removeOldIcon();
this._container.appendChild(this._image);
this._container.classList.remove('initial-load');
this._image = null;
this._hasIcon = true;

if (!this._hasPredefinedIcon && !this._hasUserSetIcon) {
this.dispatchEvent(new CustomEvent('icon-loaded'));
}

// Set icon URL on dataset, for testing.
if (this._pendingIconUrl) {
this.dataset.testIconUrl = this._pendingIconUrl;
this._pendingIconUrl = null;
}

if (this._pendingIconRefresh) {
this._image = null;
this._pendingIconRefresh = false;
this.refresh();
}
};

if (!this._hasUserSetIcon) {
// Process the image
var canvas = document.createElement('canvas');
Expand Down Expand Up @@ -309,29 +339,15 @@ window.GaiaAppIcon = (function(exports) {
}.bind(this, this._image));
}

this._image.onload = () => {
// Add new icon
this._removeOldIcon();
this._container.appendChild(this._image);
this._container.classList.remove('initial-load');
this._image = null;
this._hasIcon = true;

if (this._pendingIconRefresh) {
this._pendingIconRefresh = false;
this.refresh();
} else if (!this._hasPredefinedIcon) {
this.dispatchEvent(new CustomEvent('icon-loaded'));
}
};

if (this._hasUserSetIcon) {
this._image.onload();
}
};

this._image.onerror = (e) => {
console.error('Failed to load icon', e);
this._pendingIconUrl = null;

if (this._pendingIconRefresh) {
this._pendingIconRefresh = false;
this._image = null;
Expand All @@ -347,7 +363,7 @@ window.GaiaAppIcon = (function(exports) {

proto._setPredefinedIcon = function(name) {
this._hasPredefinedIcon = true;
this._image.src = this._predefinedIcons[name];
this._image.src = this._pendingIconUrl = this._predefinedIcons[name];
};

proto._relayout = function() {
Expand Down Expand Up @@ -388,6 +404,8 @@ window.GaiaAppIcon = (function(exports) {
});
} else if (this.bookmark) {
this._subtitle.textContent = this.bookmark.name;
} else {
this._subtitle.textContent = '';
}
};

Expand All @@ -401,7 +419,18 @@ window.GaiaAppIcon = (function(exports) {
this._relayout();
this._container.classList.remove('downloading');

if (this._size < 1 || (!this.app && !this.bookmark)) {
// Set an identifier on the icon to help with automated testing.
if (this.app) {
this.dataset.identifier = this.app.manifestURL +
(this.entryPoint ? '/' + this.entryPoint : '');
} else if (this.bookmark) {
this.dataset.identifier = this.bookmark.id;
} else {
delete this.dataset.identifier;
}

// If the icon won't be visible, don't bother fetching and processing it.
if (this._size < 1) {
return;
}

Expand All @@ -424,6 +453,8 @@ window.GaiaAppIcon = (function(exports) {
xhr.responseType = 'blob';
xhr.timeout = ICON_FETCH_TIMEOUT;

this._pendingIconUrl = this.bookmark.icon;

xhr.onload = function load(image) {
if (!image.onload) {
return;
Expand Down Expand Up @@ -481,6 +512,7 @@ window.GaiaAppIcon = (function(exports) {
case 'installed':
navigator.mozApps.mgmt.getIcon(this.app, this._size, this.entryPoint).
then(function(image, blob) {
this._pendingIconUrl = 'app-icon';
if (image.onload) {
image.src = URL.createObjectURL(blob);
}
Expand All @@ -489,6 +521,8 @@ window.GaiaAppIcon = (function(exports) {
console.error('Failed to retrieve icon', e);
if (image.onload && !this._hasIcon) {
this._setPredefinedIcon('default');
} else {
this._image = null;
}
}.bind(this, this._image));
break;
Expand Down
30 changes: 27 additions & 3 deletions apps/homescreen/js/app.js
Expand Up @@ -103,6 +103,7 @@ const SETTINGS_VERSION = 0;
navigator.mozApps.mgmt.addEventListener('uninstall', this);
window.addEventListener('hashchange', this);
window.addEventListener('localized', this);
window.addEventListener('online', this);

// Restore settings
this.restoreSettings();
Expand Down Expand Up @@ -149,6 +150,7 @@ const SETTINGS_VERSION = 0;
};

this.startupMetadata = [];
this.iconsToRetry = [];
this.metadata = new HomeMetadata();
this.bookmarks = new Datastore('bookmarks_store');
Promise.all([
Expand Down Expand Up @@ -327,8 +329,15 @@ const SETTINGS_VERSION = 0;
}

// Save the refreshed icon
this.iconsToRetry.push(id);
icon.addEventListener('icon-loaded', function(icon, id) {
icon.icon.then((blob) => {
// Remove icon from list of icons to retry when we go online
var retryIndex = this.iconsToRetry.indexOf(id);
if (retryIndex !== -1) {
this.iconsToRetry.splice(retryIndex, 1);
}

this.metadata.set([{ id: id, icon: blob }]).then(
() => {},
(e) => {
Expand Down Expand Up @@ -423,7 +432,7 @@ const SETTINGS_VERSION = 0;
},

handleEvent: function(e) {
var icon;
var icon, child, id;

switch (e.type) {
// Show the settings menu when the user long-presses and we aren't in
Expand Down Expand Up @@ -683,10 +692,10 @@ const SETTINGS_VERSION = 0;
this.snapScrollPosition(0);
};

for (var child of this.icons.children) {
for (child of this.icons.children) {
icon = child.firstElementChild;
if (icon.app && icon.app.manifestURL === e.application.manifestURL) {
var id = e.application.manifestURL + '/' +
id = e.application.manifestURL + '/' +
(icon.entryPoint ? icon.entryPoint : '');
this.metadata.remove(id).then(() => {},
(e) => {
Expand All @@ -713,6 +722,21 @@ const SETTINGS_VERSION = 0;
}
this.icons.synchronise();
break;

case 'online':
for (var i = 0, iLen = this.iconsToRetry.length; i < iLen; i++) {
for (child of this.icons.children) {
icon = child.firstElementChild;
id = icon.app ?
(icon.app.manifestURL + '/' +
(icon.entryPoint ? icon.entryPoint : '')) : icon.bookmark.id;
if (id === this.iconsToRetry[i]) {
icon.refresh();
break;
}
}
}
break;
}
}
};
Expand Down
65 changes: 65 additions & 0 deletions apps/homescreen/test/marionette/app_appcache_pending_test.js
@@ -0,0 +1,65 @@
/* global __dirname */
'use strict';

var assert = require('assert');
var AppInstall =
require('../../../../apps/system/test/marionette/lib/app_install');
var createAppServer = require('./server/parent');

marionette('Homescreen - Appcache installed download', function() {
var client = marionette.client({
profile: require(__dirname + '/client_options.js')
});
var server;
setup(function(done) {
var app = __dirname + '/fixtures/appcache';
createAppServer(app, client, function(err, _server) {
server = _server;
done(err);
});
});

var home, system, appInstall;
setup(function() {
home = client.loader.getAppClass('homescreen');
system = client.loader.getAppClass('system');
appInstall = new AppInstall(client);

system.waitForFullyLoaded();
home.waitForLaunch();
});

teardown(function(done) {
server.close(done);
});

test('shows spinner while downloading', function() {
// correctly install the app...
client.switchToFrame();

// ensure appcache path is delayed
server.cork(server.manifest.appcache_path);
appInstall.install(server.manifestURL);

// switch back to the homescreen
client.switchToFrame(system.getHomescreenIframe());

var icon = home.getIcon(server.manifestURL);

// wait until we see the nice spinner thing
client.waitFor(function() {
return home.iconIsLoading(icon);
});

// let the rest of the app come through
server.uncork(server.manifest.appcache_path);
// wait until it is no longer loading
client.waitFor(function() {
return !home.iconIsLoading(icon);
});

// ensure the app launches!
home.launchIcon(icon);
assert.equal(client.title(), 'iwrotethis');
});
});
@@ -0,0 +1,63 @@
/* global __dirname */
'use strict';

var assert = require('assert');
var AppInstall =
require('../../../../apps/system/test/marionette/lib/app_install');
var createAppServer = require('./server/parent');

marionette('Homescreen - Hosted app failed icon fetch', function() {
var client = marionette.client({
profile: require(__dirname + '/client_options.js')
});
var server;
setup(function(done) {
var app = __dirname + '/fixtures/template_app';
createAppServer(app, client, function(err, _server) {
server = _server;
done(err);
});
});

var home, system, appInstall;
setup(function() {
home = client.loader.getAppClass('homescreen');
system = client.loader.getAppClass('system');
appInstall = new AppInstall(client);

system.waitForFullyLoaded();
home.waitForLaunch();
});

teardown(function(done) {
server.close(done);
});

test('fallback to default icon', function() {
var iconURL = server.manifest.icons['128'];

// correctly install the app...
client.switchToFrame();

// ensure the icon fails to download!
server.fail(iconURL);
appInstall.install(server.manifestURL);

// switch back to the homescreen
client.switchToFrame(system.getHomescreenIframe());

var icon = home.getIcon(server.manifestURL);

// ensure the default icon is shown
client.waitFor(function() {
var src = home.getIconImageUrl(icon);
return src && src.indexOf('default') !== -1;
});

// ensure the icon can be launched!
home.launchIcon(icon);
assert.equal(client.title(), 'iwrotethis');
});
});


0 comments on commit 415825f

Please sign in to comment.