|
4 | 4 | dispatchKeyboardEvent, |
5 | 5 | dispatchMouseEvent, |
6 | 6 | patchElementFocus, |
| 7 | + createMouseEvent, |
| 8 | + dispatchEvent, |
7 | 9 | } from '@angular/cdk/testing/private'; |
8 | 10 | import {Component, NgZone} from '@angular/core'; |
9 | 11 | import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing'; |
@@ -118,6 +120,26 @@ describe('FocusMonitor', () => { |
118 | 120 | expect(changeHandler).toHaveBeenCalledWith('program'); |
119 | 121 | })); |
120 | 122 |
|
| 123 | + it('should detect fake mousedown from a screen reader', fakeAsync(() => { |
| 124 | + // Simulate focus via a fake mousedown from a screen reader. |
| 125 | + dispatchMouseEvent(buttonElement, 'mousedown'); |
| 126 | + const event = createMouseEvent('mousedown'); |
| 127 | + Object.defineProperty(event, 'buttons', {get: () => 0}); |
| 128 | + dispatchEvent(buttonElement, event); |
| 129 | + |
| 130 | + buttonElement.focus(); |
| 131 | + fixture.detectChanges(); |
| 132 | + flush(); |
| 133 | + |
| 134 | + expect(buttonElement.classList.length) |
| 135 | + .toBe(2, 'button should have exactly 2 focus classes'); |
| 136 | + expect(buttonElement.classList.contains('cdk-focused')) |
| 137 | + .toBe(true, 'button should have cdk-focused class'); |
| 138 | + expect(buttonElement.classList.contains('cdk-keyboard-focused')) |
| 139 | + .toBe(true, 'button should have cdk-keyboard-focused class'); |
| 140 | + expect(changeHandler).toHaveBeenCalledWith('keyboard'); |
| 141 | + })); |
| 142 | + |
121 | 143 | it('focusVia keyboard should simulate keyboard focus', fakeAsync(() => { |
122 | 144 | focusMonitor.focusVia(buttonElement, 'keyboard'); |
123 | 145 | flush(); |
|
0 commit comments