/*! * ngCordova * v0.1.15-alpha * Copyright 2014 Drifty Co. http://drifty.com/ * See LICENSE in this repository for license information */ (function(){ var ngCordovaMocks = angular.module('ngCordovaMocks', []); ngCordovaMocks.factory('$cordovaAppVersion', ['$q', function($q) { var throwsError = false; return { throwsError: throwsError, getAppVersion: function() { var defer = $q.defer(); defer.resolve('mock v'); return defer.promise; } }; }]); /** * @ngdoc service * @name ngCordovaMocks.cordovaBarcodeScanner * * @description * A service for testing barcode scanner features * in an app build with ngCordova. **/ ngCordovaMocks.factory('$cordovaBarcodeScanner', ['$q', function($q) { var throwsError = false; var scannedText = ''; var scannedFormat = ''; var wasCancelled = false; return { /** * @ngdoc property * @name throwsError * @propertyOf ngCordovaMocks.cordovaBarcodeScanner * * @description * A flag that signals whether a promise should be rejected or not. * This property should only be used in automated tests. **/ throwsError: throwsError, /** * @ngdoc property * @name scannedText * @propertyOf ngCordovaMocks.cordovaBarcodeScanner * * @description * Used to simulate the result.text property of a * successful scan. For more information, see the text at * https://github.com/wildabeast/BarcodeScanner/#using-the-plugin * This property should only be used in automated tests. **/ scannedText: scannedText, /** * @ngdoc property * @name scannedFormat * @propertyOf ngCordovaMocks.cordovaBarcodeScanner * * @description * Used to simulate the result.format property of a * successful scan. For more information, see the text at * https://github.com/wildabeast/BarcodeScanner/#using-the-plugin * This property should only be used in automated tests. **/ scannedFormat: scannedFormat, /** * @ngdoc property * @name wasCancelled * @propertyOf ngCordovaMocks.cordovaBarcodeScanner * * @description * Used to simulate the result.cancelled property of a * successful scan. For more information, see the text at * https://github.com/wildabeast/BarcodeScanner/#using-the-plugin * This property should only be used in automated tests. **/ wasCancelled: wasCancelled, scan: function() { var defer = $q.defer(); if (this.throwsError) { defer.reject('There was an error scanning.'); } else { defer.resolve({text: this.scannedText, format: this.scannedFormat, cancelled: this.wasCancelled }); } return defer.promise; }, encode: function(type, data) { this.scannedFormat = type; this.scannedText = data; var defer = $q.defer(); if (this.throwsError) { defer.reject('There was an error encoding the data.'); } else { defer.resolve(); } return defer.promise; } }; }]); /** * @ngdoc service * @name ngCordovaMocks.cordovaBLE * * @description * A service for ble features * in an app build with ngCordova. **/ ngCordovaMocks.factory('$cordovaBLE', ['$q', '$timeout', function($q, $timeout) { var deviceScan = { "name": "Test Device", "id": "AA:BB:CC:DD:EE:FF", "advertising": [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121], "rssi": -55 }; var deviceConnect = { "name": "Test Device", "id": "AA:BB:CC:DD:EE:FF", "advertising": [2,1,6,3,3,15,24,8,9,66,97,116,116,101,114,121], "rssi": -55, "services": [ "1800", "1801", "180f" ], "characteristics": [ { "service": "1800", "characteristic": "2a00", "properties": [ "Read" ] }, { "service": "1800", "characteristic": "2a01", "properties": [ "Read" ] }, { "service": "1801", "characteristic": "2a05", "properties": [ "Read" ] }, { "service": "180f", "characteristic": "2a19", "properties": [ "Read" ], "descriptors": [ { "uuid": "2901" }, { "uuid": "2904" } ] } ] }; var readData = new ArrayBuffer(8); return { scan: function (services, seconds) { var q = $q.defer(); $timeout(function () { q.resolve(deviceScan); }, seconds*1000); return q.promise; }, connect: function (deviceID) { var q = $q.defer(); $timeout(function () { q.resolve(deviceConnect); }, 1500); return q.promise; }, disconnect: function (deviceID) { var q = $q.defer(); $timeout(function () { q.resolve(true); }, 500); return q.promise; }, read: function (deviceID, serviceUUID, characteristicUUID) { var q = $q.defer(); $timeout(function () { q.resolve(readData); }, 100); return q.promise; }, write: function (deviceID, serviceUUID, characteristicUUID, data) { var q = $q.defer(); $timeout(function () { q.resolve(true); }, 100); return q.promise; }, writeCommand: function (deviceID, serviceUUID, characteristicUUID, data) { var q = $q.defer(); $timeout(function () { q.resolve(true); }, 100); return q.promise; }, notify: function (deviceID, serviceUUID, characteristicUUID) { var q = $q.defer(); $timeout(function () { q.resolve(true); }, 100); return q.promise; }, indicate: function (deviceID, serviceUUID, characteristicUUID) { var q = $q.defer(); $timeout(function () { q.resolve(true); }, 100); return q.promise; }, isConnected: function (deviceID) { var q = $q.defer(); q.resolve(true); return q.promise; }, isEnabled: function () { var q = $q.defer(); q.resolve(true); return q.promise; } }; }]); /** * @ngdoc service * @name ngCordovaMocks.cordovaCamera * * @description * A service for testing camera features * in an app build with ngCordova. **/ ngCordovaMocks.factory('$cordovaCamera', ['$q', function($q) { var throwsError = false; var imageData = ''; return { /** * @ngdoc property * @name throwsError * @propertyOf ngCordovaMocks.cordovaCamera * * @description * A flag that signals whether a promise should be rejected or not. * This property should only be used in automated tests. **/ throwsError: throwsError, /** * @ngdoc property * @name imageData * @propertyOf ngCordovaMocks.cordovaCamera * * @description * The imagedata (e.g. an url) which will be returned from the device. * This property should only be used in automated tests. **/ imageData: imageData, getPicture: function(options) { var defer = $q.defer(); if (this.throwsError) { defer.reject('There was an error getting the picture.'); } else { if (options) { options = options; // This is just to get by JSHint. } defer.resolve(this.imageData); } return defer.promise; } }; }]); /** * @ngdoc service * @name ngCordovaMocks.cordovaCapture * * @description * A service for testing media capture * in an app build with ngCordova. * * @example */ ngCordovaMocks.factory('$cordovaCapture', ['$q', function($q) { var throwsError = false; return { /** * @ngdoc property * @name throwsError * @propertyOf ngCordovaMocks.cordovaCapture * * @description * A flag that signals whether a promise should be rejected or not. * This property should only be used in automated tests. **/ throwsError: throwsError, captureAudio : function() { var defer = $q.defer(); if (this.throwsError) { defer.reject('There was an error capturing the audio.'); } else { defer.resolve(); } return defer.promise; }, captureImage: function() { var defer = $q.defer(); if (this.throwsError) { defer.reject('There was an error capturing the image.'); } else { defer.resolve(); } return defer.promise; }, captureVideo: function() { var defer = $q.defer(); if (this.throwsError) { defer.reject('There was an error capturing the video.'); } else { defer.resolve(); } return defer.promise; } }; }]); /** * @ngdoc service * @name ngCordovaMocks.cordovaContacts * * @description * A service for testing features related with contacts * in an app build with ngCordova. **/ ngCordovaMocks.factory('$cordovaContacts', ['$q', function($q) { var throwsError = false; var contacts = []; return { /** * @ngdoc property * @name throwsError * @propertyOf ngCordovaMocks.cordovaContacts * * @description * A flag that signals whether a promise should be rejected or not. * This property should only be used in automated tests. **/ throwsError: throwsError, /** * @ngdoc contacts * @name throwsError * @propertyOf ngCordovaMocks.cordovaContacts * * @description * An in-memory collection of contacts. * This property should only be used in automated tests. **/ contacts: contacts, save: function(contact) { var defer = $q.defer(); if (this.throwsError) { defer.reject('There was an error saving the contact.'); } else { var existingIndex = null; for (var i=0; i 0) { this.isVibrating = true; self = this; if (time instanceof Array) { // TODO: Implement pattern here. // The following is a temporary timer that just looks at the first value this.vibrateTimer = $timeout( function() { self.isVibrating = false; self.vibrateTimer = null; }, time[0] ); } else { this.vibrateTimer = $timeout( function() { self.isVibrating = false; self.vibrateTimer = null; }, time ); } } }, /* jshint ignore:start */ vibrateWithPattern: function(pattern, repeat) { // Based on the documentation (https://github.com/apache/cordova-plugin-vibration/blob/master/doc/index.md) // This method is deprecated. For that reason, this isn't implemented at this time. }, /* jshint ignore:end */ cancelVibration: function() { if (this.vibrateTimer !== null) { if (this.isVibrating === true) { $timeout.cancel(this.vibrateTimer); this.isVibrating = false; } } } }; }]); })();