|
5 | 5 | @import Flutter;
|
6 | 6 | @import XCTest;
|
7 | 7 | @import webview_flutter_wkwebview;
|
| 8 | +@import webview_flutter_wkwebview.Test; |
8 | 9 |
|
9 | 10 | // OCMock library doesn't generate a valid modulemap.
|
10 | 11 | #import <OCMock/OCMock.h>
|
@@ -301,4 +302,196 @@ - (void)testRunJavascriptReturningResultReturnsErrorResultForWKError {
|
301 | 302 | [self waitForExpectationsWithTimeout:30.0 handler:nil];
|
302 | 303 | }
|
303 | 304 |
|
| 305 | +- (void)testBuildNSURLRequestReturnsNilForNonDictionaryValue { |
| 306 | + // Setup |
| 307 | + FLTWebViewController *controller = |
| 308 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 309 | + viewIdentifier:1 |
| 310 | + arguments:nil |
| 311 | + binaryMessenger:self.mockBinaryMessenger]; |
| 312 | + |
| 313 | + // Run |
| 314 | + NSURLRequest *request = [controller buildNSURLRequest:@{@"request" : @"Non Dictionary Value"}]; |
| 315 | + |
| 316 | + // Verify |
| 317 | + XCTAssertNil(request); |
| 318 | +} |
| 319 | + |
| 320 | +- (void)testBuildNSURLRequestReturnsNilForMissingURI { |
| 321 | + // Setup |
| 322 | + FLTWebViewController *controller = |
| 323 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 324 | + viewIdentifier:1 |
| 325 | + arguments:nil |
| 326 | + binaryMessenger:self.mockBinaryMessenger]; |
| 327 | + |
| 328 | + // Run |
| 329 | + NSURLRequest *request = [controller buildNSURLRequest:@{@"request" : @{}}]; |
| 330 | + |
| 331 | + // Verify |
| 332 | + XCTAssertNil(request); |
| 333 | +} |
| 334 | + |
| 335 | +- (void)testBuildNSURLRequestReturnsNilForInvalidURI { |
| 336 | + // Setup |
| 337 | + FLTWebViewController *controller = |
| 338 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 339 | + viewIdentifier:1 |
| 340 | + arguments:nil |
| 341 | + binaryMessenger:self.mockBinaryMessenger]; |
| 342 | + |
| 343 | + // Run |
| 344 | + NSDictionary *requestData = @{@"uri" : @"invalid uri"}; |
| 345 | + NSURLRequest *request = [controller buildNSURLRequest:@{@"request" : requestData}]; |
| 346 | + |
| 347 | + // Verify |
| 348 | + XCTAssertNil(request); |
| 349 | +} |
| 350 | + |
| 351 | +- (void)testBuildNSURLRequestBuildsNSMutableURLRequestWithOptionalParameters { |
| 352 | + // Setup |
| 353 | + FLTWebViewController *controller = |
| 354 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 355 | + viewIdentifier:1 |
| 356 | + arguments:nil |
| 357 | + binaryMessenger:self.mockBinaryMessenger]; |
| 358 | + |
| 359 | + // Run |
| 360 | + NSDictionary *requestData = @{ |
| 361 | + @"uri" : @"https://flutter.dev", |
| 362 | + @"method" : @"POST", |
| 363 | + @"headers" : @{@"Foo" : @"Bar"}, |
| 364 | + @"body" : [FlutterStandardTypedData |
| 365 | + typedDataWithBytes:[@"Test Data" dataUsingEncoding:NSUTF8StringEncoding]], |
| 366 | + }; |
| 367 | + NSURLRequest *request = [controller buildNSURLRequest:@{@"request" : requestData}]; |
| 368 | + |
| 369 | + // Verify |
| 370 | + XCTAssertNotNil(request); |
| 371 | + XCTAssertEqualObjects(request.URL.absoluteString, @"https://flutter.dev"); |
| 372 | + XCTAssertEqualObjects(request.HTTPMethod, @"POST"); |
| 373 | + XCTAssertEqualObjects(request.allHTTPHeaderFields, @{@"Foo" : @"Bar"}); |
| 374 | + XCTAssertEqualObjects(request.HTTPBody, [@"Test Data" dataUsingEncoding:NSUTF8StringEncoding]); |
| 375 | +} |
| 376 | + |
| 377 | +- (void)testBuildNSURLRequestBuildsNSMutableURLRequestWithoutOptionalParameters { |
| 378 | + // Setup |
| 379 | + FLTWebViewController *controller = |
| 380 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 381 | + viewIdentifier:1 |
| 382 | + arguments:nil |
| 383 | + binaryMessenger:self.mockBinaryMessenger]; |
| 384 | + |
| 385 | + // Run |
| 386 | + NSDictionary *requestData = @{ |
| 387 | + @"uri" : @"https://flutter.dev", |
| 388 | + }; |
| 389 | + NSURLRequest *request = [controller buildNSURLRequest:@{@"request" : requestData}]; |
| 390 | + |
| 391 | + // Verify |
| 392 | + XCTAssertNotNil(request); |
| 393 | + XCTAssertEqualObjects(request.URL.absoluteString, @"https://flutter.dev"); |
| 394 | + XCTAssertEqualObjects(request.HTTPMethod, @"GET"); |
| 395 | + XCTAssertNil(request.allHTTPHeaderFields); |
| 396 | + XCTAssertNil(request.HTTPBody); |
| 397 | +} |
| 398 | + |
| 399 | +- (void)testOnLoadUrlReturnsErrorResultForInvalidRequest { |
| 400 | + // Setup |
| 401 | + FLTWebViewController *controller = |
| 402 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 403 | + viewIdentifier:1 |
| 404 | + arguments:nil |
| 405 | + binaryMessenger:self.mockBinaryMessenger]; |
| 406 | + XCTestExpectation *resultExpectation = |
| 407 | + [self expectationWithDescription:@"Should return error result when request cannot be built"]; |
| 408 | + |
| 409 | + // Run |
| 410 | + FlutterMethodCall *methodCall = [FlutterMethodCall methodCallWithMethodName:@"loadUrl" |
| 411 | + arguments:@{}]; |
| 412 | + [controller onLoadUrl:methodCall |
| 413 | + result:^(id _Nullable result) { |
| 414 | + XCTAssertTrue([result class] == [FlutterError class]); |
| 415 | + [resultExpectation fulfill]; |
| 416 | + }]; |
| 417 | + |
| 418 | + // Verify |
| 419 | + [self waitForExpectationsWithTimeout:30.0 handler:nil]; |
| 420 | +} |
| 421 | + |
| 422 | +- (void)testOnLoadUrlLoadsRequestWithSuccessResult { |
| 423 | + // Setup |
| 424 | + FLTWebViewController *controller = |
| 425 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 426 | + viewIdentifier:1 |
| 427 | + arguments:nil |
| 428 | + binaryMessenger:self.mockBinaryMessenger]; |
| 429 | + XCTestExpectation *resultExpectation = [self expectationWithDescription:@"Should return nil"]; |
| 430 | + FLTWKWebView *mockView = OCMClassMock(FLTWKWebView.class); |
| 431 | + controller.webView = mockView; |
| 432 | + |
| 433 | + // Run |
| 434 | + FlutterMethodCall *methodCall = |
| 435 | + [FlutterMethodCall methodCallWithMethodName:@"loadUrl" |
| 436 | + arguments:@{@"url" : @"https://flutter.dev/"}]; |
| 437 | + [controller onLoadUrl:methodCall |
| 438 | + result:^(id _Nullable result) { |
| 439 | + XCTAssertNil(result); |
| 440 | + [resultExpectation fulfill]; |
| 441 | + }]; |
| 442 | + |
| 443 | + // Verify |
| 444 | + OCMVerify([mockView loadRequest:[OCMArg any]]); |
| 445 | + [self waitForExpectationsWithTimeout:30.0 handler:nil]; |
| 446 | +} |
| 447 | + |
| 448 | +- (void)testOnLoadRequestReturnsErroResultForInvalidRequest { |
| 449 | + // Setup |
| 450 | + FLTWebViewController *controller = |
| 451 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 452 | + viewIdentifier:1 |
| 453 | + arguments:nil |
| 454 | + binaryMessenger:self.mockBinaryMessenger]; |
| 455 | + XCTestExpectation *resultExpectation = |
| 456 | + [self expectationWithDescription:@"Should return error result when request cannot be built"]; |
| 457 | + |
| 458 | + // Run |
| 459 | + FlutterMethodCall *methodCall = [FlutterMethodCall methodCallWithMethodName:@"loadRequest" |
| 460 | + arguments:@{}]; |
| 461 | + [controller onLoadRequest:methodCall |
| 462 | + result:^(id _Nullable result) { |
| 463 | + XCTAssertTrue([result class] == [FlutterError class]); |
| 464 | + [resultExpectation fulfill]; |
| 465 | + }]; |
| 466 | + |
| 467 | + // Verify |
| 468 | + [self waitForExpectationsWithTimeout:30.0 handler:nil]; |
| 469 | +} |
| 470 | + |
| 471 | +- (void)testOnLoadRequestLoadsRequestWithSuccessResult { |
| 472 | + // Setup |
| 473 | + FLTWebViewController *controller = |
| 474 | + [[FLTWebViewController alloc] initWithFrame:CGRectMake(0, 0, 300, 400) |
| 475 | + viewIdentifier:1 |
| 476 | + arguments:nil |
| 477 | + binaryMessenger:self.mockBinaryMessenger]; |
| 478 | + XCTestExpectation *resultExpectation = [self expectationWithDescription:@"Should return nil"]; |
| 479 | + FLTWKWebView *mockView = OCMClassMock(FLTWKWebView.class); |
| 480 | + controller.webView = mockView; |
| 481 | + |
| 482 | + // Run |
| 483 | + FlutterMethodCall *methodCall = [FlutterMethodCall |
| 484 | + methodCallWithMethodName:@"loadRequest" |
| 485 | + arguments:@{@"request" : @{@"uri" : @"https://flutter.dev/"}}]; |
| 486 | + [controller onLoadRequest:methodCall |
| 487 | + result:^(id _Nullable result) { |
| 488 | + XCTAssertNil(result); |
| 489 | + [resultExpectation fulfill]; |
| 490 | + }]; |
| 491 | + |
| 492 | + // Verify |
| 493 | + OCMVerify([mockView loadRequest:[OCMArg any]]); |
| 494 | + [self waitForExpectationsWithTimeout:30.0 handler:nil]; |
| 495 | +} |
| 496 | + |
304 | 497 | @end
|
0 commit comments