Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ea629288 authored by Lloyd Pique's avatar Lloyd Pique
Browse files

libui: PrintTo and comparisons for values for tests

Define `PrintTo` and (except for Region) ensure there is an appropriate
`operator==`. The Google Test framework will uses `PrintTo` to print out
values if available, and uses operator == to check if values match
expected values.

The following value types were modified:

  * Rect
  * FloatRect
  * Region
  * Transform

With these definitions, the Composition Engine tests no longer need
custom matchers to print/compare values (except for Region).

For Region it seemed unwise to define a general `operator==` as equality
of Regions might mean different things to different callers. Instead
this change just adds a `Region::hasSameRects(const Region& other)`
function to do the fast check that the tests were doing. As such a
matcher helper is still needed which calls that function, though now it
is much more trivial to write.

Bug: None
Test: libcompositionengine_test
Change-Id: I160cda695d99257966634e767ee1164bc6105e30
parent b35b6c42
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -280,6 +280,20 @@ bool Region::isTriviallyEqual(const Region& region) const {
    return begin() == region.begin();
}

bool Region::hasSameRects(const Region& other) const {
    size_t thisRectCount = 0;
    android::Rect const* thisRects = getArray(&thisRectCount);
    size_t otherRectCount = 0;
    android::Rect const* otherRects = other.getArray(&otherRectCount);

    if (thisRectCount != otherRectCount) return false;

    for (size_t i = 0; i < thisRectCount; i++) {
        if (thisRects[i] != otherRects[i]) return false;
    }
    return true;
}

// ----------------------------------------------------------------------------

void Region::addRectUnchecked(int l, int t, int r, int b)
+9 −0
Original line number Diff line number Diff line
@@ -49,6 +49,15 @@ bool Transform::absIsOne(float f) {
    return isZero(fabs(f) - 1.0f);
}

bool Transform::operator==(const Transform& other) const {
    return mMatrix[0][0] == other.mMatrix[0][0] && mMatrix[0][1] == other.mMatrix[0][1] &&
            mMatrix[0][2] == other.mMatrix[0][2] && mMatrix[1][0] == other.mMatrix[1][0] &&
            mMatrix[1][1] == other.mMatrix[1][1] && mMatrix[1][2] == other.mMatrix[1][2] &&
            mMatrix[2][0] == other.mMatrix[2][0] && mMatrix[2][1] == other.mMatrix[2][1] &&
            mMatrix[2][2] == other.mMatrix[2][2];
    ;
}

Transform Transform::operator * (const Transform& rhs) const
{
    if (CC_LIKELY(mType == IDENTITY))
+7 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#pragma once

#include <ostream>

namespace android {

class FloatRect {
@@ -52,4 +54,9 @@ inline bool operator==(const FloatRect& a, const FloatRect& b) {
    return a.left == b.left && a.top == b.top && a.right == b.right && a.bottom == b.bottom;
}

static inline void PrintTo(const FloatRect& rect, ::std::ostream* os) {
    *os << "FloatRect(" << rect.left << ", " << rect.top << ", " << rect.right << ", "
        << rect.bottom << ")";
}

}  // namespace android
+8 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#ifndef ANDROID_UI_RECT
#define ANDROID_UI_RECT

#include <ostream>

#include <utils/Flattenable.h>
#include <utils/Log.h>
#include <utils/TypeHelpers.h>
@@ -214,6 +216,12 @@ public:
    }
};

// Defining PrintTo helps with Google Tests.
static inline void PrintTo(const Rect& rect, ::std::ostream* os) {
    *os << "Rect(" << rect.left << ", " << rect.top << ", " << rect.right << ", " << rect.bottom
        << ")";
}

ANDROID_BASIC_TYPES_TRAITS(Rect)

}; // namespace android
+18 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <stdint.h>
#include <sys/types.h>
#include <ostream>

#include <utils/Vector.h>

@@ -119,6 +120,8 @@ public:
    // returns true if the regions share the same underlying storage
    bool isTriviallyEqual(const Region& region) const;

    // returns true if the regions consist of the same rectangle sequence
    bool hasSameRects(const Region& region) const;

    /* various ways to access the rectangle list */

@@ -213,6 +216,21 @@ Region& Region::operator -= (const Region& rhs) {
Region& Region::operator += (const Point& pt) {
    return translateSelf(pt.x, pt.y);
}

// Defining PrintTo helps with Google Tests.
static inline void PrintTo(const Region& region, ::std::ostream* os) {
    Region::const_iterator head = region.begin();
    Region::const_iterator const tail = region.end();
    bool first = true;
    while (head != tail) {
        *os << (first ? "Region(" : ", ");
        PrintTo(*head, os);
        head++;
        first = false;
    }
    *os << ")";
}

// ---------------------------------------------------------------------------
}; // namespace android

Loading