Bug 1742006 - Fix documents of rect types' Union and SaturatingUnion, and add a test. r=gfx-reviewers,mstange

The documents of Union and SaturatingUnion promise that |this| will be return
when both rects are empty, but the current implementation returns aRect instead.

This patch fixes the documents and added a test case.

Differential Revision: https://phabricator.services.mozilla.com/D131568
This commit is contained in:
Ting-Yu Lin 2021-11-22 03:26:19 +00:00
Родитель c3b1ed251a
Коммит 570a5436c4
4 изменённых файлов: 29 добавлений и 6 удалений

Просмотреть файл

@ -174,9 +174,8 @@ struct BaseRect {
}
// Returns the smallest rectangle that contains both the area of both
// this and aRect2.
// Thus, empty input rectangles are ignored.
// If both rectangles are empty, returns this.
// this and aRect. Thus, empty input rectangles are ignored.
// Note: if both rectangles are empty, returns aRect.
// WARNING! This is not safe against overflow, prefer using SafeUnion instead
// when dealing with int-based rects.
[[nodiscard]] Sub Union(const Sub& aRect) const {

Просмотреть файл

@ -142,9 +142,8 @@ struct BaseRectAbsolute {
void SetEmpty() { left = right = top = bottom = 0; }
// Returns the smallest rectangle that contains both the area of both
// this and aRect2.
// Thus, empty input rectangles are ignored.
// If both rectangles are empty, returns this.
// this and aRect. Thus, empty input rectangles are ignored.
// Note: if both rectangles are empty, returns aRect.
// WARNING! This is not safe against overflow, prefer using SafeUnion instead
// when dealing with int-based rects.
[[nodiscard]] Sub Union(const Sub& aRect) const {

Просмотреть файл

@ -54,6 +54,9 @@ struct nsRect : public mozilla::gfx::BaseRect<nscoord, nsRect, nsPoint, nsSize,
// overflowing nscoord values in the 'width' and 'height' fields by
// clamping the width and height values to nscoord_MAX if necessary.
// Returns the smallest rectangle that contains both the area of both
// this and aRect. Thus, empty input rectangles are ignored.
// Note: if both rectangles are empty, returns aRect.
[[nodiscard]] nsRect SaturatingUnion(const nsRect& aRect) const {
if (IsEmpty()) {
return aRect;

Просмотреть файл

@ -9,6 +9,7 @@
#include "gfxTypes.h"
#include "nsRect.h"
#include "nsRectAbsolute.h"
#include "gfxRect.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/gfx/RectAbsolute.h"
@ -435,6 +436,18 @@ static bool TestUnion() {
return true;
}
template <class RectType>
static void TestUnionEmptyRects() {
RectType rect1(10, 10, 0, 50);
RectType rect2(5, 5, 40, 0);
EXPECT_TRUE(rect1.IsEmpty() && rect2.IsEmpty());
RectType dest = rect1.Union(rect2);
EXPECT_TRUE(dest.IsEmpty() && dest.IsEqualEdges(rect2))
<< "Test the case where both rects are empty, and the result is the "
"same value passing into Union()";
}
static bool TestFiniteGfx() {
float posInf = std::numeric_limits<float>::infinity();
float negInf = -std::numeric_limits<float>::infinity();
@ -578,6 +591,7 @@ TEST(Gfx, nsRect)
TestIntersects<nsRect>();
TestIntersection<nsRect>();
TestUnion<nsRect>();
TestUnionEmptyRects<nsRect>();
TestBug1135677<nsRect>();
TestSetWH<nsRect>();
TestSwap<nsRect>();
@ -591,6 +605,7 @@ TEST(Gfx, nsIntRect)
TestIntersects<nsIntRect>();
TestIntersection<nsIntRect>();
TestUnion<nsIntRect>();
TestUnionEmptyRects<nsIntRect>();
TestBug1135677<nsIntRect>();
TestSetWH<nsIntRect>();
TestSwap<nsIntRect>();
@ -604,12 +619,19 @@ TEST(Gfx, gfxRect)
TestIntersects<gfxRect>();
TestIntersection<gfxRect>();
TestUnion<gfxRect>();
TestUnionEmptyRects<gfxRect>();
TestBug1135677<gfxRect>();
TestFiniteGfx();
TestSetWH<gfxRect>();
TestSwap<gfxRect>();
}
TEST(Gfx, nsRectAbsolute)
{ TestUnionEmptyRects<nsRectAbsolute>(); }
TEST(Gfx, IntRectAbsolute)
{ TestUnionEmptyRects<IntRectAbsolute>(); }
static void TestMoveInsideAndClamp(IntRect aSrc, IntRect aTarget,
IntRect aExpected) {
// Test the implementation in BaseRect (x/y/width/height representation)