1
1
import { A , ALT , B , C , CONTROL , MAC_META , META , SHIFT } from '@angular/cdk/keycodes' ;
2
2
import { Platform } from '@angular/cdk/platform' ;
3
- import { NgZone , PLATFORM_ID } from '@angular/core' ;
4
3
5
4
import {
6
5
createMouseEvent ,
@@ -10,26 +9,36 @@ import {
10
9
dispatchEvent ,
11
10
createTouchEvent ,
12
11
} 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' ;
12
+ import { fakeAsync , TestBed , tick } from '@angular/core/testing' ;
13
+ import {
14
+ InputModality ,
15
+ InputModalityDetector ,
16
+ InputModalityDetectorOptions ,
17
+ INPUT_MODALITY_DETECTOR_OPTIONS ,
18
+ TOUCH_BUFFER_MS ,
19
+ } from './input-modality-detector' ;
15
20
16
21
describe ( 'InputModalityDetector' , ( ) => {
17
- let platform : Platform ;
18
- let ngZone : NgZone ;
19
22
let detector : InputModalityDetector ;
20
23
21
- beforeEach ( inject ( [ PLATFORM_ID ] , ( platformId : Object ) => {
22
- platform = new Platform ( platformId ) ;
23
- ngZone = new NgZone ( { } ) ;
24
- } ) ) ;
24
+ function setupTest ( isBrowser = true , options : InputModalityDetectorOptions = { } ) {
25
+ TestBed . configureTestingModule ( {
26
+ providers : [
27
+ { provide : Platform , useValue : { isBrowser} } ,
28
+ { provide : INPUT_MODALITY_DETECTOR_OPTIONS , useValue : options } ,
29
+ ]
30
+ } ) ;
31
+
32
+ detector = TestBed . inject ( InputModalityDetector ) ;
33
+ }
25
34
26
35
afterEach ( ( ) => {
27
36
detector ?. ngOnDestroy ( ) ;
37
+ detector = undefined ! ;
28
38
} ) ;
29
39
30
40
it ( 'should do nothing on non-browser platforms' , ( ) => {
31
- platform . isBrowser = false ;
32
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
41
+ setupTest ( false ) ;
33
42
expect ( detector . mostRecentModality ) . toBe ( null ) ;
34
43
35
44
dispatchKeyboardEvent ( document , 'keydown' ) ;
@@ -43,25 +52,25 @@ describe('InputModalityDetector', () => {
43
52
} ) ;
44
53
45
54
it ( 'should detect keyboard input modality' , ( ) => {
46
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
55
+ setupTest ( ) ;
47
56
dispatchKeyboardEvent ( document , 'keydown' ) ;
48
57
expect ( detector . mostRecentModality ) . toBe ( 'keyboard' ) ;
49
58
} ) ;
50
59
51
60
it ( 'should detect mouse input modality' , ( ) => {
52
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
61
+ setupTest ( ) ;
53
62
dispatchMouseEvent ( document , 'mousedown' ) ;
54
63
expect ( detector . mostRecentModality ) . toBe ( 'mouse' ) ;
55
64
} ) ;
56
65
57
66
it ( 'should detect touch input modality' , ( ) => {
58
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
67
+ setupTest ( ) ;
59
68
dispatchTouchEvent ( document , 'touchstart' ) ;
60
69
expect ( detector . mostRecentModality ) . toBe ( 'touch' ) ;
61
70
} ) ;
62
71
63
72
it ( 'should detect changes in input modality' , ( ) => {
64
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
73
+ setupTest ( ) ;
65
74
66
75
dispatchKeyboardEvent ( document , 'keydown' ) ;
67
76
expect ( detector . mostRecentModality ) . toBe ( 'keyboard' ) ;
@@ -77,9 +86,9 @@ describe('InputModalityDetector', () => {
77
86
} ) ;
78
87
79
88
it ( 'should emit when input modalities are detected' , ( ) => {
80
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
89
+ setupTest ( ) ;
81
90
const emitted : InputModality [ ] = [ ] ;
82
- detector . modalityDetected . subscribe ( ( modality : InputModality ) => {
91
+ detector . modalityDetected . subscribe ( modality => {
83
92
emitted . push ( modality ) ;
84
93
} ) ;
85
94
@@ -102,9 +111,9 @@ describe('InputModalityDetector', () => {
102
111
} ) ;
103
112
104
113
it ( 'should emit changes in input modality' , ( ) => {
105
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
114
+ setupTest ( ) ;
106
115
const emitted : InputModality [ ] = [ ] ;
107
- detector . modalityChanged . subscribe ( ( modality : InputModality ) => {
116
+ detector . modalityChanged . subscribe ( modality => {
108
117
emitted . push ( modality ) ;
109
118
} ) ;
110
119
@@ -130,7 +139,7 @@ describe('InputModalityDetector', () => {
130
139
} ) ;
131
140
132
141
it ( 'should detect fake screen reader mouse events as keyboard input modality' , ( ) => {
133
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
142
+ setupTest ( ) ;
134
143
135
144
// Create a fake screen-reader mouse event.
136
145
const event = createMouseEvent ( 'mousedown' ) ;
@@ -141,7 +150,7 @@ describe('InputModalityDetector', () => {
141
150
} ) ;
142
151
143
152
it ( 'should detect fake screen reader touch events as keyboard input modality' , ( ) => {
144
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
153
+ setupTest ( ) ;
145
154
146
155
// Create a fake screen-reader touch event.
147
156
const event = createTouchEvent ( 'touchstart' ) ;
@@ -152,7 +161,7 @@ describe('InputModalityDetector', () => {
152
161
} ) ;
153
162
154
163
it ( 'should ignore certain modifier keys by default' , ( ) => {
155
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
164
+ setupTest ( ) ;
156
165
157
166
dispatchKeyboardEvent ( document , 'keydown' , ALT ) ;
158
167
dispatchKeyboardEvent ( document , 'keydown' , CONTROL ) ;
@@ -164,13 +173,13 @@ describe('InputModalityDetector', () => {
164
173
} ) ;
165
174
166
175
it ( 'should not ignore modifier keys if specified' , ( ) => {
167
- detector = new InputModalityDetector ( platform , ngZone , document , { ignoreKeys : [ ] } ) ;
176
+ setupTest ( true , { ignoreKeys : [ ] } ) ;
168
177
dispatchKeyboardEvent ( document , 'keydown' , CONTROL ) ;
169
178
expect ( detector . mostRecentModality ) . toBe ( 'keyboard' ) ;
170
179
} ) ;
171
180
172
181
it ( 'should ignore keys if specified' , ( ) => {
173
- detector = new InputModalityDetector ( platform , ngZone , document , { ignoreKeys : [ A , B , C ] } ) ;
182
+ setupTest ( true , { ignoreKeys : [ A , B , C ] } ) ;
174
183
175
184
dispatchKeyboardEvent ( document , 'keydown' , A ) ;
176
185
dispatchKeyboardEvent ( document , 'keydown' , B ) ;
@@ -180,7 +189,7 @@ describe('InputModalityDetector', () => {
180
189
} ) ;
181
190
182
191
it ( 'should ignore mouse events that occur too closely after a touch event' , fakeAsync ( ( ) => {
183
- detector = new InputModalityDetector ( platform , ngZone , document ) ;
192
+ setupTest ( ) ;
184
193
185
194
dispatchTouchEvent ( document , 'touchstart' ) ;
186
195
dispatchMouseEvent ( document , 'mousedown' ) ;
0 commit comments