|
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';
|
@@ -112,6 +114,26 @@ describe('FocusMonitor', () => {
|
112 | 114 | expect(changeHandler).toHaveBeenCalledWith('program');
|
113 | 115 | }));
|
114 | 116 |
|
| 117 | + it('should detect fake mousedown from a screen reader', fakeAsync(() => { |
| 118 | + // Simulate focus via a fake mousedown from a screen reader. |
| 119 | + dispatchMouseEvent(buttonElement, 'mousedown'); |
| 120 | + const event = createMouseEvent('mousedown'); |
| 121 | + Object.defineProperty(event, 'buttons', {get: () => 0}); |
| 122 | + dispatchEvent(buttonElement, event); |
| 123 | + |
| 124 | + buttonElement.focus(); |
| 125 | + fixture.detectChanges(); |
| 126 | + flush(); |
| 127 | + |
| 128 | + expect(buttonElement.classList.length) |
| 129 | + .toBe(2, 'button should have exactly 2 focus classes'); |
| 130 | + expect(buttonElement.classList.contains('cdk-focused')) |
| 131 | + .toBe(true, 'button should have cdk-focused class'); |
| 132 | + expect(buttonElement.classList.contains('cdk-keyboard-focused')) |
| 133 | + .toBe(true, 'button should have cdk-keyboard-focused class'); |
| 134 | + expect(changeHandler).toHaveBeenCalledWith('keyboard'); |
| 135 | + })); |
| 136 | + |
115 | 137 | it('focusVia keyboard should simulate keyboard focus', fakeAsync(() => {
|
116 | 138 | focusMonitor.focusVia(buttonElement, 'keyboard');
|
117 | 139 | flush();
|
|
0 commit comments