@@ -89,6 +89,201 @@ - (void)testContentInsetsSumAlwaysZeroAfterSetFrame {
8989 }
9090}
9191
92+ - (void )testLoadFileSucceeds {
93+ NSString *testFilePath = @" /assets/file.html" ;
94+ NSURL *url = [NSURL fileURLWithPath: testFilePath isDirectory: NO ];
95+ XCTestExpectation *resultExpectation =
96+ [self expectationWithDescription: @" Should return successful result over the method channel." ];
97+ FLTWebViewController *controller =
98+ [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
99+ viewIdentifier: 1
100+ arguments: nil
101+ binaryMessenger: self .mockBinaryMessenger];
102+ FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
103+ controller.webView = mockWebView;
104+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile"
105+ arguments: testFilePath]
106+ result: ^(id _Nullable result) {
107+ XCTAssertNil (result);
108+ [resultExpectation fulfill ];
109+ }];
110+
111+ [self waitForExpectations: @[ resultExpectation ] timeout: 30.0 ];
112+ OCMVerify ([mockWebView loadFileURL: url
113+ allowingReadAccessToURL: [url URLByDeletingLastPathComponent ]]);
114+ }
115+
116+ - (void )testLoadFileFailsWithInvalidPath {
117+ NSArray *resultExpectations = @[
118+ [self expectationWithDescription: @" Should return failed result when argument is nil." ],
119+ [self expectationWithDescription:
120+ @" Should return failed result when argument is not of type NSString*." ],
121+ [self expectationWithDescription:
122+ @" Should return failed result when argument is an empty string." ],
123+ ];
124+
125+ FLTWebViewController *controller =
126+ [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
127+ viewIdentifier: 1
128+ arguments: nil
129+ binaryMessenger: self .mockBinaryMessenger];
130+ FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
131+ controller.webView = mockWebView;
132+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: nil ]
133+ result: ^(id _Nullable result) {
134+ FlutterError *expected =
135+ [FlutterError errorWithCode: @" loadFile_failed"
136+ message: @" Failed parsing file path."
137+ details: @" Argument is nil." ];
138+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
139+ [resultExpectations[0 ] fulfill ];
140+ }];
141+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: @(10 )]
142+ result: ^(id _Nullable result) {
143+ FlutterError *expected =
144+ [FlutterError errorWithCode: @" loadFile_failed"
145+ message: @" Failed parsing file path."
146+ details: @" Argument is not of type NSString." ];
147+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
148+ [resultExpectations[1 ] fulfill ];
149+ }];
150+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: @" " ]
151+ result: ^(id _Nullable result) {
152+ FlutterError *expected =
153+ [FlutterError errorWithCode: @" loadFile_failed"
154+ message: @" Failed parsing file path."
155+ details: @" Argument contains an empty string." ];
156+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
157+ [resultExpectations[2 ] fulfill ];
158+ }];
159+
160+ [self waitForExpectations: resultExpectations timeout: 1.0 ];
161+ OCMReject ([mockWebView loadFileURL: [OCMArg any ] allowingReadAccessToURL: [OCMArg any ]]);
162+ }
163+
164+ - (void )testLoadFileSucceedsWithBaseUrl {
165+ NSURL *baseUrl = [NSURL URLWithString: @" https://flutter.dev" ];
166+ XCTestExpectation *resultExpectation =
167+ [self expectationWithDescription: @" Should return successful result over the method channel." ];
168+ FLTWebViewController *controller =
169+ [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
170+ viewIdentifier: 1
171+ arguments: nil
172+ binaryMessenger: self .mockBinaryMessenger];
173+ FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
174+ controller.webView = mockWebView;
175+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
176+ arguments: @{
177+ @" html" : @" some HTML string" ,
178+ @" baseUrl" : @" https://flutter.dev"
179+ }]
180+ result: ^(id _Nullable result) {
181+ XCTAssertNil (result);
182+ [resultExpectation fulfill ];
183+ }];
184+
185+ [self waitForExpectations: @[ resultExpectation ] timeout: 30.0 ];
186+ OCMVerify ([mockWebView loadHTMLString: @" some HTML string" baseURL: baseUrl]);
187+ }
188+
189+ - (void )testLoadFileSucceedsWithoutBaseUrl {
190+ XCTestExpectation *resultExpectation =
191+ [self expectationWithDescription: @" Should return successful result over the method channel." ];
192+ FLTWebViewController *controller =
193+ [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
194+ viewIdentifier: 1
195+ arguments: nil
196+ binaryMessenger: self .mockBinaryMessenger];
197+ FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
198+ controller.webView = mockWebView;
199+ [controller
200+ onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
201+ arguments: @{@" html" : @" some HTML string" }]
202+ result: ^(id _Nullable result) {
203+ XCTAssertNil (result);
204+ [resultExpectation fulfill ];
205+ }];
206+
207+ [self waitForExpectations: @[ resultExpectation ] timeout: 30.0 ];
208+ OCMVerify ([mockWebView loadHTMLString: @" some HTML string" baseURL: nil ]);
209+ }
210+
211+ - (void )testLoadHtmlStringFailsWithInvalidArgument {
212+ NSArray *resultExpectations = @[
213+ [self expectationWithDescription: @" Should return failed result when argument is nil." ],
214+ [self expectationWithDescription:
215+ @" Should return failed result when argument is not of type NSDictionary*." ],
216+ [self expectationWithDescription: @" Should return failed result when HTML argument is nil." ],
217+ [self expectationWithDescription:
218+ @" Should return failed result when HTML argument is not of type NSString*." ],
219+ [self expectationWithDescription:
220+ @" Should return failed result when HTML argument is an empty string." ],
221+ ];
222+
223+ FLTWebViewController *controller =
224+ [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
225+ viewIdentifier: 1
226+ arguments: nil
227+ binaryMessenger: self .mockBinaryMessenger];
228+ FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
229+ controller.webView = mockWebView;
230+ FlutterError *expected = [FlutterError
231+ errorWithCode: @" loadHtmlString_failed"
232+ message: @" Failed parsing arguments."
233+ details: @" Arguments should be a dictionary containing at least a 'html' element and "
234+ @" optionally a 'baseUrl' argument. For example: `@{ @\" html\" : @\" some html "
235+ @" code\" , @\" baseUrl\" : @\" https://flutter.dev\" }`" ];
236+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
237+ arguments: nil ]
238+ result: ^(id _Nullable result) {
239+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
240+ [resultExpectations[0 ] fulfill ];
241+ }];
242+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
243+ arguments: @" " ]
244+ result: ^(id _Nullable result) {
245+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
246+ [resultExpectations[1 ] fulfill ];
247+ }];
248+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
249+ arguments: @{}]
250+ result: ^(id _Nullable result) {
251+ FlutterError *expected =
252+ [FlutterError errorWithCode: @" loadHtmlString_failed"
253+ message: @" Failed parsing HTML string argument."
254+ details: @" Argument is nil." ];
255+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
256+ [resultExpectations[2 ] fulfill ];
257+ }];
258+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
259+ arguments: @{
260+ @" html" : @(42 ),
261+ }]
262+ result: ^(id _Nullable result) {
263+ FlutterError *expected =
264+ [FlutterError errorWithCode: @" loadHtmlString_failed"
265+ message: @" Failed parsing HTML string argument."
266+ details: @" Argument is not of type NSString." ];
267+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
268+ [resultExpectations[3 ] fulfill ];
269+ }];
270+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
271+ arguments: @{
272+ @" html" : @" " ,
273+ }]
274+ result: ^(id _Nullable result) {
275+ FlutterError *expected =
276+ [FlutterError errorWithCode: @" loadHtmlString_failed"
277+ message: @" Failed parsing HTML string argument."
278+ details: @" Argument contains an empty string." ];
279+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
280+ [resultExpectations[4 ] fulfill ];
281+ }];
282+
283+ [self waitForExpectations: resultExpectations timeout: 1.0 ];
284+ OCMReject ([mockWebView loadHTMLString: [OCMArg any ] baseURL: [OCMArg any ]]);
285+ }
286+
92287- (void )testRunJavascriptFailsForNullString {
93288 // Setup
94289 FLTWebViewController *controller =
@@ -302,6 +497,14 @@ - (void)testRunJavascriptReturningResultReturnsErrorResultForWKError {
302497 [self waitForExpectationsWithTimeout: 30.0 handler: nil ];
303498}
304499
500+ + (void )assertFlutterError : (id )actual withExpected : (FlutterError *)expected {
501+ XCTAssertTrue ([actual class ] == [FlutterError class ]);
502+ FlutterError *errorResult = actual;
503+ XCTAssertEqualObjects (errorResult.code , expected.code );
504+ XCTAssertEqualObjects (errorResult.message , expected.message );
505+ XCTAssertEqualObjects (errorResult.details , expected.details );
506+ }
507+
305508- (void )testBuildNSURLRequestReturnsNilForNonDictionaryValue {
306509 // Setup
307510 FLTWebViewController *controller =
0 commit comments