Skip to content

Commit 6a55285

Browse files
committed
fix(material-experimental/mdc-slider): code review changes
* try checking if pointer events are supported and use touch events if they are not (ios unit test bug).
1 parent 10035f1 commit 6a55285

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/cdk/testing/testbed/fake-events/event-objects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ export function createPointerEvent(type: string, clientX = 0, clientY = 0,
7979
* Creates a browser TouchEvent with the specified pointer coordinates.
8080
* @docs-private
8181
*/
82-
export function createTouchEvent(type: string, pageX = 0, pageY = 0) {
82+
export function createTouchEvent(type: string, pageX = 0, pageY = 0, clientX = 0, clientY = 0) {
8383
// In favor of creating events that work for most of the browsers, the event is created
8484
// as a basic UI Event. The necessary details for the event will be set manually.
8585
const event = document.createEvent('UIEvent');
86-
const touchDetails = {pageX, pageY};
86+
const touchDetails = {pageX, pageY, clientX, clientY};
8787

8888
// TS3.6 removes the initUIEvent method and suggests porting to "new UIEvent()".
8989
(event as any).initUIEvent(type, true, true, window, 0);

src/material-experimental/mdc-slider/slider.spec.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {dispatchMouseEvent, dispatchPointerEvent} from '@angular/cdk/testing/private';
9+
import {dispatchMouseEvent, dispatchPointerEvent, dispatchTouchEvent} from '@angular/cdk/testing/private';
1010
import {Component, DebugElement, Type} from '@angular/core';
1111
import {ComponentFixture, fakeAsync, TestBed, tick, waitForAsync} from '@angular/core/testing';
1212
import {By} from '@angular/platform-browser';
@@ -182,11 +182,11 @@ describe('MDC-based MatSlider' , () => {
182182
}
183183

184184
function pointerdown() {
185-
dispatchPointerEvent(thumbElement, 'pointerdown', thumbX, thumbY);
185+
dispatchPointerOrTouchEvent(thumbElement, PointerEventType.POINTER_DOWN, thumbX, thumbY);
186186
}
187187

188188
function pointerup() {
189-
dispatchPointerEvent(thumbElement, 'pointerup', thumbX, thumbY);
189+
dispatchPointerOrTouchEvent(thumbElement, PointerEventType.POINTER_UP, thumbX, thumbY);
190190
}
191191

192192
it('should show the hover ripple on mouseenter', fakeAsync(() => {
@@ -299,8 +299,8 @@ function setValueByClick(slider: MatSlider, value: number) {
299299
const x = left + (width * percent);
300300
const y = top + (height / 2);
301301

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);
304304
}
305305

306306
/** Slides the MatSlider's thumb to the given value. */
@@ -320,7 +320,42 @@ function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb = T
320320
const endX = sliderDimensions.left + (sliderDimensions.width * percent);
321321
const endY = sliderDimensions.top + (sliderDimensions.height / 2);
322322

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+
}
326361
}

0 commit comments

Comments
 (0)