Skip to content

Commit

Permalink
added short-name-length check
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Apr 7, 2016
1 parent 0acab27 commit b0d756e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aggregators/will-get-add-to-homescreen-prompt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const manifestIconsMin144 = require('../../audits/manifest/icons-min-144').name;
/** @type {string} */
const manifestShortName = require('../../audits/manifest/short-name').name;

/** @type {string} */
const manifestShortNameLength = require('../../audits/manifest/short-name-length').name;

class AddToHomescreen extends Aggregate {

/**
Expand All @@ -52,6 +55,7 @@ class AddToHomescreen extends Aggregate {
* - valid start_url
* - valid name
* - valid short_name
* - short_name of reasonable length
* - icon of size >= 144x144 and png (either type `image/png` or filename ending in `.png`
* @see https://github.com/GoogleChrome/lighthouse/issues/23
*
Expand Down Expand Up @@ -86,6 +90,11 @@ class AddToHomescreen extends Aggregate {
weight: 0
};

criteria[manifestShortNameLength] = {
value: true,
weight: 0
};

return criteria;
}
}
Expand Down
71 changes: 71 additions & 0 deletions audits/manifest/short-name-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @license
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const Audit = require('../audit');

class ManifestShortNameLength extends Audit {
/**
* @override
*/
static get tags() {
return ['Manifest'];
}

/**
* @override
*/
static get name() {
return 'manifest-short-name-length';
}

/**
* @override
*/
static get description() {
return 'App short_name won\'t be truncated on the homescreen';
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
let isShortNameShortEnough = false;
let debugString;
const manifest = artifacts.manifest.value;
const suggestedLength = 12;

if (manifest && manifest.short_name && manifest.short_name.value) {
// Historically, Chrome recommended 12 chars as the maximum length to prevent truncation.
// See #69 for more discussion.
isShortNameShortEnough = (manifest.short_name.value.length <= suggestedLength);
if (!isShortNameShortEnough) {
debugString = `${suggestedLength} chars is the suggested maximum length`;
}
}

return ManifestShortNameLength.generateAuditResult(
isShortNameShortEnough,
undefined,
debugString
);
}
}

module.exports = ManifestShortNameLength;
1 change: 1 addition & 0 deletions extension/app/scripts.babel/pwa-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const audits = [
require('../../../audits/manifest/icons-min-144'),
require('../../../audits/manifest/name'),
require('../../../audits/manifest/short-name'),
require('../../../audits/manifest/short-name-length'),
require('../../../audits/manifest/start-url'),
require('../../../audits/html/meta-theme-color')
];
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const audits = [
require('./audits/manifest/icons-min-144'),
require('./audits/manifest/name'),
require('./audits/manifest/short-name'),
require('./audits/manifest/short-name-length'),
require('./audits/manifest/start-url'),
require('./audits/html/meta-theme-color')
];
Expand Down
55 changes: 55 additions & 0 deletions test/audits/manifest/short-name-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const Audit = require('../../../audits/manifest/short-name-length.js');
const assert = require('assert');
const manifestParser = require('../../../helpers/manifest-parser');

/* global describe, it*/

describe('Manifest: short_name_length audit', () => {
it('fails when an empty manifest is present', () => {
const manifest = manifestParser('{}');
console.info('got empty manifest', manifest);
return assert.equal(Audit.audit({manifest}).value, false);
});

// Need to disable camelcase check for dealing with short_name.
/* eslint-disable camelcase */
it('fails when a manifest contains no short_name', () => {
const manifestSrc = JSON.stringify({
short_name: undefined
});
const manifest = manifestParser(manifestSrc);
return assert.equal(Audit.audit({manifest}).value, false);
});

it('fails when a manifest contains a long short_name', () => {
const manifestSrc = JSON.stringify({
short_name: 'i\'m much longer than the recommended size'
});
const manifest = manifestParser(manifestSrc);
return assert.equal(Audit.audit({manifest}).value, false);
});

it('succeeds when a manifest contains a short_name', () => {
const manifestSrc = JSON.stringify({
short_name: 'Lighthouse'
});
const manifest = manifestParser(manifestSrc);
return assert.equal(Audit.audit({manifest}).value, true);
});
/* eslint-enable camelcase */
});

0 comments on commit b0d756e

Please sign in to comment.