Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CDVWebViewDelegateTests.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import <XCTest/XCTest.h>
  18. #import <Cordova/CDVUIWebViewDelegate.h>
  19. @interface CDVWebViewDelegate2 : CDVUIWebViewDelegate {}
  20. - (void)setState:(NSInteger)state;
  21. - (NSInteger)state;
  22. @end
  23. @implementation CDVWebViewDelegate2
  24. - (void)setState:(NSInteger)state
  25. {
  26. _state = state;
  27. }
  28. - (NSInteger)state
  29. {
  30. return _state;
  31. }
  32. @end
  33. @interface CDVUIWebViewDelegate ()
  34. // expose private interface
  35. - (BOOL)shouldLoadRequest:(NSURLRequest*)request;
  36. -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType;
  37. @end
  38. @interface CDVWebViewDelegateTests : XCTestCase
  39. @end
  40. @implementation CDVWebViewDelegateTests
  41. - (void)setUp
  42. {
  43. [super setUp];
  44. }
  45. - (void)tearDown
  46. {
  47. [super tearDown];
  48. }
  49. - (void)testShouldLoadRequestWithStateWaitForStart
  50. {
  51. NSInteger initialState = 1; // STATE_WAITING_FOR_LOAD_START;
  52. NSInteger expectedState = 0; // STATE_IDLE;
  53. CDVWebViewDelegate2* wvd = [[CDVWebViewDelegate2 alloc] initWithDelegate:nil]; // not really testing delegate handling
  54. wvd.state = initialState;
  55. // Testing a top-level navigation
  56. [wvd webView:[UIWebView new] shouldStartLoadWithRequest:nil navigationType:UIWebViewNavigationTypeLinkClicked];
  57. XCTAssertTrue(wvd.state == expectedState, @"If the navigation started with state STATE_WAITING_FOR_LOAD_START then it must fail and the state should be STATE_IDLE");
  58. }
  59. - (void)testFailLoadStateCancelled
  60. {
  61. NSInteger initialState = 1; // STATE_WAITING_FOR_LOAD_START;
  62. NSInteger expectedState = 5; // STATE_CANCELLED;
  63. NSError* errorCancelled = [NSError errorWithDomain:NSCocoaErrorDomain code:NSURLErrorCancelled userInfo:nil];
  64. CDVWebViewDelegate2* wvd = [[CDVWebViewDelegate2 alloc] initWithDelegate:nil]; // not really testing delegate handling
  65. wvd.state = initialState;
  66. [wvd webView:[UIWebView new] didFailLoadWithError:errorCancelled];
  67. XCTAssertTrue(wvd.state == expectedState, @"If the load error was through an iframe redirect (NSURLErrorCancelled), the state should be STATE_CANCELLED");
  68. }
  69. - (void)testShouldLoadRequest
  70. {
  71. CDVUIWebViewDelegate* wvd = [[CDVUIWebViewDelegate alloc] initWithDelegate:nil]; // not really testing delegate handling
  72. NSURLRequest* mailtoUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:@"mailto:dev@cordova.apache.org"]];
  73. NSURLRequest* telUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:12345"]];
  74. NSURLRequest* plainUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apache.org"]];
  75. NSURLRequest* dataUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:@"data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="]];
  76. NSURLRequest* blobUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:@"blob:d3958f5c-0777-0845-9dcf-2cb28783acaf"]];
  77. XCTAssertTrue([wvd shouldLoadRequest:mailtoUrl], @"mailto urls should be allowed");
  78. XCTAssertTrue([wvd shouldLoadRequest:telUrl], @"tel urls should be allowed");
  79. // as long as this is in the whitelist it should pass
  80. XCTAssertTrue([wvd shouldLoadRequest:plainUrl], @"http urls should be allowed");
  81. XCTAssertTrue([wvd shouldLoadRequest:dataUrl], @"data urls should be allowed");
  82. XCTAssertTrue([wvd shouldLoadRequest:blobUrl], @"blob urls should be allowed");
  83. }
  84. - (void)testFragmentIdentifiersWithHttpUrl
  85. {
  86. [self doTestFragmentIdentifiersWithBaseUrl:@"http://cordova.apache.org" fragment:@"myfragment"];
  87. }
  88. - (void)testFragmentIdentifiersWithFileUrl
  89. {
  90. [self doTestFragmentIdentifiersWithBaseUrl:@"file:///var/mobile/GASGEQGQsdga3313/www/index.html" fragment:@"myfragment"];
  91. }
  92. - (void)testFragmentIdentifiersWithFileUrlAndMalformedFragment
  93. {
  94. [self doTestFragmentIdentifiersWithBaseUrl:@"file:///var/mobile/GASGEQGQsdga3313/www/index.html" fragment:@"/var/mobile/GASGEQGQsdga3313/www/index.html"];
  95. }
  96. - (void)doTestFragmentIdentifiersWithBaseUrl:(NSString*)baseUrl fragment:(NSString*)fragment
  97. {
  98. CDVUIWebViewDelegate* wvd = [[CDVUIWebViewDelegate alloc] initWithDelegate:nil]; // not really testing delegate handling
  99. NSString* originalUrlString = baseUrl;
  100. NSURL* originalUrl = [NSURL URLWithString:originalUrlString];
  101. NSURL* originalUrlWithFragmentOnly = [NSURL URLWithString:[NSString stringWithFormat:@"%@#%@", originalUrlString, fragment]];
  102. NSURL* originalUrlWithFragmentOnlyNoIdentifier = [NSURL URLWithString:[NSString stringWithFormat:@"%@#", originalUrlString]];
  103. NSURL* originalUrlWithQueryParamsAndFragment = [NSURL URLWithString:[NSString stringWithFormat:@"%@?foo=bar#%@", originalUrlString, fragment]];
  104. NSURLRequest* originalRequest = [NSURLRequest requestWithURL:originalUrl];
  105. NSURLRequest* originalRequestWithFragmentOnly = [NSURLRequest requestWithURL:originalUrlWithFragmentOnly];
  106. NSURLRequest* originalRequestWithFragmentOnlyNoIdentifier = [NSURLRequest requestWithURL:originalUrlWithFragmentOnlyNoIdentifier];
  107. NSURLRequest* originalRequestWithQueryParamsAndFragment = [NSURLRequest requestWithURL:originalUrlWithQueryParamsAndFragment];
  108. NSURLRequest* notOriginalRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://httpd.apache.org"]];
  109. XCTAssertTrue([wvd request:originalRequest isEqualToRequestAfterStrippingFragments:originalRequest], @"originalRequest should be a be equal to originalRequest after stripping fragments");
  110. XCTAssertTrue([wvd request:originalRequestWithFragmentOnly isEqualToRequestAfterStrippingFragments:originalRequest], @"originalRequestWithFragment should be equal to originalRequest after stripping fragment");
  111. XCTAssertTrue([wvd request:originalRequestWithFragmentOnlyNoIdentifier isEqualToRequestAfterStrippingFragments:originalRequest], @"originalRequestWithFragmentNoIdentifier should be equal to originalRequest after stripping fragment");
  112. XCTAssertFalse([wvd request:originalRequestWithQueryParamsAndFragment isEqualToRequestAfterStrippingFragments:originalRequest], @"originalRequestWithQueryParamsAndFragment should not be equal to originalRequest after stripping fragment");
  113. XCTAssertFalse([wvd request:notOriginalRequest isEqualToRequestAfterStrippingFragments:originalRequest], @"notOriginalRequest should not be equal to originalRequest after stripping fragment");
  114. // equality tests
  115. XCTAssertTrue([wvd request:originalRequestWithFragmentOnly isEqualToRequestAfterStrippingFragments:originalRequestWithFragmentOnly], @"originalRequestWithFragment should be a equal to itself after stripping fragments");
  116. XCTAssertTrue([wvd request:originalRequestWithFragmentOnlyNoIdentifier isEqualToRequestAfterStrippingFragments:originalRequestWithFragmentOnlyNoIdentifier], @"originalRequestWithFragmentNoIdentifier should be a equal to itself after stripping fragments");
  117. XCTAssertTrue([wvd request:originalRequestWithQueryParamsAndFragment isEqualToRequestAfterStrippingFragments:originalRequestWithQueryParamsAndFragment], @"originalRequestWithQueryParamsAndFragment should be equal to itself after stripping fragments");
  118. }
  119. @end