Commit 5ab6b025 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

QUnit&Sinon.js localStorage complete (todo simplify tests).

Local storage save document bugs fixed.
parent f32b249f
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>JIO QUnit Test</title> <title>JIO QUnit/Sinon Test</title>
<link rel="stylesheet" href="qunit/qunit-1.5.0.css" /> <link rel="stylesheet" href="qunit/qunit-1.5.0.css" />
</head> </head>
<body> <body>
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
<script type="text/javascript" src="unhosted/jio.js"></script> <script type="text/javascript" src="unhosted/jio.js"></script>
<script type="text/javascript" src="unhosted/base64.js"></script> <script type="text/javascript" src="unhosted/base64.js"></script>
<script type="text/javascript" src="unhosted/jio.storage.js"></script> <script type="text/javascript" src="unhosted/jio.storage.js"></script>
<script type="text/javascript" src="sinon/sinon-1.3.4.js"></script>
<script type="text/javascript" src="sinon/sinon-qunit-1.0.0.js"></script>
<script type="text/javascript" src="jiotests.js"></script> <script type="text/javascript" src="jiotests.js"></script>
</body> </body>
</html> </html>
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
/**
* sinon-qunit 1.0.0, 2010/12/09
*
* @author Christian Johansen (christian@cjohansen.no)
*
* (The BSD License)
*
* Copyright (c) 2010-2011, Christian Johansen, christian@cjohansen.no
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Christian Johansen nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*global sinon, QUnit, test*/
sinon.assert.fail = function (msg) {
QUnit.ok(false, msg);
};
sinon.assert.pass = function (assertion) {
QUnit.ok(true, assertion);
};
sinon.config = {
injectIntoThis: true,
injectInto: null,
properties: ["spy", "stub", "mock", "clock", "sandbox"],
useFakeTimers: true,
useFakeServer: false
};
(function (global) {
var qTest = QUnit.test;
QUnit.test = global.test = function (testName, expected, callback, async) {
if (arguments.length === 2) {
callback = expected;
expected = null;
}
return qTest(testName, expected, sinon.test(callback), async);
};
}(this));
This diff is collapsed.
...@@ -25,6 +25,10 @@ ...@@ -25,6 +25,10 @@
'start_event':'start_loading', 'start_event':'start_loading',
'stop_event':'stop_loading', 'stop_event':'stop_loading',
'retvalue':'fileContent' }, // returns the file content 'string' 'retvalue':'fileContent' }, // returns the file content 'string'
'getDocument': {
'start_event':'start_gettingDocument',
'stop_event':'stop_gettingDocument',
'retvalue':'document' }, // returns the document object
'getDocumentList': { 'getDocumentList': {
'start_event':'start_gettingList', 'start_event':'start_gettingList',
'stop_event':'stop_gettingList', 'stop_event':'stop_gettingList',
...@@ -600,7 +604,7 @@ ...@@ -600,7 +604,7 @@
// or in the storage set at init. At the end of the job, // or in the storage set at init. At the end of the job,
// 'job_done' will be sent with this job and its 'isAvailable' // 'job_done' will be sent with this job and its 'isAvailable'
// return value. // return value.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to check (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result. // options.callback(result) : called to get the result.
...@@ -628,7 +632,7 @@ ...@@ -628,7 +632,7 @@
// Load a document in the storage set in [options] // Load a document in the storage set in [options]
// or in the storage set at init. At the end of the job, // or in the storage set at init. At the end of the job,
// 'job_done' will be sent with this job. // 'job_done' will be sent with this job.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to save (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result. // options.callback(result) : called to get the result.
...@@ -658,13 +662,14 @@ ...@@ -658,13 +662,14 @@
// or in the storage set at init. At the end of the job, // or in the storage set at init. At the end of the job,
// 'job_done' will be sent with this job and its 'fileContent' // 'job_done' will be sent with this job and its 'fileContent'
// return value. // return value.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to load (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result. // options.callback(result) : called to get the result.
// jio.loadDocument({'fileName':'file','callback': // jio.loadDocument({'fileName':'file','callback':
// function (result) { alert('content: '+ // function (result) { alert('content: '+
// result.fileContent); }}); // result.doc.fileContent + ' creation date: ' +
// result.doc.creationDate); }});
console.log ('load'); console.log ('load');
if (!this.isReady()) return null; if (!this.isReady()) return null;
...@@ -676,19 +681,15 @@ ...@@ -676,19 +681,15 @@
},options); },options);
// check dependencies // check dependencies
if ( settings.fileName && settings.storage && settings.applicant) { if ( settings.fileName && settings.storage && settings.applicant) {
return jioGlobalObj.queue.addJob ( new Job ( settings ) ); return this.queue.addJob ( new Job ( settings ) );
} }
return null; return null;
}, },
getDocument: function ( options ) {
// TODO (don't forgot to add this method to constants)
},
getDocumentList: function ( options ) { getDocumentList: function ( options ) {
// Get a document list of the user in the storage set in [options] // Get a document list of the user in the storage set in [options]
// or in the storage set at init. // or in the storage set at init.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to get the list (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result. // options.callback(result) : called to get the result.
......
...@@ -34,6 +34,10 @@ ...@@ -34,6 +34,10 @@
// if the name already exists, it is not available. // if the name already exists, it is not available.
// job: the job object // job: the job object
// job.userName: the name we want to check. // job.userName: the name we want to check.
// jobendcallback : the function called at the end of the job.
// returns {'status':string,'message':string,'isAvailable':boolean}
// in the jobendcallback arguments.
// wait a little in order to simulate asynchronous operation // wait a little in order to simulate asynchronous operation
setTimeout(function () { setTimeout(function () {
...@@ -73,6 +77,10 @@ ...@@ -73,6 +77,10 @@
// job.options.overwrite : true -> overwrite // job.options.overwrite : true -> overwrite
// job.options.force : true -> save even if jobdate < existingdate // job.options.force : true -> save even if jobdate < existingdate
// or overwrite: false // or overwrite: false
// jobendcallback : the function called at the end of the job.
// returns {'status':string,'message':string,'isSaved':boolean}
// in the jobendcallback arguments.
var settings = $.extend({'overwrite':true, var settings = $.extend({'overwrite':true,
'force':false},job.options); 'force':false},job.options);
...@@ -88,8 +96,8 @@ ...@@ -88,8 +96,8 @@
doc = { doc = {
'fileName': job.fileName, 'fileName': job.fileName,
'fileContent': job.fileContent, 'fileContent': job.fileContent,
'creationDate': Date.now (), 'creationDate': job.lastModified,
'lastModified': Date.now () 'lastModified': job.lastModified
} }
// writing // writing
jioGlobalObj.localStorage.setItem( jioGlobalObj.localStorage.setItem(
...@@ -119,7 +127,7 @@ ...@@ -119,7 +127,7 @@
return; return;
} }
// overwriting // overwriting
doc.lastModified = Date.now(); doc.lastModified = job.lastModified;
doc.fileContent = job.fileContent; doc.fileContent = job.fileContent;
// writing // writing
jioGlobalObj.localStorage.setItem( jioGlobalObj.localStorage.setItem(
...@@ -145,9 +153,17 @@ ...@@ -145,9 +153,17 @@
}, // end saveDocument }, // end saveDocument
loadDocument: function ( job, jobendcallback ) { loadDocument: function ( job, jobendcallback ) {
// load a document in the storage, copy the content into the job // Load a document from the storage. It returns a document object
// job : the job // containing all information of the document and its content.
// job : the job object
// job.fileName : the document name we want to load.
// jobendcallback : the function called at the end of the job.
// returns {'status':string,'message':string,'document':object}
// in the jobendcallback arguments.
// document object is {'fileName':string,'fileContent':string,
// 'creationDate':date,'lastModified':date}
var t = this; var t = this;
// wait a little in order to simulate asynchronous operation // wait a little in order to simulate asynchronous operation
setTimeout(function () { setTimeout(function () {
...@@ -164,7 +180,11 @@ ...@@ -164,7 +180,11 @@
} else { } else {
res.status = job.status = 'done'; res.status = job.status = 'done';
res.message = 'Document loaded.'; res.message = 'Document loaded.';
res.fileContent = doc.fileContent; res.document = {
'fileContent': doc.fileContent,
'fileName': doc.fileName,
'creationDate': doc.creationDate,
'lastModified': doc.lastModified};
jobendcallback(job); jobendcallback(job);
job.callback(res); job.callback(res);
} }
...@@ -172,6 +192,17 @@ ...@@ -172,6 +192,17 @@
}, // end loadDocument }, // end loadDocument
getDocumentList: function ( job, jobendcallback) { getDocumentList: function ( job, jobendcallback) {
// Get a document list from the storage. It returns a document
// array containing all the user documents informations, but not
// their content.
// job : the job object
// jobendcallback : the function called at the end of the job.
// returns {'status':string,'message':string,'list':array}
// in the jobendcallback arguments.
// the list is [object,object] -> object = {'fileName':string,
// 'lastModified':date,'creationDate':date}
var t = this; var t = this;
setTimeout(function () { setTimeout(function () {
var res = {}; var res = {};
...@@ -198,6 +229,13 @@ ...@@ -198,6 +229,13 @@
}, // end getDocumentList }, // end getDocumentList
removeDocument: function ( job, jobendcallback ) { removeDocument: function ( job, jobendcallback ) {
// Remove a document from the storage.
// job : the job object
// jobendcallback : the function called at the end of the job.
// returns {'status':string,'message':string,'isRemoved':boolean}
// in the jobendcallback arguments.
var t = this; var t = this;
setTimeout (function () { setTimeout (function () {
var res = {}; var res = {};
...@@ -228,7 +266,7 @@ ...@@ -228,7 +266,7 @@
} }
}; };
// add key to storageObject // add key to storageObjectType of global jio
Jio.addStorageType('local', function (options) { Jio.addStorageType('local', function (options) {
return new LocalStorage(options); return new LocalStorage(options);
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment