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

CDVViewControllerTest.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/CDVViewController.h>
  19. #define CDVViewControllerTestSettingKey @"test_cdvconfigfile"
  20. #define CDVViewControllerTestSettingValueDefault @"config.xml"
  21. #define CDVViewControllerTestSettingValueCustom @"config-custom.xml"
  22. @interface CDVViewControllerTest : XCTestCase
  23. @end
  24. @interface CDVViewController ()
  25. // expose private interface
  26. - (bool)checkAndReinitViewUrl;
  27. - (bool)isUrlEmpty:(NSURL*)url;
  28. @end
  29. @implementation CDVViewControllerTest
  30. -(CDVViewController*)viewController{
  31. CDVViewController* viewController = [CDVViewController new];
  32. return viewController;
  33. }
  34. -(void)doTestInitWithConfigFile:(NSString*)configFile expectedSettingValue:(NSString*)value{
  35. // Create a CDVViewController
  36. CDVViewController* viewController = [self viewController];
  37. if(configFile){
  38. // Set custom config file
  39. viewController.configFile = configFile;
  40. }else{
  41. // Do not specify config file ==> fallback to default config.xml
  42. }
  43. // Trigger -viewDidLoad
  44. [viewController view];
  45. // Assert that the proper file was actually loaded, checking the value of a test setting it must contain
  46. NSString* settingValue = [viewController.settings objectForKey:CDVViewControllerTestSettingKey];
  47. XCTAssertEqualObjects(settingValue, value);
  48. }
  49. -(void)testInitWithDefaultConfigFile{
  50. [self doTestInitWithConfigFile:nil expectedSettingValue:CDVViewControllerTestSettingValueDefault];
  51. }
  52. -(void)testInitWithCustomConfigFileAbsolutePath{
  53. NSString* configFileAbsolutePath = [[NSBundle mainBundle] pathForResource:@"config-custom" ofType:@"xml"];
  54. [self doTestInitWithConfigFile:configFileAbsolutePath expectedSettingValue:CDVViewControllerTestSettingValueCustom];
  55. }
  56. -(void)testInitWithCustomConfigFileRelativePath{
  57. NSString* configFileRelativePath = @"config-custom.xml";
  58. [self doTestInitWithConfigFile:configFileRelativePath expectedSettingValue:CDVViewControllerTestSettingValueCustom];
  59. }
  60. -(void)testColorFromColorString{
  61. CDVViewController* viewController = [self viewController];
  62. // Comparison values: #FFAABB and #99FFAABB
  63. UIColor* referenceColor = [UIColor colorWithRed:255.0/255.0 green:170.0/255.0 blue:187.0/255.0 alpha:1.0];
  64. UIColor* referenceColorAlpha = [UIColor colorWithRed:255.0/255.0 green:170.0/255.0 blue:187.0/255.0 alpha:0.6];
  65. // Valid values
  66. XCTAssertTrue([[viewController colorFromColorString:@"#FAB"] isEqual:referenceColor]);
  67. XCTAssertTrue([[viewController colorFromColorString:@"#FFAABB"] isEqual:referenceColor]);
  68. XCTAssertTrue([[viewController colorFromColorString:@"0xFFAABB"] isEqual:referenceColor]);
  69. XCTAssertTrue([[viewController colorFromColorString:@"#99FFAABB"] isEqual:referenceColorAlpha]);
  70. XCTAssertTrue([[viewController colorFromColorString:@"0x99FFAABB"] isEqual:referenceColorAlpha]);
  71. // Invalid values
  72. XCTAssertNil([viewController colorFromColorString:nil]);
  73. XCTAssertNil([viewController colorFromColorString:@"black"]);
  74. XCTAssertNil([viewController colorFromColorString:@"0xFAB"]);
  75. XCTAssertNil([viewController colorFromColorString:@"#1234"]);
  76. XCTAssertNil([viewController colorFromColorString:@"#12345"]);
  77. XCTAssertNil([viewController colorFromColorString:@"#1234567"]);
  78. XCTAssertNil([viewController colorFromColorString:@"#NOTHEX"]);
  79. }
  80. -(void)testIsUrlEmpty{
  81. CDVViewController* viewController = [self viewController];
  82. XCTAssertTrue([viewController isUrlEmpty:(id)[NSNull null]]);
  83. XCTAssertTrue([viewController isUrlEmpty:nil]);
  84. XCTAssertTrue([viewController isUrlEmpty:[NSURL URLWithString:@""]]);
  85. XCTAssertTrue([viewController isUrlEmpty:[NSURL URLWithString:@"about:blank"]]);
  86. }
  87. -(void)testIfItLoadsAppUrlIfCurrentViewIsBlank{
  88. CDVViewController* viewController = [self viewController];
  89. NSString* appUrl = @"about:blank";
  90. NSString* html = @"<html><body></body></html>";
  91. [viewController.webViewEngine loadHTMLString:html baseURL:[NSURL URLWithString:appUrl]];
  92. XCTAssertFalse([viewController checkAndReinitViewUrl]);
  93. appUrl = @"https://cordova.apache.org";
  94. viewController.startPage = appUrl;
  95. [viewController.webViewEngine loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:appUrl]]];
  96. XCTAssertTrue([viewController checkAndReinitViewUrl]);
  97. }
  98. @end