Skip to content

Commit

Permalink
add goOffline/goOnline methods to driver
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Aug 31, 2016
1 parent db69edd commit ceb5f36
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
24 changes: 24 additions & 0 deletions lighthouse-core/gather/drivers/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,30 @@ class Driver {
]);
}

/**
* Emulate internet disconnection.
* @return {!Promise}
*/
goOffline() {
return this.sendCommand('Network.enable').then(_ => emulation.goOffline(this));
}

/**
* Enable internet connection, using emulated mobile settings if
* `options.flags.mobile` is true.
* @param {!Object} options
* @return {!Promise}
*/
goOnline(options) {
return this.sendCommand('Network.enable').then(_ => {
if (options.flags.mobile) {
return emulation.enableNetworkThrottling(this);
}

return emulation.disableNetworkThrottling(this);
});
}

cleanAndDisableBrowserCaches() {
return Promise.all([
this.clearBrowserCache(),
Expand Down
26 changes: 2 additions & 24 deletions lighthouse-core/gather/gatherers/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,8 @@
const Gatherer = require('./gatherer');

class Offline extends Gatherer {

static config(opts) {
return {
offline: opts.offline,
// values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0
};
}

static goOffline(driver) {
// Network.enable must be called for Network.emulateNetworkConditions to work
return driver.sendCommand('Network.enable').then(_ => {
driver.sendCommand('Network.emulateNetworkConditions', Offline.config({offline: true}));
});
}

static goOnline(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', Offline.config({offline: false}));
}

beforePass(options) {
return Offline.goOffline(options.driver);
return options.driver.goOffline();
}

afterPass(options, tracingData) {
Expand All @@ -52,7 +30,7 @@ class Offline extends Gatherer {

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

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

Expand Down
27 changes: 26 additions & 1 deletion lighthouse-core/lib/emulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ const TYPICAL_MOBILE_THROTTLING_METRICS = {
offline: false
};

const OFFLINE_METRICS = {
offline: true,
// values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0
};

const NO_THROTTLING_METRICS = {
latency: 0,
downloadThroughput: 0,
uploadThroughput: 0,
offline: false
};

function enableNexus5X(driver) {
/**
* Finalizes touch emulation by enabling `"ontouchstart" in window` feature detect
Expand Down Expand Up @@ -90,7 +105,17 @@ function enableNetworkThrottling(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', TYPICAL_MOBILE_THROTTLING_METRICS);
}

function disableNetworkThrottling(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', NO_THROTTLING_METRICS);
}

function goOffline(driver) {
return driver.sendCommand('Network.emulateNetworkConditions', OFFLINE_METRICS);
}

module.exports = {
enableNexus5X,
enableNetworkThrottling
enableNetworkThrottling,
disableNetworkThrottling,
goOffline
};
5 changes: 4 additions & 1 deletion lighthouse-core/test/gather/gatherers/offline-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const assert = require('assert');
const tracingData = require('../../fixtures/traces/network-records.json');

const mockDriver = {
sendCommand() {
goOffline() {
return Promise.resolve();
},
goOnline() {
return Promise.resolve();
}
};
Expand Down

0 comments on commit ceb5f36

Please sign in to comment.