Add track factory for tests

This commit is contained in:
Bartek Fabiszewski 2019-12-14 17:01:26 +01:00
parent e5de35f433
commit ec01086049
3 changed files with 118 additions and 87 deletions

View File

@ -20,10 +20,7 @@
import * as gmStub from './googlemaps.stub.js'; import * as gmStub from './googlemaps.stub.js';
import { config, lang } from '../src/initializer.js' import { config, lang } from '../src/initializer.js'
import GoogleMapsApi from '../src/mapapi/api_gmaps.js'; import GoogleMapsApi from '../src/mapapi/api_gmaps.js';
import uPosition from '../src/position.js'; import TrackFactory from './helpers/trackfactory.js';
import uPositionSet from '../src/positionset.js';
import uTrack from '../src/track.js';
import uUser from '../src/user.js';
import uUtils from '../src/utils.js'; import uUtils from '../src/utils.js';
describe('Google Maps map API tests', () => { describe('Google Maps map API tests', () => {
@ -203,7 +200,7 @@ describe('Google Maps map API tests', () => {
it('should construct track polyline and markers', () => { it('should construct track polyline and markers', () => {
// given // given
const track = getTrack(); const track = TrackFactory.getTrack();
spyOn(api, 'setMarker'); spyOn(api, 'setMarker');
spyOn(google.maps, 'LatLng').and.callThrough(); spyOn(google.maps, 'LatLng').and.callThrough();
spyOn(google.maps.LatLngBounds.prototype, 'extend').and.callThrough(); spyOn(google.maps.LatLngBounds.prototype, 'extend').and.callThrough();
@ -231,7 +228,7 @@ describe('Google Maps map API tests', () => {
it('should construct non-continuous track markers without polyline', () => { it('should construct non-continuous track markers without polyline', () => {
// given // given
const track = getPositionSet(); const track = TrackFactory.getPositionSet();
spyOn(api, 'setMarker'); spyOn(api, 'setMarker');
// when // when
api.displayTrack(track, false); api.displayTrack(track, false);
@ -243,7 +240,7 @@ describe('Google Maps map API tests', () => {
it('should fit bounds if update without zoom (should not add listener for "bounds_changed")', () => { it('should fit bounds if update without zoom (should not add listener for "bounds_changed")', () => {
// given // given
const track = getTrack(); const track = TrackFactory.getTrack();
spyOn(google.maps.event, 'addListenerOnce'); spyOn(google.maps.event, 'addListenerOnce');
spyOn(google.maps.Map.prototype, 'fitBounds'); spyOn(google.maps.Map.prototype, 'fitBounds');
spyOn(api, 'setMarker'); spyOn(api, 'setMarker');
@ -262,7 +259,7 @@ describe('Google Maps map API tests', () => {
it('should fit bounds and zoom (add listener for "bounds_changed") if update with single position', () => { it('should fit bounds and zoom (add listener for "bounds_changed") if update with single position', () => {
// given // given
const track = getTrack(1); const track = TrackFactory.getTrack(1);
spyOn(google.maps.event, 'addListenerOnce'); spyOn(google.maps.event, 'addListenerOnce');
spyOn(google.maps.Map.prototype, 'fitBounds'); spyOn(google.maps.Map.prototype, 'fitBounds');
spyOn(api, 'setMarker'); spyOn(api, 'setMarker');
@ -281,7 +278,7 @@ describe('Google Maps map API tests', () => {
it('should create marker from track position and add it to markers array', () => { it('should create marker from track position and add it to markers array', () => {
// given // given
const track = getTrack(1); const track = TrackFactory.getTrack(1);
track.positions[0].timestamp = 1; track.positions[0].timestamp = 1;
spyOn(google.maps.Marker.prototype, 'addListener'); spyOn(google.maps.Marker.prototype, 'addListener');
spyOn(google.maps.Marker.prototype, 'setIcon'); spyOn(google.maps.Marker.prototype, 'setIcon');
@ -307,7 +304,7 @@ describe('Google Maps map API tests', () => {
it('should create marker different marker icon for start, end and normal position', () => { it('should create marker different marker icon for start, end and normal position', () => {
// given // given
const track = getTrack(3); const track = TrackFactory.getTrack(3);
spyOn(google.maps.Marker.prototype, 'setIcon'); spyOn(google.maps.Marker.prototype, 'setIcon');
spyOn(GoogleMapsApi, 'getMarkerIcon'); spyOn(GoogleMapsApi, 'getMarkerIcon');
api.map = new google.maps.Map(container); api.map = new google.maps.Map(container);
@ -330,7 +327,7 @@ describe('Google Maps map API tests', () => {
it('should create different marker for position with comment or image', () => { it('should create different marker for position with comment or image', () => {
// given // given
const track = getTrack(4); const track = TrackFactory.getTrack(4);
const positionWithComment = 0; const positionWithComment = 0;
const positionWithImage = 1; const positionWithImage = 1;
const positionWithImageAndComment = 2; const positionWithImageAndComment = 2;
@ -473,36 +470,4 @@ describe('Google Maps map API tests', () => {
expect(GoogleMapsApi.loadTimeoutMs).toEqual(jasmine.any(Number)); expect(GoogleMapsApi.loadTimeoutMs).toEqual(jasmine.any(Number));
}); });
function getSet(length = 2, type) {
let track;
if (type === uTrack) {
track = new uTrack(1, 'test track', new uUser(1, 'testUser'));
} else {
track = new uPositionSet();
}
track.positions = [];
let lat = 21.01;
let lon = 52.23;
for (let i = 0; i < length; i++) {
track.positions.push(getPosition(lat, lon));
lat += 0.5;
lon += 0.5;
}
return track;
}
function getTrack(length = 2) {
return getSet(length, uTrack);
}
function getPositionSet(length = 2) {
return getSet(length, uPositionSet);
}
function getPosition(latitude = 52.23, longitude = 21.01) {
const position = new uPosition();
position.latitude = latitude;
position.longitude = longitude;
return position;
}
}); });

View File

@ -20,11 +20,8 @@
/* global ol */ /* global ol */
import OpenlayersApi from '../src/mapapi/api_openlayers.js'; import OpenlayersApi from '../src/mapapi/api_openlayers.js';
import TrackFactory from './helpers/trackfactory.js';
import { config } from '../src/initializer.js' import { config } from '../src/initializer.js'
import uPosition from '../src/position.js';
import uPositionSet from '../src/positionset.js';
import uTrack from '../src/track.js';
import uUser from '../src/user.js';
import uUtils from '../src/utils.js'; import uUtils from '../src/utils.js';
describe('Openlayers map API tests', () => { describe('Openlayers map API tests', () => {
@ -249,7 +246,7 @@ describe('Openlayers map API tests', () => {
api.map = mockMap; api.map = mockMap;
api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() });
api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() });
const track = getTrack(); const track = TrackFactory.getTrack();
spyOn(api, 'setMarker'); spyOn(api, 'setMarker');
spyOn(api, 'fitToExtent'); spyOn(api, 'fitToExtent');
// when // when
@ -277,7 +274,7 @@ describe('Openlayers map API tests', () => {
api.map.addControl(new ol.control.ZoomToExtent()); api.map.addControl(new ol.control.ZoomToExtent());
api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() });
api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() });
const track = getPositionSet(); const track = TrackFactory.getPositionSet();
spyOn(api, 'setMarker'); spyOn(api, 'setMarker');
spyOn(api, 'fitToExtent'); spyOn(api, 'fitToExtent');
// when // when
@ -303,7 +300,7 @@ describe('Openlayers map API tests', () => {
api.map = mockMap; api.map = mockMap;
api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() });
api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() });
const track = getTrack(); const track = TrackFactory.getTrack();
spyOn(api, 'setMarker'); spyOn(api, 'setMarker');
const markersExtent = [ 3, 2, 1, 0 ]; const markersExtent = [ 3, 2, 1, 0 ];
spyOn(api, 'fitToExtent').and.callFake((_extent) => _extent); spyOn(api, 'fitToExtent').and.callFake((_extent) => _extent);
@ -358,7 +355,7 @@ describe('Openlayers map API tests', () => {
it('should create marker from track position and add it to markers layer', () => { it('should create marker from track position and add it to markers layer', () => {
// given // given
const track = getTrack(1); const track = TrackFactory.getTrack(1);
track.positions[0].timestamp = 1; track.positions[0].timestamp = 1;
const id = 0; const id = 0;
api.map = mockMap; api.map = mockMap;
@ -374,7 +371,7 @@ describe('Openlayers map API tests', () => {
it('should get different marker style for start, end and normal position', () => { it('should get different marker style for start, end and normal position', () => {
// given // given
const track = getTrack(3); const track = TrackFactory.getTrack(3);
api.markerStyles = { api.markerStyles = {
normal: 'normal', normal: 'normal',
stop: 'stop', stop: 'stop',
@ -396,7 +393,7 @@ describe('Openlayers map API tests', () => {
it('should create different marker for position with comment', () => { it('should create different marker for position with comment', () => {
// given // given
const track = getTrack(3); const track = TrackFactory.getTrack(3);
track.positions[0].comment = 'comment'; track.positions[0].comment = 'comment';
track.positions[1].comment = 'comment'; track.positions[1].comment = 'comment';
track.positions[2].comment = 'comment'; track.positions[2].comment = 'comment';
@ -421,7 +418,7 @@ describe('Openlayers map API tests', () => {
it('should create different marker for position with image', () => { it('should create different marker for position with image', () => {
// given // given
const track = getTrack(3); const track = TrackFactory.getTrack(3);
track.positions[0].image = 'image'; track.positions[0].image = 'image';
track.positions[1].image = 'image'; track.positions[1].image = 'image';
track.positions[2].image = 'image'; track.positions[2].image = 'image';
@ -548,37 +545,4 @@ describe('Openlayers map API tests', () => {
expect(api.popup.getElement().firstElementChild.innerHTML).toBe('content 1'); expect(api.popup.getElement().firstElementChild.innerHTML).toBe('content 1');
expect(mockViewModel.model.markerSelect).toBe(null); expect(mockViewModel.model.markerSelect).toBe(null);
}); });
function getSet(length = 2, type) {
let track;
if (type === uTrack) {
track = new uTrack(1, 'test track', new uUser(1, 'testUser'));
} else {
track = new uPositionSet();
}
track.positions = [];
let lat = 21.01;
let lon = 52.23;
for (let i = 0; i < length; i++) {
track.positions.push(getPosition(lat, lon));
lat += 0.5;
lon += 0.5;
}
return track;
}
function getTrack(length = 2) {
return getSet(length, uTrack);
}
function getPositionSet(length = 2) {
return getSet(length, uPositionSet);
}
function getPosition(latitude = 52.23, longitude = 21.01) {
const position = new uPosition();
position.latitude = latitude;
position.longitude = longitude;
return position;
}
}); });

View File

@ -0,0 +1,102 @@
/*
* μlogger
*
* Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
import uPosition from '../../src/position.js';
import uPositionSet from '../../src/positionset.js';
import uTrack from '../../src/track.js';
import uUser from '../../src/user.js';
export default class TrackFactory {
/**
* @template T
* @param {number} length
* @param {T} type
* @param {Object} params
* @return {T}
*/
static getSet(length = 2, type, params) {
let track;
if (type === uTrack) {
track = new uTrack(params.id, params.name, params.user);
} else {
track = new uPositionSet();
}
if (length) {
track.positions = [];
let lat = 21.01;
let lon = 52.23;
for (let i = 0; i < length; i++) {
track.positions.push(this.getPosition(i + 1, lat, lon));
lat += 0.5;
lon += 0.5;
}
}
return track;
}
/**
* @param {number=} length
* @param {{ id: number, name: string, user: uUser }=} params
* @return {uTrack}
*/
static getTrack(length = 2, params) {
params = params || {};
params.id = params.id || 1;
params.name = params.name || 'test track';
params.user = params.user || new uUser(1, 'testUser');
return this.getSet(length, uTrack, params);
}
/**
* @param {number} length
* @return {uPositionSet}
*/
static getPositionSet(length = 2) {
return this.getSet(length, uPositionSet);
}
/**
* @param {number=} id
* @param {number=} latitude
* @param {number=} longitude
* @return {uPosition}
*/
static getPosition(id = 1, latitude = 52.23, longitude = 21.01) {
const position = new uPosition();
position.id = id;
position.latitude = latitude;
position.longitude = longitude;
position.altitude = null;
position.speed = null;
position.bearing = null;
position.timestamp = 1;
position.accuracy = null;
position.provider = null;
position.comment = null;
position.image = null;
position.username = 'testUser';
position.trackid = 1;
position.trackname = 'test track';
position.meters = 0;
position.seconds = 0;
return position;
}
}