Skip to content
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
Mock IFrame
  • Loading branch information
ThomasAunvik authored and ditman committed Jul 9, 2024
commit d03c9fc16eebc88da3210e176aa0977cc820ffc2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Migrates to `package:web`
* Updates `HttpRequestFactory.request` to use `package:http` `BrowserClient`
* Updates `ìndex.html` in the example to use `flutter_bootstrap.js`
* Updates minimum dart sdk to `^3.3.0`
* Updates minimum supported SDK version to Flutter 3.16/Dart 3.2.

## 0.2.2+4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<html>

<head>
<!--
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.

Expand All @@ -18,27 +18,27 @@
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">
<base href="$FLUTTER_BASE_HREF">

<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="webview_flutter_web Example">
<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="webview_flutter_web Example">

<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="example">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="example">
<link rel="apple-touch-icon" href="icons/Icon-192.png">

<title>webview_flutter_web Example</title>
<link rel="manifest" href="manifest.json">
<title>webview_flutter_web Example</title>
<link rel="manifest" href="manifest.json">
</head>

<body>
<!-- This script installs service_worker.js to provide PWA functionality to
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script src="flutter_bootstrap.js" async></script>
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script src="flutter_bootstrap.js" async></script>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'dart:js_interop';
import 'package:web/web.dart' as html;

@JSExport()
class FakeIFrameElement {
@JSExport('src')
String? src;
}

extension type MockHTMLIFrameElement(JSObject _)
implements html.HTMLIFrameElement, JSObject {
external String? src;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:js_interop';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:web/web.dart' as html;
import 'package:webview_flutter_platform_interface/src/webview_flutter_platform_interface_legacy.dart';
import 'package:webview_flutter_web/src/http_request_factory.dart';
import 'package:webview_flutter_web/src/webview_flutter_web_legacy.dart';

import 'mock_fake_iframe_element.dart';
import 'webview_flutter_web_test.mocks.dart';

@GenerateMocks(<Type>[
Expand Down Expand Up @@ -45,7 +46,11 @@ void main() {
group('WebWebViewPlatformController', () {
test('loadUrl sets url on iframe src attribute', () {
// Setup
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final FakeIFrameElement fakeElem = FakeIFrameElement();
final MockHTMLIFrameElement mockElement =
createJSInteropWrapper<FakeIFrameElement>(fakeElem)
as MockHTMLIFrameElement;

final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand All @@ -59,7 +64,11 @@ void main() {
group('loadHtmlString', () {
test('loadHtmlString loads html into iframe', () {
// Setup
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final FakeIFrameElement fakeElem = FakeIFrameElement();
final MockHTMLIFrameElement mockElement =
createJSInteropWrapper<FakeIFrameElement>(fakeElem)
as MockHTMLIFrameElement;

final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand All @@ -73,22 +82,30 @@ void main() {

test('loadHtmlString escapes "#" correctly', () {
// Setup
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final FakeIFrameElement fakeElem = FakeIFrameElement();
final MockHTMLIFrameElement mockElement =
createJSInteropWrapper<FakeIFrameElement>(fakeElem)
as MockHTMLIFrameElement;

final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
);
// Run
controller.loadHtmlString('#');
// Verify
verify(mockElement.src = argThat(contains('%23')) ?? '');
verify(mockElement.src = argThat(contains('%23')));
});
});

group('loadRequest', () {
test('loadRequest throws ArgumentError on missing scheme', () {
// Setup
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final FakeIFrameElement fakeElem = FakeIFrameElement();
final MockHTMLIFrameElement mockElement =
createJSInteropWrapper<FakeIFrameElement>(fakeElem)
as MockHTMLIFrameElement;

final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand All @@ -107,7 +124,11 @@ void main() {
test('loadRequest makes request and loads response into iframe',
() async {
// Setup
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final FakeIFrameElement fakeElem = FakeIFrameElement();
final MockHTMLIFrameElement mockElement =
createJSInteropWrapper<FakeIFrameElement>(fakeElem)
as MockHTMLIFrameElement;

final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand Down Expand Up @@ -147,7 +168,11 @@ void main() {

test('loadRequest escapes "#" correctly', () async {
// Setup
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final FakeIFrameElement fakeElem = FakeIFrameElement();
final MockHTMLIFrameElement mockElement =
createJSInteropWrapper<FakeIFrameElement>(fakeElem)
as MockHTMLIFrameElement;

final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand Down Expand Up @@ -175,7 +200,7 @@ void main() {
headers: <String, String>{'Foo': 'Bar'}),
);
// Verify
verify(mockElement.src = argThat(contains('%23')) ?? '');
verify(mockElement.src = argThat(contains('%23')));
});
});
});
Expand Down