From fca4a1b0274d0ca7a1d6e268405105ae2e524e6f Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:12:48 +0100 Subject: [PATCH 1/8] Create geo-mock.js --- .../javascript/geolocation/geo-mock.js | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 programming/javascript/geolocation/geo-mock.js diff --git a/programming/javascript/geolocation/geo-mock.js b/programming/javascript/geolocation/geo-mock.js new file mode 100644 index 0000000..3c3f113 --- /dev/null +++ b/programming/javascript/geolocation/geo-mock.js @@ -0,0 +1,147 @@ +var mockGeo; + +window.mockGeo = { + + PERMISSION_DENIED: { + PERMISSION_DENIED: 1, + POSITION_UNAVAILABLE: 2, + TIMEOUT: 3, + code: 1, + message: "Geolocation error: PERMISSION_DENIED" + }, + + POSITION_UNAVAILABLE: { + PERMISSION_DENIED: 1, + POSITION_UNAVAILABLE: 2, + TIMEOUT: 3, + code: 2, + message: "Geolocation error: POSITION_UNAVAILABLE" + }, + + TIMEOUT: { + PERMISSION_DENIED: 1, + POSITION_UNAVAILABLE: 2, + TIMEOUT: 3, + code: 3, + message: "Geolocation error: TIMEOUT" + }, + + _SUCCESS: 0, + _ERROR: 1, + + _watches: [], + _curErr: null, + _curPos: null, + + _notInited: [], + + _isInited: function () { + return this._curErr !== null || this._curPos !== null; + }, + + _asyncExec: function (callback, args) { + setTimeout(function () { + callback(args); + }, 0); + }, + + _cbExec: function (resultsArray) { + if (this._curErr && resultsArray[this._ERROR]) { + this._asyncExec(resultsArray[this._ERROR], this._curErr); + } else if (this._curPos && resultsArray[this._SUCCESS]) { + this._asyncExec(resultsArray[this._SUCCESS], this._curPos); + } + }, + + _cbWatch: function () { + for (var idx = 0; idx < this._notInited.length; idx++) { + this._cbExec(this._notInited[idx]); + } + + this._notInited = []; + + for (var idx2 = 0; idx2 < this._watches.length; idx2++) { + if (this._watches[idx2]) { + this._cbExec(this._watches[idx2]); + } + } + }, + + _setCurPosition: function (currentPosition) { + this._curErr = null; + this._curPos = currentPosition; + if (currentPosition) { + this._cbWatch(); + } + }, + + /* + * Simulate geolocation error + */ + setPositionErr: function (currentError) { + this._curPos = null; + this._curErr = currentError; + if (currentError) { + this._cbWatch(); + } + }, + + /* + * Simulate current position + */ + setPosition: function (lat, lng) { + this._setCurPosition({ + coords: { + latitude: lat, + longitude: lng, + accuracy: 20 + } + }); + }, + + /* + * Simulate current speed + * based on current position + */ + setSpeed: function(spd) { + /* + * Here you should add your default coordinates or take them from geolocation + * as speed mock doesn't work without lat and lon emulation + */ + this._setCurPosition({ + coords: { + /* + * Seems like it's impossible to mock + * only speed, without lat and lon + */ + latitude: 1, + longitude: 1, + accuracy: 20, + speed: spd + } + }); + } +}; + +if (!navigator.geolocation) { + window.navigator.geolocation = {}; +} + +navigator.geolocation.getCurrentPosition = function(success, error) { + if (mockGeo._isInited()) { + mockGeo._cbExec([success, error]); + } else { + mockGeo._notInited.push([success, error]); + } +}; + +navigator.geolocation.watchPosition = function(success, error) { + if (mockGeo._isInited()) { + mockGeo._cbExec([success, error]); + } + return mockGeo._watches.push([success, error]) - 1; +}; + +navigator.geolocation.clearWatch = function(id) { + mockGeo._watches[id] = null; +}; From 61356f47563ac478e32bb9a21287ca31b1eee98f Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:13:19 +0100 Subject: [PATCH 2/8] Create README.md --- programming/javascript/geolocation/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 programming/javascript/geolocation/README.md diff --git a/programming/javascript/geolocation/README.md b/programming/javascript/geolocation/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/programming/javascript/geolocation/README.md @@ -0,0 +1 @@ + From a376bb7aee2ea8bd01210be8b40c8843e680c753 Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:17:28 +0100 Subject: [PATCH 3/8] Update README.md --- programming/javascript/geolocation/README.md | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/programming/javascript/geolocation/README.md b/programming/javascript/geolocation/README.md index 8b13789..ef59424 100644 --- a/programming/javascript/geolocation/README.md +++ b/programming/javascript/geolocation/README.md @@ -1 +1,36 @@ +# js-geo-mock + +If you are testing geolocation-based features, you may have been need to change your geolocation according to different tests. One more purpose is to use geolocation speed change. This javascript mock will help you, you can use it inside any test within any programming language. + +Just include this file to your testing page or execute the whole code in page context. +``` + +``` + +And use it: + +
    +
  • +latitude, longitude mock: + + window.mockGeo.setPosition(10, -15.1212); +
  • +
  • + geolocation error mock: + + window.mockGeo.setPositionErr(mockGeo.PERMISSION_DENIED); + window.mockGeo.setPositionErr(mockGeo.POSITION_UNAVAILABLE); + window.mockGeo.setPositionErr(mockGeo.TIMEOUT); +
  • + +
  • + speed mock (m/s): + + window.mockGeo.setSpeed(9); +
  • +
+ + + + From 7bfc69fd1db990fef461841cd2796ad2c17d7be2 Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:17:56 +0100 Subject: [PATCH 4/8] Update README.md --- programming/javascript/geolocation/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programming/javascript/geolocation/README.md b/programming/javascript/geolocation/README.md index ef59424..4782541 100644 --- a/programming/javascript/geolocation/README.md +++ b/programming/javascript/geolocation/README.md @@ -1,4 +1,4 @@ -# js-geo-mock +# geo-mock by https://github.com/fat-troll/js-geo-mock If you are testing geolocation-based features, you may have been need to change your geolocation according to different tests. One more purpose is to use geolocation speed change. This javascript mock will help you, you can use it inside any test within any programming language. From f53d0171daff46789e34525a2a0acd75aa44b955 Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:18:20 +0100 Subject: [PATCH 5/8] Update README.md --- programming/javascript/geolocation/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programming/javascript/geolocation/README.md b/programming/javascript/geolocation/README.md index 4782541..f1cf7a8 100644 --- a/programming/javascript/geolocation/README.md +++ b/programming/javascript/geolocation/README.md @@ -1,4 +1,4 @@ -# geo-mock by https://github.com/fat-troll/js-geo-mock +# geo-mock by fat-troll If you are testing geolocation-based features, you may have been need to change your geolocation according to different tests. One more purpose is to use geolocation speed change. This javascript mock will help you, you can use it inside any test within any programming language. From ae27053b9845faf3abf2748e46e653148e7b4b11 Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:18:40 +0100 Subject: [PATCH 6/8] Update README.md --- programming/javascript/geolocation/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programming/javascript/geolocation/README.md b/programming/javascript/geolocation/README.md index f1cf7a8..3febd03 100644 --- a/programming/javascript/geolocation/README.md +++ b/programming/javascript/geolocation/README.md @@ -1,4 +1,4 @@ -# geo-mock by fat-troll +# geo-mock If you are testing geolocation-based features, you may have been need to change your geolocation according to different tests. One more purpose is to use geolocation speed change. This javascript mock will help you, you can use it inside any test within any programming language. From bcf8f9b56f72f342641f1c21e2f1134dea909fc1 Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:19:53 +0100 Subject: [PATCH 7/8] Update geo-mock.js --- programming/javascript/geolocation/geo-mock.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/programming/javascript/geolocation/geo-mock.js b/programming/javascript/geolocation/geo-mock.js index 3c3f113..3dfb8b2 100644 --- a/programming/javascript/geolocation/geo-mock.js +++ b/programming/javascript/geolocation/geo-mock.js @@ -1,3 +1,9 @@ +/* +* https://github.com/fat-troll/js-geo-mock +* Mock lat, long, speed, geolocation errors +* in real time within your tests :) +*/ + var mockGeo; window.mockGeo = { From c704930525e43a5aa34772b06d7e3c44b9ca26d1 Mon Sep 17 00:00:00 2001 From: Stan Date: Fri, 20 Feb 2015 15:22:34 +0100 Subject: [PATCH 8/8] Update README.md --- programming/javascript/geolocation/README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/programming/javascript/geolocation/README.md b/programming/javascript/geolocation/README.md index 3febd03..0f21f14 100644 --- a/programming/javascript/geolocation/README.md +++ b/programming/javascript/geolocation/README.md @@ -12,21 +12,21 @@ And use it:
  • latitude, longitude mock: - - window.mockGeo.setPosition(10, -15.1212); +
    • window.mockGeo.setPosition(10, -15.1212);
  • -
  • - geolocation error mock: - window.mockGeo.setPositionErr(mockGeo.PERMISSION_DENIED); - window.mockGeo.setPositionErr(mockGeo.POSITION_UNAVAILABLE); - window.mockGeo.setPositionErr(mockGeo.TIMEOUT); +
  • +geolocation error mock: +
      +
    • window.mockGeo.setPositionErr(mockGeo.PERMISSION_DENIED);
    • +
    • window.mockGeo.setPositionErr(mockGeo.POSITION_UNAVAILABLE);
    • +
    • window.mockGeo.setPositionErr(mockGeo.TIMEOUT);
    • +
  • - speed mock (m/s): - - window.mockGeo.setSpeed(9); +speed mock (m/s): +
    • window.mockGeo.setSpeed(9);