Skip to content

Commit

Permalink
fmt & fix ios
Browse files Browse the repository at this point in the history
  • Loading branch information
sagudev committed May 12, 2022
1 parent 6e3953f commit 79a8b9f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 57 deletions.
52 changes: 36 additions & 16 deletions frb_example/pure_dart/dart/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ void main(List<String> args) async {

print('dart call primitiveTypes');
{
expect(await api.primitiveTypes(myI32: 123, myI64: 10000000000000, myF64: 12345678901234567890.123, myBool: true),
expect(
await api.primitiveTypes(
myI32: 123,
myI64: 10000000000000,
myF64: 12345678901234567890.123,
myBool: true),
42);
}

Expand All @@ -40,13 +45,15 @@ void main(List<String> args) async {

print('dart call handleString');
{
expect(await api.handleString(s: "Hello, world!"), "Hello, world!Hello, world!");
expect(await api.handleString(s: "Hello, world!"),
"Hello, world!Hello, world!");
}

print('dart call handleVecU8');
{
final len = 100000;
expect(await api.handleVecU8(v: Uint8List.fromList(List.filled(len, 127))),
expect(
await api.handleVecU8(v: Uint8List.fromList(List.filled(len, 127))),
Uint8List.fromList(List.filled(len * 2, 127)));
}

Expand Down Expand Up @@ -84,8 +91,9 @@ void main(List<String> args) async {

print('dart call handleStruct');
{
final structResp =
await api.handleStruct(arg: MySize(width: 42, height: 100), boxed: MySize(width: 1000, height: 10000));
final structResp = await api.handleStruct(
arg: MySize(width: 42, height: 100),
boxed: MySize(width: 1000, height: 10000));
expect(structResp.width, 42 + 1000);
expect(structResp.height, 100 + 10000);
}
Expand All @@ -98,8 +106,10 @@ void main(List<String> args) async {

print('dart call handleListOfStruct');
{
final listOfStructResp =
await api.handleListOfStruct(l: [MySize(width: 42, height: 100), MySize(width: 420, height: 1000)]);
final listOfStructResp = await api.handleListOfStruct(l: [
MySize(width: 42, height: 100),
MySize(width: 420, height: 1000)
]);
expect(listOfStructResp.length, 4);
expect(listOfStructResp[0].width, 42);
expect(listOfStructResp[1].width, 420);
Expand All @@ -116,12 +126,16 @@ void main(List<String> args) async {
print('dart call handleComplexStruct');
{
final arrLen = 5;
final complexStructResp = await api.handleComplexStruct(s: _createMyTreeNode(arrLen: arrLen));
final complexStructResp =
await api.handleComplexStruct(s: _createMyTreeNode(arrLen: arrLen));
expect(complexStructResp.valueI32, 100);
expect(complexStructResp.valueVecU8, List.filled(arrLen, 100));
expect(complexStructResp.children[0].valueVecU8, List.filled(arrLen, 110));
expect(complexStructResp.children[0].children[0].valueVecU8, List.filled(arrLen, 111));
expect(complexStructResp.children[1].valueVecU8, List.filled(arrLen, 120));
expect(
complexStructResp.children[0].valueVecU8, List.filled(arrLen, 110));
expect(complexStructResp.children[0].children[0].valueVecU8,
List.filled(arrLen, 111));
expect(
complexStructResp.children[1].valueVecU8, List.filled(arrLen, 120));
}

print('dart call handle_sync_return');
Expand Down Expand Up @@ -187,7 +201,8 @@ void main(List<String> args) async {
{
final message = 'Hello there.';
final ret = await api.handleOptionalStruct(document: message);
if (ret == null) fail('handleOptionalStruct returned null for non-null document');
if (ret == null)
fail('handleOptionalStruct returned null for non-null document');
expect(ret.tag, 'div');
expect(ret.text, null);
expect(ret.attributes?[0].key, 'id');
Expand All @@ -204,7 +219,8 @@ void main(List<String> args) async {
{
expect(await api.handleOptionalIncrement(), null);

var ret = await api.handleOptionalIncrement(opt: ExoticOptionals(attributesNullable: []));
var ret = await api.handleOptionalIncrement(
opt: ExoticOptionals(attributesNullable: []));
if (ret == null) fail('increment returned null for non-null params');
final loopFor = 20;
for (var i = 1; i < loopFor; i++) {
Expand Down Expand Up @@ -246,7 +262,8 @@ void main(List<String> args) async {
{
final optional10 = await api.handleOptionBoxArguments(
boolbox: true,
structbox: await api.handleOptionalIncrement(opt: ExoticOptionals(attributesNullable: [])),
structbox: await api.handleOptionalIncrement(
opt: ExoticOptionals(attributesNullable: [])),
);
print(optional10);
}
Expand All @@ -260,7 +277,8 @@ void main(List<String> args) async {

print('dart call handleEnumParameter');
{
expect(await api.handleEnumParameter(weekday: Weekdays.Saturday), Weekdays.Saturday);
expect(await api.handleEnumParameter(weekday: Weekdays.Saturday),
Weekdays.Saturday);
}

print('dart call handleEnumStruct');
Expand Down Expand Up @@ -330,7 +348,9 @@ void main(List<String> args) async {
name: "from dart",
version: "XX",
mode: ApplicationMode.Embedded,
env: ApplicationEnv(vars: [ApplicationEnvVar(field0: "sendback", field1: true)]))),
env: ApplicationEnv(vars: [
ApplicationEnvVar(field0: "sendback", field1: true)
]))),
true);
}

Expand Down
83 changes: 59 additions & 24 deletions frb_example/with_flutter/integration_test/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'dart:io';
import 'dart:isolate';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/material.dart' hide Size;
import 'package:flutter_rust_bridge_example/bridge_generated.dart';
import 'package:flutter_rust_bridge_example/main.dart' as app;
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -18,14 +18,16 @@ void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('end-to-end test', () {
testWidgets('run and wait to see if there is memory problem', (WidgetTester tester) async {
testWidgets('run and wait to see if there is memory problem',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();

// run many times to see memory leaks or other problems
for (var i = 0; i < 20; ++i) {
await tester.pumpAndSettle();
expect(find.textContaining('Hi this string is from Rust'), findsOneWidget);
expect(
find.textContaining('Hi this string is from Rust'), findsOneWidget);

Future.delayed(const Duration(milliseconds: 50));

Expand All @@ -35,7 +37,8 @@ void main() {
}
});

testWidgets('test Rust deliberately have error', (WidgetTester tester) async {
testWidgets('test Rust deliberately have error',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();

Expand Down Expand Up @@ -64,49 +67,80 @@ void main() {
}
});

testWidgets('repeat call to offTopicMemoryTestInputComplexStruct', (WidgetTester tester) async {
testWidgets('repeat call to offTopicMemoryTestInputComplexStruct',
(WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(
tester,
() async => expect(
await app.api.offTopicMemoryTestInputComplexStruct(
input: TreeNode(
name: 'root', children: [for (var i = 0; i < 2000; ++i) TreeNode(name: 'child', children: [])])),
input: TreeNode(name: 'root', children: [
for (var i = 0; i < 2000; ++i)
TreeNode(name: 'child', children: [])
])),
2000));
});
testWidgets('repeat call to offTopicMemoryTestOutputComplexStruct', (WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(tester,
() async => expect((await app.api.offTopicMemoryTestOutputComplexStruct(len: 2000)).children.length, 2000));
testWidgets('repeat call to offTopicMemoryTestOutputComplexStruct',
(WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(
tester,
() async => expect(
(await app.api.offTopicMemoryTestOutputComplexStruct(len: 2000))
.children
.length,
2000));
});

testWidgets('repeat call to offTopicMemoryTestInputVecOfObject', (WidgetTester tester) async {
testWidgets('repeat call to offTopicMemoryTestInputVecOfObject',
(WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(
tester,
() async => expect(
await app.api
.offTopicMemoryTestInputVecOfObject(input: List.filled(100000, Size(width: 42, height: 100))),
await app.api.offTopicMemoryTestInputVecOfObject(
input: List.filled(100000, Size(width: 42, height: 100))),
100000));
});
testWidgets('repeat call to offTopicMemoryTestOutputVecOfObject', (WidgetTester tester) async {
testWidgets('repeat call to offTopicMemoryTestOutputVecOfObject',
(WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(
tester, () async => expect((await app.api.offTopicMemoryTestOutputVecOfObject(len: 100000)).length, 100000));
tester,
() async => expect(
(await app.api.offTopicMemoryTestOutputVecOfObject(len: 100000))
.length,
100000));
});

testWidgets('repeat call to offTopicMemoryTestInputArray', (WidgetTester tester) async {
testWidgets('repeat call to offTopicMemoryTestInputArray',
(WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(
tester, () async => expect(await app.api.offTopicMemoryTestInputArray(input: Uint8List(1000000)), 1000000));
tester,
() async => expect(
await app.api
.offTopicMemoryTestInputArray(input: Uint8List(1000000)),
1000000));
});
testWidgets('repeat call to offTopicMemoryTestOutputZeroCopyBuffer', (WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(tester,
() async => expect((await app.api.offTopicMemoryTestOutputZeroCopyBuffer(len: 1000000)).length, 1000000));
testWidgets('repeat call to offTopicMemoryTestOutputZeroCopyBuffer',
(WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(
tester,
() async => expect(
(await app.api
.offTopicMemoryTestOutputZeroCopyBuffer(len: 1000000))
.length,
1000000));
});
testWidgets('repeat call to offTopicMemoryTestOutputVecU8', (WidgetTester tester) async {
testWidgets('repeat call to offTopicMemoryTestOutputVecU8',
(WidgetTester tester) async {
await _testMemoryProblemForSingleTypeOfMethod(
tester, () async => expect((await app.api.offTopicMemoryTestOutputVecU8(len: 200000)).length, 200000));
tester,
() async => expect(
(await app.api.offTopicMemoryTestOutputVecU8(len: 200000)).length,
200000));
});
});
}

Future<void> _testMemoryProblemForSingleTypeOfMethod(WidgetTester tester, Future<void> Function() callFfi) async {
Future<void> _testMemoryProblemForSingleTypeOfMethod(
WidgetTester tester, Future<void> Function() callFfi) async {
print('testMemoryProblemForSingleTypeOfMethod start');

app.main();
Expand Down Expand Up @@ -163,7 +197,8 @@ Future<void> _maybeGC([String hint = '']) async {
final vmService = await vmServiceConnectUri(_toWebSocket(serverUri));

// notice this variable is also large and can consume megabytes of memory...
final profileAfterMaybeGc = await vmService.getAllocationProfile(isolateId, reset: true, gc: true);
final profileAfterMaybeGc =
await vmService.getAllocationProfile(isolateId, reset: true, gc: true);
print('Memory usage after maybe GC $hint: ${profileAfterMaybeGc.memoryUsage} '
'dateLastServiceGC=${profileAfterMaybeGc.dateLastServiceGC} now=${DateTime.now().millisecondsSinceEpoch}');
}
Expand Down
8 changes: 6 additions & 2 deletions frb_example/with_flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ class _MyAppState extends State<MyApp> {

Future<void> _callExampleFfiOne() async {
final receivedImage = await api.drawMandelbrot(
imageSize: Size(width: 50, height: 50), zoomPoint: examplePoint, scale: generateScale(), numThreads: 4);
imageSize: Size(width: 50, height: 50),
zoomPoint: examplePoint,
scale: generateScale(),
numThreads: 4);
if (mounted) setState(() => exampleImage = receivedImage);
}

Future<void> _callExampleFfiTwo() async {
final receivedText = await api.passingComplexStructs(root: createExampleTree());
final receivedText =
await api.passingComplexStructs(root: createExampleTree());
if (mounted) setState(() => exampleText = receivedText);
}
}
Loading

0 comments on commit 79a8b9f

Please sign in to comment.