Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move test to integration test
  • Loading branch information
bparrishMines committed Jun 30, 2022
commit 1610344015b87307c616d9152419a7076041bdcd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import 'package:webview_flutter_platform_interface/webview_flutter_platform_inte
import 'package:webview_flutter_wkwebview_example/navigation_decision.dart';
import 'package:webview_flutter_wkwebview_example/navigation_request.dart';
import 'package:webview_flutter_wkwebview_example/web_view.dart';
import 'package:webview_flutter_wkwebview/src/common/weak_reference_utils.dart';
import 'package:webview_flutter_wkwebview/src/common/instance_manager.dart';

Future<void> main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -66,6 +68,30 @@ Future<void> main() async {
expect(currentUrl, primaryUrl);
});

testWidgets(
'withWeakRefenceTo allows encapsulating class to be garbage collected',
(WidgetTester tester) async {
final Completer<int> gcCompleter = Completer<int>();
final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {
print(_);
gcCompleter.complete(_);
},
);

ClassWithCallbackClass? instance = ClassWithCallbackClass();
instanceManager.addHostCreatedInstance(instance.callbackClass, 0);
instance = null;

// Force garbage collection.
await IntegrationTestWidgetsFlutterBinding.instance
.watchPerformance(() async {
await tester.pumpAndSettle();
});

expect(gcCompleter.future, completion(0));
}, timeout: const Timeout(Duration(seconds: 10)));

testWidgets('loadUrl', (WidgetTester tester) async {
final Completer<WebViewController> controllerCompleter =
Completer<WebViewController>();
Expand Down Expand Up @@ -1253,3 +1279,33 @@ class ResizableWebViewState extends State<ResizableWebView> {
);
}
}

class CopyableObjectWithCallback with Copyable {
CopyableObjectWithCallback(this.callback);

final VoidCallback callback;

@override
CopyableObjectWithCallback copy() {
return CopyableObjectWithCallback(callback);
}
}

class ClassWithCallbackClass {
ClassWithCallbackClass() {
callbackClass = CopyableObjectWithCallback(
withWeakRefenceTo(
this,
(WeakReference<ClassWithCallbackClass> weakReference) {
return () {
// Weak reference to `this` in callback.
// ignore: unnecessary_statements
weakReference;
};
},
),
);
}

late final CopyableObjectWithCallback callbackClass;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ dev_dependencies:
mockito: ^5.1.0
pedantic: ^1.10.0
pigeon: ^3.0.3
vm_service: ^8.3.0
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:developer';
import 'dart:isolate';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:vm_service/vm_service.dart' as vm_service;
import 'package:vm_service/vm_service_io.dart';
import 'package:webview_flutter_wkwebview/src/common/instance_manager.dart';
import 'package:webview_flutter_wkwebview/src/common/weak_reference_utils.dart';

void main() {
group('InstanceManager', () {
Expand Down Expand Up @@ -140,28 +132,6 @@ void main() {
)!;
expect(identical(object, newWeakCopy), isFalse);
});

test('withWeakRefenceTo allows encapsulating class to be garbage collected',
() async {
final Completer<int> gcCompleter = Completer<int>();
final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: gcCompleter.complete,
);

ClassWithCallbackClass? instance = ClassWithCallbackClass();
instanceManager.addHostCreatedInstance(instance.callbackClass, 0);
instance = null;

// Force garbage collection.
final Uri serverUri = (await Service.getInfo()).serverUri!;

final String isolateId = Service.getIsolateID(Isolate.current)!;
final vm_service.VmService vmService =
await vmServiceConnectUri(_toWebSocket(serverUri));
await vmService.getAllocationProfile(isolateId, gc: true);

expect(gcCompleter.future, completion(0));
});
});
}

Expand All @@ -181,49 +151,3 @@ class CopyableObject with Copyable {
return other is CopyableObject;
}
}

class CopyableObjectWithCallback with Copyable {
CopyableObjectWithCallback(this.callback);

final VoidCallback callback;

@override
CopyableObjectWithCallback copy() {
return CopyableObjectWithCallback(callback);
}
}

class ClassWithCallbackClass {
ClassWithCallbackClass() {
callbackClass = CopyableObjectWithCallback(
withWeakRefenceTo(
this,
(WeakReference<ClassWithCallbackClass> weakReference) {
return () {
// Weak reference to `this` in callback.
// ignore: unnecessary_statements
weakReference;
};
},
),
);
}

late final CopyableObjectWithCallback callbackClass;
}

List<String> _cleanupPathSegments(Uri uri) {
final List<String> pathSegments = <String>[];
if (uri.pathSegments.isNotEmpty) {
pathSegments.addAll(uri.pathSegments.where(
(String s) => s.isNotEmpty,
));
}
return pathSegments;
}

String _toWebSocket(Uri uri) {
final List<String> pathSegments = _cleanupPathSegments(uri);
pathSegments.add('ws');
return uri.replace(scheme: 'ws', pathSegments: pathSegments).toString();
}