|
5 | 5 | @TestOn('chrome') // Uses web-only Flutter SDK
|
6 | 6 |
|
7 | 7 | import 'dart:convert';
|
8 |
| -import 'dart:html' as html; |
| 8 | +import 'dart:js_interop'; |
9 | 9 | import 'dart:typed_data';
|
10 | 10 |
|
11 | 11 | import 'package:cross_file/cross_file.dart';
|
12 |
| -import 'package:js/js_util.dart' as js_util; |
13 | 12 | import 'package:test/test.dart';
|
| 13 | +import 'package:web/helpers.dart' as html; |
14 | 14 |
|
15 | 15 | const String expectedStringContents = 'Hello, world! I ❤ ñ! 空手';
|
16 | 16 | final Uint8List bytes = Uint8List.fromList(utf8.encode(expectedStringContents));
|
17 |
| -final html.File textFile = html.File(<Object>[bytes], 'hello.txt'); |
18 |
| -final String textFileUrl = html.Url.createObjectUrl(textFile); |
| 17 | +final html.File textFile = |
| 18 | + html.File(<JSUint8Array>[bytes.toJS].toJS, 'hello.txt'); |
| 19 | +final String textFileUrl = |
| 20 | + // TODO(kevmoo): drop ignore when pkg:web constraint excludes v0.3 |
| 21 | + // ignore: unnecessary_cast |
| 22 | + html.URL.createObjectURL(textFile as JSObject); |
19 | 23 |
|
20 | 24 | void main() {
|
21 | 25 | group('Create with an objectUrl', () {
|
@@ -63,16 +67,16 @@ void main() {
|
63 | 67 |
|
64 | 68 | test('Stores data as a Blob', () async {
|
65 | 69 | // Read the blob from its path 'natively'
|
66 |
| - final Object response = await html.window.fetch(file.path) as Object; |
67 |
| - // Call '.arrayBuffer()' on the fetch response object to look at its bytes. |
68 |
| - final ByteBuffer data = await js_util.promiseToFuture( |
69 |
| - js_util.callMethod(response, 'arrayBuffer', <Object?>[]), |
70 |
| - ); |
| 70 | + final html.Response response = |
| 71 | + (await html.window.fetch(file.path.toJS).toDart)! as html.Response; |
| 72 | + |
| 73 | + final JSAny? arrayBuffer = await response.arrayBuffer().toDart; |
| 74 | + final ByteBuffer data = (arrayBuffer! as JSArrayBuffer).toDart; |
71 | 75 | expect(data.asUint8List(), equals(bytes));
|
72 | 76 | });
|
73 | 77 |
|
74 | 78 | test('Data may be purged from the blob!', () async {
|
75 |
| - html.Url.revokeObjectUrl(file.path); |
| 79 | + html.URL.revokeObjectURL(file.path); |
76 | 80 |
|
77 | 81 | expect(() async {
|
78 | 82 | await file.readAsBytes();
|
@@ -102,17 +106,24 @@ void main() {
|
102 | 106 |
|
103 | 107 | final html.Element container =
|
104 | 108 | html.querySelector('#$crossFileDomElementId')!;
|
105 |
| - final html.AnchorElement element = container.children |
106 |
| - .firstWhere((html.Element element) => element.tagName == 'A') |
107 |
| - as html.AnchorElement; |
| 109 | + |
| 110 | + late html.HTMLAnchorElement element; |
| 111 | + for (int i = 0; i < container.childNodes.length; i++) { |
| 112 | + final html.Element test = container.children.item(i)!; |
| 113 | + if (test.tagName == 'A') { |
| 114 | + element = test as html.HTMLAnchorElement; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
108 | 118 |
|
109 | 119 | // if element is not found, the `firstWhere` call will throw StateError.
|
110 | 120 | expect(element.href, file.path);
|
111 | 121 | expect(element.download, file.name);
|
112 | 122 | });
|
113 | 123 |
|
114 | 124 | test('anchor element is clicked', () async {
|
115 |
| - final html.AnchorElement mockAnchor = html.AnchorElement(); |
| 125 | + final html.HTMLAnchorElement mockAnchor = |
| 126 | + html.document.createElement('a') as html.HTMLAnchorElement; |
116 | 127 |
|
117 | 128 | final CrossFileTestOverrides overrides = CrossFileTestOverrides(
|
118 | 129 | createAnchorElement: (_, __) => mockAnchor,
|
|
0 commit comments