Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

CDVWebViewTest.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "CDVWebViewTest.h"
  18. #import "AppDelegate.h"
  19. #import "ViewController.h"
  20. @interface CDVWebViewTest ()
  21. // Runs the run loop until the webview has finished loading.
  22. - (void)waitForPageLoad;
  23. @end
  24. @implementation CDVWebViewTest
  25. @synthesize webView;
  26. - (void)setUp
  27. {
  28. [super setUp];
  29. // Stop tests on the first failed assertion. Having the test stop on the
  30. // first exception makes it much easier to identify the source of the error.
  31. // On iOS < 5 there is a bug in SenTestingKit where the exception is
  32. // uncaught and the app crashes upon a failed STAssert (oh well).
  33. // [self raiseAfterFailure];
  34. }
  35. - (void)tearDown
  36. {
  37. // Enforce that the view controller is released between tests to ensure
  38. // tests don't affect each other.
  39. [self.appDelegate destroyViewController];
  40. [super tearDown];
  41. }
  42. - (AppDelegate*)appDelegate
  43. {
  44. return [[UIApplication sharedApplication] delegate];
  45. }
  46. - (CDVViewController*)viewController
  47. {
  48. // Lazily create the view controller so that tests that do not require it
  49. // are not slowed down by it.
  50. if (self.appDelegate.viewController == nil) {
  51. [self.appDelegate createViewController];
  52. // Things break if tearDown is called before the page has finished
  53. // loading (a JS error happens and an alert pops up), so enforce a wait
  54. // here.
  55. [self waitForPageLoad];
  56. }
  57. XCTAssertNotNil(self.appDelegate.viewController, @"createViewController failed");
  58. return self.appDelegate.viewController;
  59. }
  60. - (UIWebView*)webView
  61. {
  62. return (UIWebView*)self.viewController.webView;
  63. }
  64. - (id)pluginInstance:(NSString*)pluginName
  65. {
  66. id ret = [self.viewController getCommandInstance:pluginName];
  67. XCTAssertNotNil(ret, @"Missing plugin %@", pluginName);
  68. return ret;
  69. }
  70. - (void)reloadWebView
  71. {
  72. [self.appDelegate destroyViewController];
  73. [self.appDelegate createViewController];
  74. }
  75. - (void)waitForConditionName:(NSString*)conditionName block:(BOOL (^)())block
  76. {
  77. // Number of seconds to wait for a condition to become true before giving up.
  78. const NSTimeInterval kConditionTimeout = 5.0;
  79. // Useful when debugging so that it does not timeout after one loop.
  80. const int kMinIterations = 4;
  81. NSDate* startTime = [NSDate date];
  82. int i = 0;
  83. while (!block()) {
  84. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  85. NSTimeInterval elapsed = -[startTime timeIntervalSinceNow];
  86. XCTAssertTrue(i < kMinIterations || elapsed < kConditionTimeout,
  87. @"Timed out waiting for condition %@", conditionName);
  88. ++i;
  89. }
  90. }
  91. - (void)waitForPageLoad
  92. {
  93. [self waitForConditionName:@"PageLoad" block:^{
  94. return [@"true" isEqualToString:[self evalJs:@"window.pageIsLoaded"]];
  95. }];
  96. }
  97. - (NSString*)evalJs:(NSString*)code
  98. {
  99. return [self.webView stringByEvaluatingJavaScriptFromString:code];
  100. }
  101. @end