Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Prev Previous commit
Next Next commit
return legacy value
  • Loading branch information
bparrishMines committed Jun 23, 2022
commit 42231a433e14107f1affb070b61e4e51b40777b0
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'dart:io';
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -1169,6 +1170,9 @@ Future<void> main() async {
// JavaScript booleans evaluate to different string values on Android and iOS.
// This utility method returns the string boolean value of the current platform.
String _webviewBool(bool value) {
if (defaultTargetPlatform == TargetPlatform.iOS) {
return value ? '1' : '0';
}
return value ? 'true' : 'false';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,6 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {
@override
Future<String> evaluateJavascript(String javascript) async {
final Object? result = await webView.evaluateJavaScript(javascript);
// The legacy implementation of webview_flutter_wkwebview would convert
// objects to strings before returning them to Dart. This method attempts
// to converts Dart objects to Strings the way it is done in Objective-C
// to avoid breaking users expecting the same String format.
return _asObjectiveCString(result);
}

Expand Down Expand Up @@ -347,7 +343,7 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {
'Use `runJavascript` when expecting a null return value.',
);
}
return result.toString();
return _asObjectiveCString(result);
}

@override
Expand Down Expand Up @@ -570,6 +566,10 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {
);
}

// The legacy implementation of webview_flutter_wkwebview would convert
// objects to strings before returning them to Dart. This method attempts
// to converts Dart objects to Strings the way it is done in Objective-C
// to avoid breaking users expecting the same String format.
String _asObjectiveCString(Object? value, {bool inContainer = false}) {
if (value == null) {
// An NSNull inside an NSArray or NSDictionary is represented as a String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,23 @@ void main() {
);
});

testWidgets('runJavascriptReturningResult with bool return value',
(WidgetTester tester) async {
await buildWidget(tester);

when(mockWebView.evaluateJavaScript('runJavaScript')).thenAnswer(
(_) => Future<Object?>.value(false),
);
// The legacy implementation of webview_flutter_wkwebview would convert
// objects to strings before returning them to Dart. This verifies bool
// is represented the way it is in Objective-C.
// `NSNumber.description` converts bool values to a 1 or 0.
expect(
testController.runJavascriptReturningResult('runJavaScript'),
completion('0'),
);
});

testWidgets('runJavascript', (WidgetTester tester) async {
await buildWidget(tester);

Expand Down