Skip to content

Commit

Permalink
feat(cdk/a11y): Respond to PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zelliott committed Apr 7, 2021
1 parent 3ee15ef commit f0ed573
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
40 changes: 20 additions & 20 deletions src/cdk/a11y/input-modality/input-modality-detector.spec.ts
Expand Up @@ -30,57 +30,57 @@ describe('InputModalityDetector', () => {
it('should do nothing on non-browser platforms', () => {
platform.isBrowser = false;
detector = new InputModalityDetector(platform, ngZone, document);
expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);

dispatchKeyboardEvent(document, 'keydown');
expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);

dispatchMouseEvent(document, 'mousedown');
expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);

dispatchTouchEvent(document, 'touchstart');
expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);
});

it('should detect keyboard input modality', () => {
detector = new InputModalityDetector(platform, ngZone, document);
dispatchKeyboardEvent(document, 'keydown');
expect(detector.inputModality).toBe('keyboard');
expect(detector.mostRecentModality).toBe('keyboard');
});

it('should detect mouse input modality', () => {
detector = new InputModalityDetector(platform, ngZone, document);
dispatchMouseEvent(document, 'mousedown');
expect(detector.inputModality).toBe('mouse');
expect(detector.mostRecentModality).toBe('mouse');
});

it('should detect touch input modality', () => {
detector = new InputModalityDetector(platform, ngZone, document);
dispatchTouchEvent(document, 'touchstart');
expect(detector.inputModality).toBe('touch');
expect(detector.mostRecentModality).toBe('touch');
});

it('should detect changes in input modality', () => {
detector = new InputModalityDetector(platform, ngZone, document);

dispatchKeyboardEvent(document, 'keydown');
expect(detector.inputModality).toBe('keyboard');
expect(detector.mostRecentModality).toBe('keyboard');

dispatchMouseEvent(document, 'mousedown');
expect(detector.inputModality).toBe('mouse');
expect(detector.mostRecentModality).toBe('mouse');

dispatchTouchEvent(document, 'touchstart');
expect(detector.inputModality).toBe('touch');
expect(detector.mostRecentModality).toBe('touch');

dispatchKeyboardEvent(document, 'keydown');
expect(detector.inputModality).toBe('keyboard');
expect(detector.mostRecentModality).toBe('keyboard');
});

it('should emit changes in input modality', () => {
detector = new InputModalityDetector(platform, ngZone, document);
const emitted: InputModality[] = [];
detector.inputModalityChange.subscribe((inputModality: InputModality) => {
emitted.push(inputModality);
detector.modalityChanges.subscribe((modality: InputModality) => {
emitted.push(modality);
});

expect(emitted.length).toBe(0);
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('InputModalityDetector', () => {
Object.defineProperty(event, 'buttons', {get: () => 0});
dispatchEvent(document, event);

expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);
});

it('should ignore fake screen-reader touch events', () => {
Expand All @@ -123,7 +123,7 @@ describe('InputModalityDetector', () => {
Object.defineProperty(event, 'touches', {get: () => [{identifier: -1}]});
dispatchEvent(document, event);

expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);
});

it('should ignore certain modifier keys by default', () => {
Expand All @@ -134,13 +134,13 @@ describe('InputModalityDetector', () => {
dispatchKeyboardEvent(document, 'keydown', META);
dispatchKeyboardEvent(document, 'keydown', SHIFT);

expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);
});

it('should not ignore modifier keys if specified', () => {
detector = new InputModalityDetector(platform, ngZone, document, {ignoreKeys: []});
dispatchKeyboardEvent(document, 'keydown', CONTROL);
expect(detector.inputModality).toBe('keyboard');
expect(detector.mostRecentModality).toBe('keyboard');
});

it('should ignore additional keys if specified', () => {
Expand All @@ -150,18 +150,18 @@ describe('InputModalityDetector', () => {
dispatchKeyboardEvent(document, 'keydown', B);
dispatchKeyboardEvent(document, 'keydown', C);

expect(detector.inputModality).toBe(null);
expect(detector.mostRecentModality).toBe(null);
});

it('should ignore mouse events that occur too closely after a touch event', fakeAsync(() => {
detector = new InputModalityDetector(platform, ngZone, document);

dispatchTouchEvent(document, 'touchstart');
dispatchMouseEvent(document, 'mousedown');
expect(detector.inputModality).toBe('touch');
expect(detector.mostRecentModality).toBe('touch');

tick(TOUCH_BUFFER_MS);
dispatchMouseEvent(document, 'mousedown');
expect(detector.inputModality).toBe('mouse');
expect(detector.mostRecentModality).toBe('mouse');
}));
});
30 changes: 15 additions & 15 deletions src/cdk/a11y/input-modality/input-modality-detector.ts
Expand Up @@ -72,12 +72,12 @@ const modalityEventListenerOptions = normalizePassiveListenerOptions({
/**
* Service that detects the user's input modality.
*
* Note: This service will not update the input modality when a user is navigating with a screen
* reader (e.g. linear navigation with VoiceOver, object navigation / browse mode with NVDA, virtual
* PC cursor mode with JAWS). This is in part due to technical limitations (i.e. keyboard events
* do not fire as expected in these modes) but is also arguably the correct behavior. Navigating
* with a screen reader is akin to visually scanning a page, and should not be interpreted as actual
* user input interaction.
* This service does not update the input modality when a user navigates with a screen reader
* (e.g. linear navigation with VoiceOver, object navigation / browse mode with NVDA, virtual PC
* cursor mode with JAWS). This is in part due to technical limitations (i.e. keyboard events do not
* fire as expected in these modes) but is also arguably the correct behavior. Navigating with a
* screen reader is akin to visually scanning a page, and should not be interpreted as actual user
* input interaction.
*
* When a user is not navigating but *interacting* with a screen reader, this service's behavior is
* largely undefined and depends on the events fired. For example, in VoiceOver, no keyboard events
Expand All @@ -87,15 +87,15 @@ const modalityEventListenerOptions = normalizePassiveListenerOptions({
@Injectable({ providedIn: 'root' })
export class InputModalityDetector implements OnDestroy {
/** Emits when the input modality changes. */
readonly inputModalityChange: Observable<InputModality>;
readonly modalityChanges: Observable<InputModality>;

/** Returns the most recently detected input modality. */
get inputModality(): InputModality {
return this._inputModality.value;
/** The most recently detected input modality. */
get mostRecentModality(): InputModality {
return this._modality.value;
}

/** The underlying BehaviorSubject that emits whenever an input modality is detected. */
private readonly _inputModality = new BehaviorSubject<InputModality>(null);
private readonly _modality = new BehaviorSubject<InputModality>(null);

/** Options for this InputModalityDetector. */
private readonly _options: InputModalityDetectorOptions;
Expand All @@ -115,7 +115,7 @@ export class InputModalityDetector implements OnDestroy {
// modality to keyboard.
if (this._options?.ignoreKeys?.some(keyCode => keyCode === event.keyCode)) { return; }

this._inputModality.next('keyboard');
this._modality.next('keyboard');
}

/**
Expand All @@ -130,7 +130,7 @@ export class InputModalityDetector implements OnDestroy {
// after the previous touch event.
if (Date.now() - this._lastTouchMs < TOUCH_BUFFER_MS) { return; }

this._inputModality.next('mouse');
this._modality.next('mouse');
}

/**
Expand All @@ -144,7 +144,7 @@ export class InputModalityDetector implements OnDestroy {
// triggered via mouse vs touch.
this._lastTouchMs = Date.now();

this._inputModality.next('touch');
this._modality.next('touch');
}

constructor(
Expand All @@ -160,7 +160,7 @@ export class InputModalityDetector implements OnDestroy {
};

// Only emit if the input modality changes, and skip the first emission as it's null.
this.inputModalityChange = this._inputModality.pipe(distinctUntilChanged(), skip(1));
this.modalityChanges = this._modality.pipe(distinctUntilChanged(), skip(1));

// If we're not in a browser, this service should do nothing, as there's no relevant input
// modality to detect.
Expand Down
2 changes: 1 addition & 1 deletion src/dev-app/input-modality/input-modality-detector-demo.ts
Expand Up @@ -24,7 +24,7 @@ export class InputModalityDetectorDemo implements OnDestroy {
inputModalityDetector: InputModalityDetector,
ngZone: NgZone,
) {
inputModalityDetector.inputModalityChange
inputModalityDetector.modalityChanges
.pipe(takeUntil(this._destroyed))
.subscribe(modality => ngZone.run(() => { this._modality = modality; }));
}
Expand Down

0 comments on commit f0ed573

Please sign in to comment.