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