99 dispatchKeyboardEvent ,
1010 dispatchMouseEvent ,
1111} from '@angular/cdk/testing/private' ;
12- import { Component , Type , ViewChild , Provider , Directive } from '@angular/core' ;
12+ import { Component , Type , ViewChild , Provider , Directive , ViewEncapsulation } from '@angular/core' ;
1313import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
1414import {
1515 FormControl ,
@@ -27,6 +27,7 @@ import {
2727import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
2828import { DEC , JAN , JUL , JUN , SEP } from '@angular/material/testing' ;
2929import { By } from '@angular/platform-browser' ;
30+ import { _supportsShadowDom } from '@angular/cdk/platform' ;
3031import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing' ;
3132import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
3233import { MAT_DIALOG_DEFAULT_OPTIONS , MatDialogConfig } from '@angular/material/dialog' ;
@@ -1139,6 +1140,33 @@ describe('MatDatepicker', () => {
11391140 expect ( document . activeElement ) . toBe ( toggle , 'Expected focus to be restored to toggle.' ) ;
11401141 } ) ;
11411142
1143+ it ( 'should restore focus when placed inside a shadow root' , ( ) => {
1144+ if ( ! _supportsShadowDom ( ) ) {
1145+ return ;
1146+ }
1147+
1148+ fixture . destroy ( ) ;
1149+ TestBed . resetTestingModule ( ) ;
1150+ fixture = createComponent ( DatepickerWithToggleInShadowDom , [ MatNativeDateModule ] ) ;
1151+ fixture . detectChanges ( ) ;
1152+ testComponent = fixture . componentInstance ;
1153+
1154+ const toggle = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
1155+ fixture . componentInstance . touchUI = false ;
1156+ fixture . detectChanges ( ) ;
1157+
1158+ toggle . focus ( ) ;
1159+ spyOn ( toggle , 'focus' ) . and . callThrough ( ) ;
1160+ fixture . componentInstance . datepicker . open ( ) ;
1161+ fixture . detectChanges ( ) ;
1162+ fixture . componentInstance . datepicker . close ( ) ;
1163+ fixture . detectChanges ( ) ;
1164+
1165+ // We have to assert by looking at the `focus` method, because
1166+ // `document.activeElement` will return the shadow root.
1167+ expect ( toggle . focus ) . toHaveBeenCalled ( ) ;
1168+ } ) ;
1169+
11421170 it ( 'should allow for focus restoration to be disabled' , ( ) => {
11431171 let toggle = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
11441172
@@ -2352,6 +2380,17 @@ class DatepickerWithToggle {
23522380}
23532381
23542382
2383+ @Component ( {
2384+ encapsulation : ViewEncapsulation . ShadowDom ,
2385+ template : `
2386+ <input [matDatepicker]="d">
2387+ <mat-datepicker-toggle [for]="d" [aria-label]="ariaLabel"></mat-datepicker-toggle>
2388+ <mat-datepicker #d [touchUi]="touchUI" [restoreFocus]="restoreFocus"></mat-datepicker>
2389+ ` ,
2390+ } )
2391+ class DatepickerWithToggleInShadowDom extends DatepickerWithToggle { }
2392+
2393+
23552394@Component ( {
23562395 template : `
23572396 <input [matDatepicker]="d">
0 commit comments