Skip to content

Commit

Permalink
update offline gatherer to use network recording changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Aug 30, 2016
1 parent 34509b1 commit fa6e524
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions lighthouse-cli/test/fixtures/smoketest-offline-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
]
},{
"loadPage": true,
"network": true,
"gatherers": [
"service-worker",
"offline"
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
]
},
{
"passName": "offlinePass",
"loadPage": true,
"network": true,
"gatherers": [
Expand Down
17 changes: 5 additions & 12 deletions lighthouse-core/gather/gatherers/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const Gatherer = require('./gatherer');
Expand Down Expand Up @@ -43,26 +42,20 @@ class Offline extends Gatherer {
}

beforePass(options) {
const driver = options.driver;

return driver.gotoURL(options.url, {waitForLoad: true})
.then(_ => Offline.goOffline(driver))
// Navigate away, then back, to allow a service worker that doesn't call
// clients.claim() to take control of the page load.
.then(_ => driver.gotoURL('about:blank'))
.then(_ => driver.gotoURL(options.url, {waitForLoad: true}))
.then(_ => Offline.goOnline(driver));
return Offline.goOffline(options.driver);
}

afterPass(options, tracingData) {
const navigationRecord = tracingData.networkRecords.filter(record => {
// If options.url is just an origin without a path, the Chrome will
// implicitly add in a path of '/'.
// If options.url is just an origin without a path, Chrome will implicitly
// add in a path of '/'.
return (record._url === options.url || record._url === options.url + '/') &&
record._fetchedViaServiceWorker;
}).pop(); // Take the last record that matches.

this.artifact = navigationRecord ? navigationRecord.statusCode : -1;

return Offline.goOnline(options.driver);
}
}

Expand Down
8 changes: 1 addition & 7 deletions lighthouse-core/lib/network-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,8 @@ class NetworkRecorder extends EventEmitter {
}

onResourceChangedPriority(data) {
// TODO: this.networkManager._dispatcher.resourceChangedPriority is set to
// undefined when onResourceChangedPriority is triggered in
// gatherers/offline.js. The underlying cause may need to be investigated.
// In the meantime, explicitly check that it's a function.
if (typeof this.networkManager._dispatcher.resourceChangedPriority === 'function') {
this.networkManager._dispatcher.resourceChangedPriority(data.requestId,
this.networkManager._dispatcher.resourceChangedPriority(data.requestId,
data.newPriority, data.timestamp);
}
}

static recordsFromLogs(logs) {
Expand Down
32 changes: 22 additions & 10 deletions lighthouse-core/test/gather/gatherers/offline-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,33 @@
const OfflineGather = require('../../../gather/gatherers/offline');
const assert = require('assert');
const tracingData = require('../../fixtures/traces/network-records.json');
let offlineGather;

describe('Offline gatherer', () => {
// Reset the Gatherer before each test.
beforeEach(() => {
offlineGather = new OfflineGather();
});
const mockDriver = {
sendCommand() {
return Promise.resolve();
}
};

describe('Offline gatherer', () => {
it('returns an artifact set to -1 when offline loading fails', () => {
offlineGather.afterPass({url: 'https://do-not-match.com'}, tracingData);
assert.deepEqual(offlineGather.artifact, -1);
const offlineGather = new OfflineGather();
const options = {
url: 'https://do-not-match.com',
driver: mockDriver
};
return offlineGather.afterPass(options, tracingData).then(_ => {
assert.strictEqual(offlineGather.artifact, -1);
});
});

it('returns an artifact set to 200 when offline loading succeeds', () => {
offlineGather.afterPass({url: 'https://ifixit-pwa.appspot.com'}, tracingData);
assert.deepEqual(offlineGather.artifact, 200);
const offlineGather = new OfflineGather();
const options = {
url: 'https://ifixit-pwa.appspot.com',
driver: mockDriver
};
return offlineGather.afterPass(options, tracingData).then(_ => {
assert.strictEqual(offlineGather.artifact, 200);
});
});
});

0 comments on commit fa6e524

Please sign in to comment.