[Mac WK2] Add an API test to cover r219765 (null dereference in [WKWebView dealloc])
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Jul 2017 20:58:09 +0000 (20:58 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Jul 2017 20:58:09 +0000 (20:58 +0000)
https://bugs.webkit.org/show_bug.cgi?id=174793

Reviewed by Tim Horton.

Adds a new unit test that invokes some asynchronous NSTextInputClient SPI methods as the web view is tearing
down. Without r219765, this test will dereference null and crash.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/TestWebKitAPI/mac/NSTextInputClientSPI.h: Added.
* TestWebKitAPI/Tests/mac/WKWebViewSelectionTests.mm:
(TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219836 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/TestWebKitAPI/mac/NSTextInputClientSPI.h [new file with mode: 0644]
Tools/TestWebKitAPI/Tests/mac/WKWebViewSelectionTests.mm

index 21c62754e0584772f9fd0c4427158ad3bb2c34fd..c40f1c3f97b8870607bfd189a9354ae16efe66df 100644 (file)
@@ -1,3 +1,18 @@
+2017-07-24  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        [Mac WK2] Add an API test to cover r219765 (null dereference in [WKWebView dealloc])
+        https://bugs.webkit.org/show_bug.cgi?id=174793
+
+        Reviewed by Tim Horton.
+
+        Adds a new unit test that invokes some asynchronous NSTextInputClient SPI methods as the web view is tearing
+        down. Without r219765, this test will dereference null and crash.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/TestWebKitAPI/mac/NSTextInputClientSPI.h: Added.
+        * TestWebKitAPI/Tests/mac/WKWebViewSelectionTests.mm:
+        (TEST):
+
 2017-07-24  Basuke Suzuki  <Basuke.Suzuki@sony.com>
 
         [Win] Implement Authentication dialog in MiniBrowser
index abacbaecec148a139219bbb1ab982aeb5f81aa78..d73c380ad93652b5422bc039455d1daed2a76c85 100644 (file)
                2E691AF21D79E75400129407 /* large-video-playing-scroll-away.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "large-video-playing-scroll-away.html"; sourceTree = "<group>"; };
                2E7765CC16C4D80A00BA2BB1 /* mainIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = mainIOS.mm; sourceTree = "<group>"; };
                2E7765CE16C4D81100BA2BB1 /* mainMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = mainMac.mm; sourceTree = "<group>"; };
+               2E7EF7AC1F266A8100DFB67C /* NSTextInputClientSPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NSTextInputClientSPI.h; sourceTree = "<group>"; };
                2E9896141D8F092B00739892 /* text-and-password-inputs.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "text-and-password-inputs.html"; sourceTree = "<group>"; };
                2ECFF5541D9B12F800B55394 /* NowPlayingControlsTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = NowPlayingControlsTests.mm; sourceTree = "<group>"; };
                2EFF06C21D8862120004BB30 /* large-video-offscreen.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "large-video-offscreen.html"; sourceTree = "<group>"; };
                        isa = PBXGroup;
                        children = (
                                C08587FB13FEC39B001EF4E5 /* InstanceMethodSwizzler.mm */,
+                               2E7EF7AC1F266A8100DFB67C /* NSTextInputClientSPI.h */,
                        );
                        path = mac;
                        sourceTree = "<group>";
diff --git a/Tools/TestWebKitAPI/Tests/TestWebKitAPI/mac/NSTextInputClientSPI.h b/Tools/TestWebKitAPI/Tests/TestWebKitAPI/mac/NSTextInputClientSPI.h
new file mode 100644 (file)
index 0000000..39deb07
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if PLATFORM(MAC)
+
+#if USE(APPLE_INTERNAL_SDK)
+
+#import <AppKit/NSTextInputClient_Private.h>
+
+#else
+
+@protocol NSTextInputClient_Async
+- (void)selectedRangeWithCompletionHandler:(void(^)(NSRange selectedRange))completionHandler;
+- (void)markedRangeWithCompletionHandler:(void(^)(NSRange markedRange))completionHandler;
+- (void)hasMarkedTextWithCompletionHandler:(void(^)(BOOL hasMarkedText))completionHandler;
+@end
+
+#endif
+
+#endif // PLATFORM(MAC)
index 1353e5dff53a45eeb859b7b9521e951e68a9f70c..8efcaab59e983f35c67b6f5e50b2dbdd0283ece6 100644 (file)
@@ -27,6 +27,7 @@
 
 #if WK_API_ENABLED && PLATFORM(MAC)
 
+#import "NSTextInputClientSPI.h"
 #import "PlatformUtilities.h"
 #import "TestWKWebView.h"
 
@@ -46,4 +47,22 @@ TEST(WKWebViewSelectionTests, DoubleClickDoesNotSelectTrailingSpace)
     EXPECT_STREQ("Hello", selectedText.UTF8String);
 }
 
+TEST(WKWebViewSelectionTests, DoNotCrashWhenCallingTextInputClientMethodsWhileDeallocatingView)
+{
+    NSString *textContent = @"This test should not cause us to dereference null.";
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 400, 400)]);
+    [webView synchronouslyLoadHTMLString:[NSString stringWithFormat:@"<p>%@</p>", textContent]];
+    [webView removeFromSuperview];
+
+    __unsafe_unretained id <NSTextInputClient_Async> inputClient = (id <NSTextInputClient_Async>)webView.get();
+    [inputClient hasMarkedTextWithCompletionHandler:^(BOOL) {
+        [inputClient selectedRangeWithCompletionHandler:^(NSRange) {
+            [inputClient markedRangeWithCompletionHandler:^(NSRange) { }];
+        }];
+    }];
+
+    EXPECT_WK_STREQ(textContent, [webView stringByEvaluatingJavaScript:@"document.body.textContent"]);
+}
+
 #endif // WK_API_ENABLED && PLATFORM(MAC)