@@ -16,7 +16,7 @@ import {MdInputModule} from '../input/index';
1616import { Dir , LayoutDirection } from '../core/rtl/dir' ;
1717import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
1818import { 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' ;
2020import { MdOption } from '../core/option/option' ;
2121import { MdAutocomplete } from './autocomplete' ;
2222import { MdInputContainer } from '../input/input-container' ;
@@ -758,6 +758,53 @@ describe('MdAutocomplete', () => {
758758 expect ( scrollContainer . scrollTop ) . toEqual ( 272 , `Expected panel to reveal last option.` ) ;
759759 } ) ) ;
760760
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+
761808 } ) ;
762809
763810 describe ( 'aria' , ( ) => {
0 commit comments