From 36d74df2f5740490ed5cc8f19d29ad5b17cee2bf Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Tue, 24 Jul 2018 14:58:33 -0700 Subject: [PATCH] Offline mode tests --- src/vs/platform/actions/common/offlineMode.ts | 18 +- .../platform/actions/test/offlineMode.test.ts | 161 ++++++++++++++++++ 2 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 src/vs/platform/actions/test/offlineMode.test.ts diff --git a/src/vs/platform/actions/common/offlineMode.ts b/src/vs/platform/actions/common/offlineMode.ts index 1fecd4c605f78..ad60a30e3f09d 100644 --- a/src/vs/platform/actions/common/offlineMode.ts +++ b/src/vs/platform/actions/common/offlineMode.ts @@ -15,6 +15,7 @@ import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage'; import { CommandsRegistry } from 'vs/platform/commands/common/commands'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { dispose, IDisposable } from 'vs/base/common/lifecycle'; export const offlineModeSetting = 'workbench.enableOfflineMode'; export const unSupportedInOfflineModeMsg = localize('offlineModeUnsupportedFeature', "This feature is not supported in offline mode"); @@ -23,6 +24,7 @@ export class EnableOfflineMode extends Action { static readonly ID = 'workbench.action.enableOfflineMode'; static LABEL = localize('enableOfflineMode', 'Enable Offline Mode'); + private disposables: IDisposable[] = []; private readonly disclaimerStorageKey = 'workbench.offlineMode.disclaimer.dontShowAgain'; constructor( @@ -38,7 +40,7 @@ export class EnableOfflineMode extends Action { if (e.affectsConfiguration(offlineModeSetting)) { this.updateEnabled(); } - }); + }, this, this.disposables); } private updateEnabled() { @@ -58,12 +60,19 @@ export class EnableOfflineMode extends Action { } return this.configurationService.updateValue(offlineModeSetting, true); } + + dispose(): void { + this.disposables = dispose(this.disposables); + super.dispose(); + } } export class DisableOfflineMode extends Action { static readonly ID = 'workbench.action.disableOfflineMode'; static LABEL = localize('disableOfflineMode', 'Disable Offline Mode'); + private disposables: IDisposable[] = []; + constructor( id: string = DisableOfflineMode.ID, label: string = DisableOfflineMode.LABEL, @@ -75,7 +84,7 @@ export class DisableOfflineMode extends Action { if (e.affectsConfiguration(offlineModeSetting)) { this.updateEnabled(); } - }); + }, this, this.disposables); } private updateEnabled() { @@ -85,6 +94,11 @@ export class DisableOfflineMode extends Action { run(): TPromise { return this.configurationService.updateValue(offlineModeSetting, false); } + + dispose(): void { + this.disposables = dispose(this.disposables); + super.dispose(); + } } export class NotifyUnsupportedFeatureInOfflineMode extends Action { diff --git a/src/vs/platform/actions/test/offlineMode.test.ts b/src/vs/platform/actions/test/offlineMode.test.ts new file mode 100644 index 0000000000000..43abf4d07ebdf --- /dev/null +++ b/src/vs/platform/actions/test/offlineMode.test.ts @@ -0,0 +1,161 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +'use strict'; + +import * as assert from 'assert'; +import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock'; +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; +import { EnableOfflineMode, DisableOfflineMode, offlineModeSetting, NotifyUnsupportedFeatureInOfflineMode, unSupportedInOfflineModeMsg } from 'vs/platform/actions/common/offlineMode'; +import { IAction } from 'vs/base/common/actions'; +import { TPromise } from 'vs/base/common/winjs.base'; +import { IStorageService } from 'vs/platform/storage/common/storage'; +import { INotificationService } from 'vs/platform/notification/common/notification'; +import { Emitter } from 'vs/base/common/event'; +import { ConfigurationChangeEvent } from 'vs/platform/configuration/common/configurationModels'; + +suite('OfflineMode', () => { + let instantiationService: TestInstantiationService; + let offlineMode = false; + let dontShowAgain = false; + let promptShown = false; + let configChangeEmitter = new Emitter(); + let enableOfflineAction: IAction; + let disableOfflineAction: IAction; + + suiteSetup(() => { + instantiationService = new TestInstantiationService(); + instantiationService.stub(IConfigurationService, { + onDidUpdateConfiguration: () => { }, + onDidChangeConfiguration: configChangeEmitter.event, + getConfigurationData: () => ({}), + getValue: key => { + return key === offlineModeSetting ? offlineMode : undefined; + }, + updateValue: (key, value) => { + if (key === offlineModeSetting) { + offlineMode = value; + } + return TPromise.as(null); + } + }); + instantiationService.stub(IStorageService, { + getBoolean: key => { + return key === 'workbench.offlineMode.disclaimer.dontShowAgain' ? dontShowAgain : undefined; + }, + store: (key, value) => { + if (key === 'workbench.offlineMode.disclaimer.dontShowAgain') { + dontShowAgain = value; + } + } + }); + }); + + setup(() => { + promptShown = false; + dontShowAgain = false; + instantiationService.stub(INotificationService, { + prompt: (a, b, c) => { + promptShown = true; + } + }); + + enableOfflineAction = instantiationService.createInstance(EnableOfflineMode); + disableOfflineAction = instantiationService.createInstance(DisableOfflineMode); + + offlineMode = false; + configChangeEmitter.fire(new ConfigurationChangeEvent().change([offlineModeSetting])); + }); + + teardown(() => { + enableOfflineAction.dispose(); + disableOfflineAction.dispose(); + }); + + suiteTeardown(() => { + configChangeEmitter.dispose(); + }); + + test('Test Enablement of actions in online mode', () => { + assert.equal(enableOfflineAction.enabled, true); + assert.equal(disableOfflineAction.enabled, false); + }); + + test('Test Enablement of actions in offline mode', () => { + offlineMode = true; + configChangeEmitter.fire(new ConfigurationChangeEvent().change([offlineModeSetting])); + + assert.equal(enableOfflineAction.enabled, false); + assert.equal(disableOfflineAction.enabled, true); + }); + + test('EnableOfflineMode action enables offline mode with prompt', () => { + return enableOfflineAction.run().then(() => { + assert.equal(offlineMode, true); + assert.equal(promptShown, true); + assert.equal(dontShowAgain, false); + }); + }); + + test('EnableOfflineMode action prompt choose dont show again', () => { + instantiationService.stub(INotificationService, { + prompt: (a, b, c) => { + promptShown = true; + if (c[0].label === 'Don\'t Show Again') { + c[0].run(); + } + } + }); + enableOfflineAction.dispose(); + enableOfflineAction = instantiationService.createInstance(EnableOfflineMode); + return enableOfflineAction.run().then(() => { + assert.equal(offlineMode, true); + assert.equal(promptShown, true); + assert.equal(dontShowAgain, true); + }); + }); + + test('EnableOfflineMode action prompt dont show again if chosen so before', () => { + dontShowAgain = true; + return enableOfflineAction.run().then(() => { + assert.equal(offlineMode, true); + assert.equal(promptShown, false); + assert.equal(dontShowAgain, true); + }); + }); + + test('DisableOfflineMode action enables offline mode', () => { + offlineMode = true; + configChangeEmitter.fire(new ConfigurationChangeEvent().change([offlineModeSetting])); + return disableOfflineAction.run().then(() => { + assert.equal(offlineMode, false); + }); + }); + + test('Notify Action shows prompt', () => { + offlineMode = true; + const notifyAction: IAction = instantiationService.createInstance(NotifyUnsupportedFeatureInOfflineMode); + return notifyAction.run().then(() => { + assert.equal(promptShown, true); + assert.equal(offlineMode, true); + }); + }); + + test('Notify Action shows prompt that disables offline mode', () => { + offlineMode = true; + instantiationService.stub(INotificationService, { + prompt: (a, b, c) => { + promptShown = true; + if (c[0].label === DisableOfflineMode.LABEL && b === unSupportedInOfflineModeMsg) { + c[0].run(); + } + } + }); + const notifyAction: IAction = instantiationService.createInstance(NotifyUnsupportedFeatureInOfflineMode); + return notifyAction.run().then(() => { + assert.equal(promptShown, true); + assert.equal(offlineMode, false); + }); + }); +}); \ No newline at end of file