Test Utf8FromUtf16 (#118647)
This commit is contained in:
parent
392dffeb08
commit
5e50ed972b
@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter_driver/driver_extension.dart';
|
||||
@ -44,6 +45,18 @@ void main() async {
|
||||
return (app == system)
|
||||
? 'success'
|
||||
: 'error: app dark mode ($app) does not match system dark mode ($system)';
|
||||
} else if (message == 'verifyStringConversion') {
|
||||
// Use a test string that contains code points that fit in both 8 and 16 bits.
|
||||
// The code points are passed a list of integers through the method channel,
|
||||
// which will use the UTF16 to UTF8 utility function to convert them to a
|
||||
// std::string, which should equate to the original expected string.
|
||||
// TODO(schectman): Remove trailing null from returned string
|
||||
const String expected = 'ABCℵ\x00';
|
||||
final Int32List codePoints = Int32List.fromList(expected.codeUnits);
|
||||
final String converted = await testStringConversion(codePoints);
|
||||
return (converted == expected)
|
||||
? 'success'
|
||||
: 'error: conversion of UTF16 string to UTF8 failed, expected "${expected.codeUnits}" but got "${converted.codeUnits}"';
|
||||
}
|
||||
|
||||
throw 'Unrecognized message: $message';
|
||||
|
@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
const MethodChannel _kMethodChannel =
|
||||
@ -36,3 +38,13 @@ Future<bool> isSystemDarkModeEnabled() async {
|
||||
|
||||
return enabled;
|
||||
}
|
||||
|
||||
/// Test conversion of a UTF16 string to UTF8 using the app template utils.
|
||||
Future<String> testStringConversion(Int32List twoByteCodes) async {
|
||||
final String? converted = await _kMethodChannel.invokeMethod<String?>('convertString', twoByteCodes);
|
||||
if (converted == null) {
|
||||
throw 'Method channel unavailable.';
|
||||
}
|
||||
|
||||
return converted;
|
||||
}
|
||||
|
@ -23,4 +23,13 @@ void main() {
|
||||
|
||||
await driver.close();
|
||||
}, timeout: Timeout.none);
|
||||
|
||||
test('Windows app template can convert string from UTF16 to UTF8', () async {
|
||||
final FlutterDriver driver = await FlutterDriver.connect(printCommunication: true);
|
||||
final String result = await driver.requestData('verifyStringConversion');
|
||||
|
||||
expect(result, equals('success'));
|
||||
|
||||
await driver.close();
|
||||
}, timeout: Timeout.none);
|
||||
}
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include <flutter/standard_method_codec.h>
|
||||
|
||||
#include "flutter/generated_plugin_registrant.h"
|
||||
#include "utils.h"
|
||||
|
||||
/// Window attribute that enables dark mode window decorations.
|
||||
///
|
||||
@ -106,6 +107,15 @@ bool FlutterWindow::OnCreate() {
|
||||
} else {
|
||||
result->Error("error", "Received status " + status);
|
||||
}
|
||||
} else if (method == "convertString") {
|
||||
const flutter::EncodableValue* argument = call.arguments();
|
||||
const std::vector<int32_t> code_points = std::get<std::vector<int32_t>>(*argument);
|
||||
std::vector<wchar_t> wide_str;
|
||||
for (int32_t code_point : code_points) {
|
||||
wide_str.push_back((wchar_t)(code_point));
|
||||
}
|
||||
const std::string string = Utf8FromUtf16(wide_str.data());
|
||||
result->Success(string);
|
||||
} else {
|
||||
result->NotImplemented();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user