Add Googletest assertion support for CGRect and NSRect
authordbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Nov 2019 23:55:15 +0000 (23:55 +0000)
committerdbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 4 Nov 2019 23:55:15 +0000 (23:55 +0000)
https://bugs.webkit.org/show_bug.cgi?id=203817

Reviewed by Myles C. Maxfield.

Add operator== and operator<< overloads for CGRect and NSRect so that these types can be
passed to EXPECT_EQ() and other Googletest assertions.

To make use of these overloads Cocoa tests should include #import "TestCocoa.h" instead of
"Test.h". TestCocoa.h imports Test.h. With these overloads an assertion like this:

    EXPECT_EQ(CGRectMake(0, 0, 23, 24), rects[0].CGRectValue);

will produce output like this when it fails:

    Expected equality of these values:
      CGRectMake(138, 0, 23, 24)
        Which is: (origin = (x = 138, y = 0), size = (width = 23, height = 24))
      rects.firstObject.CGRectValue
        Which is: (origin = (x = 96, y = 0), size = (width = 16, height = 17))

* TestWebKitAPI/SourcesCocoa.txt:
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
Add source files.

* TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:
* TestWebKitAPI/Tests/WebKitCocoa/RequestTextInputContext.mm:
Write in terms of EXPECT_EQ() now that it just works when passed CGRects.

* TestWebKitAPI/cocoa/TestCocoa.h: Added.
* TestWebKitAPI/cocoa/TestCocoa.mm: Added.
(ostreamRectCommon):
(operator<<):
(operator==):

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

Tools/ChangeLog
Tools/TestWebKitAPI/SourcesCocoa.txt
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm
Tools/TestWebKitAPI/Tests/WebKitCocoa/RequestTextInputContext.mm
Tools/TestWebKitAPI/cocoa/TestCocoa.h [new file with mode: 0644]
Tools/TestWebKitAPI/cocoa/TestCocoa.mm [new file with mode: 0644]

index 65e4092..3c8f2df 100644 (file)
@@ -1,3 +1,40 @@
+2019-11-04  Daniel Bates  <dabates@apple.com>
+
+        Add Googletest assertion support for CGRect and NSRect
+        https://bugs.webkit.org/show_bug.cgi?id=203817
+
+        Reviewed by Myles C. Maxfield.
+
+        Add operator== and operator<< overloads for CGRect and NSRect so that these types can be
+        passed to EXPECT_EQ() and other Googletest assertions.
+
+        To make use of these overloads Cocoa tests should include #import "TestCocoa.h" instead of
+        "Test.h". TestCocoa.h imports Test.h. With these overloads an assertion like this:
+
+            EXPECT_EQ(CGRectMake(0, 0, 23, 24), rects[0].CGRectValue);
+
+        will produce output like this when it fails:
+
+            Expected equality of these values:
+              CGRectMake(138, 0, 23, 24)
+                Which is: (origin = (x = 138, y = 0), size = (width = 23, height = 24))
+              rects.firstObject.CGRectValue
+                Which is: (origin = (x = 96, y = 0), size = (width = 16, height = 17))
+
+        * TestWebKitAPI/SourcesCocoa.txt:
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        Add source files.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:
+        * TestWebKitAPI/Tests/WebKitCocoa/RequestTextInputContext.mm:
+        Write in terms of EXPECT_EQ() now that it just works when passed CGRects.
+
+        * TestWebKitAPI/cocoa/TestCocoa.h: Added.
+        * TestWebKitAPI/cocoa/TestCocoa.mm: Added.
+        (ostreamRectCommon):
+        (operator<<):
+        (operator==):
+
 2019-11-04  Aakash Jain  <aakash_jain@apple.com>
 
         [ews] Status bubble should be white for CANCELLED builds
index 7db4c39..044a665 100644 (file)
@@ -25,6 +25,7 @@ EditingTestHarness.mm
 WKWebViewConfigurationExtras.mm
 
 cocoa/PlatformUtilitiesCocoa.mm
+cocoa/TestCocoa.mm
 cocoa/TestNavigationDelegate.mm
 cocoa/TestProtocol.mm
 cocoa/TestWKWebView.mm
index 8e49b5f..553fc91 100644 (file)
                CE449E1021AE0F7200E7ADA1 /* WKWebViewFindString.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebViewFindString.mm; sourceTree = "<group>"; };
                CE4D5DE51F6743BA0072CFC6 /* StringWithDirection.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = StringWithDirection.cpp; sourceTree = "<group>"; };
                CE50D8C81C8665CE0072EA5A /* OptionSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OptionSet.cpp; sourceTree = "<group>"; };
+               CE640CA52370A4F300C5CAA4 /* TestCocoa.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = TestCocoa.h; path = cocoa/TestCocoa.h; sourceTree = "<group>"; };
+               CE640CA62370A4F300C5CAA4 /* TestCocoa.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = TestCocoa.mm; path = cocoa/TestCocoa.mm; sourceTree = "<group>"; };
                CE6E819F20A6935F00E2C80F /* SetTimeoutFunction.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SetTimeoutFunction.mm; sourceTree = "<group>"; };
                CE6E81A320A933B800E2C80F /* set-timeout-function.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = "set-timeout-function.html"; path = "ios/set-timeout-function.html"; sourceTree = SOURCE_ROOT; };
                CE78705C2107AB8C0053AC67 /* MoveOnlyLifecycleLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MoveOnlyLifecycleLogger.h; sourceTree = "<group>"; };
                                F44A530D21B8976900DBB99C /* InstanceMethodSwizzler.h */,
                                F44A531021B8976900DBB99C /* InstanceMethodSwizzler.mm */,
                                0F139E721A423A2B00F590F5 /* PlatformUtilitiesCocoa.mm */,
+                               CE640CA52370A4F300C5CAA4 /* TestCocoa.h */,
+                               CE640CA62370A4F300C5CAA4 /* TestCocoa.mm */,
                                5CE7594822A883A500C12409 /* TestContextMenuDriver.h */,
                                5CE7594722A883A500C12409 /* TestContextMenuDriver.mm */,
                                2D1C04A51D76298B000A6816 /* TestNavigationDelegate.h */,
index 271ce8c..abf3aaf 100644 (file)
@@ -28,7 +28,7 @@
 #if PLATFORM(IOS_FAMILY) && HAVE(UI_WK_DOCUMENT_CONTEXT)
 
 #import "PlatformUtilities.h"
-#import "Test.h"
+#import "TestCocoa.h"
 #import "TestNavigationDelegate.h"
 #import "TestWKWebView.h"
 #import "UIKitSPI.h"
     EXPECT_TRUE([actual isKindOfClass:[NSAttributedString class]]); \
     EXPECT_WK_STREQ(expected, [(NSAttributedString *)actual string]);
 
-#define EXPECT_RECT_EQ(xExpected, yExpected, widthExpected, heightExpected, rect) \
-    EXPECT_DOUBLE_EQ(xExpected, rect.origin.x); \
-    EXPECT_DOUBLE_EQ(yExpected, rect.origin.y); \
-    EXPECT_DOUBLE_EQ(widthExpected, rect.size.width); \
-    EXPECT_DOUBLE_EQ(heightExpected, rect.size.height);
-
 @interface WKContentView ()
 - (void)requestDocumentContext:(UIWKDocumentRequest *)request completionHandler:(void (^)(UIWKDocumentContext *))completionHandler;
 - (void)adjustSelectionWithDelta:(NSRange)deltaRange completionHandler:(void (^)(void))completionHandler;
@@ -210,10 +204,10 @@ TEST(WebKit, DocumentEditingContext)
         return [@(a.CGRectValue.origin.x) compare:@(b.CGRectValue.origin.x)];
     }];
     EXPECT_EQ(4UL, rects.count);
-    EXPECT_RECT_EQ(0, 0, 23, 24, rects[0].CGRectValue);
-    EXPECT_RECT_EQ(23, 0, 23, 24, rects[1].CGRectValue);
-    EXPECT_RECT_EQ(46, 0, 23, 24, rects[2].CGRectValue);
-    EXPECT_RECT_EQ(69, 0, 23, 24, rects[3].CGRectValue);
+    EXPECT_EQ(CGRectMake(0, 0, 23, 24), rects[0].CGRectValue);
+    EXPECT_EQ(CGRectMake(23, 0, 23, 24), rects[1].CGRectValue);
+    EXPECT_EQ(CGRectMake(46, 0, 23, 24), rects[2].CGRectValue);
+    EXPECT_EQ(CGRectMake(69, 0, 23, 24), rects[3].CGRectValue);
     rects = [context characterRectsForCharacterRange:NSMakeRange(5, 1)];
     EXPECT_EQ(0UL, rects.count);
 
@@ -221,16 +215,16 @@ TEST(WebKit, DocumentEditingContext)
     EXPECT_NSSTRING_EQ(" MMM", context.contextAfter);
     rects = [context characterRectsForCharacterRange:NSMakeRange(0, 1)];
     EXPECT_EQ(1UL, rects.count);
-    EXPECT_RECT_EQ(0, 0, 23, 24, rects.firstObject.CGRectValue);
+    EXPECT_EQ(CGRectMake(0, 0, 23, 24), rects.firstObject.CGRectValue);
     rects = [context characterRectsForCharacterRange:NSMakeRange(6, 1)];
     EXPECT_EQ(1UL, rects.count);
-    EXPECT_RECT_EQ(138, 0, 23, 24, rects.firstObject.CGRectValue);
+    EXPECT_EQ(CGRectMake(138, 0, 23, 24), rects.firstObject.CGRectValue);
 
     // Text Input Context
     [webView synchronouslyLoadHTMLString:applyStyle(@"<input type='text' style='width: 50px; height: 50px;' value='hello, world'>")];
     NSArray<_WKTextInputContext *> *textInputContexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, textInputContexts.count);
-    EXPECT_RECT_EQ(0, 0, 50, 50, textInputContexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 0, 50, 50), textInputContexts[0].boundingRect);
 
     context = [webView synchronouslyRequestDocumentContext:makeRequest(UIWKDocumentRequestText, UITextGranularityWord, 0, CGRectZero, textInputContexts[0])];
     EXPECT_NSSTRING_EQ("hello,", context.contextBefore);
index 85c3486..70b4711 100644 (file)
@@ -26,7 +26,7 @@
 #import "config.h"
 
 #import "PlatformUtilities.h"
-#import "Test.h"
+#import "TestCocoa.h"
 #import "TestNavigationDelegate.h"
 #import "TestWKWebView.h"
 #import <WebKit/WKPreferencesRefPrivate.h>
 #import <WebKit/_WKTextInputContext.h>
 #import <wtf/RetainPtr.h>
 
-#define EXPECT_RECT_EQ(xExpected, yExpected, widthExpected, heightExpected, rect) \
-    EXPECT_DOUBLE_EQ(xExpected, rect.origin.x); \
-    EXPECT_DOUBLE_EQ(yExpected, rect.origin.y); \
-    EXPECT_DOUBLE_EQ(widthExpected, rect.size.width); \
-    EXPECT_DOUBLE_EQ(heightExpected, rect.size.height);
-
 @implementation WKWebView (SynchronousTextInputContext)
 
 - (NSArray<_WKTextInputContext *> *)synchronouslyRequestTextInputContextsInRect:(CGRect)rect
@@ -92,34 +86,34 @@ TEST(WebKit, RequestTextInputContext)
     [webView synchronouslyLoadHTMLString:applyStyle(@"<input type='text' style='width: 50px; height: 50px;'>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 0, 50, 50, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 0, 50, 50), contexts[0].boundingRect);
 
     [webView synchronouslyLoadHTMLString:applyStyle(@"<textarea style='width: 100px; height: 100px;'></textarea>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 0, 100, 100, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 0, 100, 100), contexts[0].boundingRect);
 
     [webView synchronouslyLoadHTMLString:applyStyle(@"<div contenteditable style='width: 100px; height: 100px;'></div>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 0, 100, 100, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 0, 100, 100), contexts[0].boundingRect);
 
     // Basic inputs inside subframe.
 
     [webView synchronouslyLoadHTMLString:applyIframe(@"<input type='text' style='width: 50px; height: 50px;'>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 200, 50, 50, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 200, 50, 50), contexts[0].boundingRect);
 
     [webView synchronouslyLoadHTMLString:applyIframe(@"<textarea style='width: 100px; height: 100px;'></textarea>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 200, 100, 100, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 200, 100, 100), contexts[0].boundingRect);
 
     [webView synchronouslyLoadHTMLString:applyIframe(@"<div contenteditable style='width: 100px; height: 100px;'></div>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 200, 100, 100, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 200, 100, 100), contexts[0].boundingRect);
 
     // Read only inputs; should not be included.
 
@@ -158,23 +152,23 @@ TEST(WebKit, RequestTextInputContext)
 #endif
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 0, 50, 50, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 0, 50, 50), contexts[0].boundingRect);
 
     // Multiple inputs.
 
     [webView synchronouslyLoadHTMLString:applyStyle(@"<input type='text' style='width: 50px; height: 50px;'><br/><input type='text' style='width: 50px; height: 50px;'><br/><input type='text' style='width: 50px; height: 50px;'>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(3UL, contexts.count);
-    EXPECT_RECT_EQ(0, 0, 50, 50, contexts[0].boundingRect);
-    EXPECT_RECT_EQ(0, 50, 50, 50, contexts[1].boundingRect);
-    EXPECT_RECT_EQ(0, 100, 50, 50, contexts[2].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 0, 50, 50), contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 50, 50, 50), contexts[1].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 100, 50, 50), contexts[2].boundingRect);
 
     // Nested <input>-inside-contenteditable. Only the contenteditable is considered.
 
     [webView synchronouslyLoadHTMLString:applyStyle(@"<div contenteditable style='width: 100px; height: 100px;'><input type='text' style='width: 50px; height: 50px;'></div>")];
     contexts = [webView synchronouslyRequestTextInputContextsInRect:[webView frame]];
     EXPECT_EQ(1UL, contexts.count);
-    EXPECT_RECT_EQ(0, 0, 100, 100, contexts[0].boundingRect);
+    EXPECT_EQ(CGRectMake(0, 0, 100, 100), contexts[0].boundingRect);
 }
 
 TEST(WebKit, DISABLED_FocusTextInputContext)
diff --git a/Tools/TestWebKitAPI/cocoa/TestCocoa.h b/Tools/TestWebKitAPI/cocoa/TestCocoa.h
new file mode 100644 (file)
index 0000000..6e45395
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#pragma once
+
+#import "Test.h"
+
+#if USE(CG)
+
+std::ostream& operator<<(std::ostream&, const CGRect&);
+bool operator==(const CGRect&, const CGRect&);
+
+#endif
+
+#if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
+
+std::ostream& operator<<(std::ostream&, const NSRect&);
+bool operator==(const NSRect&, const NSRect&);
+
+#endif
diff --git a/Tools/TestWebKitAPI/cocoa/TestCocoa.mm b/Tools/TestWebKitAPI/cocoa/TestCocoa.mm
new file mode 100644 (file)
index 0000000..419bde2
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#import "config.h"
+#import "TestCocoa.h"
+
+template<typename T>
+static inline std::ostream& ostreamRectCommon(std::ostream& os, const T& rect)
+{
+    return os << "(origin = (x = " << rect.origin.x << ", y = " << rect.origin.y << "), size = (width = " << rect.size.width << ", height = " << rect.size.height << "))";
+}
+
+#if USE(CG)
+
+std::ostream& operator<<(std::ostream& os, const CGRect& rect)
+{
+    return ostreamRectCommon(os, rect);
+}
+
+bool operator==(const CGRect& a, const CGRect& b)
+{
+    return CGRectEqualToRect(a, b);
+}
+
+#endif
+
+#if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
+
+std::ostream& operator<<(std::ostream& os, const NSRect& rect)
+{
+    return ostreamRectCommon(os, rect);
+}
+
+bool operator==(const NSRect& a, const NSRect& b)
+{
+    return NSEqualRects(a, b);
+}
+
+#endif