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 { dispatchMouseEvent , dispatchPointerEvent , dispatchTouchEvent } from '@angular/cdk/testing/private' ;
10
10
import { Component , DebugElement , Type } from '@angular/core' ;
11
11
import { ComponentFixture , fakeAsync , TestBed , tick , waitForAsync } from '@angular/core/testing' ;
12
12
import { By } from '@angular/platform-browser' ;
@@ -182,11 +182,11 @@ describe('MDC-based MatSlider' , () => {
182
182
}
183
183
184
184
function pointerdown ( ) {
185
- dispatchPointerEvent ( thumbElement , 'pointerdown' , thumbX , thumbY ) ;
185
+ dispatchPointerOrTouchEvent ( thumbElement , PointerEventType . POINTER_DOWN , thumbX , thumbY ) ;
186
186
}
187
187
188
188
function pointerup ( ) {
189
- dispatchPointerEvent ( thumbElement , 'pointerup' , thumbX , thumbY ) ;
189
+ dispatchPointerOrTouchEvent ( thumbElement , PointerEventType . POINTER_UP , thumbX , thumbY ) ;
190
190
}
191
191
192
192
it ( 'should show the hover ripple on mouseenter' , fakeAsync ( ( ) => {
@@ -299,8 +299,8 @@ function setValueByClick(slider: MatSlider, value: number) {
299
299
const x = left + ( width * percent ) ;
300
300
const y = top + ( height / 2 ) ;
301
301
302
- dispatchPointerEvent ( sliderElement , 'pointerdown' , x , y ) ;
303
- dispatchPointerEvent ( sliderElement , 'pointerup' , x , y ) ;
302
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_DOWN , x , y ) ;
303
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_UP , x , y ) ;
304
304
}
305
305
306
306
/** Slides the MatSlider's thumb to the given value. */
@@ -320,7 +320,42 @@ function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb = T
320
320
const endX = sliderDimensions . left + ( sliderDimensions . width * percent ) ;
321
321
const endY = sliderDimensions . top + ( sliderDimensions . height / 2 ) ;
322
322
323
- dispatchPointerEvent ( sliderElement , 'pointerdown' , startX , startY ) ;
324
- dispatchPointerEvent ( sliderElement , 'pointermove' , endX , endY ) ;
325
- dispatchPointerEvent ( sliderElement , 'pointerup' , endX , endY ) ;
323
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_DOWN , startX , startY ) ;
324
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_MOVE , endX , endY ) ;
325
+ dispatchPointerOrTouchEvent ( sliderElement , PointerEventType . POINTER_UP , endX , endY ) ;
326
+ }
327
+
328
+ /** Dispatch a pointerdown or pointerup event if supported, otherwise dispatch the touch event. */
329
+ function dispatchPointerOrTouchEvent ( node : Node , type : PointerEventType , x ?: number , y ?: number ) {
330
+ if ( typeof PointerEvent !== 'undefined' && PointerEvent ) {
331
+ dispatchPointerEvent ( node , type , x , y ) ;
332
+ } else {
333
+ dispatchTouchEvent ( node , pointerEventTypeToTouchEventType ( type ) , x , y ) ;
334
+ }
335
+ }
336
+
337
+ /** The pointer event types used by the MDC Slider. */
338
+ enum PointerEventType {
339
+ POINTER_DOWN = 'pointerdown' ,
340
+ POINTER_UP = 'pointerup' ,
341
+ POINTER_MOVE = 'pointermove' ,
342
+ }
343
+
344
+ /** The touch event types used by the MDC Slider. */
345
+ enum TouchEventType {
346
+ TOUCH_START = 'touchstart' ,
347
+ TOUCH_END = 'touchend' ,
348
+ TOUCH_MOVE = 'touchmove' ,
349
+ }
350
+
351
+ /** Returns the touch event equivalent of the given pointer event. */
352
+ function pointerEventTypeToTouchEventType ( pointerEventType : PointerEventType ) {
353
+ switch ( pointerEventType ) {
354
+ case PointerEventType . POINTER_DOWN :
355
+ return TouchEventType . TOUCH_START ;
356
+ case PointerEventType . POINTER_UP :
357
+ return TouchEventType . TOUCH_END ;
358
+ case PointerEventType . POINTER_MOVE :
359
+ return TouchEventType . TOUCH_MOVE ;
360
+ }
326
361
}
0 commit comments