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 #30894 from albertopq/1181602-icon-apps
Browse files Browse the repository at this point in the history
Bug 1181602 - Display pin badge for Firefox Apps r=benfrancis
  • Loading branch information
albertopq committed Jul 13, 2015
2 parents dda7775 + 3632424 commit 0585f7e
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 23 deletions.
53 changes: 52 additions & 1 deletion apps/sharedtest/test/unit/icons_helper_test.js
Expand Up @@ -56,7 +56,7 @@ suite('Icons Helper', () => {
suite('IconsHelper.getIcon()', () => {
var placeObj, siteObj;

suiteSetup(() => {
setup(() => {
placeObj = {
icons: {
'http://example.com/metaTagIconUrl': {
Expand All @@ -77,6 +77,57 @@ suite('Icons Helper', () => {
};
});

test('Prioritise icons from the Web manifest over the rest', done => {
var origin = 'http://origin.com';
var path = '/test.png';
siteObj.manifest = {
origin: origin,
icons: {
'32': path
}
};
IconsHelper.getIcon('http://example.com', 32, placeObj, siteObj)
.then(iconUrl => {
assert.equal((new URL(iconUrl)).pathname, '/webManifestIconUrl');
done();
});
});

test('Prioritise Firefox manifest after Web manifest', done => {
var origin = 'http://origin.com';
var path = '/test.png';
siteObj.webManifest = null;
siteObj.manifest = {
origin: origin,
icons: {
'32': path
}
};
IconsHelper.getIcon('http://example.com', 32, placeObj, siteObj)
.then(iconUrl => {
assert.equal((new URL(iconUrl)).href, origin + path);
done();
});
});


test('Works with external icons', done => {
var origin = 'http://origin.com';
var path = 'http://test.com/test.png';
siteObj.webManifest = null;
siteObj.manifest = {
origin: origin,
icons: {
'32': path
}
};
IconsHelper.getIcon('http://example.com', 32, placeObj, siteObj)
.then(iconUrl => {
assert.equal((new URL(iconUrl)).href, path);
done();
});
});

test('Prioritise icons from the web manifest over the rest', done => {
IconsHelper.getIcon('http://example.com', 32, placeObj, siteObj)
.then(iconUrl => {
Expand Down
2 changes: 2 additions & 0 deletions apps/system/js/app_chrome.js
Expand Up @@ -102,6 +102,8 @@
this.app.element.classList.add('scrollable');
}
}

this.setSiteIcon();
};

AppChrome.prototype.combinedView = function an_combinedView() {
Expand Down
16 changes: 11 additions & 5 deletions apps/system/js/app_window.js
Expand Up @@ -2461,10 +2461,17 @@
var siteObj = {};

if (this.webManifestURL && this.webManifest) {
siteObj = {
webManifestUrl: this.webManifestURL,
webManifest: this.webManifest
};
siteObj.webManifestUrl = this.webManifestURL;
siteObj.webManifest = this.webManifest;
}

if (this.manifest && this.manifest.icons) {
//Getting the icons from the FirefoxOS manifest
if (!this.manifest.origin) {
this.manifest.origin = new URL(this.manifestURL).origin;
}
siteObj.manifestUrl = this.manifestURL;
siteObj.manifest = this.manifest;
}

if (this.webManifestURL && !this.webManifest) {
Expand All @@ -2476,7 +2483,6 @@
siteObj);
};


/**
* Return a promise resolving to an icon blob.
*
Expand Down
14 changes: 11 additions & 3 deletions apps/system/test/unit/app_chrome_test.js
Expand Up @@ -36,15 +36,17 @@ suite('system/AppChrome', function() {

stubById = this.sinon.stub(document, 'getElementById');
stubById.returns(document.createElement('div'));

requireApp('system/js/base_ui.js');
requireApp('system/js/app_chrome.js', function() {
this.sinon.stub(AppChrome.prototype, 'setSiteIcon');
app = new AppWindow(cloneConfig(fakeWebSite));
app.contextmenu = {
isShown: function() {return false;}
};
chrome = new AppChrome(app);
done();
});
}.bind(this));

window.SettingsListener = { observe: function() {} };
});
Expand Down Expand Up @@ -289,7 +291,7 @@ suite('system/AppChrome', function() {

suite('Navigation events', function() {
setup(function() {
this.sinon.stub(chrome, 'setSiteIcon');
chrome.setSiteIcon.reset();
});

test('loadstart', function() {
Expand Down Expand Up @@ -574,7 +576,6 @@ suite('system/AppChrome', function() {
});

test('theme resets on navigation', function() {
this.sinon.stub(chrome, 'setSiteIcon');
chrome.setThemeColor('orange');
chrome.handleEvent({type: 'mozbrowserloadstart'});
chrome.handleEvent({type: 'mozbrowserloadend'});
Expand Down Expand Up @@ -755,6 +756,12 @@ suite('system/AppChrome', function() {
chrome.reConfig();
assert.isFalse(chrome.app.element.classList.contains('search-app'));
});

test('sets the site icon', function() {
var app = new AppWindow(fakeSearchApp);
var chrome = new AppChrome(app);
assert.isTrue(chrome.setSiteIcon.called);
});
});

suite('transition events', function() {
Expand Down Expand Up @@ -797,6 +804,7 @@ suite('system/AppChrome', function() {
setup(function() {
var app = new AppWindow(cloneConfig(fakeWebSite));
combinedChrome = new AppChrome(app);
combinedChrome.setSiteIcon.restore();
getIconPromise = new MockPromise();
this.sinon.stub(combinedChrome.app, 'getSiteIconUrl')
.returns(getIconPromise);
Expand Down
53 changes: 40 additions & 13 deletions apps/system/test/unit/app_window_test.js
Expand Up @@ -2555,21 +2555,48 @@ suite('system/AppWindow', function() {
blobPromise.mFulfillToValue({ url: dataURI });
});

test('getSiteIconUrl', function() {
this.sinon.stub(window, 'Promise', MockPromise);
var app1 = new AppWindow(fakeAppConfig1);
app1.webManifestURL = 'https://example.com/webapp.json';
app1.webManifest = {};
var blobPromise = new MockPromise();
var dataURI = 'data:image/png;base64,abc+';
this.sinon.stub(app1, 'getIconBlob').returns(blobPromise);
suite('getSiteIconUrl', function() {
var app1;
var SIZE = 64;
var origin = 'http://test.com';

var promisedIcon = app1.getSiteIconUrl();
promisedIcon.then(function(icon) {
assert.ok(icon && icon.url);
assert.equal(icon.url, dataURI);
setup(function() {
app1 = new AppWindow(fakeAppConfig1);
});


test('getSiteIconUrl', function() {
this.sinon.stub(window, 'Promise', MockPromise);
app1.webManifestURL = 'https://example.com/webapp.json';
app1.webManifest = {};
var blobPromise = new MockPromise();
var dataURI = 'data:image/png;base64,abc+';
this.sinon.stub(app1, 'getIconBlob').returns(blobPromise);

var promisedIcon = app1.getSiteIconUrl();
promisedIcon.then(function(icon) {
assert.ok(icon && icon.url);
assert.equal(icon.url, dataURI);
});
blobPromise.mFulfillToValue({ url: dataURI });
});

test('getSiteIconUrl uses manifest icons if available', function() {
app1.manifestURL = 'https://example.com/webapp.json';
app1.manifest = {
origin: origin,
icons: {
'64': '/test.png'
}
};

this.sinon.stub(app1, 'getIconBlob', function (url, size, place, site) {
assert.isTrue(size === SIZE);
assert.isTrue(site.manifest === app1.manifest);
});

app1.getSiteIconUrl(SIZE);
});
blobPromise.mFulfillToValue({ url: dataURI });
});

test('Change URL at run time', function() {
Expand Down
25 changes: 24 additions & 1 deletion shared/js/icons_helper.js
Expand Up @@ -47,14 +47,23 @@

iconTargetSize = iconTargetSize * window.devicePixelRatio;

// First look for an icon in the manifest.
// First look for an icon in the Webmanifest.
if (siteObj.webManifestUrl && siteObj.webManifest) {
iconUrl = getBestIconFromWebManifest(siteObj.webManifest, iconTargetSize);
if (DEBUG && iconUrl) {
console.log('Icon from Web Manifest');
}
}

// Then look for an icon in the Firefox manifest.
if (!iconUrl && siteObj.manifest) {
siteObj.manifest.icons = _convertToWebManifestIcons(siteObj.manifest);
iconUrl = getBestIconFromWebManifest(siteObj.manifest, iconTargetSize);
if (DEBUG && iconUrl) {
console.log('Icon from Firefox App Manifest');
}
}

// Otherwise, look into the meta tags.
if (!iconUrl && placeObj.icons) {
iconUrl = getBestIconFromMetaTags(placeObj.icons, iconTargetSize);
Expand Down Expand Up @@ -140,6 +149,7 @@
if (!iconURL) {
iconURL = potentialIcon.src;
}

var sizes = Array.from(potentialIcon.sizes);
var nearestSize = getNearestSize(sizes, iconSize, bestSize);

Expand All @@ -152,6 +162,19 @@
return iconURL ? iconURL.href : null;
}

function _convertToWebManifestIcons(manifest) {
return Object.keys(manifest.icons).map(function(size) {
var url = manifest.icons[size];
var sizes = new Set().add(size + 'x' + size);
url = url.indexOf('http') > -1 ? url : manifest.origin + url;

return {
src: new URL(url),
sizes: sizes
};
});
}

// See bug 1041482, we will need to support better
// icons for different part of the system application.
// A web page have different ways to defining icons
Expand Down

0 comments on commit 0585f7e

Please sign in to comment.