Update a number of APIs to return Uint8List (#323)
Specifically: captureScreenshotAsList, captureElementScreenshotAsList
Also changed some internal storage to use Uint8List and ByteBuffer
And removed some superfluous casts
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1254b84..4871a5c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
* Require Dart 3.4 and add a dependency on `package:web`.
* Ensure HTTP clients are closed if creating a session fails.
+* Update functions that return `List<int>` to return `Uint8List`.
## 3.1.0
diff --git a/lib/src/async/web_driver.dart b/lib/src/async/web_driver.dart
index d71cfb6..1987c41 100644
--- a/lib/src/async/web_driver.dart
+++ b/lib/src/async/web_driver.dart
@@ -14,6 +14,7 @@
import 'dart:async';
import 'dart:convert';
+import 'dart:typed_data';
import '../../sync_core.dart' as sync_core;
import '../common/by.dart';
@@ -203,13 +204,13 @@
_handler.core.parseScreenshotResponse);
/// Take a screenshot of the current page as PNG as list of uint8.
- Future<List<int>> captureScreenshotAsList() async {
+ Future<Uint8List> captureScreenshotAsList() async {
final base64Encoded = captureScreenshotAsBase64();
return base64.decode(await base64Encoded);
}
/// Take a screenshot of the specified element as PNG as list of uint8.
- Future<List<int>> captureElementScreenshotAsList(WebElement element) async {
+ Future<Uint8List> captureElementScreenshotAsList(WebElement element) async {
final base64Encoded = captureElementScreenshotAsBase64(element);
return base64.decode(await base64Encoded);
}
diff --git a/lib/src/common/zip.dart b/lib/src/common/zip.dart
index 342e3ed..cde1fd9 100644
--- a/lib/src/common/zip.dart
+++ b/lib/src/common/zip.dart
@@ -33,7 +33,7 @@
/// This class represents a file in an archive.
class ArchiveFile {
final String name;
- final List<int> content;
+ final Uint8List content;
ArchiveFile(this.name, this.content);
diff --git a/lib/src/request/async_io_request_client.dart b/lib/src/request/async_io_request_client.dart
index ae5971e..627e160 100644
--- a/lib/src/request/async_io_request_client.dart
+++ b/lib/src/request/async_io_request_client.dart
@@ -3,7 +3,6 @@
import 'dart:io' show ContentType, HttpClient, HttpClientRequest, HttpHeaders;
import '../../support/async.dart';
-
import '../common/request.dart';
import '../common/request_client.dart';
@@ -52,7 +51,7 @@
final response = await httpRequest.close();
return WebDriverResponse(response.statusCode, response.reasonPhrase,
- await utf8.decodeStream(response.cast<List<int>>()));
+ await utf8.decodeStream(response));
} finally {
_lock.release();
}
diff --git a/lib/src/sync/web_driver.dart b/lib/src/sync/web_driver.dart
index cffd807..6d33405 100644
--- a/lib/src/sync/web_driver.dart
+++ b/lib/src/sync/web_driver.dart
@@ -13,6 +13,7 @@
// limitations under the License.
import 'dart:convert' show base64;
+import 'dart:typed_data';
import '../../async_core.dart' as async_core;
import '../common/by.dart';
@@ -213,13 +214,13 @@
_handler.core.parseScreenshotResponse);
/// Take a screenshot of the current page as PNG as list of uint8.
- List<int> captureScreenshotAsList() {
+ Uint8List captureScreenshotAsList() {
final base64Encoded = captureScreenshotAsBase64();
return base64.decode(base64Encoded);
}
/// Take a screenshot of the specified element as PNG as list of uint8.
- List<int> captureElementScreenshotAsList(WebElement element) {
+ Uint8List captureElementScreenshotAsList(WebElement element) {
final base64Encoded = captureElementScreenshotAsBase64(element);
return base64.decode(base64Encoded);
}
diff --git a/lib/support/firefox_profile.dart b/lib/support/firefox_profile.dart
index db655c6..4f17325 100644
--- a/lib/support/firefox_profile.dart
+++ b/lib/support/firefox_profile.dart
@@ -13,7 +13,7 @@
// limitations under the License.
import 'dart:collection';
-import 'dart:convert' show LineSplitter, base64;
+import 'dart:convert' show LineSplitter, base64, utf8;
import 'dart:io' as io;
import 'package:path/path.dart' as path;
@@ -237,11 +237,11 @@
}
final prefsJsContent =
- prefs.map((option) => option.asPrefString).join('\n').codeUnits;
+ utf8.encode(prefs.map((option) => option.asPrefString).join('\n'));
archive.addFile(ArchiveFile('prefs.js', prefsJsContent));
final userJsContent =
- userPrefs.map((option) => option.asPrefString).join('\n').codeUnits;
+ utf8.encode(userPrefs.map((option) => option.asPrefString).join('\n'));
archive.addFile(ArchiveFile('user.js', userJsContent));
final zipData = ZipEncoder.encode(archive);
diff --git a/lib/support/stdio_stepper.dart b/lib/support/stdio_stepper.dart
index fef29f3..25371e2 100644
--- a/lib/support/stdio_stepper.dart
+++ b/lib/support/stdio_stepper.dart
@@ -15,6 +15,7 @@
import 'dart:async' show StreamController;
import 'dart:convert' show Encoding, json;
import 'dart:io' show Stdin, exit, stdin, systemEncoding;
+import 'dart:typed_data';
import '../src/async/stepper.dart';
@@ -82,7 +83,7 @@
static const lf = 10;
bool _crPrevious = false;
- final _bytes = <int>[];
+ final _bytes = BytesBuilder();
final _controller = StreamController<String>.broadcast();
final Encoding encoding;
@@ -113,10 +114,9 @@
}
_crPrevious = byte == cr;
if (byte == cr || byte == lf) {
- _controller.add(encoding.decode(_bytes));
- _bytes.clear();
+ _controller.add(encoding.decode(_bytes.takeBytes()));
} else {
- _bytes.add(byte);
+ _bytes.addByte(byte);
}
}
diff --git a/test/configs/async_io_config.dart b/test/configs/async_io_config.dart
index e42175f..55e46d4 100644
--- a/test/configs/async_io_config.dart
+++ b/test/configs/async_io_config.dart
@@ -58,7 +58,7 @@
request.response
..statusCode = HttpStatus.ok
..headers.set('Content-type', 'text/html');
- file.openRead().cast<List<int>>().pipe(request.response);
+ file.openRead().pipe(request.response);
} else {
request.response
..statusCode = HttpStatus.notFound
diff --git a/test/configs/sync_io_config.dart b/test/configs/sync_io_config.dart
index 76c6f4d..db3cb91 100644
--- a/test/configs/sync_io_config.dart
+++ b/test/configs/sync_io_config.dart
@@ -60,7 +60,7 @@
request.response
..statusCode = HttpStatus.ok
..headers.set('Content-type', 'text/html');
- file.openRead().cast<List<int>>().pipe(request.response);
+ file.openRead().pipe(request.response);
} else {
request.response
..statusCode = HttpStatus.notFound
diff --git a/test/support/firefox_profile_test.dart b/test/support/firefox_profile_test.dart
index 113ef1c..3be1e65 100644
--- a/test/support/firefox_profile_test.dart
+++ b/test/support/firefox_profile_test.dart
@@ -141,8 +141,7 @@
final prefs = FirefoxProfile.loadPrefsFile(MockFile(
String.fromCharCodes(
- zipArchive.files.firstWhere((f) => f.name == 'user.js').content
- as List<int>,
+ zipArchive.files.firstWhere((f) => f.name == 'user.js').content,
),
));
expect(
@@ -179,8 +178,7 @@
final prefs = FirefoxProfile.loadPrefsFile(
MockFile(
String.fromCharCodes(
- zipArchive.files.firstWhere((f) => f.name == 'user.js').content
- as List<int>,
+ zipArchive.files.firstWhere((f) => f.name == 'user.js').content,
),
),
);
diff --git a/test/support/zip_test.dart b/test/support/zip_test.dart
index da105b2..4bcfe86 100644
--- a/test/support/zip_test.dart
+++ b/test/support/zip_test.dart
@@ -29,13 +29,13 @@
var file = zipArchive.files[0];
expect(file.name, 'dart_test.yaml');
expect(file.size, 166);
- expect(utf8.decode(file.content as List<int>),
+ expect(utf8.decode(file.content),
contains('See https://github.com/dart-lang/test/'));
file = zipArchive.files[1];
expect(file.name, 'lib/src/common/spec.dart');
expect(file.size, 209);
- expect(utf8.decode(file.content as List<int>),
+ expect(utf8.decode(file.content),
contains('Defines the WebDriver spec to use'));
});
@@ -75,7 +75,7 @@
group('crc32', () {
test('0 bytes', () {
- expect(crc32([]), 0x00000000);
+ expect(crc32(<int>[]), 0x00000000);
});
test('1 byte', () {