Skip to content

Commit 74f4364

Browse files
crisbetojosephperrott
authored andcommitted
fix(tooltip): don't open for mouse and touch focus (#10728)
1 parent be1b5e6 commit 74f4364

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/lib/tooltip/tooltip.spec.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
2121
import {Direction, Directionality} from '@angular/cdk/bidi';
2222
import {OverlayContainer, OverlayModule, CdkScrollable} from '@angular/cdk/overlay';
2323
import {Platform} from '@angular/cdk/platform';
24-
import {dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
24+
import {dispatchFakeEvent, dispatchKeyboardEvent, patchElementFocus} from '@angular/cdk/testing';
2525
import {ESCAPE} from '@angular/cdk/keycodes';
26+
import {FocusMonitor} from '@angular/cdk/a11y';
2627
import {
2728
MatTooltip,
2829
MatTooltipModule,
@@ -39,6 +40,7 @@ describe('MatTooltip', () => {
3940
let overlayContainerElement: HTMLElement;
4041
let dir: {value: Direction};
4142
let platform: {IOS: boolean, isBrowser: boolean};
43+
let focusMonitor: FocusMonitor;
4244

4345
beforeEach(async(() => {
4446
// Set the default Platform override that can be updated before component creation.
@@ -63,9 +65,10 @@ describe('MatTooltip', () => {
6365

6466
TestBed.compileComponents();
6567

66-
inject([OverlayContainer], (oc: OverlayContainer) => {
68+
inject([OverlayContainer, FocusMonitor], (oc: OverlayContainer, fm: FocusMonitor) => {
6769
overlayContainer = oc;
6870
overlayContainerElement = oc.getContainerElement();
71+
focusMonitor = fm;
6972
})();
7073
}));
7174

@@ -576,16 +579,40 @@ describe('MatTooltip', () => {
576579
}));
577580

578581
it('should not show the tooltip on progammatic focus', fakeAsync(() => {
582+
patchElementFocus(buttonElement);
579583
assertTooltipInstance(tooltipDirective, false);
580584

581-
buttonElement.focus();
585+
focusMonitor.focusVia(buttonElement, 'program');
582586
tick(0);
583587
fixture.detectChanges();
584588
tick(500);
585589

586590
expect(overlayContainerElement.querySelector('.mat-tooltip')).toBeNull();
587591
}));
588592

593+
it('should not show the tooltip on mouse focus', fakeAsync(() => {
594+
patchElementFocus(buttonElement);
595+
assertTooltipInstance(tooltipDirective, false);
596+
597+
focusMonitor.focusVia(buttonElement, 'mouse');
598+
tick(0);
599+
fixture.detectChanges();
600+
tick(500);
601+
602+
expect(overlayContainerElement.querySelector('.mat-tooltip')).toBeNull();
603+
}));
604+
605+
it('should not show the tooltip on touch focus', fakeAsync(() => {
606+
patchElementFocus(buttonElement);
607+
assertTooltipInstance(tooltipDirective, false);
608+
609+
focusMonitor.focusVia(buttonElement, 'touch');
610+
tick(0);
611+
fixture.detectChanges();
612+
tick(500);
613+
614+
expect(overlayContainerElement.querySelector('.mat-tooltip')).toBeNull();
615+
}));
589616

590617
});
591618

src/lib/tooltip/tooltip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export class MatTooltip implements OnDestroy {
227227
// Note that the focus monitor runs outside the Angular zone.
228228
if (!origin) {
229229
_ngZone.run(() => this.hide(0));
230-
} else if (origin !== 'program') {
230+
} else if (origin === 'keyboard') {
231231
_ngZone.run(() => this.show());
232232
}
233233
});

0 commit comments

Comments
 (0)