-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathweb_test_utils.dart
99 lines (81 loc) · 2.77 KB
/
web_test_utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import 'dart:async';
import 'dart:js_interop';
import 'dart:math';
import 'package:sqlite_async/sqlite3_wasm.dart';
import 'package:sqlite_async/sqlite_async.dart';
import 'package:test/test.dart';
import 'package:web/web.dart' show Blob, BlobPart, BlobPropertyBag;
import 'abstract_test_utils.dart';
@JS('URL.createObjectURL')
external String _createObjectURL(Blob blob);
String? _dbPath;
class TestSqliteOpenFactory extends TestDefaultSqliteOpenFactory {
TestSqliteOpenFactory(
{required super.path, super.sqliteOptions, super.sqlitePath = ''});
@override
Future<CommonDatabase> openDatabaseForSingleConnection() async {
final sqlite = await WasmSqlite3.loadFromUrl(
Uri.parse(sqliteOptions.webSqliteOptions.wasmUri));
sqlite.registerVirtualFileSystem(InMemoryFileSystem(), makeDefault: true);
return sqlite.openInMemory();
}
}
class TestUtils extends AbstractTestUtils {
late Future<void> _isInitialized;
late final SqliteOptions webOptions;
TestUtils() {
_isInitialized = _init();
}
Future<void> _init() async {
final channel = spawnHybridUri('/test/server/worker_server.dart');
final port = (await channel.stream.first as num).toInt();
final sqliteWasmUri = 'http://localhost:$port/sqlite3.wasm';
// Cross origin workers are not supported, but we can supply a Blob
var sqliteUri = 'http://localhost:$port/db_worker.js';
final blob = Blob(<BlobPart>['importScripts("$sqliteUri");'.toJS].toJS,
BlobPropertyBag(type: 'application/javascript'));
sqliteUri = _createObjectURL(blob);
webOptions = SqliteOptions(
webSqliteOptions: WebSqliteOptions(
wasmUri: sqliteWasmUri.toString(), workerUri: sqliteUri));
}
@override
String dbPath() {
if (_dbPath case final path?) {
return path;
}
final created = _dbPath = 'test-db/${Random().nextInt(1 << 31)}/test.db';
addTearDown(() {
// Pick a new path for the next test.
_dbPath = null;
});
return created;
}
@override
Future<void> cleanDb({required String path}) async {}
@override
Future<TestDefaultSqliteOpenFactory> testFactory(
{String? path,
String sqlitePath = '',
List<String> initStatements = const [],
SqliteOptions options = const SqliteOptions.defaults()}) async {
await _isInitialized;
return TestSqliteOpenFactory(
path: path ?? dbPath(),
sqlitePath: sqlitePath,
sqliteOptions: webOptions,
);
}
@override
Future<SqliteDatabase> setupDatabase(
{String? path,
List<String> initStatements = const [],
int maxReaders = SqliteDatabase.defaultMaxReaders}) async {
await _isInitialized;
return super.setupDatabase(path: path);
}
@override
List<String> findSqliteLibraries() {
return ['sqlite3.wasm'];
}
}