6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { dispatchMouseEvent , dispatchPointerEvent } from '@angular/cdk/testing/private' ;
9
+ import { Platform } from '@angular/cdk/platform' ;
10
+ import {
11
+ dispatchMouseEvent ,
12
+ dispatchPointerEvent ,
13
+ dispatchTouchEvent ,
14
+ } from '@angular/cdk/testing/private' ;
10
15
import { Component , DebugElement , Type } from '@angular/core' ;
11
16
import { ComponentFixture , fakeAsync , TestBed , tick , waitForAsync } from '@angular/core/testing' ;
12
17
import { By } from '@angular/platform-browser' ;
@@ -15,7 +20,10 @@ import {MatSliderModule} from './module';
15
20
import { MatSlider , MatSliderThumb , MatSliderVisualThumb } from './slider' ;
16
21
17
22
describe ( 'MDC-based MatSlider' , ( ) => {
23
+ let platform : Platform ;
24
+
18
25
beforeAll ( ( ) => {
26
+ platform = TestBed . inject ( Platform ) ;
19
27
// Mock #setPointerCapture as it throws errors on pointerdown without a real pointerId.
20
28
spyOn ( Element . prototype , 'setPointerCapture' ) ;
21
29
} ) ;
@@ -49,28 +57,28 @@ describe('MDC-based MatSlider' , () => {
49
57
} ) ;
50
58
51
59
it ( 'should update the value on mousedown' , ( ) => {
52
- setValueByClick ( sliderInstance , 19 ) ;
60
+ setValueByClick ( sliderInstance , 19 , platform . IOS ) ;
53
61
expect ( inputInstance . value ) . toBe ( 19 ) ;
54
62
} ) ;
55
63
56
64
it ( 'should update the value on a slide' , ( ) => {
57
- slideToValue ( sliderInstance , 77 ) ;
65
+ slideToValue ( sliderInstance , 77 , Thumb . END , platform . IOS ) ;
58
66
expect ( inputInstance . value ) . toBe ( 77 ) ;
59
67
} ) ;
60
68
61
69
it ( 'should set the value as min when sliding before the track' , ( ) => {
62
- slideToValue ( sliderInstance , - 1 ) ;
70
+ slideToValue ( sliderInstance , - 1 , Thumb . END , platform . IOS ) ;
63
71
expect ( inputInstance . value ) . toBe ( 0 ) ;
64
72
} ) ;
65
73
66
74
it ( 'should set the value as max when sliding past the track' , ( ) => {
67
- slideToValue ( sliderInstance , 101 ) ;
75
+ slideToValue ( sliderInstance , 101 , Thumb . END , platform . IOS ) ;
68
76
expect ( inputInstance . value ) . toBe ( 100 ) ;
69
77
} ) ;
70
78
71
79
it ( 'should focus the slider input when clicking on the slider' , ( ) => {
72
80
expect ( document . activeElement ) . not . toBe ( inputInstance . _hostElement ) ;
73
- setValueByClick ( sliderInstance , 0 ) ;
81
+ setValueByClick ( sliderInstance , 0 , platform . IOS ) ;
74
82
expect ( document . activeElement ) . toBe ( inputInstance . _hostElement ) ;
75
83
} ) ;
76
84
} ) ;
@@ -99,46 +107,46 @@ describe('MDC-based MatSlider' , () => {
99
107
} ) ;
100
108
101
109
it ( 'should update the start value on a slide' , ( ) => {
102
- slideToValue ( sliderInstance , 19 , Thumb . START ) ;
110
+ slideToValue ( sliderInstance , 19 , Thumb . START , platform . IOS ) ;
103
111
expect ( startInputInstance . value ) . toBe ( 19 ) ;
104
112
} ) ;
105
113
106
114
it ( 'should update the end value on a slide' , ( ) => {
107
- slideToValue ( sliderInstance , 27 , Thumb . END ) ;
115
+ slideToValue ( sliderInstance , 27 , Thumb . END , platform . IOS ) ;
108
116
expect ( endInputInstance . value ) . toBe ( 27 ) ;
109
117
} ) ;
110
118
111
119
it ( 'should update the start value on mousedown behind the start thumb' , ( ) => {
112
120
sliderInstance . _setValue ( 19 , Thumb . START ) ;
113
- setValueByClick ( sliderInstance , 12 ) ;
121
+ setValueByClick ( sliderInstance , 12 , platform . IOS ) ;
114
122
expect ( startInputInstance . value ) . toBe ( 12 ) ;
115
123
} ) ;
116
124
117
125
it ( 'should update the end value on mousedown in front of the end thumb' , ( ) => {
118
126
sliderInstance . _setValue ( 27 , Thumb . END ) ;
119
- setValueByClick ( sliderInstance , 55 ) ;
127
+ setValueByClick ( sliderInstance , 55 , platform . IOS ) ;
120
128
expect ( endInputInstance . value ) . toBe ( 55 ) ;
121
129
} ) ;
122
130
123
131
it ( 'should set the start value as min when sliding before the track' , ( ) => {
124
- slideToValue ( sliderInstance , - 1 , Thumb . START ) ;
132
+ slideToValue ( sliderInstance , - 1 , Thumb . START , platform . IOS ) ;
125
133
expect ( startInputInstance . value ) . toBe ( 0 ) ;
126
134
} ) ;
127
135
128
136
it ( 'should set the end value as max when sliding past the track' , ( ) => {
129
- slideToValue ( sliderInstance , 101 , Thumb . START ) ;
137
+ slideToValue ( sliderInstance , 101 , Thumb . START , platform . IOS ) ;
130
138
expect ( startInputInstance . value ) . toBe ( 100 ) ;
131
139
} ) ;
132
140
133
141
it ( 'should not let the start thumb slide past the end thumb' , ( ) => {
134
142
sliderInstance . _setValue ( 50 , Thumb . END ) ;
135
- slideToValue ( sliderInstance , 75 , Thumb . START ) ;
143
+ slideToValue ( sliderInstance , 75 , Thumb . START , platform . IOS ) ;
136
144
expect ( startInputInstance . value ) . toBe ( 50 ) ;
137
145
} ) ;
138
146
139
147
it ( 'should not let the end thumb slide before the start thumb' , ( ) => {
140
148
sliderInstance . _setValue ( 50 , Thumb . START ) ;
141
- slideToValue ( sliderInstance , 25 , Thumb . END ) ;
149
+ slideToValue ( sliderInstance , 25 , Thumb . END , platform . IOS ) ;
142
150
expect ( startInputInstance . value ) . toBe ( 50 ) ;
143
151
} ) ;
144
152
} ) ;
@@ -182,11 +190,15 @@ describe('MDC-based MatSlider' , () => {
182
190
}
183
191
184
192
function pointerdown ( ) {
185
- dispatchPointerEvent ( thumbElement , 'pointerdown' , thumbX , thumbY ) ;
193
+ dispatchPointerOrTouchEvent (
194
+ thumbElement , PointerEventType . POINTER_DOWN , thumbX , thumbY , platform . IOS
195
+ ) ;
186
196
}
187
197
188
198
function pointerup ( ) {
189
- dispatchPointerEvent ( thumbElement , 'pointerup' , thumbX , thumbY ) ;
199
+ dispatchPointerOrTouchEvent (
200
+ thumbElement , PointerEventType . POINTER_UP , thumbX , thumbY , platform . IOS
201
+ ) ;
190
202
}
191
203
192
204
it ( 'should show the hover ripple on mouseenter' , fakeAsync ( ( ) => {
@@ -289,8 +301,22 @@ class StandardSlider {}
289
301
} )
290
302
class StandardRangeSlider { }
291
303
304
+ /** The pointer event types used by the MDC Slider. */
305
+ const enum PointerEventType {
306
+ POINTER_DOWN = 'pointerdown' ,
307
+ POINTER_UP = 'pointerup' ,
308
+ POINTER_MOVE = 'pointermove' ,
309
+ }
310
+
311
+ /** The touch event types used by the MDC Slider. */
312
+ const enum TouchEventType {
313
+ TOUCH_START = 'touchstart' ,
314
+ TOUCH_END = 'touchend' ,
315
+ TOUCH_MOVE = 'touchmove' ,
316
+ }
317
+
292
318
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
293
- function setValueByClick ( slider : MatSlider , value : number ) {
319
+ function setValueByClick ( slider : MatSlider , value : number , isIOS : boolean ) {
294
320
const { min, max} = slider ;
295
321
const percent = ( value - min ) / ( max - min ) ;
296
322
@@ -299,12 +325,12 @@ function setValueByClick(slider: MatSlider, value: number) {
299
325
const x = left + ( width * percent ) ;
300
326
const y = top + ( height / 2 ) ;
301
327
302
- dispatchPointerEvent ( sliderElement , 'pointerdown' , x , y ) ;
303
- dispatchPointerEvent ( sliderElement , 'pointerup' , x , y ) ;
328
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_DOWN , x , y , isIOS ) ;
329
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_UP , x , y , isIOS ) ;
304
330
}
305
331
306
332
/** Slides the MatSlider's thumb to the given value. */
307
- function slideToValue ( slider : MatSlider , value : number , thumbPosition : Thumb = Thumb . END ) {
333
+ function slideToValue ( slider : MatSlider , value : number , thumbPosition : Thumb , isIOS : boolean ) {
308
334
const { min, max} = slider ;
309
335
const percent = ( value - min ) / ( max - min ) ;
310
336
@@ -320,7 +346,29 @@ function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb = T
320
346
const endX = sliderDimensions . left + ( sliderDimensions . width * percent ) ;
321
347
const endY = sliderDimensions . top + ( sliderDimensions . height / 2 ) ;
322
348
323
- dispatchPointerEvent ( sliderElement , 'pointerdown' , startX , startY ) ;
324
- dispatchPointerEvent ( sliderElement , 'pointermove' , endX , endY ) ;
325
- dispatchPointerEvent ( sliderElement , 'pointerup' , endX , endY ) ;
349
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_DOWN , startX , startY , isIOS ) ;
350
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_MOVE , endX , endY , isIOS ) ;
351
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_UP , endX , endY , isIOS ) ;
352
+ }
353
+
354
+ /** Dispatch a pointerdown or pointerup event if supported, otherwise dispatch the touch event. */
355
+ function dispatchPointerOrTouchEvent (
356
+ node : Node , type : PointerEventType , x : number , y : number , isIOS : boolean ) {
357
+ if ( isIOS ) {
358
+ dispatchTouchEvent ( node , pointerEventTypeToTouchEventType ( type ) , x , y , x , y ) ;
359
+ } else {
360
+ dispatchPointerEvent ( node , type , x , y ) ;
361
+ }
362
+ }
363
+
364
+ /** Returns the touch event equivalent of the given pointer event. */
365
+ function pointerEventTypeToTouchEventType ( pointerEventType : PointerEventType ) {
366
+ switch ( pointerEventType ) {
367
+ case PointerEventType . POINTER_DOWN :
368
+ return TouchEventType . TOUCH_START ;
369
+ case PointerEventType . POINTER_UP :
370
+ return TouchEventType . TOUCH_END ;
371
+ case PointerEventType . POINTER_MOVE :
372
+ return TouchEventType . TOUCH_MOVE ;
373
+ }
326
374
}
0 commit comments