From b2c63f450382388d07a213621b2e5141d0d12527 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 30 May 2019 12:28:13 +0200 Subject: [PATCH] MOBILE-3039 core: Allow forcing offline mode from javascript console --- src/providers/app.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/providers/app.ts b/src/providers/app.ts index 50deda52a..abbf4dd9f 100644 --- a/src/providers/app.ts +++ b/src/providers/app.ts @@ -73,6 +73,7 @@ export class CoreAppProvider { protected backActions = []; protected mainMenuId = 0; protected mainMenuOpen: number; + protected forceOffline = false; constructor(dbProvider: CoreDbProvider, private platform: Platform, private keyboard: Keyboard, private appCtrl: App, private network: Network, logger: CoreLoggerProvider, private events: CoreEventsProvider, zone: NgZone, @@ -102,6 +103,9 @@ export class CoreAppProvider { this.platform.registerBackButtonAction(() => { this.backButtonAction(); }, 100); + + // Export the app provider so Behat tests can change the forceOffline flag. + ( window).appProvider = this; } /** @@ -257,6 +261,10 @@ export class CoreAppProvider { * @return {boolean} Whether the app is online. */ isOnline(): boolean { + if (this.forceOffline) { + return false; + } + let online = this.network.type !== null && this.network.type != Connection.NONE && this.network.type != Connection.UNKNOWN; // Double check we are not online because we cannot rely 100% in Cordova APIs. Also, check it in browser. if (!online && navigator.onLine) { @@ -567,4 +575,13 @@ export class CoreAppProvider { this.statusBar.styleLightContent() : this.statusBar.styleDefault(); } } + + /** + * Set value of forceOffline flag. If true, the app will think the device is offline. + * + * @param {boolean} value Value to set. + */ + setForceOffline(value: boolean): void { + this.forceOffline = !!value; + } }