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