Skip to content

Commit

Permalink
Add offline gather / audit
Browse files Browse the repository at this point in the history
Fixes #21. Deeply enjoyable.
  • Loading branch information
samccone authored and paullewis committed Apr 5, 2016
1 parent fcdb24c commit bcef87b
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
7 changes: 7 additions & 0 deletions aggregators/can-load-offline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ class WorksOffline extends Aggregate {

static get criteria() {
const serviceWorker = require('../../audits/offline/service-worker').name;
const worksOffline = require('../../audits/offline/works-offline').name;

const criteria = {};
criteria[serviceWorker] = {
value: true,
weight: 1
};

criteria[worksOffline] = {
value: true,
weight: 1
};

return criteria;
}
}
Expand Down
52 changes: 52 additions & 0 deletions audits/offline/works-offline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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 WorksOffline extends Audit {
/**
* @override
*/
static get tags() {
return ['Offline'];
}

/**
* @override
*/
static get name() {
return 'works offline';
}

/**
* @override
*/
static get description() {
return 'URL responds with a 200 when offline';
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
return WorksOffline.generateAuditResult(artifacts.responseCode === 200);
}
}

module.exports = WorksOffline;
74 changes: 74 additions & 0 deletions gatherers/offline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @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 Gather = require('./gather');

// Request the current page by issuing a sync request to ''.
const requestPage = `
(function() {
var request = new XMLHttpRequest();
request.open('GET', '', false);
request.send(null);
return request.status;
})();
`;

class Offline extends Gather {

static goOffline(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', {
offline: true,
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0
});
}

static goOnline(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', {
offline: false,
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0
});
}

static getOfflinePageStatus(driver) {
return driver.sendCommand('Runtime.evaluate', {
expression: requestPage
});
}

static gather(options) {
const driver = options.driver;

// TODO eventually we will want to walk all network
// requests that the page initially made and retry them.
return Offline.goOffline(driver).then(_ => {
let responseCode;

return Offline.getOfflinePageStatus(driver).then(ret => {
responseCode = ret.result.value;
}).then(_ => Offline.goOnline(driver)).then(_ => {
return {responseCode};
});
});
}
}

module.exports = Offline;
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ const gatherers = [
require('./gatherers/viewport'),
require('./gatherers/theme-color'),
require('./gatherers/html'),
require('./gatherers/manifest')
require('./gatherers/manifest'),
require('./gatherers/offline')
];

const audits = [
require('./audits/security/is-on-https'),
require('./audits/offline/service-worker'),
require('./audits/offline/works-offline'),
require('./audits/mobile-friendly/viewport'),
require('./audits/performance/first-meaningful-paint'),
require('./audits/manifest/exists'),
Expand All @@ -48,6 +51,7 @@ const audits = [
require('./audits/manifest/start-url'),
require('./audits/html/meta-theme-color')
];

const aggregators = [
require('./aggregators/will-get-add-to-homescreen-prompt'),
require('./aggregators/launches-with-splash-screen'),
Expand Down

0 comments on commit bcef87b

Please sign in to comment.