From 5e50ed972b17ce450862e785eb5dd6e75417ce16 Mon Sep 17 00:00:00 2001 From: yaakovschectman <109111084+yaakovschectman@users.noreply.github.com> Date: Mon, 23 Jan 2023 16:07:55 -0500 Subject: [PATCH] Test Utf8FromUtf16 (#118647) --- .../windows_startup_test/lib/main.dart | 13 +++++++++++++ .../windows_startup_test/lib/windows.dart | 12 ++++++++++++ .../windows_startup_test/test_driver/main_test.dart | 9 +++++++++ .../windows/runner/flutter_window.cpp | 10 ++++++++++ 4 files changed, 44 insertions(+) diff --git a/dev/integration_tests/windows_startup_test/lib/main.dart b/dev/integration_tests/windows_startup_test/lib/main.dart index 1ebd1e4180..d83a911781 100644 --- a/dev/integration_tests/windows_startup_test/lib/main.dart +++ b/dev/integration_tests/windows_startup_test/lib/main.dart @@ -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'; diff --git a/dev/integration_tests/windows_startup_test/lib/windows.dart b/dev/integration_tests/windows_startup_test/lib/windows.dart index 6773180b4f..d9ab32e6df 100644 --- a/dev/integration_tests/windows_startup_test/lib/windows.dart +++ b/dev/integration_tests/windows_startup_test/lib/windows.dart @@ -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 isSystemDarkModeEnabled() async { return enabled; } + +/// Test conversion of a UTF16 string to UTF8 using the app template utils. +Future testStringConversion(Int32List twoByteCodes) async { + final String? converted = await _kMethodChannel.invokeMethod('convertString', twoByteCodes); + if (converted == null) { + throw 'Method channel unavailable.'; + } + + return converted; +} diff --git a/dev/integration_tests/windows_startup_test/test_driver/main_test.dart b/dev/integration_tests/windows_startup_test/test_driver/main_test.dart index a002fb6763..b34e950fbb 100644 --- a/dev/integration_tests/windows_startup_test/test_driver/main_test.dart +++ b/dev/integration_tests/windows_startup_test/test_driver/main_test.dart @@ -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); } diff --git a/dev/integration_tests/windows_startup_test/windows/runner/flutter_window.cpp b/dev/integration_tests/windows_startup_test/windows/runner/flutter_window.cpp index a370a277a8..36023a54e3 100644 --- a/dev/integration_tests/windows_startup_test/windows/runner/flutter_window.cpp +++ b/dev/integration_tests/windows_startup_test/windows/runner/flutter_window.cpp @@ -12,6 +12,7 @@ #include #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 code_points = std::get>(*argument); + std::vector 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(); }