SetDocumentURI.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2012 Google 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. #include "config.h"
  26. #include "PlatformUtilities.h"
  27. #include "PlatformWebView.h"
  28. #include <wtf/RetainPtr.h>
  29. #import <WebKit/DOM.h>
  30. #import <WebKit/WebViewPrivate.h>
  31. @interface SetDocumentURITest : NSObject {
  32. }
  33. @end
  34. static bool didFinishLoad;
  35. @implementation SetDocumentURITest
  36. - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
  37. {
  38. didFinishLoad = true;
  39. }
  40. @end
  41. namespace TestWebKitAPI {
  42. TEST(WebKit1, SetDocumentURITestFile)
  43. {
  44. RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
  45. RetainPtr<SetDocumentURITest> testController = adoptNS([SetDocumentURITest new]);
  46. webView.get().frameLoadDelegate = testController.get();
  47. [[webView.get() mainFrame] loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"SetDocumentURI" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]];
  48. Util::run(&didFinishLoad);
  49. didFinishLoad = false;
  50. DOMDocument *document = webView.get().mainFrameDocument;
  51. [document setDocumentURI:@"file:///test"];
  52. // documentURI set correctly.
  53. EXPECT_WK_STREQ(@"file:///test", [document documentURI]);
  54. // baseURI follows along.
  55. EXPECT_WK_STREQ(@"file:///test", [document baseURI]);
  56. }
  57. TEST(WebKit1, SetDocumentURITestURL)
  58. {
  59. RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
  60. RetainPtr<SetDocumentURITest> testController = adoptNS([SetDocumentURITest new]);
  61. webView.get().frameLoadDelegate = testController.get();
  62. [[webView.get() mainFrame] loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"SetDocumentURI" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]];
  63. Util::run(&didFinishLoad);
  64. didFinishLoad = false;
  65. DOMDocument *document = webView.get().mainFrameDocument;
  66. [document setDocumentURI:@"http://example.com/"];
  67. // documentURI set correctly.
  68. EXPECT_WK_STREQ(@"http://example.com/", [document documentURI]);
  69. // baseURI follows along.
  70. EXPECT_WK_STREQ(@"http://example.com/", [document baseURI]);
  71. // Relative links too.
  72. NSString *result = [webView.get() stringByEvaluatingJavaScriptFromString:@"document.getElementById('relative').href"];
  73. EXPECT_WK_STREQ(@"http://example.com/relativeURL.html", result);
  74. }
  75. TEST(WebKit1, SetDocumentURITestString)
  76. {
  77. RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
  78. RetainPtr<SetDocumentURITest> testController = adoptNS([SetDocumentURITest new]);
  79. webView.get().frameLoadDelegate = testController.get();
  80. [[webView.get() mainFrame] loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"SetDocumentURI" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]];
  81. Util::run(&didFinishLoad);
  82. didFinishLoad = false;
  83. DOMDocument *document = webView.get().mainFrameDocument;
  84. [document setDocumentURI:@"A non-URL string."];
  85. // documentURI accepts random strings.
  86. EXPECT_WK_STREQ(@"A non-URL string.", [document documentURI]);
  87. // baseURI is empty for non-URL strings.
  88. EXPECT_WK_STREQ(@"", [document baseURI]);
  89. }
  90. TEST(WebKit1, SetDocumentURITestNull)
  91. {
  92. RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]);
  93. RetainPtr<SetDocumentURITest> testController = adoptNS([SetDocumentURITest new]);
  94. webView.get().frameLoadDelegate = testController.get();
  95. [[webView.get() mainFrame] loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"SetDocumentURI" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]]];
  96. Util::run(&didFinishLoad);
  97. didFinishLoad = false;
  98. DOMDocument *document = webView.get().mainFrameDocument;
  99. [document setDocumentURI:nil];
  100. // documenturi is empty.
  101. EXPECT_WK_STREQ(@"", [document documentURI]);
  102. // baseURI is null as well.
  103. EXPECT_WK_STREQ(@"", [document baseURI]);
  104. }
  105. } // namespace TestWebKitAPI