@@ -112,9 +112,15 @@ - (void)testLoadFileSucceeds {
112112 allowingReadAccessToURL: [url URLByDeletingLastPathComponent ]]);
113113}
114114
115- - (void )testLoadFileFailsWithNilPath {
116- XCTestExpectation *resultExpectation =
117- [self expectationWithDescription: @" Should return failed result over the method channel." ];
115+ - (void )testLoadFileFailsWithInvalidPath {
116+ NSArray *resultExpectations = @[
117+ [self expectationWithDescription: @" Should return failed result when argument is nil." ],
118+ [self expectationWithDescription:
119+ @" Should return failed result when argument is not of type NSString*." ],
120+ [self expectationWithDescription:
121+ @" Should return failed result when argument is an empty string." ],
122+ ];
123+
118124 FLTWebViewController *controller =
119125 [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
120126 viewIdentifier: 1
@@ -124,46 +130,64 @@ - (void)testLoadFileFailsWithNilPath {
124130 controller.webView = mockWebView;
125131 [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: nil ]
126132 result: ^(id _Nullable result) {
127- XCTAssertTrue ([result class ] == [FlutterError class ]);
128- FlutterError *errorResult = result;
129- XCTAssertEqualObjects (errorResult.code , @" loadFile_failed" );
130- XCTAssertEqualObjects (errorResult.message , @" Failed parsing file path." );
131- XCTAssertEqualObjects (errorResult.details , @" Argument is nil." );
132- [resultExpectation fulfill ];
133+ FlutterError *expected =
134+ [FlutterError errorWithCode: @" loadFile_failed"
135+ message: @" Failed parsing file path."
136+ details: @" Argument is nil." ];
137+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
138+ [resultExpectations[0 ] fulfill ];
139+ }];
140+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: @(10 )]
141+ result: ^(id _Nullable result) {
142+ FlutterError *expected =
143+ [FlutterError errorWithCode: @" loadFile_failed"
144+ message: @" Failed parsing file path."
145+ details: @" Argument is not of type NSString." ];
146+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
147+ [resultExpectations[1 ] fulfill ];
148+ }];
149+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: @" " ]
150+ result: ^(id _Nullable result) {
151+ FlutterError *expected =
152+ [FlutterError errorWithCode: @" loadFile_failed"
153+ message: @" Failed parsing file path."
154+ details: @" Argument contains an empty string." ];
155+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
156+ [resultExpectations[2 ] fulfill ];
133157 }];
134158
135- [self waitForExpectations: @[ resultExpectation ] timeout: 1.0 ];
159+ [self waitForExpectations: resultExpectations timeout: 1.0 ];
136160 OCMReject ([mockWebView loadFileURL: [OCMArg any ] allowingReadAccessToURL: [OCMArg any ]]);
137161}
138162
139- - (void )testLoadFileFailsWithNonStringPath {
163+ - (void )testLoadFileSucceedsWithBaseUrl {
164+ NSURL *baseUrl = [NSURL URLWithString: @" https://flutter.dev" ];
140165 XCTestExpectation *resultExpectation =
141- [self expectationWithDescription: @" Should return failed result over the method channel." ];
166+ [self expectationWithDescription: @" Should return successful result over the method channel." ];
142167 FLTWebViewController *controller =
143168 [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
144169 viewIdentifier: 1
145170 arguments: nil
146171 binaryMessenger: self .mockBinaryMessenger];
147172 FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
148173 controller.webView = mockWebView;
149- [controller
150- onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: @(10 )]
151- result: ^(id _Nullable result) {
152- XCTAssertTrue ([result class ] == [FlutterError class ]);
153- FlutterError *errorResult = result;
154- XCTAssertEqualObjects (errorResult.code , @" loadFile_failed" );
155- XCTAssertEqualObjects (errorResult.message , @" Failed parsing file path." );
156- XCTAssertEqualObjects (errorResult.details , @" Argument is not of type NSString." );
157- [resultExpectation fulfill ];
158- }];
174+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
175+ arguments: @{
176+ @" html" : @" some HTML string" ,
177+ @" baseUrl" : @" https://flutter.dev"
178+ }]
179+ result: ^(id _Nullable result) {
180+ XCTAssertNil (result);
181+ [resultExpectation fulfill ];
182+ }];
159183
160- [self waitForExpectations: @[ resultExpectation ] timeout: 1 .0 ];
161- OCMReject ([mockWebView loadFileURL: [OCMArg any ] allowingReadAccessToURL: [OCMArg any ] ]);
184+ [self waitForExpectations: @[ resultExpectation ] timeout: 30 .0 ];
185+ OCMVerify ([mockWebView loadHTMLString: @" some HTML string " baseURL: baseUrl ]);
162186}
163187
164- - (void )testLoadFileFailsWithEmptyPath {
188+ - (void )testLoadFileSucceedsWithoutBaseUrl {
165189 XCTestExpectation *resultExpectation =
166- [self expectationWithDescription: @" Should return failed result over the method channel." ];
190+ [self expectationWithDescription: @" Should return successful result over the method channel." ];
167191 FLTWebViewController *controller =
168192 [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
169193 viewIdentifier: 1
@@ -172,18 +196,91 @@ - (void)testLoadFileFailsWithEmptyPath {
172196 FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
173197 controller.webView = mockWebView;
174198 [controller
175- onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadFile" arguments: @" " ]
199+ onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
200+ arguments: @{@" html" : @" some HTML string" }]
176201 result: ^(id _Nullable result) {
177- XCTAssertTrue ([result class ] == [FlutterError class ]);
178- FlutterError *errorResult = result;
179- XCTAssertEqualObjects (errorResult.code , @" loadFile_failed" );
180- XCTAssertEqualObjects (errorResult.message , @" Failed parsing file path." );
181- XCTAssertEqualObjects (errorResult.details , @" Argument contains an empty string." );
202+ XCTAssertNil (result);
182203 [resultExpectation fulfill ];
183204 }];
184205
185- [self waitForExpectations: @[ resultExpectation ] timeout: 1.0 ];
186- OCMReject ([mockWebView loadFileURL: [OCMArg any ] allowingReadAccessToURL: [OCMArg any ]]);
206+ [self waitForExpectations: @[ resultExpectation ] timeout: 30.0 ];
207+ OCMVerify ([mockWebView loadHTMLString: @" some HTML string" baseURL: nil ]);
208+ }
209+
210+ - (void )testLoadHtmlStringFailsWithInvalidArgument {
211+ NSArray *resultExpectations = @[
212+ [self expectationWithDescription: @" Should return failed result when argument is nil." ],
213+ [self expectationWithDescription:
214+ @" Should return failed result when argument is not of type NSDictionary*." ],
215+ [self expectationWithDescription: @" Should return failed result when HTML argument is nil." ],
216+ [self expectationWithDescription:
217+ @" Should return failed result when HTML argument is not of type NSString*." ],
218+ [self expectationWithDescription:
219+ @" Should return failed result when HTML argument is an empty string." ],
220+ ];
221+
222+ FLTWebViewController *controller =
223+ [[FLTWebViewController alloc ] initWithFrame: CGRectMake (0 , 0 , 300 , 400 )
224+ viewIdentifier: 1
225+ arguments: nil
226+ binaryMessenger: self .mockBinaryMessenger];
227+ FLTWKWebView *mockWebView = OCMClassMock (FLTWKWebView.class );
228+ controller.webView = mockWebView;
229+ FlutterError *expected = [FlutterError
230+ errorWithCode: @" loadHtmlString_failed"
231+ message: @" Failed parsing arguments."
232+ details: @" Arguments should be a dictionary containing at least a 'html' element and "
233+ @" optionally a 'baseUrl' argument. For example: `@{ @\" html\" : @\" some html "
234+ @" code\" , @\" baseUrl\" : @\" https://flutter.dev\" }`" ];
235+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
236+ arguments: nil ]
237+ result: ^(id _Nullable result) {
238+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
239+ [resultExpectations[0 ] fulfill ];
240+ }];
241+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
242+ arguments: @" " ]
243+ result: ^(id _Nullable result) {
244+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
245+ [resultExpectations[1 ] fulfill ];
246+ }];
247+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
248+ arguments: @{}]
249+ result: ^(id _Nullable result) {
250+ FlutterError *expected =
251+ [FlutterError errorWithCode: @" loadHtmlString_failed"
252+ message: @" Failed parsing HTML string argument."
253+ details: @" Argument is nil." ];
254+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
255+ [resultExpectations[2 ] fulfill ];
256+ }];
257+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
258+ arguments: @{
259+ @" html" : @(42 ),
260+ }]
261+ result: ^(id _Nullable result) {
262+ FlutterError *expected =
263+ [FlutterError errorWithCode: @" loadHtmlString_failed"
264+ message: @" Failed parsing HTML string argument."
265+ details: @" Argument is not of type NSString." ];
266+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
267+ [resultExpectations[3 ] fulfill ];
268+ }];
269+ [controller onMethodCall: [FlutterMethodCall methodCallWithMethodName: @" loadHtmlString"
270+ arguments: @{
271+ @" html" : @" " ,
272+ }]
273+ result: ^(id _Nullable result) {
274+ FlutterError *expected =
275+ [FlutterError errorWithCode: @" loadHtmlString_failed"
276+ message: @" Failed parsing HTML string argument."
277+ details: @" Argument contains an empty string." ];
278+ [FLTWebViewTests assertFlutterError: result withExpected: expected];
279+ [resultExpectations[4 ] fulfill ];
280+ }];
281+
282+ [self waitForExpectations: resultExpectations timeout: 1.0 ];
283+ OCMReject ([mockWebView loadHTMLString: [OCMArg any ] baseURL: [OCMArg any ]]);
187284}
188285
189286- (void )testRunJavascriptFailsForNullString {
@@ -399,4 +496,12 @@ - (void)testRunJavascriptReturningResultReturnsErrorResultForWKError {
399496 [self waitForExpectationsWithTimeout: 30.0 handler: nil ];
400497}
401498
499+ + (void )assertFlutterError : (id )actual withExpected : (FlutterError *)expected {
500+ XCTAssertTrue ([actual class ] == [FlutterError class ]);
501+ FlutterError *errorResult = actual;
502+ XCTAssertEqualObjects (errorResult.code , expected.code );
503+ XCTAssertEqualObjects (errorResult.message , expected.message );
504+ XCTAssertEqualObjects (errorResult.details , expected.details );
505+ }
506+
402507@end
0 commit comments