123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- /* The following functions are taken and slightly modified from mock-phantom-touch-events.
- *
- * The original mock-phantom-touch-events can be found at https://github.com/gardr/mock-phantom-touch-events
- *
- * mock-phantom-touch-events is licensed under:
- *
- * The MIT License (MIT)
- * Copyright (c) 2013 FINN.no AS
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- */
- /*
- list can be either [[x, y], [x, y]] or [x, y]
- */
- function createTouchList(target, list) {
- if (Array.isArray(list) && list[0] && !Array.isArray(list[0])) {
- list = [list];
- }
- list = list.map(function (entry, index) {
- var x = entry[0], y = entry[1], id = entry[2] ? entry[2] : index + 1;
- return createTouch(x, y, target, id);
- });
- return document.createTouchList.apply(document, list);
- }
- function createTouch(x, y, target, id) {
- return document.createTouch(window, target,
- id || 1, //identifier
- x, //pageX / clientX
- y, //pageY / clientY
- x, //screenX
- y //screenY
- );
- }
- function initTouchEvent(touchEvent, type, touches) {
- var touch1 = touches[0];
- return touchEvent.initTouchEvent(
- touches, //touches
- touches, //targetTouches
- touches, //changedTouches
- type, //type
- window, //view
- touch1.screenX, //screenX
- touch1.screenY, //screenY
- touch1.clientX, //clientX
- touch1.clientY, //clientY
- false, //ctrlKey
- false, //altKey
- false, //shiftKey
- false //metaKey
- );
- }
- function createTouchEvent(elem, type, touches) {
- var touchEvent = document.createEvent('TouchEvent');
- if (Array.isArray(touches)) {
- touches = createTouchList(elem, touches);
- }
- initTouchEvent(touchEvent, type, touches);
- return touchEvent;
- }
- function calcTouchEventCoords(element) {
- var elementBB = element.getBoundingClientRect();
- return {
- x: elementBB.left,
- y: elementBB.top
- };
- }
- describe("Touch Capable Tests", function() {
- var touchStart;
- var touchMove;
- var touchEnd;
- var $testSlider;
- var sliderInst;
- var inputId;
- var sliderId;
- var sliderOptions;
- beforeEach(function() {
- inputId = 'testSlider1';
- sliderId = 'mySlider';
- sliderOptions = {
- id: sliderId,
- step: 1,
- value: 5,
- ticks: [0, 3, 5, 7, 10]
- };
- // Enable touch
- window.ontouchstart = true;
- });
- afterEach(function() {
- window.ontouchstart = null;
- if ($testSlider) {
- $testSlider.slider('destroy');
- $testSlider = null;
- }
- });
- describe("single slider", function() {
- beforeEach(function() {
- // Initialize the slider
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should slide the handle to the left from 5 to 3", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toBe(3);
- done();
- });
- var tick = sliderInst.ticks[2]; // 5
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[1]; // 3
- coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- it("should slide the handle to the right from 5 to 7", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toBe(7);
- done();
- });
- var tick = sliderInst.ticks[2]; // 5
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[3]; // 7
- coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- describe("single, vertical slider", function() {
- beforeEach(function() {
- // Initialize the slider
- sliderOptions.orientation = 'vertical';
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should slide the handle to the top from 5 to 3", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toBe(3);
- done();
- });
- var tick = sliderInst.ticks[2]; // 5
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[1]; // 3
- coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- it("should slide the handle to the bottom from 5 to 7", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toBe(7);
- done();
- });
- var tick = sliderInst.ticks[2]; // 5
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[3]; // 7
- coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- describe("range slider", function() {
- beforeEach(function() {
- sliderOptions.range = true;
- sliderOptions.value = [3, 7];
- // Initialize the slider
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should slide the first handle to the left from 3 to 0", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toEqual([0, 7]);
- done();
- });
- var tick = sliderInst.ticks[1]; // 3
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[0]; // 0
- coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- it("should slide the second handle to the right from 7 to 10", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toEqual([3, 10]);
- done();
- });
- var tick = sliderInst.ticks[3]; // 7
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[4]; // 10
- coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- describe("range, vertical slider", function() {
- beforeEach(function() {
- sliderOptions.range = true;
- sliderOptions.value = [3, 7];
- sliderOptions.orientation = 'vertical';
- // Initialize the slider
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should slide the first handle to the top from 3 to 0", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toEqual([0, 7]);
- done();
- });
- var tick = sliderInst.ticks[1]; // 3
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[0]; // 0
- coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- it("should slide the second handle to the bottom from 7 to 10", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- var value = $testSlider.slider('getValue');
- expect(value).toEqual([3, 10]);
- done();
- });
- var tick = sliderInst.ticks[3]; // 7
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[4]; // 10
- coords = [sliderCoords.x, sliderCoords.y + tick.offsetTop];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- describe("Tooltip tests", function() {
- var $tooltip;
- describe("single slider", function() {
- beforeEach(function() {
- // Initialize the slider
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should show the tooltip when touching the slider at value 5", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $tooltip = $(sliderElem).find('.tooltip.tooltip-main');
- /* Note: You can't use $testSlider.on('slideStart', function() {}) because jQuery
- * maintains its own list of event handlers and you may get unexpected results
- * when you add event handlers using $.on() versus DOM.addEventListener()
- * as they are called in a different order.
- *
- * The browser will call the event handlers registered with addEventListener()
- * in the order in which they are registered. For example, you'll get the following
- * execution order when listening for "touchstart" events.
- *
- * 1) _touchstart()
- * 2) _showTooltip()
- * 3) your event handler here
- */
- sliderElem.addEventListener('touchstart', function() {
- expect($tooltip.hasClass('in')).toBe(true);
- }, false);
- $testSlider.on('slideStop', function() {
- expect($tooltip.hasClass('in')).toBe(false);
- done();
- });
- var tick = sliderInst.ticks[2]; // 5
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- describe("range slider", function() {
- var touchStart2;
- beforeEach(function() {
- sliderOptions.range = true;
- sliderOptions.value = [3, 7];
- // Initialize the slider
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should show the tooltip when touching the slider at value 3 and 7", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $tooltip = $(sliderElem).find('.tooltip.tooltip-main');
- sliderElem.addEventListener('touchstart', function() {
- expect($tooltip.hasClass('in')).toBe(true);
- }, false);
- $testSlider.on('slideStop', function() {
- expect($tooltip.hasClass('in')).toBe(false);
- done();
- });
- var tick = sliderInst.ticks[1]; // 3
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- // For second handle
- tick = sliderInst.ticks[3]; // 7
- coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart2 = createTouchEvent(sliderElem, 'touchstart', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchStart2);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- });
- describe("Scrolling tests", function() {
- describe("horizontal sliding tests", function() {
- var horzScrollDiv;
- beforeEach(function() {
- // Initialize the slider
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- horzScrollDiv = document.getElementById('horz-scroll-div');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should not scroll the div horizontally while sliding the slider", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- expect(horzScrollDiv.scrollLeft).toBe(0);
- done();
- });
- var tick = sliderInst.ticks[2]; // 5
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[3]; // 7
- coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- describe("vertical sliding tests", function() {
- var vertScrollDiv;
- beforeEach(function() {
- sliderOptions.orientation = 'vertical';
- // Initialize the slider
- $testSlider = $('#' + inputId).slider(sliderOptions);
- // Get slider instance
- sliderInst = $testSlider.data('slider');
- vertScrollDiv = document.getElementById('vert-scroll-div');
- });
- // index= [0 1 2 3 4]
- // ticks= [0 3 5 7 10]
- it("should not scroll the div vertically while sliding the slider", function(done) {
- var sliderElem = $testSlider.slider('getElement');
- $testSlider.on('slideStop', function() {
- expect(vertScrollDiv.scrollTop).toBe(0);
- done();
- });
- var tick = sliderInst.ticks[2]; // 5
- var sliderCoords = calcTouchEventCoords(sliderElem);
- var coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchStart = createTouchEvent(sliderElem, 'touchstart', coords);
- tick = sliderInst.ticks[3]; // 7
- coords = [sliderCoords.x + tick.offsetLeft, sliderCoords.y];
- touchMove = createTouchEvent(sliderElem, 'touchmove', coords);
- touchEnd = createTouchEvent(sliderElem, 'touchend', coords);
- sliderElem.dispatchEvent(touchStart);
- sliderElem.dispatchEvent(touchMove);
- sliderElem.dispatchEvent(touchEnd);
- });
- });
- });
- });
|