@@ -16,7 +16,7 @@ import {MdInputModule} from '../input/index';
16
16
import { Dir , LayoutDirection } from '../core/rtl/dir' ;
17
17
import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
18
18
import { Subscription } from 'rxjs/Subscription' ;
19
- import { ENTER , DOWN_ARROW , SPACE , UP_ARROW } from '../core/keyboard/keycodes' ;
19
+ import { ENTER , DOWN_ARROW , SPACE , UP_ARROW , HOME , END , ESCAPE } from '../core/keyboard/keycodes' ;
20
20
import { MdOption } from '../core/option/option' ;
21
21
import { MdAutocomplete } from './autocomplete' ;
22
22
import { MdInputContainer } from '../input/input-container' ;
@@ -758,6 +758,53 @@ describe('MdAutocomplete', () => {
758
758
expect ( scrollContainer . scrollTop ) . toEqual ( 272 , `Expected panel to reveal last option.` ) ;
759
759
} ) ) ;
760
760
761
+ it ( 'should scroll the active option into view when pressing END' , fakeAsync ( ( ) => {
762
+ tick ( ) ;
763
+ const scrollContainer =
764
+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
765
+
766
+ const END_EVENT = createKeyboardEvent ( 'keydown' , END ) ;
767
+ fixture . componentInstance . trigger . _handleKeydown ( END_EVENT ) ;
768
+ tick ( ) ;
769
+ fixture . detectChanges ( ) ;
770
+
771
+ // Expect option bottom minus the panel height (528 - 256 = 272)
772
+ expect ( scrollContainer . scrollTop ) . toEqual ( 272 , 'Expected panel to reveal the last option.' ) ;
773
+ } ) ) ;
774
+
775
+ it ( 'should scroll the active option into view when pressing HOME' , fakeAsync ( ( ) => {
776
+ tick ( ) ;
777
+ const scrollContainer =
778
+ document . querySelector ( '.cdk-overlay-pane .mat-autocomplete-panel' ) ;
779
+
780
+ scrollContainer . scrollTop = 100 ;
781
+ fixture . detectChanges ( ) ;
782
+
783
+ const HOME_EVENT = createKeyboardEvent ( 'keydown' , HOME ) ;
784
+ fixture . componentInstance . trigger . _handleKeydown ( HOME_EVENT ) ;
785
+ tick ( ) ;
786
+ fixture . detectChanges ( ) ;
787
+
788
+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , 'Expected panel to reveal the first option.' ) ;
789
+ } ) ) ;
790
+
791
+ it ( 'should close the panel when pressing escape' , async ( ( ) => {
792
+ const trigger = fixture . componentInstance . trigger ;
793
+ const escapeEvent = createKeyboardEvent ( 'keydown' , ESCAPE ) ;
794
+
795
+ input . focus ( ) ;
796
+
797
+ fixture . whenStable ( ) . then ( ( ) => {
798
+ expect ( document . activeElement ) . toBe ( input , 'Expected input to be focused.' ) ;
799
+ expect ( trigger . panelOpen ) . toBe ( true , 'Expected panel to be open.' ) ;
800
+
801
+ trigger . _handleKeydown ( escapeEvent ) ;
802
+
803
+ expect ( document . activeElement ) . toBe ( input , 'Expected input to continue to be focused.' ) ;
804
+ expect ( trigger . panelOpen ) . toBe ( false , 'Expected panel to be closed.' ) ;
805
+ } ) ;
806
+ } ) ) ;
807
+
761
808
} ) ;
762
809
763
810
describe ( 'aria' , ( ) => {
0 commit comments