@@ -164,6 +164,24 @@ describe('MatMenu', () => {
164
164
expect ( document . activeElement ) . toBe ( triggerEl ) ;
165
165
} ) ) ;
166
166
167
+ it ( 'should restore focus to the root trigger when the menu was opened by touch' , fakeAsync ( ( ) => {
168
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
169
+ fixture . detectChanges ( ) ;
170
+
171
+ const triggerEl = fixture . componentInstance . triggerEl . nativeElement ;
172
+ dispatchFakeEvent ( triggerEl , 'touchstart' ) ;
173
+ triggerEl . click ( ) ;
174
+ fixture . detectChanges ( ) ;
175
+
176
+ expect ( overlayContainerElement . querySelector ( '.mat-menu-panel' ) ) . toBeTruthy ( ) ;
177
+
178
+ fixture . componentInstance . trigger . closeMenu ( ) ;
179
+ fixture . detectChanges ( ) ;
180
+ flush ( ) ;
181
+
182
+ expect ( document . activeElement ) . toBe ( triggerEl ) ;
183
+ } ) ) ;
184
+
167
185
it ( 'should scroll the panel to the top on open, when it is scrollable' , fakeAsync ( ( ) => {
168
186
const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
169
187
fixture . detectChanges ( ) ;
@@ -247,6 +265,27 @@ describe('MatMenu', () => {
247
265
focusMonitor . stopMonitoring ( triggerEl ) ;
248
266
} ) ) ;
249
267
268
+ it ( 'should set the proper focus origin when restoring focus after opening by touch' ,
269
+ fakeAsync ( ( ) => {
270
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
271
+ fixture . detectChanges ( ) ;
272
+ const triggerEl = fixture . componentInstance . triggerEl . nativeElement ;
273
+
274
+ dispatchMouseEvent ( triggerEl , 'touchstart' ) ;
275
+ triggerEl . click ( ) ;
276
+ fixture . detectChanges ( ) ;
277
+ patchElementFocus ( triggerEl ) ;
278
+ focusMonitor . monitor ( triggerEl , false ) ;
279
+ fixture . componentInstance . trigger . closeMenu ( ) ;
280
+ fixture . detectChanges ( ) ;
281
+ tick ( 500 ) ;
282
+ fixture . detectChanges ( ) ;
283
+ flush ( ) ;
284
+
285
+ expect ( triggerEl . classList ) . toContain ( 'cdk-touch-focused' ) ;
286
+ focusMonitor . stopMonitoring ( triggerEl ) ;
287
+ } ) ) ;
288
+
250
289
it ( 'should close the menu when pressing ESCAPE' , fakeAsync ( ( ) => {
251
290
const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
252
291
fixture . detectChanges ( ) ;
@@ -378,6 +417,21 @@ describe('MatMenu', () => {
378
417
expect ( fixture . componentInstance . items . first . focus ) . toHaveBeenCalledWith ( 'mouse' ) ;
379
418
} ) ) ;
380
419
420
+ it ( 'should set the proper focus origin when opening by touch' , fakeAsync ( ( ) => {
421
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
422
+ fixture . detectChanges ( ) ;
423
+ spyOn ( fixture . componentInstance . items . first , 'focus' ) . and . callThrough ( ) ;
424
+
425
+ const triggerEl = fixture . componentInstance . triggerEl . nativeElement ;
426
+
427
+ dispatchMouseEvent ( triggerEl , 'touchstart' ) ;
428
+ triggerEl . click ( ) ;
429
+ fixture . detectChanges ( ) ;
430
+ flush ( ) ;
431
+
432
+ expect ( fixture . componentInstance . items . first . focus ) . toHaveBeenCalledWith ( 'touch' ) ;
433
+ } ) ) ;
434
+
381
435
it ( 'should close the menu when using the CloseScrollStrategy' , fakeAsync ( ( ) => {
382
436
const scrolledSubject = new Subject ( ) ;
383
437
const fixture = createComponent ( SimpleMenu , [
0 commit comments