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,21 +1,20 @@
(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;
function uniqueName() {
if(!uniqueName.seed) { if(!uniqueName.seed) {
uniqueName.seed = Date.now(); uniqueName.seed = Date.now();
} }
return 'filer-testdb-' + uniqueName.seed++; return 'filer-testdb-' + uniqueName.seed++;
} }
function findBestProvider() { function findBestProvider() {
var providers = Filer.FileSystem.providers; var providers = Filer.FileSystem.providers;
if(providers.IndexedDB.isSupported()) { if(providers.IndexedDB.isSupported()) {
return IndexedDBTestProvider; return IndexedDBTestProvider;
@ -24,15 +23,37 @@
return WebSQLTestProvider; return WebSQLTestProvider;
} }
return MemoryTestProvider; return MemoryTestProvider;
}
function getUrlParams() {
// Check if we are running in node
if(!global.location) {
return null;
} }
function setup(callback) { var url = Url.parse(global.location.href, true);
return url.query;
}
function getProviderType() {
var defaultProvider = 'Memory';
var queryString = getUrlParams();
// If the environment is node or the query string is empty,
// the memory provider will be used.
if(!queryString) {
return defaultProvider;
}
return queryString['filer-provider'] || defaultProvider;
}
function setup(callback) {
// In browser we support specifying the provider via the query string // In browser we support specifying the provider via the query string
// (e.g., ?filer-provider=IndexedDB). If not specified, we use // (e.g., ?filer-provider=IndexedDB). If not specified, we use
// the Memory provider by default. See test/require-config.js // the Memory provider by default.
// for definition of window.filerArgs. var providerType = getProviderType();
var providerType = global.filerArgs && global.filerArgs.provider ?
global.filerArgs.provider : 'Memory';
var name = uniqueName(); var name = uniqueName();
@ -68,28 +89,28 @@
provider: _provider.provider, provider: _provider.provider,
flags: flags flags: flags
}, complete); }, complete);
} }
function fs() { function fs() {
if(!_fs) { if(!_fs) {
throw "TestUtil: call setup() before fs()"; throw "TestUtil: call setup() before fs()";
} }
return _fs; return _fs;
} }
function provider() { function provider() {
if(!_provider) { if(!_provider) {
throw "TestUtil: call setup() before provider()"; throw "TestUtil: call setup() before provider()";
} }
return _provider; return _provider;
} }
function shell(options) { function shell(options) {
var _fs = fs(); var _fs = fs();
return new _fs.Shell(options); return new _fs.Shell(options);
} }
function cleanup(callback) { function cleanup(callback) {
if(!_provider) { if(!_provider) {
return; return;
} }
@ -98,9 +119,9 @@
_fs = null; _fs = null;
callback(); callback();
}); });
} }
function typedArrayEqual(a, b) { function typedArrayEqual(a, b) {
if(!(a && b)) { if(!(a && b)) {
return false; return false;
} }
@ -115,9 +136,9 @@
} }
return true; return true;
} }
module.exports = { module.exports = {
uniqueName: uniqueName, uniqueName: uniqueName,
setup: setup, setup: setup,
fs: fs, fs: fs,
@ -130,6 +151,4 @@
}, },
cleanup: cleanup, cleanup: cleanup,
typedArrayEqual: typedArrayEqual typedArrayEqual: typedArrayEqual
}; };
}(this));