AttributedString.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2012 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 "PlatformUtilities.h"
  28. #import "PlatformWebView.h"
  29. #import "WebKitAgnosticTest.h"
  30. #import <wtf/RetainPtr.h>
  31. namespace TestWebKitAPI {
  32. static NSAttributedString *attributedString(WebView *webView, NSRange range)
  33. {
  34. return [(NSView <NSTextInput> *)[[[webView mainFrame] frameView] documentView] attributedSubstringFromRange:range];
  35. }
  36. static NSAttributedString *attributedString(WKView *wkView, NSRange range)
  37. {
  38. NSRange actualRange;
  39. return [wkView attributedSubstringForProposedRange:range actualRange:&actualRange];
  40. }
  41. class AttributedStringTest_CustomFont : public WebKitAgnosticTest {
  42. public:
  43. template <typename View> void runTest(View);
  44. // WebKitAgnosticTest
  45. virtual void didLoadURL(WebView *webView) { runTest(webView); }
  46. virtual void didLoadURL(WKView *wkView) { runTest(wkView); }
  47. virtual NSURL *url() const { return [[NSBundle mainBundle] URLForResource:@"attributedStringCustomFont" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; }
  48. };
  49. template <typename View>
  50. void AttributedStringTest_CustomFont::runTest(View view)
  51. {
  52. NSAttributedString *attrString = attributedString(view, NSMakeRange(0, 5));
  53. EXPECT_WK_STREQ("Lorem", [attrString string]);
  54. }
  55. TEST_F(AttributedStringTest_CustomFont, WebKit)
  56. {
  57. runWebKit1Test();
  58. }
  59. TEST_F(AttributedStringTest_CustomFont, WebKit2)
  60. {
  61. runWebKit2Test();
  62. }
  63. class AttributedStringTest_Strikethrough : public WebKitAgnosticTest {
  64. public:
  65. template <typename View> void runTest(View);
  66. // WebKitAgnosticTest
  67. virtual void didLoadURL(WebView *webView) { runTest(webView); }
  68. virtual void didLoadURL(WKView *wkView) { runTest(wkView); }
  69. virtual NSURL *url() const { return [[NSBundle mainBundle] URLForResource:@"attributedStringStrikethrough" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; }
  70. };
  71. template <typename View>
  72. void AttributedStringTest_Strikethrough::runTest(View view)
  73. {
  74. NSAttributedString *attrString = attributedString(view, NSMakeRange(0, 5));
  75. EXPECT_WK_STREQ("Lorem", [attrString string]);
  76. NSDictionary *attributes = [attrString attributesAtIndex:0 effectiveRange:0];
  77. ASSERT_NOT_NULL([attributes objectForKey:NSStrikethroughStyleAttributeName]);
  78. ASSERT_TRUE([[attributes objectForKey:NSStrikethroughStyleAttributeName] isKindOfClass:[NSNumber class]]);
  79. ASSERT_EQ(NSUnderlineStyleSingle, [(NSNumber *)[attributes objectForKey:NSStrikethroughStyleAttributeName] intValue]);
  80. }
  81. TEST_F(AttributedStringTest_Strikethrough, WebKit)
  82. {
  83. runWebKit1Test();
  84. }
  85. TEST_F(AttributedStringTest_Strikethrough, WebKit2)
  86. {
  87. runWebKit2Test();
  88. }
  89. } // namespace TestWebKitAPI