@@ -28,6 +28,7 @@ import { FakeExtensionContext } from '../fakeExtensionContext'
28
28
import { InstanceIdentity } from '../../shared/clients/ec2MetadataClient'
29
29
import { extensionVersion } from '../../shared/vscode/env'
30
30
import { sleep } from '../../shared/utilities/timeoutUtils'
31
+ import globals from '../../shared/extensionGlobals'
31
32
32
33
describe ( 'extensionUtilities' , function ( ) {
33
34
describe ( 'safeGet' , function ( ) {
@@ -205,102 +206,112 @@ describe('ExtensionUserActivity', function () {
205
206
} )
206
207
207
208
it ( 'triggers twice when multiple user activities are fired in separate intervals' , async function ( ) {
208
- const throttleDelay = 2000
209
- const middleOfInterval = Math . floor ( throttleDelay / 2 )
210
- const waitFor = throttleDelay * 3 // delay * (2 event intervals + test buffer)
211
-
212
- const eventsFirst = [
213
- delayedFiringEvent ( middleOfInterval + 1 ) ,
214
- delayedFiringEvent ( middleOfInterval + 2 ) ,
215
- delayedFiringEvent ( middleOfInterval + 3 ) ,
216
- delayedFiringEvent ( middleOfInterval + 4 ) ,
217
- ]
218
- const eventsSecond = [
219
- delayedFiringEvent ( throttleDelay + middleOfInterval + 1 ) ,
220
- delayedFiringEvent ( throttleDelay + middleOfInterval + 2 ) ,
221
- delayedFiringEvent ( throttleDelay + middleOfInterval + 2 ) ,
222
- delayedFiringEvent ( throttleDelay + middleOfInterval + 3 ) ,
209
+ const throttleDelay = 200
210
+
211
+ const firstInvervalMillisUntilFire = [ 51 , 52 , 53 , 54 ]
212
+
213
+ const secondIntervalStart = firstInvervalMillisUntilFire [ 0 ] + throttleDelay + 1
214
+ const secondIntervalMillisUntilFire = [
215
+ secondIntervalStart + 10 ,
216
+ secondIntervalStart + 11 ,
217
+ secondIntervalStart + 11 ,
218
+ secondIntervalStart + 12 ,
223
219
]
224
-
225
- const instance = new ExtensionUserActivity ( throttleDelay , [ ...eventsFirst , ...eventsSecond ] )
220
+
221
+ const instance = new ExtensionUserActivity ( throttleDelay , [
222
+ ...firstInvervalMillisUntilFire . map ( delayedTriggeredEvent ) ,
223
+ ...secondIntervalMillisUntilFire . map ( delayedTriggeredEvent ) ,
224
+ ] )
226
225
instance . onUserActivity ( onEventTriggered )
227
- await sleep ( waitFor )
226
+ await sleep ( secondIntervalStart + throttleDelay + 1 )
228
227
229
- assert . strictEqual ( count , 2 , 'This may be flaky in CI, so we may need to increase the intervals for better tolerance' )
228
+ assert . strictEqual ( count , 2 )
230
229
} )
231
230
232
- describe ( 'does not fire user activity events in specific scenarios' , function ( ) {
231
+ describe ( 'does not fire user activity events in specific scenarios' , function ( ) {
233
232
let userActivitySubscriber : sinon . SinonStubbedMember < ( ) => void >
234
233
let _triggerUserActivity : ( obj : any ) => void
235
234
let instance : ExtensionUserActivity
236
235
237
236
beforeEach ( function ( ) {
238
237
userActivitySubscriber = sandbox . stub ( )
239
- _triggerUserActivity = ( ) => { throw Error ( 'Called before ExtensionUserActivity was instantiated' ) }
238
+ _triggerUserActivity = ( ) => {
239
+ throw Error ( 'Called before ExtensionUserActivity was instantiated' )
240
+ }
240
241
} )
241
242
242
243
afterEach ( function ( ) {
243
244
instance . dispose ( )
244
245
sandbox . restore ( )
245
246
} )
246
247
247
- it ( 'does not fire onDidChangeWindowState when not active' , function ( ) {
248
+ it ( 'does not fire onDidChangeWindowState when not active' , function ( ) {
248
249
stubUserActivityEvent ( vscode . window , 'onDidChangeWindowState' )
249
-
250
+
250
251
const triggerUserActivity = createTriggerActivityFunc ( )
251
252
252
- triggerUserActivity ( { active : false } )
253
+ triggerUserActivity ( { active : false } )
253
254
assert . strictEqual ( userActivitySubscriber . callCount , 0 )
254
255
255
- triggerUserActivity ( { active : true } )
256
+ triggerUserActivity ( { active : true } )
256
257
assert . strictEqual ( userActivitySubscriber . callCount , 1 )
257
258
} )
258
259
259
- it ( 'does not fire onDidChangeTextEditorSelection when editor is `Output` panel' , function ( ) {
260
+ it ( 'does not fire onDidChangeTextEditorSelection when editor is `Output` panel' , function ( ) {
260
261
stubUserActivityEvent ( vscode . window , 'onDidChangeTextEditorSelection' )
261
-
262
+
262
263
const triggerUserActivity = createTriggerActivityFunc ( )
263
264
264
- triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'output' } } } } )
265
+ triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'output' } } } } )
265
266
assert . strictEqual ( userActivitySubscriber . callCount , 0 )
266
267
267
- triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'NOToutput' } } } } )
268
+ triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'NOToutput' } } } } )
268
269
assert . strictEqual ( userActivitySubscriber . callCount , 1 )
269
270
} )
270
271
271
- it ( 'does not fire onDidChangeTextEditorVisibleRanges when when editor is `Output` panel' , function ( ) {
272
+ it ( 'does not fire onDidChangeTextEditorVisibleRanges when when editor is `Output` panel' , function ( ) {
272
273
stubUserActivityEvent ( vscode . window , 'onDidChangeTextEditorVisibleRanges' )
273
-
274
+
274
275
const triggerUserActivity = createTriggerActivityFunc ( )
275
276
276
- triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'output' } } } } )
277
+ triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'output' } } } } )
277
278
assert . strictEqual ( userActivitySubscriber . callCount , 0 )
278
279
279
- triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'NOToutput' } } } } )
280
+ triggerUserActivity ( { textEditor : { document : { uri : { scheme : 'NOToutput' } } } } )
280
281
assert . strictEqual ( userActivitySubscriber . callCount , 1 )
281
282
} )
282
283
283
- it ( 'does not fire onDidChangeTextDocument when not the active user document' , function ( ) {
284
+ it ( 'does not fire onDidChangeTextDocument when not the active user document' , function ( ) {
284
285
stubUserActivityEvent ( vscode . workspace , 'onDidChangeTextDocument' )
285
286
const activeEditorStub = sandbox . stub ( vscode . window , 'activeTextEditor' )
286
-
287
+
287
288
const triggerUserActivity = createTriggerActivityFunc ( )
288
289
289
290
activeEditorStub . get ( ( ) => undefined )
290
291
triggerUserActivity ( { } )
291
292
assert . strictEqual ( userActivitySubscriber . callCount , 0 , 'Was not ignored when no active editor' )
292
293
293
- activeEditorStub . get ( ( ) => { return { document : { uri : 'myUri' } } } )
294
- triggerUserActivity ( { document : { uri : 'myOtherUri' } } )
295
- assert . strictEqual ( userActivitySubscriber . callCount , 0 , 'Was not ignored when active editor document was different from the event' )
294
+ activeEditorStub . get ( ( ) => {
295
+ return { document : { uri : 'myUri' } }
296
+ } )
297
+ triggerUserActivity ( { document : { uri : 'myOtherUri' } } )
298
+ assert . strictEqual (
299
+ userActivitySubscriber . callCount ,
300
+ 0 ,
301
+ 'Was not ignored when active editor document was different from the event'
302
+ )
296
303
297
- triggerUserActivity ( { document : { uri : 'myUri' } } )
298
- assert . strictEqual ( userActivitySubscriber . callCount , 1 , 'Was ignored when the active editor document was the same as the event' )
304
+ triggerUserActivity ( { document : { uri : 'myUri' } } )
305
+ assert . strictEqual (
306
+ userActivitySubscriber . callCount ,
307
+ 1 ,
308
+ 'Was ignored when the active editor document was the same as the event'
309
+ )
299
310
} )
300
311
301
312
it ( 'fires for onDidChangeActiveColorTheme (sanity check)' , function ( ) {
302
313
stubUserActivityEvent ( vscode . window , 'onDidChangeActiveColorTheme' )
303
-
314
+
304
315
const triggerUserActivity = createTriggerActivityFunc ( )
305
316
306
317
triggerUserActivity ( { } )
@@ -309,23 +320,20 @@ describe('ExtensionUserActivity', function () {
309
320
310
321
/**
311
322
* Helper to stub a vscode event object.
312
- *
323
+ *
313
324
* Once stubbed, you can call {@link _triggerUserActivity} to fire
314
325
* the event.
315
326
*/
316
- function stubUserActivityEvent < T , K extends keyof T > ( vscodeObj : T , eventName : K ) {
317
- const eventStub = sandbox . stub (
318
- vscodeObj ,
319
- eventName
320
- )
321
-
327
+ function stubUserActivityEvent < T , K extends keyof T > ( vscodeObj : T , eventName : K ) {
328
+ const eventStub = sandbox . stub ( vscodeObj , eventName )
329
+
322
330
eventStub . callsFake ( ( callback : any ) => {
323
331
_triggerUserActivity = callback
324
332
return {
325
- dispose : sandbox . stub ( )
333
+ dispose : sandbox . stub ( ) ,
326
334
}
327
335
} )
328
-
336
+
329
337
return eventStub
330
338
}
331
339
@@ -339,9 +347,9 @@ describe('ExtensionUserActivity', function () {
339
347
}
340
348
} )
341
349
342
- function delayedFiringEvent ( fireInMillis : number ) : vscode . Event < any > {
350
+ function delayedTriggeredEvent ( millisUntilFire : number ) : vscode . Event < any > {
343
351
const event = new vscode . EventEmitter < void > ( )
344
- setTimeout ( ( ) => event . fire ( ) , fireInMillis )
352
+ globals . clock . setTimeout ( ( ) => event . fire ( ) , millisUntilFire )
345
353
return event . event
346
354
}
347
355
} )
0 commit comments