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