Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e4b2dd1

Browse files
committed
Implements loadFlutterAsset method from platform interface.
1 parent 3e3d799 commit e4b2dd1

3 files changed

Lines changed: 148 additions & 2 deletions

File tree

packages/webview_flutter/webview_flutter_wkwebview/example/assets/www/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<!DOCTYPE html>
2+
<!-- Copyright 2013 The Flutter Authors. All rights reserved.
3+
Use of this source code is governed by a BSD-style license that can be
4+
found in the LICENSE file. -->
25
<html lang="en">
36
<head>
47
<title>Load file or HTML string example</title>

packages/webview_flutter/webview_flutter_wkwebview/example/ios/RunnerTests/FLTWebViewTests.m

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,120 @@ - (void)testLoadFileFailsWithInvalidPath {
161161
OCMReject([mockWebView loadFileURL:[OCMArg any] allowingReadAccessToURL:[OCMArg any]]);
162162
}
163163

164-
- (void)testLoadFileSucceedsWithBaseUrl {
164+
- (void)testLoadFlutterAssetSucceeds {
165+
NSBundle *mockBundle = OCMPartialMock([NSBundle mainBundle]);
166+
NSString *filePath = [FlutterDartProject lookupKeyForAsset:@"assets/file.html"];
167+
NSURL *url = [NSURL URLWithString:[@"file:///" stringByAppendingString:filePath]];
168+
[OCMStub([mockBundle URLForResource:[filePath stringByDeletingPathExtension]
169+
withExtension:@"html"]) andReturn:(url)];
170+
171+
XCTestExpectation *resultExpectation =
172+
[self expectationWithDescription:@"Should return successful result over the method channel."];
173+
FLTWebViewController *controller =
174+
[[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400)
175+
viewIdentifier:1
176+
arguments:nil
177+
binaryMessenger:self.mockBinaryMessenger];
178+
FLTWKWebView *mockWebView = OCMClassMock(FLTWKWebView.class);
179+
controller.webView = mockWebView;
180+
[controller onMethodCall:[FlutterMethodCall methodCallWithMethodName:@"loadFlutterAsset"
181+
arguments:@"assets/file.html"]
182+
result:^(id _Nullable result) {
183+
XCTAssertNil(result);
184+
[resultExpectation fulfill];
185+
}];
186+
187+
[self waitForExpectations:@[ resultExpectation ] timeout:1.0];
188+
OCMVerify([mockWebView loadFileURL:url
189+
allowingReadAccessToURL:[url URLByDeletingLastPathComponent]]);
190+
}
191+
192+
- (void)testLoadFlutterAssetFailsWithInvalidKey {
193+
NSArray *resultExpectations = @[
194+
[self expectationWithDescription:@"Should return failed result when argument is nil."],
195+
[self expectationWithDescription:
196+
@"Should return failed result when argument is not of type NSString*."],
197+
[self expectationWithDescription:
198+
@"Should return failed result when argument is an empty string."],
199+
];
200+
201+
FLTWebViewController *controller =
202+
[[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400)
203+
viewIdentifier:1
204+
arguments:nil
205+
binaryMessenger:self.mockBinaryMessenger];
206+
FLTWKWebView *mockWebView = OCMClassMock(FLTWKWebView.class);
207+
controller.webView = mockWebView;
208+
[controller onMethodCall:[FlutterMethodCall methodCallWithMethodName:@"loadFlutterAsset"
209+
arguments:nil]
210+
result:^(id _Nullable result) {
211+
FlutterError *expected =
212+
[FlutterError errorWithCode:@"loadFlutterAsset_invalidKey"
213+
message:@"Supplied asset key is not valid."
214+
details:@"Argument is nil."];
215+
[FLTWebViewTests assertFlutterError:result withExpected:expected];
216+
[resultExpectations[0] fulfill];
217+
}];
218+
[controller onMethodCall:[FlutterMethodCall methodCallWithMethodName:@"loadFlutterAsset"
219+
arguments:@(10)]
220+
result:^(id _Nullable result) {
221+
FlutterError *expected =
222+
[FlutterError errorWithCode:@"loadFlutterAsset_invalidKey"
223+
message:@"Supplied asset key is not valid."
224+
details:@"Argument is not of type NSString."];
225+
[FLTWebViewTests assertFlutterError:result withExpected:expected];
226+
[resultExpectations[1] fulfill];
227+
}];
228+
[controller onMethodCall:[FlutterMethodCall methodCallWithMethodName:@"loadFlutterAsset"
229+
arguments:@""]
230+
result:^(id _Nullable result) {
231+
FlutterError *expected =
232+
[FlutterError errorWithCode:@"loadFlutterAsset_invalidKey"
233+
message:@"Supplied asset key is not valid."
234+
details:@"Argument contains an empty string."];
235+
[FLTWebViewTests assertFlutterError:result withExpected:expected];
236+
[resultExpectations[2] fulfill];
237+
}];
238+
239+
[self waitForExpectations:resultExpectations timeout:1.0];
240+
OCMReject([mockWebView loadFileURL:[OCMArg any] allowingReadAccessToURL:[OCMArg any]]);
241+
}
242+
243+
- (void)testLoadFlutterAssetFailsWithParsingError {
244+
NSBundle *mockBundle = OCMPartialMock([NSBundle mainBundle]);
245+
NSString *filePath = [FlutterDartProject lookupKeyForAsset:@"assets/file.html"];
246+
[OCMStub([mockBundle URLForResource:[filePath stringByDeletingPathExtension]
247+
withExtension:@"html"]) andReturn:(nil)];
248+
249+
XCTestExpectation *resultExpectation =
250+
[self expectationWithDescription:@"Should return failed result over the method channel."];
251+
FLTWebViewController *controller =
252+
[[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400)
253+
viewIdentifier:1
254+
arguments:nil
255+
binaryMessenger:self.mockBinaryMessenger];
256+
FLTWKWebView *mockWebView = OCMClassMock(FLTWKWebView.class);
257+
controller.webView = mockWebView;
258+
[controller
259+
onMethodCall:[FlutterMethodCall methodCallWithMethodName:@"loadFlutterAsset"
260+
arguments:@"assets/file.html"]
261+
result:^(id _Nullable result) {
262+
FlutterError *expected = [FlutterError
263+
errorWithCode:@"loadFlutterAsset_invalidKey"
264+
message:@"Failed parsing file path for supplied key."
265+
details:[NSString
266+
stringWithFormat:
267+
@"Failed to convert path '%@' into NSURL for key '%@'.",
268+
filePath, @"assets/file.html"]];
269+
[FLTWebViewTests assertFlutterError:result withExpected:expected];
270+
[resultExpectation fulfill];
271+
}];
272+
273+
[self waitForExpectations:@[ resultExpectation ] timeout:1.0];
274+
OCMReject([mockWebView loadFileURL:[OCMArg any] allowingReadAccessToURL:[OCMArg any]]);
275+
}
276+
277+
- (void)testLoadHtmlStringSucceedsWithBaseUrl {
165278
NSURL *baseUrl = [NSURL URLWithString:@"https://flutter.dev"];
166279
XCTestExpectation *resultExpectation =
167280
[self expectationWithDescription:@"Should return successful result over the method channel."];
@@ -186,7 +299,7 @@ - (void)testLoadFileSucceedsWithBaseUrl {
186299
OCMVerify([mockWebView loadHTMLString:@"some HTML string" baseURL:baseUrl]);
187300
}
188301

189-
- (void)testLoadFileSucceedsWithoutBaseUrl {
302+
- (void)testLoadHtmlStringSucceedsWithoutBaseUrl {
190303
XCTestExpectation *resultExpectation =
191304
[self expectationWithDescription:@"Should return successful result over the method channel."];
192305
FLTWebViewController *controller =

packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FlutterWebView.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ - (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
156156
[self onUpdateSettings:call result:result];
157157
} else if ([[call method] isEqualToString:@"loadFile"]) {
158158
[self onLoadFile:call result:result];
159+
} else if ([[call method] isEqualToString:@"loadFlutterAsset"]) {
160+
[self onLoadFlutterAsset:call result:result];
159161
} else if ([[call method] isEqualToString:@"loadHtmlString"]) {
160162
[self onLoadHtmlString:call result:result];
161163
} else if ([[call method] isEqualToString:@"loadUrl"]) {
@@ -237,6 +239,34 @@ - (void)onLoadFile:(FlutterMethodCall*)call result:(FlutterResult)result {
237239
result(nil);
238240
}
239241

242+
- (void)onLoadFlutterAsset:(FlutterMethodCall*)call result:(FlutterResult)result {
243+
NSString* error = nil;
244+
if (![FLTWebViewController isValidStringArgument:[call arguments] withErrorMessage:&error]) {
245+
result([FlutterError errorWithCode:@"loadFlutterAsset_invalidKey"
246+
message:@"Supplied asset key is not valid."
247+
details:error]);
248+
return;
249+
}
250+
251+
NSString* assetKey = [call arguments];
252+
NSString* assetFilePath = [FlutterDartProject lookupKeyForAsset:assetKey];
253+
NSURL* url = [[NSBundle mainBundle] URLForResource:[assetFilePath stringByDeletingPathExtension]
254+
withExtension:assetFilePath.pathExtension];
255+
256+
if (!url) {
257+
result([FlutterError
258+
errorWithCode:@"loadFlutterAsset_invalidKey"
259+
message:@"Failed parsing file path for supplied key."
260+
details:[NSString
261+
stringWithFormat:@"Failed to convert path '%@' into NSURL for key '%@'.",
262+
assetFilePath, assetKey]]);
263+
return;
264+
}
265+
266+
[_webView loadFileURL:url allowingReadAccessToURL:[url URLByDeletingLastPathComponent]];
267+
result(nil);
268+
}
269+
240270
- (void)onLoadHtmlString:(FlutterMethodCall*)call result:(FlutterResult)result {
241271
NSDictionary* arguments = [call arguments];
242272
if (![arguments isKindOfClass:NSDictionary.class]) {

0 commit comments

Comments
 (0)