var platform = cordova.platformId.match(/ios/i) ? 'ios' : 'android';

var safe = cordova.plugins.disusered.safe;

var key = 'someKey';
var prefix = 'https://raw.githubusercontent.com/disusered/cordova-safe/develop/tests/';

exports.defineAutoTests = function() {
  describe('cordova.plugins.disusered', function() {
    it('should exist', function() {
      expect(window.cordova.plugins.disusered).toBeDefined();
    });

    it('should pass a test', function() {
      expect(window).toBeDefined();
    });
  });

  describe('cordova.plugins.disusered.safe', function() {
    it('should exist', function() {
      expect(safe).toBeDefined();
    });

    it('should have an "encrypt" method', function() {
      expect(safe.encrypt).toBeDefined();
    });

    it('should have an "decrypt" method', function() {
      expect(safe.decrypt).toBeDefined();
    });
  });

  describe('decrypted test file', function() {
    var uri;
    beforeEach(function(done) {
      var url = prefix + 'decrypted.png';
      function success(path) { test(path); }
      function error(code) { test(); }
      function test(path) { uri = path; done(); }
      download(url, success, error, true);
    });

    it('should exist locally', function() {
      expect(uri).toBeDefined();
    });

    describe('encrypt method', function() {
      var encryptedFile;

      beforeEach(function(done) {
        function success(path) { test(path); }
        function error(code) { test(); }
        function test(path) { encryptedFile = path; done(); }
        safe.encrypt(uri, key, success, error);
      });

      it('should encrypt the test file', function() {
        expect(encryptedFile).toBeDefined();
      });
    });
  });

  describe('encrypted test file', function() {
    var uri;
    beforeEach(function(done) {
      var url = prefix + 'encrypted-' + platform + '.png';
      function success(path) { test(path); }
      function error(code) { test(); }
      function test(path) { uri = path; done(); }
      download(url, success, error, true);
    });

    it('should exist locally', function() {
      expect(uri).toBeDefined();
    });

    describe('decrypt method', function() {
      var decryptedFile;

      beforeEach(function(done) {
        function success(path) { test(path); }
        function error(code) { test(); }
        function test(path) { decryptedFile = path; done(); }
        safe.decrypt(uri, key, success, error);
      });

      it('should decrypt the test file', function() {
        expect(decryptedFile).toBeDefined();
      });
    });
  });
};

/**
 * download
 *
 * @param {String} url File URI
 * @param {Function} success Success callback
 * @param {Function} error Failure callback
 * @param {Boolean} trustAllCertificates Trusts any certificate when the connection is done over HTTPS.
 * @returns {void}
 */
function download(url, success, error, trustAllCertificates) {
  var ft = new FileTransfer(); // eslint-disable-line no-undef
  var ios = cordova.file.cacheDirectory;
  var ext = cordova.file.externalCacheDirectory;
  var dir = (ext) ? ext : ios;
  var name = url.substring(url.lastIndexOf('/') + 1);
  var path = dir + name;

  if (typeof success === 'boolean') {
    trustAllCertificates = success;
  }

  return new Promise(function(resolve, reject) {
    function onSuccess(uri, callback) {
      if (typeof callback === 'function') {
        callback(uri);
      }
      resolve(uri);
      return uri;
    }

    function onError(callback) {
      var code = (arguments.length > 1) ? arguments[1] : 0;
      if (typeof callback === 'function') {
        callback(code);
      }
      reject(code);
      return code;
    }

    if (typeof trustAllCertificates !== 'boolean') {
      // Defaults to false
      trustAllCertificates = false;
    }

    ft.download(encodeURI(url), path, function(entry) {
          var file = entry.toURL();
          onSuccess(file, success);
        }, onError.bind(this, error), trustAllCertificates);

  });
}

@@ -2570,16 +2570,1357 @@
 %7D);%0A%7D;%0A%0A
+exports.defineManualTests = function(contentEl, createActionButton) %7B%0A  var testInfo;%0A%0A  var decrypted = prefix + 'decrypted.png';%0A  var encrypted = prefix + 'encrypted-' + platform + '.png';%0A%0A  testInfo = '%3Ch3%3EPress Open File and a test file will open in a ' +%0A    'native context%3C/h3%3E%3Cdiv id=%22crypto%22%3E%3C/div%3E' +%0A    'Expected result: File crypto operation succeeds.';%0A%0A  contentEl.innerHTML = testInfo;%0A%0A  function error(code) %7B%0A    if (code.error === 1 %7C%7C code === 1) %7B%0A      console.log('No file handler found');%0A    %7D else %7B%0A      console.log('Undefined error');%0A    %7D%0A    return Promise.reject(code);%0A  %7D%0A%0A  function promisify(uri, action, resolve, reject) %7B%0A    safe%5Baction%5D(uri, key, function(encryptedFileUri) %7B%0A      resolve(encryptedFileUri);%0A    %7D, function() %7B%0A      reject('Error with cryptographic operation');%0A    %7D);%0A  %7D%0A%0A  createActionButton('Decrypt', function() %7B%0A    download(encrypted, true).then(function(uri) %7B%0A      return new Promise(promisify.bind(this, uri, 'decrypt'));%0A    %7D).then(function(uri) %7B%0A      console.log(uri);%0A    %7D).catch(error);%0A  %7D, 'crypto');%0A%0A  createActionButton('Encrypt', function() %7B%0A    download(decrypted, true).then(function(uri) %7B%0A      return new Promise(promisify.bind(this, uri, 'encrypt'));%0A    %7D).then(function(uri) %7B%0A      console.log(uri);%0A    %7D).catch(error);%0A  %7D, 'crypto');%0A%7D;%0A%0A
 /**%0A * d

Add manual test for encrypt and decrypt