WKBrowsingContextLoadDelegateTest.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import "config.h"
  26. #import "Test.h"
  27. #import <WebKit2/WKBrowsingContextController.h>
  28. #import <WebKit2/WKBrowsingContextGroup.h>
  29. #import <WebKit2/WKBrowsingContextLoadDelegate.h>
  30. #import <WebKit2/WKProcessGroup.h>
  31. #import <WebKit2/WKRetainPtr.h>
  32. #import <WebKit2/WKView.h>
  33. #import "PlatformUtilities.h"
  34. namespace {
  35. class WKBrowsingContextLoadDelegateTest : public ::testing::Test {
  36. public:
  37. WKProcessGroup *processGroup;
  38. WKBrowsingContextGroup *browsingContextGroup;
  39. WKView *view;
  40. WKBrowsingContextLoadDelegateTest()
  41. : processGroup(nil)
  42. , browsingContextGroup(nil)
  43. , view(nil)
  44. {
  45. }
  46. virtual void SetUp()
  47. {
  48. processGroup = [[WKProcessGroup alloc] init];
  49. browsingContextGroup = [[WKBrowsingContextGroup alloc] initWithIdentifier:@"TestIdentifier"];
  50. view = [[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) processGroup:processGroup browsingContextGroup:browsingContextGroup];
  51. }
  52. virtual void TearDown()
  53. {
  54. [view release];
  55. [browsingContextGroup release];
  56. [processGroup release];
  57. }
  58. };
  59. } // namespace
  60. @interface SimpleLoadDelegate : NSObject <WKBrowsingContextLoadDelegate>
  61. {
  62. bool* _simpleLoadDone;
  63. }
  64. - (id)initWithFlag:(bool*)flag;
  65. @end
  66. @implementation SimpleLoadDelegate
  67. - (id)initWithFlag:(bool*)flag
  68. {
  69. self = [super init];
  70. if (!self)
  71. return nil;
  72. _simpleLoadDone = flag;
  73. return self;
  74. }
  75. - (void)browsingContextControllerDidFinishLoad:(WKBrowsingContextController *)sender
  76. {
  77. *_simpleLoadDone = true;
  78. }
  79. @end
  80. TEST_F(WKBrowsingContextLoadDelegateTest, Empty)
  81. {
  82. // Just make sure the setup/tear down works.
  83. }
  84. TEST_F(WKBrowsingContextLoadDelegateTest, SimpleLoad)
  85. {
  86. bool simpleLoadDone = false;
  87. // Add the load delegate.
  88. SimpleLoadDelegate *loadDelegate = [[SimpleLoadDelegate alloc] initWithFlag:&simpleLoadDone];
  89. view.browsingContextController.loadDelegate = loadDelegate;
  90. // Load the file.
  91. NSURL *nsURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
  92. [view.browsingContextController loadFileURL:nsURL restrictToFilesWithin:nil];
  93. // Wait for the load to finish.
  94. TestWebKitAPI::Util::run(&simpleLoadDone);
  95. // Tear down the delegate.
  96. view.browsingContextController.loadDelegate = nil;
  97. [loadDelegate release];
  98. }
  99. TEST_F(WKBrowsingContextLoadDelegateTest, SimpleLoadOfHTMLString)
  100. {
  101. bool simpleLoadDone = false;
  102. // Add the load delegate.
  103. SimpleLoadDelegate *loadDelegate = [[SimpleLoadDelegate alloc] initWithFlag:&simpleLoadDone];
  104. view.browsingContextController.loadDelegate = loadDelegate;
  105. // Load the HTML string.
  106. [view.browsingContextController loadHTMLString:@"<html><body>Simple HTML String</body></html>" baseURL:[NSURL URLWithString:@"about:blank"]];
  107. // Wait for the load to finish.
  108. TestWebKitAPI::Util::run(&simpleLoadDone);
  109. // Tear down the delegate.
  110. view.browsingContextController.loadDelegate = nil;
  111. [loadDelegate release];
  112. }
  113. TEST_F(WKBrowsingContextLoadDelegateTest, SimpleLoadOfHTMLString_NilBaseURL)
  114. {
  115. bool simpleLoadDone = false;
  116. // Add the load delegate.
  117. SimpleLoadDelegate *loadDelegate = [[SimpleLoadDelegate alloc] initWithFlag:&simpleLoadDone];
  118. view.browsingContextController.loadDelegate = loadDelegate;
  119. // Load the HTML string, pass nil as the baseURL.
  120. [view.browsingContextController loadHTMLString:@"<html><body>Simple HTML String</body></html>" baseURL:nil];
  121. // Wait for the load to finish.
  122. TestWebKitAPI::Util::run(&simpleLoadDone);
  123. // Tear down the delegate.
  124. view.browsingContextController.loadDelegate = nil;
  125. [loadDelegate release];
  126. }
  127. TEST_F(WKBrowsingContextLoadDelegateTest, SimpleLoadOfHTMLString_NilHTMLStringAndBaseURL)
  128. {
  129. bool simpleLoadDone = false;
  130. // Add the load delegate.
  131. SimpleLoadDelegate *loadDelegate = [[SimpleLoadDelegate alloc] initWithFlag:&simpleLoadDone];
  132. view.browsingContextController.loadDelegate = loadDelegate;
  133. // Load the HTML string (as nil).
  134. [view.browsingContextController loadHTMLString:nil baseURL:nil];
  135. // Wait for the load to finish.
  136. TestWebKitAPI::Util::run(&simpleLoadDone);
  137. // Tear down the delegate.
  138. view.browsingContextController.loadDelegate = nil;
  139. [loadDelegate release];
  140. }
  141. @interface SimpleLoadFailDelegate : NSObject <WKBrowsingContextLoadDelegate>
  142. {
  143. bool* _simpleLoadFailDone;
  144. }
  145. - (id)initWithFlag:(bool*)flag;
  146. @end
  147. @implementation SimpleLoadFailDelegate
  148. - (id)initWithFlag:(bool*)flag
  149. {
  150. self = [super init];
  151. if (!self)
  152. return nil;
  153. _simpleLoadFailDone = flag;
  154. return self;
  155. }
  156. - (void)browsingContextControllerDidFailProvisionalLoad:(WKBrowsingContextController *)sender withError:(NSError *)error
  157. {
  158. EXPECT_EQ(-1100, error.code);
  159. EXPECT_WK_STREQ(NSURLErrorDomain, error.domain);
  160. *_simpleLoadFailDone = true;
  161. }
  162. @end
  163. TEST_F(WKBrowsingContextLoadDelegateTest, SimpleLoadFail)
  164. {
  165. bool simpleLoadFailDone = false;
  166. // Add the load delegate.
  167. SimpleLoadFailDelegate *loadDelegate = [[SimpleLoadFailDelegate alloc] initWithFlag:&simpleLoadFailDone];
  168. view.browsingContextController.loadDelegate = loadDelegate;
  169. // Load a non-existent file.
  170. NSURL *nsURL = [NSURL URLWithString:@"file:///does-not-exist.html"];
  171. [view.browsingContextController loadFileURL:nsURL restrictToFilesWithin:nil];
  172. // Wait for the load to fail.
  173. TestWebKitAPI::Util::run(&simpleLoadFailDone);
  174. // Tear down the delegate.
  175. view.browsingContextController.loadDelegate = nil;
  176. [loadDelegate release];
  177. }