@@ -12,6 +12,9 @@ import { CommandService } from 'vs/platform/commands/common/commandService';
1212import { IExtensionService , ExtensionPointContribution , IExtensionDescription } from 'vs/platform/extensions/common/extensions' ;
1313import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService' ;
1414import { IExtensionPoint } from 'vs/platform/extensions/common/extensionsRegistry' ;
15+ import { ContextKeyService } from 'vs/platform/contextkey/browser/contextKeyService' ;
16+ import { SimpleConfigurationService } from 'vs/editor/standalone/browser/simpleServices' ;
17+ import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey' ;
1518
1619class SimpleExtensionService implements IExtensionService {
1720 _serviceBrand : any ;
@@ -62,7 +65,7 @@ suite('CommandService', function () {
6265 lastEvent = activationEvent ;
6366 return super . activateByEvent ( activationEvent ) ;
6467 }
65- } ) ;
68+ } , new ContextKeyService ( new SimpleConfigurationService ( ) ) ) ;
6669
6770 return service . executeCommand ( 'foo' ) . then ( ( ) => {
6871 assert . ok ( lastEvent , 'onCommand:foo' ) ;
@@ -80,7 +83,7 @@ suite('CommandService', function () {
8083 activateByEvent ( activationEvent : string ) : TPromise < void > {
8184 return TPromise . wrapError < void > ( new Error ( 'bad_activate' ) ) ;
8285 }
83- } ) ;
86+ } , new ContextKeyService ( new SimpleConfigurationService ( ) ) ) ;
8487
8588 return service . executeCommand ( 'foo' ) . then ( ( ) => assert . ok ( false ) , err => {
8689 assert . equal ( err . message , 'bad_activate' ) ;
@@ -97,12 +100,37 @@ suite('CommandService', function () {
97100 onReady ( ) {
98101 return new TPromise < boolean > ( _resolve => { resolve = _resolve ; } ) ;
99102 }
100- } ) ;
103+ } , new ContextKeyService ( new SimpleConfigurationService ( ) ) ) ;
101104
102105 return service . executeCommand ( 'bar' ) . then ( ( ) => {
103106 reg . dispose ( ) ;
104107 assert . equal ( callCounter , 1 ) ;
105108 } ) ;
106109 } ) ;
107110
108- } ) ;
111+ test ( 'honor command-precondition' , function ( ) {
112+ let contextKeyService = new ContextKeyService ( new SimpleConfigurationService ( ) ) ;
113+ let commandService = new CommandService (
114+ new InstantiationService ( ) ,
115+ new SimpleExtensionService ( ) ,
116+ contextKeyService
117+ ) ;
118+
119+ let counter = 0 ;
120+ let reg = CommandsRegistry . registerCommand ( 'bar' , {
121+ handler : ( ) => { counter += 1 ; } ,
122+ precondition : ContextKeyExpr . has ( 'foocontext' )
123+ } ) ;
124+
125+ return commandService . executeCommand ( 'bar' ) . then ( ( ) => {
126+ assert . throws ( ( ) => { } ) ;
127+ } , ( ) => {
128+ contextKeyService . setContext ( 'foocontext' , true ) ;
129+ return commandService . executeCommand ( 'bar' ) ;
130+ } ) . then ( ( ) => {
131+ assert . equal ( counter , 1 ) ;
132+ reg . dispose ( ) ;
133+ } ) ;
134+
135+ } ) ;
136+ } ) ;
0 commit comments