Skip to content

Commit

Permalink
Refactored functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Sep 19, 2019
1 parent cdef35f commit 2292854
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/client/common/insidersBuild/insidersExtensionPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export class InsidersExtensionPrompt implements IInsiderExtensionPrompt {
}

@traceDecorators.error('Error in prompting to install insiders')
public async notifyToInstallInsiders(): Promise<void> {
public async promptToInstallInsiders(): Promise<void> {
await this.showPrompt(ExtensionChannels.promptMessage(), this.hasUserBeenNotified, EventName.INSIDERS_PROMPT);
}

@traceDecorators.error('Error in prompting to entroll back to insiders program')
@traceDecorators.error('Error in prompting to enroll back to insiders program')
public async promptToEnrollBackToInsiders(): Promise<void> {
await this.showPrompt(ExtensionChannels.optIntoProgramAgainMessage(), this.hasUserBeenAskedToOptAgain, EventName.OPT_INTO_INSIDERS_AGAIN_PROMPT);
}
Expand Down
58 changes: 44 additions & 14 deletions src/client/common/insidersBuild/insidersExtensionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,16 @@ export class InsidersExtensionService implements IExtensionSingleActivationServi
* @returns `true` if install channel is handled in these miscellaneous cases, `false` if install channel needs further handling
*/
public async handleEdgeCases(installChannel: ExtensionChannels): Promise<boolean> {
let caseHandled = true;
if (installChannel === 'off' && !this.extensionChannelService.isChannelUsingDefaultConfiguration) {
// If previously in the Insiders Program but not now, request them enroll in the program again
await this.insidersPrompt.promptToEnrollBackToInsiders();
} else if (this.appEnvironment.channel === 'insiders' && !this.insidersPrompt.hasUserBeenNotified.value && this.extensionChannelService.isChannelUsingDefaultConfiguration) {
// Only when using VSC insiders and if they have not been notified before (usually the first session), notify to enroll into the insiders program
await this.insidersPrompt.notifyToInstallInsiders();
} else if (installChannel !== 'off' && this.appEnvironment.extensionChannel === 'stable') {
// When install channel is not in sync with what is installed, we have to resolve discrepency
// Install channel is set to "weekly" or "daily" but stable version of extension is installed. Switch channel to "off" to use the installed version
await this.extensionChannelService.updateChannel('off');
} else {
caseHandled = false;
if (await this.promptToEnrollBackToInsidersIfApplicable(installChannel)) {
return true;
}
return caseHandled;
if (await this.promptToInstallInsidersIfApplicable()) {
return true;
}
if (await this.setInsidersChannelToOffIfApplicable(installChannel)) {
return true;
}
return false;
}

public registerCommandsAndHandlers(): void {
Expand All @@ -74,4 +69,39 @@ export class InsidersExtensionService implements IExtensionSingleActivationServi
this.disposables.push(this.cmdManager.registerCommand(Commands.SwitchToInsidersDaily, () => this.extensionChannelService.updateChannel('daily')));
this.disposables.push(this.cmdManager.registerCommand(Commands.SwitchToInsidersWeekly, () => this.extensionChannelService.updateChannel('weekly')));
}

/**
* If previously in the Insiders Program but not now, request them enroll in the program again
*/
private async promptToEnrollBackToInsidersIfApplicable(installChannel: ExtensionChannels): Promise<boolean> {
if (installChannel === 'off' && !this.extensionChannelService.isChannelUsingDefaultConfiguration) {
// If install channel is explicitly set to off, it means that user has used the insiders program before
await this.insidersPrompt.promptToEnrollBackToInsiders();
return true;
}
return false;
}

/**
* Only when using VSC insiders and if they have not been notified before (usually the first session), notify to enroll into the insiders program
*/
private async promptToInstallInsidersIfApplicable(): Promise<boolean> {
if (this.appEnvironment.channel === 'insiders' && !this.insidersPrompt.hasUserBeenNotified.value && this.extensionChannelService.isChannelUsingDefaultConfiguration) {
await this.insidersPrompt.promptToInstallInsiders();
return true;
}
return false;
}

/**
* When install channel is not in sync with what is installed, resolve discrepency by setting channel to "off"
*/
private async setInsidersChannelToOffIfApplicable(installChannel: ExtensionChannels): Promise<boolean> {
if (installChannel !== 'off' && this.appEnvironment.extensionChannel === 'stable') {
// Install channel is set to "weekly" or "daily" but stable version of extension is installed. Switch channel to "off" to use the installed version
await this.extensionChannelService.updateChannel('off');
return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion src/client/common/insidersBuild/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface IInsiderExtensionPrompt {
* Gets updated to `true` once user has been prompted to install insiders.
*/
readonly hasUserBeenNotified: IPersistentState<boolean>;
notifyToInstallInsiders(): Promise<void>;
promptToInstallInsiders(): Promise<void>;
promptToEnrollBackToInsiders(): Promise<void>;
promptToReload(): Promise<void>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { IPersistentState, IPersistentStateFactory } from '../../../client/commo
import { Common, DataScienceSurveyBanner, ExtensionChannels } from '../../../client/common/utils/localize';

// tslint:disable-next-line: max-func-body-length
suite('xInsiders Extension prompt', () => {
suite('Insiders Extension prompt', () => {
let appShell: IApplicationShell;
let extensionChannelService: IExtensionChannelService;
let cmdManager: ICommandManager;
Expand Down Expand Up @@ -55,7 +55,7 @@ suite('xInsiders Extension prompt', () => {
.setup(u => u.updateValue(true))
.returns(() => Promise.resolve(undefined))
.verifiable(TypeMoq.Times.once());
await insidersPrompt.notifyToInstallInsiders();
await insidersPrompt.promptToInstallInsiders();
verify(appShell.showInformationMessage(ExtensionChannels.promptMessage(), ...prompts)).once();
verify(extensionChannelService.updateChannel(ExtensionChannel.daily)).once();
hasUserBeenNotifiedState.verifyAll();
Expand All @@ -77,7 +77,7 @@ suite('xInsiders Extension prompt', () => {
.setup(u => u.updateValue(true))
.returns(() => Promise.resolve(undefined))
.verifiable(TypeMoq.Times.once());
await insidersPrompt.notifyToInstallInsiders();
await insidersPrompt.promptToInstallInsiders();
verify(appShell.showInformationMessage(ExtensionChannels.promptMessage(), ...prompts)).once();
verify(extensionChannelService.updateChannel(ExtensionChannel.weekly)).once();
hasUserBeenNotifiedState.verifyAll();
Expand All @@ -99,7 +99,7 @@ suite('xInsiders Extension prompt', () => {
.setup(u => u.updateValue(true))
.returns(() => Promise.resolve(undefined))
.verifiable(TypeMoq.Times.once());
await insidersPrompt.notifyToInstallInsiders();
await insidersPrompt.promptToInstallInsiders();
verify(appShell.showInformationMessage(ExtensionChannels.promptMessage(), ...prompts)).once();
verify(extensionChannelService.updateChannel(anything())).never();
hasUserBeenNotifiedState.verifyAll();
Expand All @@ -121,7 +121,7 @@ suite('xInsiders Extension prompt', () => {
.setup(u => u.updateValue(true))
.returns(() => Promise.resolve(undefined))
.verifiable(TypeMoq.Times.once());
await insidersPrompt.notifyToInstallInsiders();
await insidersPrompt.promptToInstallInsiders();
verify(appShell.showInformationMessage(ExtensionChannels.promptMessage(), ...prompts)).once();
verify(extensionChannelService.updateChannel(anything())).never();
hasUserBeenNotifiedState.verifyAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,25 +291,25 @@ suite('Insiders Extension Service - Function handleEdgeCases()', () => {
.returns(() => testParams.hasUserBeenNotified !== undefined ? testParams.hasUserBeenNotified : true);
when(appEnvironment.channel).thenReturn(testParams.vscodeChannel);
when(appEnvironment.extensionChannel).thenReturn(testParams.extensionChannel ? testParams.extensionChannel : 'stable');
when(insidersPrompt.notifyToInstallInsiders()).thenResolve();
when(insidersPrompt.promptToInstallInsiders()).thenResolve();
when(extensionChannelService.updateChannel('off')).thenResolve();
when(extensionChannelService.isChannelUsingDefaultConfiguration).thenReturn(testParams.isChannelUsingDefaultConfiguration !== undefined ? testParams.isChannelUsingDefaultConfiguration : true);
await insidersExtensionService.handleEdgeCases(testParams.installChannel !== undefined ? testParams.installChannel : 'off');
if (testParams.operation === 'Enroll into the program again prompt') {
verify(insidersPrompt.promptToEnrollBackToInsiders()).once();
verify(extensionChannelService.updateChannel('off')).never();
verify(insidersPrompt.notifyToInstallInsiders()).never();
verify(insidersPrompt.promptToInstallInsiders()).never();
} else if (testParams.operation === 'Set channel to off') {
verify(insidersPrompt.promptToEnrollBackToInsiders()).never();
verify(extensionChannelService.updateChannel('off')).once();
verify(insidersPrompt.notifyToInstallInsiders()).never();
verify(insidersPrompt.promptToInstallInsiders()).never();
} else if (testParams.operation === 'Insiders Install Prompt') {
verify(insidersPrompt.promptToEnrollBackToInsiders()).never();
verify(extensionChannelService.updateChannel('off')).never();
verify(insidersPrompt.notifyToInstallInsiders()).once();
verify(insidersPrompt.promptToInstallInsiders()).once();
} else {
verify(extensionChannelService.updateChannel('off')).never();
verify(insidersPrompt.notifyToInstallInsiders()).never();
verify(insidersPrompt.promptToInstallInsiders()).never();
verify(insidersPrompt.promptToEnrollBackToInsiders()).never();
}
});
Expand Down

0 comments on commit 2292854

Please sign in to comment.