Fix #340 - Set provider type for unit tests in the browser

This commit is contained in:
Gideon Thomas 2015-03-30 10:30:50 -04:00
parent 78f9ce824d
commit e9c4cb6d7a
1 changed files with 135 additions and 116 deletions

View File

@ -1,135 +1,154 @@
(function(global) { var Filer = require('../..');
var IndexedDBTestProvider = require('./indexeddb.js');
var WebSQLTestProvider = require('./websql.js');
var MemoryTestProvider = require('./memory.js');
var Url = require('url');
var Filer = require('../..'); var _provider;
var IndexedDBTestProvider = require('./indexeddb.js'); var _fs;
var WebSQLTestProvider = require('./websql.js');
var MemoryTestProvider = require('./memory.js');
var _provider; function uniqueName() {
var _fs; if(!uniqueName.seed) {
uniqueName.seed = Date.now();
}
return 'filer-testdb-' + uniqueName.seed++;
}
function uniqueName() { function findBestProvider() {
if(!uniqueName.seed) { var providers = Filer.FileSystem.providers;
uniqueName.seed = Date.now(); if(providers.IndexedDB.isSupported()) {
} return IndexedDBTestProvider;
return 'filer-testdb-' + uniqueName.seed++; }
if(providers.WebSQL.isSupported()) {
return WebSQLTestProvider;
}
return MemoryTestProvider;
}
function getUrlParams() {
// Check if we are running in node
if(!global.location) {
return null;
} }
function findBestProvider() { var url = Url.parse(global.location.href, true);
var providers = Filer.FileSystem.providers;
if(providers.IndexedDB.isSupported()) { return url.query;
return IndexedDBTestProvider; }
}
if(providers.WebSQL.isSupported()) { function getProviderType() {
return WebSQLTestProvider; var defaultProvider = 'Memory';
} var queryString = getUrlParams();
return MemoryTestProvider;
// If the environment is node or the query string is empty,
// the memory provider will be used.
if(!queryString) {
return defaultProvider;
} }
function setup(callback) { return queryString['filer-provider'] || defaultProvider;
// In browser we support specifying the provider via the query string }
// (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.
var providerType = global.filerArgs && global.filerArgs.provider ?
global.filerArgs.provider : 'Memory';
var name = uniqueName(); function setup(callback) {
// In browser we support specifying the provider via the query string
// (e.g., ?filer-provider=IndexedDB). If not specified, we use
// the Memory provider by default.
var providerType = getProviderType();
switch(providerType.toLowerCase()) { var name = uniqueName();
case 'indexeddb':
_provider = new IndexedDBTestProvider(name);
break;
case 'websql':
_provider = new WebSQLTestProvider(name);
break;
case 'memory':
/* falls through */
default:
var BestProvider = findBestProvider();
_provider = new BestProvider(name);
break;
}
// Allow passing FS flags on query string switch(providerType.toLowerCase()) {
var flags = global.filerArgs && global.filerArgs.flags ? case 'indexeddb':
global.filerArgs.flags : 'FORMAT'; _provider = new IndexedDBTestProvider(name);
break;
// Create a file system and wait for it to get setup case 'websql':
_provider.init(); _provider = new WebSQLTestProvider(name);
break;
function complete(err, fs) { case 'memory':
if(err) throw err; /* falls through */
_fs = fs; default:
callback(); var BestProvider = findBestProvider();
} _provider = new BestProvider(name);
return new Filer.FileSystem({ break;
name: name,
provider: _provider.provider,
flags: flags
}, complete);
} }
function fs() { // Allow passing FS flags on query string
if(!_fs) { var flags = global.filerArgs && global.filerArgs.flags ?
throw "TestUtil: call setup() before fs()"; global.filerArgs.flags : 'FORMAT';
}
return _fs; // Create a file system and wait for it to get setup
_provider.init();
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;
}
function shell(options) {
var _fs = fs();
return new _fs.Shell(options);
}
function cleanup(callback) {
if(!_provider) {
return;
}
_provider.cleanup(function() {
_provider = null;
_fs = null;
callback();
});
}
function typedArrayEqual(a, b) {
if(!(a && b)) {
return false;
}
if(a.length !== b.length) {
return false;
} }
function provider() { for(var i = 0; i < a.length; ++ i) {
if(!_provider) { if(a[i] !== b[i]) {
throw "TestUtil: call setup() before provider()";
}
return _provider;
}
function shell(options) {
var _fs = fs();
return new _fs.Shell(options);
}
function cleanup(callback) {
if(!_provider) {
return;
}
_provider.cleanup(function() {
_provider = null;
_fs = null;
callback();
});
}
function typedArrayEqual(a, b) {
if(!(a && b)) {
return false; 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;
} }
module.exports = { return true;
uniqueName: uniqueName, }
setup: setup,
fs: fs,
shell: shell,
provider: provider,
providers: {
IndexedDB: IndexedDBTestProvider,
WebSQL: WebSQLTestProvider,
Memory: MemoryTestProvider
},
cleanup: cleanup,
typedArrayEqual: typedArrayEqual
};
}(this)); module.exports = {
uniqueName: uniqueName,
setup: setup,
fs: fs,
shell: shell,
provider: provider,
providers: {
IndexedDB: IndexedDBTestProvider,
WebSQL: WebSQLTestProvider,
Memory: MemoryTestProvider
},
cleanup: cleanup,
typedArrayEqual: typedArrayEqual
};