|
| 1 | +import {A, ALT, B, C, CONTROL, META, SHIFT} from '@angular/cdk/keycodes'; |
| 2 | +import {Platform} from '@angular/cdk/platform'; |
| 3 | +import {NgZone, PLATFORM_ID} from '@angular/core'; |
| 4 | + |
| 5 | +import { |
| 6 | + createMouseEvent, |
| 7 | + dispatchKeyboardEvent, |
| 8 | + dispatchMouseEvent, |
| 9 | + dispatchTouchEvent, |
| 10 | + dispatchEvent, |
| 11 | + createTouchEvent, |
| 12 | +} from '@angular/cdk/testing/private'; |
| 13 | +import {fakeAsync, inject, tick} from '@angular/core/testing'; |
| 14 | +import {InputModality, InputModalityDetector, TOUCH_BUFFER_MS} from './input-modality-detector'; |
| 15 | + |
| 16 | +describe('InputModalityDetector', () => { |
| 17 | + let platform: Platform; |
| 18 | + let ngZone: NgZone; |
| 19 | + let detector: InputModalityDetector; |
| 20 | + |
| 21 | + beforeEach(inject([PLATFORM_ID], (platformId: Object) => { |
| 22 | + platform = new Platform(platformId); |
| 23 | + ngZone = new NgZone({}); |
| 24 | + })); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + detector?.ngOnDestroy(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should do nothing on non-browser platforms', () => { |
| 31 | + platform.isBrowser = false; |
| 32 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 33 | + expect(detector.mostRecentModality).toBe(null); |
| 34 | + |
| 35 | + dispatchKeyboardEvent(document, 'keydown'); |
| 36 | + expect(detector.mostRecentModality).toBe(null); |
| 37 | + |
| 38 | + dispatchMouseEvent(document, 'mousedown'); |
| 39 | + expect(detector.mostRecentModality).toBe(null); |
| 40 | + |
| 41 | + dispatchTouchEvent(document, 'touchstart'); |
| 42 | + expect(detector.mostRecentModality).toBe(null); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should detect keyboard input modality', () => { |
| 46 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 47 | + dispatchKeyboardEvent(document, 'keydown'); |
| 48 | + expect(detector.mostRecentModality).toBe('keyboard'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should detect mouse input modality', () => { |
| 52 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 53 | + dispatchMouseEvent(document, 'mousedown'); |
| 54 | + expect(detector.mostRecentModality).toBe('mouse'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should detect touch input modality', () => { |
| 58 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 59 | + dispatchTouchEvent(document, 'touchstart'); |
| 60 | + expect(detector.mostRecentModality).toBe('touch'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should detect changes in input modality', () => { |
| 64 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 65 | + |
| 66 | + dispatchKeyboardEvent(document, 'keydown'); |
| 67 | + expect(detector.mostRecentModality).toBe('keyboard'); |
| 68 | + |
| 69 | + dispatchMouseEvent(document, 'mousedown'); |
| 70 | + expect(detector.mostRecentModality).toBe('mouse'); |
| 71 | + |
| 72 | + dispatchTouchEvent(document, 'touchstart'); |
| 73 | + expect(detector.mostRecentModality).toBe('touch'); |
| 74 | + |
| 75 | + dispatchKeyboardEvent(document, 'keydown'); |
| 76 | + expect(detector.mostRecentModality).toBe('keyboard'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should emit changes in input modality', () => { |
| 80 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 81 | + const emitted: InputModality[] = []; |
| 82 | + detector.modalityChanges.subscribe((modality: InputModality) => { |
| 83 | + emitted.push(modality); |
| 84 | + }); |
| 85 | + |
| 86 | + expect(emitted.length).toBe(0); |
| 87 | + |
| 88 | + dispatchKeyboardEvent(document, 'keydown'); |
| 89 | + expect(emitted).toEqual(['keyboard']); |
| 90 | + |
| 91 | + dispatchKeyboardEvent(document, 'keydown'); |
| 92 | + expect(emitted).toEqual(['keyboard']); |
| 93 | + |
| 94 | + dispatchMouseEvent(document, 'mousedown'); |
| 95 | + expect(emitted).toEqual(['keyboard', 'mouse']); |
| 96 | + |
| 97 | + dispatchTouchEvent(document, 'touchstart'); |
| 98 | + expect(emitted).toEqual(['keyboard', 'mouse', 'touch']); |
| 99 | + |
| 100 | + dispatchTouchEvent(document, 'touchstart'); |
| 101 | + expect(emitted).toEqual(['keyboard', 'mouse', 'touch']); |
| 102 | + |
| 103 | + dispatchKeyboardEvent(document, 'keydown'); |
| 104 | + expect(emitted).toEqual(['keyboard', 'mouse', 'touch', 'keyboard']); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should ignore fake screen-reader mouse events', () => { |
| 108 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 109 | + |
| 110 | + // Create a fake screen-reader mouse event. |
| 111 | + const event = createMouseEvent('mousedown'); |
| 112 | + Object.defineProperty(event, 'buttons', {get: () => 0}); |
| 113 | + dispatchEvent(document, event); |
| 114 | + |
| 115 | + expect(detector.mostRecentModality).toBe(null); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should ignore fake screen-reader touch events', () => { |
| 119 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 120 | + |
| 121 | + // Create a fake screen-reader touch event. |
| 122 | + const event = createTouchEvent('touchstart'); |
| 123 | + Object.defineProperty(event, 'touches', {get: () => [{identifier: -1}]}); |
| 124 | + dispatchEvent(document, event); |
| 125 | + |
| 126 | + expect(detector.mostRecentModality).toBe(null); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should ignore certain modifier keys by default', () => { |
| 130 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 131 | + |
| 132 | + dispatchKeyboardEvent(document, 'keydown', ALT); |
| 133 | + dispatchKeyboardEvent(document, 'keydown', CONTROL); |
| 134 | + dispatchKeyboardEvent(document, 'keydown', META); |
| 135 | + dispatchKeyboardEvent(document, 'keydown', SHIFT); |
| 136 | + |
| 137 | + expect(detector.mostRecentModality).toBe(null); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should not ignore modifier keys if specified', () => { |
| 141 | + detector = new InputModalityDetector(platform, ngZone, document, {ignoreKeys: []}); |
| 142 | + dispatchKeyboardEvent(document, 'keydown', CONTROL); |
| 143 | + expect(detector.mostRecentModality).toBe('keyboard'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should ignore keys if specified', () => { |
| 147 | + detector = new InputModalityDetector(platform, ngZone, document, {ignoreKeys: [A, B, C]}); |
| 148 | + |
| 149 | + dispatchKeyboardEvent(document, 'keydown', A); |
| 150 | + dispatchKeyboardEvent(document, 'keydown', B); |
| 151 | + dispatchKeyboardEvent(document, 'keydown', C); |
| 152 | + |
| 153 | + expect(detector.mostRecentModality).toBe(null); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should ignore mouse events that occur too closely after a touch event', fakeAsync(() => { |
| 157 | + detector = new InputModalityDetector(platform, ngZone, document); |
| 158 | + |
| 159 | + dispatchTouchEvent(document, 'touchstart'); |
| 160 | + dispatchMouseEvent(document, 'mousedown'); |
| 161 | + expect(detector.mostRecentModality).toBe('touch'); |
| 162 | + |
| 163 | + tick(TOUCH_BUFFER_MS); |
| 164 | + dispatchMouseEvent(document, 'mousedown'); |
| 165 | + expect(detector.mostRecentModality).toBe('mouse'); |
| 166 | + })); |
| 167 | +}); |
0 commit comments