2014-05-23 18:54:59 +00:00
|
|
|
(function(global) {
|
|
|
|
|
|
|
|
var Filer = require('../..');
|
|
|
|
var IndexedDBTestProvider = require('./indexeddb.js');
|
|
|
|
var WebSQLTestProvider = require('./websql.js');
|
|
|
|
var MemoryTestProvider = require('./memory.js');
|
2014-01-21 21:25:09 +00:00
|
|
|
|
|
|
|
var _provider;
|
|
|
|
var _fs;
|
|
|
|
|
|
|
|
function uniqueName() {
|
|
|
|
if(!uniqueName.seed) {
|
|
|
|
uniqueName.seed = Date.now();
|
|
|
|
}
|
|
|
|
return 'filer-testdb-' + uniqueName.seed++;
|
|
|
|
}
|
|
|
|
|
2014-02-19 22:47:14 +00:00
|
|
|
function findBestProvider() {
|
|
|
|
var providers = Filer.FileSystem.providers;
|
|
|
|
if(providers.IndexedDB.isSupported()) {
|
|
|
|
return IndexedDBTestProvider;
|
|
|
|
}
|
|
|
|
if(providers.WebSQL.isSupported()) {
|
|
|
|
return WebSQLTestProvider;
|
|
|
|
}
|
|
|
|
return MemoryTestProvider;
|
|
|
|
}
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
function setup(callback) {
|
2014-05-23 18:54:59 +00:00
|
|
|
// In browser we support specifying the provider via the query string
|
2014-01-21 21:25:09 +00:00
|
|
|
// (e.g., ?filer-provider=IndexedDB). If not specified, we use
|
|
|
|
// the Memory provider by default. See test/require-config.js
|
|
|
|
// for definition of window.filerArgs.
|
2014-05-23 18:54:59 +00:00
|
|
|
var providerType = global.filerArgs && global.filerArgs.provider ?
|
|
|
|
global.filerArgs.provider : 'Memory';
|
2014-01-21 21:25:09 +00:00
|
|
|
|
|
|
|
var name = uniqueName();
|
|
|
|
|
|
|
|
switch(providerType.toLowerCase()) {
|
|
|
|
case 'indexeddb':
|
|
|
|
_provider = new IndexedDBTestProvider(name);
|
|
|
|
break;
|
|
|
|
case 'websql':
|
|
|
|
_provider = new WebSQLTestProvider(name);
|
|
|
|
break;
|
|
|
|
case 'memory':
|
|
|
|
/* falls through */
|
|
|
|
default:
|
2014-02-19 22:47:14 +00:00
|
|
|
var BestProvider = findBestProvider();
|
|
|
|
_provider = new BestProvider(name);
|
2014-01-21 21:25:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow passing FS flags on query string
|
2014-05-23 20:53:50 +00:00
|
|
|
var flags = global.filerArgs && global.filerArgs.flags ?
|
2014-05-23 18:54:59 +00:00
|
|
|
global.filerArgs.flags : 'FORMAT';
|
2014-01-21 21:25:09 +00:00
|
|
|
|
|
|
|
// Create a file system and wait for it to get setup
|
|
|
|
_provider.init();
|
2014-05-16 17:18:13 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
function complete(err, fs) {
|
|
|
|
if(err) throw err;
|
|
|
|
_fs = fs;
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
return new Filer.FileSystem({
|
|
|
|
name: name,
|
|
|
|
provider: _provider.provider,
|
|
|
|
flags: flags
|
|
|
|
}, complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fs() {
|
|
|
|
if(!_fs) {
|
|
|
|
throw "TestUtil: call setup() before fs()";
|
|
|
|
}
|
|
|
|
return _fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function provider() {
|
|
|
|
if(!_provider) {
|
|
|
|
throw "TestUtil: call setup() before provider()";
|
|
|
|
}
|
|
|
|
return _provider;
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:15:02 +00:00
|
|
|
function shell(options) {
|
2014-11-06 15:31:45 +00:00
|
|
|
var _fs = fs();
|
|
|
|
return new _fs.Shell(options);
|
2014-02-15 15:54:54 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
function cleanup(callback) {
|
|
|
|
if(!_provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_provider.cleanup(function() {
|
|
|
|
_provider = null;
|
|
|
|
_fs = null;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:53:05 +00:00
|
|
|
function typedArrayEqual(a, b) {
|
|
|
|
if(!(a && b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(a.length !== b.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var i = 0; i < a.length; ++ i) {
|
|
|
|
if(a[i] !== b[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:54:59 +00:00
|
|
|
module.exports = {
|
2014-01-21 21:25:09 +00:00
|
|
|
uniqueName: uniqueName,
|
|
|
|
setup: setup,
|
|
|
|
fs: fs,
|
2014-02-15 15:54:54 +00:00
|
|
|
shell: shell,
|
2014-01-21 21:25:09 +00:00
|
|
|
provider: provider,
|
|
|
|
providers: {
|
|
|
|
IndexedDB: IndexedDBTestProvider,
|
|
|
|
WebSQL: WebSQLTestProvider,
|
|
|
|
Memory: MemoryTestProvider
|
|
|
|
},
|
2014-02-19 17:53:05 +00:00
|
|
|
cleanup: cleanup,
|
|
|
|
typedArrayEqual: typedArrayEqual
|
2014-01-21 21:25:09 +00:00
|
|
|
};
|
|
|
|
|
2014-05-23 18:54:59 +00:00
|
|
|
}(this));
|