Add SPI to expose width and height anchors for WKWebView's content view
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 16 Mar 2018 23:56:53 +0000 (23:56 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 16 Mar 2018 23:56:53 +0000 (23:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=183711
<rdar://problem/38562899>

Reviewed by Tim Horton.

Source/WebKit:

Add _contentWidthAnchor and _contentHeightAnchor SPI to WKWebView for internal clients to be able to reason
about the size of the content view on iOS using autolayout.

Test: WebKit.AutoLayoutPositionHeaderAndFooterViewsInScrollView.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _contentWidthAnchor]):
(-[WKWebView _contentHeightAnchor]):
* UIProcess/API/Cocoa/WKWebViewPrivate.h:

Tools:

Adds an API test exercising the new SPI.

* TestWebKitAPI/Tests/WebKitCocoa/AutoLayoutIntegration.mm:
(checkCGRect):
(TEST):

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

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/AutoLayoutIntegration.mm

index 842c4dd..6c09a19 100644 (file)
@@ -1,3 +1,21 @@
+2018-03-16  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Add SPI to expose width and height anchors for WKWebView's content view
+        https://bugs.webkit.org/show_bug.cgi?id=183711
+        <rdar://problem/38562899>
+
+        Reviewed by Tim Horton.
+
+        Add _contentWidthAnchor and _contentHeightAnchor SPI to WKWebView for internal clients to be able to reason
+        about the size of the content view on iOS using autolayout.
+
+        Test: WebKit.AutoLayoutPositionHeaderAndFooterViewsInScrollView.
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView _contentWidthAnchor]):
+        (-[WKWebView _contentHeightAnchor]):
+        * UIProcess/API/Cocoa/WKWebViewPrivate.h:
+
 2018-03-16  Megan Gardner  <megan_gardner@apple.com>
 
         Add _useSystemAppearance to WKView
index 18b2bc1..e191303 100644 (file)
@@ -4948,6 +4948,16 @@ static inline WebKit::FindOptions toFindOptions(_WKFindOptions wkFindOptions)
 
 #if PLATFORM(IOS)
 
+- (NSLayoutDimension *)_contentWidthAnchor
+{
+    return [_contentView widthAnchor];
+}
+
+- (NSLayoutDimension *)_contentHeightAnchor
+{
+    return [_contentView heightAnchor];
+}
+
 #if ENABLE(FULLSCREEN_API)
 - (void)removeFromSuperview
 {
index e63dd9f..e1dd5d8 100644 (file)
@@ -254,6 +254,9 @@ typedef NS_OPTIONS(NSUInteger, _WKRectEdge) {
 - (void)_accessibilityRetrieveSpeakSelectionContent WK_API_AVAILABLE(ios(11.0));
 - (void)_accessibilityDidGetSpeakSelectionContent:(NSString *)content WK_API_AVAILABLE(ios(11.0));
 
+@property (nonatomic, readonly) NSLayoutDimension *_contentWidthAnchor WK_API_AVAILABLE(ios(WK_IOS_TBA));
+@property (nonatomic, readonly) NSLayoutDimension *_contentHeightAnchor WK_API_AVAILABLE(ios(WK_IOS_TBA));
+
 #else
 
 @property (nonatomic, readonly) _WKRectEdge _pinnedState WK_API_AVAILABLE(macosx(WK_MAC_TBA));
index 6161664..e79fd7e 100644 (file)
@@ -1,3 +1,17 @@
+2018-03-16  Wenson Hsieh  <wenson_hsieh@apple.com>
+
+        Add SPI to expose width and height anchors for WKWebView's content view
+        https://bugs.webkit.org/show_bug.cgi?id=183711
+        <rdar://problem/38562899>
+
+        Reviewed by Tim Horton.
+
+        Adds an API test exercising the new SPI.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/AutoLayoutIntegration.mm:
+        (checkCGRect):
+        (TEST):
+
 2018-03-16  Chris Dumez  <cdumez@apple.com>
 
         WebKit.RestoreSessionStateContainingScrollRestorationDefault API test is failing with async policy delegates
index 7c39c3c..d125741 100644 (file)
 
 #import "PlatformUtilities.h"
 #import "TestNavigationDelegate.h"
+#import "TestWKWebView.h"
 #import <WebKit/WKWebViewPrivate.h>
 #import <wtf/RetainPtr.h>
 
-#if WK_API_ENABLED && PLATFORM(MAC)
+#if PLATFORM(IOS)
+#import "UIKitSPI.h"
+#endif
+
+#if WK_API_ENABLED
+
+#if PLATFORM(MAC)
 
 static bool didInvalidateIntrinsicContentSize;
 static bool didEvaluateJavaScript;
@@ -204,4 +211,54 @@ TEST(WebKit, AutoLayoutRenderingProgressRelativeOrdering)
     [webView setNavigationDelegate:nil];
 }
 
-#endif
+#endif // PLATFORM(MAC)
+
+#if PLATFORM(IOS)
+
+static void checkCGRect(CGRect expected, CGRect observed)
+{
+    bool success = CGRectEqualToRect(expected, observed);
+    EXPECT_TRUE(success);
+    if (!success)
+        NSLog(@"Expected rect: %@ but got: %@", NSStringFromCGRect(expected), NSStringFromCGRect(observed));
+}
+
+TEST(WebKit, AutoLayoutPositionHeaderAndFooterViewsInScrollView)
+{
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
+    UIScrollView *scrollView = [webView scrollView];
+    scrollView.contentInset = UIEdgeInsetsMake(100, 0, 100, 0);
+
+    [webView synchronouslyLoadHTMLString:@"<meta name='viewport' content='width=device-width, initial-scale=1'><body style='width: 100%; height: 5000px; margin: 0'>"];
+
+    auto headerLabel = adoptNS([[UILabel alloc] init]);
+    [headerLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
+    [scrollView addSubview:headerLabel.get()];
+
+    auto footerLabel = adoptNS([[UILabel alloc] init]);
+    [footerLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
+    [scrollView addSubview:footerLabel.get()];
+
+    [NSLayoutConstraint activateConstraints:@[
+        [[headerLabel widthAnchor] constraintEqualToConstant:320],
+        [[headerLabel heightAnchor] constraintEqualToConstant:100],
+        [[headerLabel leftAnchor] constraintEqualToAnchor:scrollView.contentLayoutGuide.leftAnchor],
+        [[headerLabel bottomAnchor] constraintEqualToAnchor:scrollView.contentLayoutGuide.topAnchor],
+        [[footerLabel widthAnchor] constraintEqualToConstant:320],
+        [[footerLabel heightAnchor] constraintEqualToConstant:100],
+        [[footerLabel leftAnchor] constraintEqualToAnchor:scrollView.contentLayoutGuide.leftAnchor],
+        [[footerLabel topAnchor] constraintEqualToAnchor:scrollView.contentLayoutGuide.bottomAnchor],
+        [[webView _contentWidthAnchor] constraintEqualToAnchor:scrollView.contentLayoutGuide.widthAnchor],
+        [[webView _contentHeightAnchor] constraintEqualToAnchor:scrollView.contentLayoutGuide.heightAnchor]
+    ]];
+
+    [webView layoutSubviews];
+    [webView waitForNextPresentationUpdate];
+
+    checkCGRect(CGRectMake(0, -100, 320, 100), [headerLabel frame]);
+    checkCGRect(CGRectMake(0, 5000, 320, 100), [footerLabel frame]);
+}
+
+#endif // PLATFORM(IOS)
+
+#endif // WK_API_ENABLED