Skip to content

Commit

Permalink
Fix CT drawing tests on ARM (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
aballway authored and Raj Seshasankaran committed Jan 24, 2017
1 parent 75effb9 commit f194201
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 61 deletions.
23 changes: 16 additions & 7 deletions tests/UnitTests/CoreGraphics.drawing/DrawingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void testing::DrawTest<TComparator>::TearDown() {
if (delta.result == ImageComparisonResult::Incomparable) {
ADD_FAILURE() << "images are incomparable due to a mismatch in dimensions, presence, or byte length";
} else {
ADD_FAILURE() << "images differ nontrivially";
ADD_FAILURE() << "images differ nontrivially with " << delta.differences << " registered differences";
}

woc::unique_cf<CFStringRef> deltaFilename{
Expand Down Expand Up @@ -205,9 +205,18 @@ void UIKitMimicTest<TComparator>::SetUpContext() {
template class ::testing::DrawTest<>;
template class WhiteBackgroundTest<>;
template class UIKitMimicTest<>;
template class ::testing::DrawTest<PixelByPixelImageComparator<ComparisonMode::Mask>>;
template class WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>;
template class UIKitMimicTest<PixelByPixelImageComparator<ComparisonMode::Mask>>;
template class ::testing::DrawTest<PixelByPixelImageComparator<ComparisonMode::Mask, 1024>>;
template class WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask, 1024>>;
template class UIKitMimicTest<PixelByPixelImageComparator<ComparisonMode::Mask, 1024>>;
template class ::testing::DrawTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>;
template class WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>;
template class UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>;
template class ::testing::DrawTest<PixelByPixelImageComparator<PixelComparisonModeMask<2300>>>;
template class ::testing::DrawTest<PixelByPixelImageComparator<PixelComparisonModeMask<1024>>>;
template class ::testing::DrawTest<PixelByPixelImageComparator<PixelComparisonModeMask<512>>>;
template class ::testing::DrawTest<PixelByPixelImageComparator<PixelComparisonModeMask<64>>>;
template class WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<2300>>>;
template class WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<1024>>>;
template class WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<512>>>;
template class WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<64>>>;
template class UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<2300>>>;
template class UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<1024>>>;
template class UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<512>>>;
template class UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<64>>>;
2 changes: 1 addition & 1 deletion tests/UnitTests/CoreGraphics.drawing/DrawingTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// Due to how templates are compiled as needed, any new usage of templates needs to be "forced" in DrawingTest.cpp

namespace testing {
template <typename TComparator = PixelByPixelImageComparator<ComparisonMode::Exact>>
template <typename TComparator = PixelByPixelImageComparator<>>
class DrawTest : public ::testing::Test {
private:
woc::unique_cf<CGContextRef> _context;
Expand Down
12 changes: 6 additions & 6 deletions tests/unittests/CoreGraphics.drawing/CGTextDrawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ static void __SetFontForContext(CGContextRef context) {
CGContextSetFontSize(context, 144);
}

TEXT_DRAW_TEST_F(CGContext, ShowGlyphs, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CGContext, ShowGlyphs, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
std::vector<CGGlyph> glyphs{ __CreateGlyphs() };
__SetFontForContext(context);
CGContextSetTextPosition(context, 25, 50);
CGContextShowGlyphs(context, glyphs.data(), glyphs.size());
}

TEXT_DRAW_TEST_F(CGContext, ShowGlyphsAtPoint, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CGContext, ShowGlyphsAtPoint, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
std::vector<CGGlyph> glyphs{ __CreateGlyphs() };
__SetFontForContext(context);
CGContextShowGlyphsAtPoint(context, 25, 50, glyphs.data(), glyphs.size());
}

TEXT_DRAW_TEST_F(CGContext, DrawWithRotatedTextMatrix, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CGContext, DrawWithRotatedTextMatrix, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
std::vector<CGGlyph> glyphs{ __CreateGlyphs() };
__SetFontForContext(context);
Expand All @@ -68,7 +68,7 @@ static const CGAffineTransform c_transforms[] = { CGAffineTransformMakeRotation(
{ 2, 2, 1.75, 2, 0, 0 },
CGAffineTransformIdentity };

class CGTransform : public WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>,
class CGTransform : public WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>,
public ::testing::WithParamInterface<::testing::tuple<CGAffineTransform, CGAffineTransform>> {};

TEXT_DRAW_TEST_P(CGTransform, TestMatrices) {
Expand All @@ -87,7 +87,7 @@ INSTANTIATE_TEST_CASE_P(TestDrawingTextWithTransformedMatrices,
CGTransform,
::testing::Combine(::testing::ValuesIn(c_transforms), ::testing::ValuesIn(c_transforms)));

class CGUIKitTransform : public UIKitMimicTest<PixelByPixelImageComparator<ComparisonMode::Mask>>,
class CGUIKitTransform : public UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>,
public ::testing::WithParamInterface<::testing::tuple<CGAffineTransform, CGAffineTransform>> {};

TEXT_DRAW_TEST_P(CGUIKitTransform, TestMatrices) {
Expand All @@ -109,7 +109,7 @@ INSTANTIATE_TEST_CASE_P(TestDrawingTextWithTransformedMatrices,
// On reference platform, CGContextShowText* can only be used with CGContextSelectFont
// Which we do not currently support.
#ifdef WINOBJC
TEXT_DRAW_TEST_F(CGContext, ShowTextAtPoint, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CGContext, ShowTextAtPoint, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
__SetFontForContext(context);
CGContextShowTextAtPoint(context, 25, 50, "TEST", 4);
Expand Down
28 changes: 14 additions & 14 deletions tests/unittests/CoreGraphics.drawing/CTDrawingTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static NSURL* __GetURLFromPathRelativeToModuleDirectory(NSString* relativePath)
}
#endif // WINOBJC

TEXT_DRAW_TEST_F(CTFrame, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CTFrame, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

Expand Down Expand Up @@ -65,7 +65,7 @@ TEXT_DRAW_TEST_F(CTFrame, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImag
CTFrameDraw(frame.get(), context);
}

TEXT_DRAW_TEST_F(CTFrame, BasicUIKitMimicDrawingTest, UIKitMimicTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CTFrame, BasicUIKitMimicDrawingTest, UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

Expand Down Expand Up @@ -104,7 +104,7 @@ TEXT_DRAW_TEST_F(CTFrame, BasicUIKitMimicDrawingTest, UIKitMimicTest<PixelByPixe
CTFrameDraw(frame.get(), context);
}

TEXT_DRAW_TEST_F(CTFrame, BasicUnicodeTest, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CTFrame, BasicUnicodeTest, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

Expand Down Expand Up @@ -133,7 +133,7 @@ TEXT_DRAW_TEST_F(CTFrame, BasicUnicodeTest, WhiteBackgroundTest<PixelByPixelImag
&kCFTypeDictionaryValueCallBacks) };

woc::unique_cf<CFAttributedStringRef> attrString{
CFAttributedStringCreate(nullptr, CFSTR("он прошел այն անցավ ມັນຜ່ານໄປ ਇਸ ਨੂੰ ਪਾਸ ਕੀਤਾ 它通過了 그것이 통과했다 minęło"), dict.get())
CFAttributedStringCreate(nullptr, CFSTR("он прошел այն անցավ ມັນຜ່ານໄປ minęło"), dict.get())
};

woc::unique_cf<CTFramesetterRef> framesetter{ CTFramesetterCreateWithAttributedString(attrString.get()) };
Expand All @@ -145,7 +145,7 @@ TEXT_DRAW_TEST_F(CTFrame, BasicUnicodeTest, WhiteBackgroundTest<PixelByPixelImag
CTFrameDraw(frame.get(), context);
}

class CTFrame : public WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>,
class CTFrame : public WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>,
public ::testing::WithParamInterface<::testing::tuple<CTTextAlignment, CTLineBreakMode, CTWritingDirection, CGFloat>> {
CFStringRef CreateOutputFilename() {
CTTextAlignment alignment = ::testing::get<0>(GetParam());
Expand Down Expand Up @@ -237,7 +237,7 @@ INSTANTIATE_TEST_CASE_P(TestAlignmentLineBreakMode,
::testing::ValuesIn(c_writingDirections),
::testing::ValuesIn(c_fontSizes)));

class Transform : public WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask, 1024>>,
class Transform : public WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<1024>>>,
public ::testing::WithParamInterface<::testing::tuple<CGAffineTransform, CGAffineTransform>> {};

TEXT_DRAW_TEST_P(Transform, TestMatrices) {
Expand Down Expand Up @@ -292,7 +292,7 @@ INSTANTIATE_TEST_CASE_P(TestDrawingTextWithTransformedMatrices,
Transform,
::testing::Combine(::testing::ValuesIn(c_transforms), ::testing::ValuesIn(c_transforms)));

class UIKitTransform : public UIKitMimicTest<PixelByPixelImageComparator<ComparisonMode::Mask>>,
class UIKitTransform : public UIKitMimicTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>,
public ::testing::WithParamInterface<::testing::tuple<CGAffineTransform, CGAffineTransform>> {};

TEXT_DRAW_TEST_P(UIKitTransform, TestMatrices) {
Expand Down Expand Up @@ -335,7 +335,7 @@ INSTANTIATE_TEST_CASE_P(TestDrawingUITransforms,
UIKitTransform,
::testing::Combine(::testing::ValuesIn(c_transforms), ::testing::ValuesIn(c_transforms)));

class ExtraKerning : public WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>,
class ExtraKerning : public WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>,
public ::testing::WithParamInterface<CGFloat> {
CFStringRef CreateOutputFilename() {
CGFloat extraKerning = GetParam();
Expand Down Expand Up @@ -373,7 +373,7 @@ TEXT_DRAW_TEST_P(ExtraKerning, TestExtraKerning) {
static constexpr CGFloat c_extraKernings[] = { -1.0, 1.0, 5.25, 25.75 };
INSTANTIATE_TEST_CASE_P(TestDrawingTextInExtraKerning, ExtraKerning, ::testing::ValuesIn(c_extraKernings));

class LineHeightMultiple : public WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>,
class LineHeightMultiple : public WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>,
public ::testing::WithParamInterface<CGFloat> {
CFStringRef CreateOutputFilename() {
CGFloat lineHeightMultiple = GetParam();
Expand Down Expand Up @@ -412,7 +412,7 @@ TEXT_DRAW_TEST_P(LineHeightMultiple, TestLineHeightMultiple) {
static constexpr CGFloat c_lineHeightMultiples[] = { -1.0, .75, 1.25 };
INSTANTIATE_TEST_CASE_P(TestDrawingTextInLineHeightMultiple, LineHeightMultiple, ::testing::ValuesIn(c_lineHeightMultiples));

TEXT_DRAW_TEST_F(CTRun, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CTRun, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

Expand Down Expand Up @@ -440,7 +440,7 @@ TEXT_DRAW_TEST_F(CTRun, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImageC
CTLineDraw(line.get(), context);
}

TEXT_DRAW_TEST_F(CTLine, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CTLine, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<>>>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

Expand Down Expand Up @@ -470,7 +470,7 @@ TEXT_DRAW_TEST_F(CTLine, BasicDrawingTest, WhiteBackgroundTest<PixelByPixelImage
CTRunDraw(run, context, {});
}

class Fonts : public WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>,
class Fonts : public WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<2300>>>,
public ::testing::WithParamInterface<CFStringRef> {
CFStringRef CreateOutputFilename() {
CFStringRef fontName = GetParam();
Expand Down Expand Up @@ -508,7 +508,7 @@ static CFStringRef c_fontNames[] = { CFSTR("Arial"), CFSTR("Times New Roman"), C
INSTANTIATE_TEST_CASE_P(TestDrawingTextInFonts, Fonts, ::testing::ValuesIn(c_fontNames));

#ifdef WINOBJC
TEXT_DRAW_TEST_F(CTFontManager, DrawWithCustomFont, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CTFontManager, DrawWithCustomFont, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<512>>>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

Expand Down Expand Up @@ -555,7 +555,7 @@ TEXT_DRAW_TEST_F(CTFontManager, DrawWithCustomFont, WhiteBackgroundTest<PixelByP
}
#endif // WINOBJC

TEXT_DRAW_TEST_F(CTFont, DrawGlyphs, WhiteBackgroundTest<PixelByPixelImageComparator<ComparisonMode::Mask>>) {
TEXT_DRAW_TEST_F(CTFont, DrawGlyphs, WhiteBackgroundTest<PixelByPixelImageComparator<PixelComparisonModeMask<64>>>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

Expand Down
47 changes: 18 additions & 29 deletions tests/unittests/CoreGraphics.drawing/ImageComparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,14 @@ struct bgraPixel {
uint8_t b, g, r, a;
};

struct rgbaPixel {
uint8_t r, g, b, a;
};

template <typename T, typename U>
bool operator==(const T& t, const U& u) {
return t.r == u.r && t.g == u.g && t.b == u.b && t.a == u.a;
}

template <ComparisonMode Mode>
struct __comparePixels {
template <typename LP, typename RP>
rgbaPixel operator()(const LP& background, const LP& bp, const RP& cp, size_t& npxchg);
};

template <>
struct __comparePixels<ComparisonMode::Exact> {
template <typename LP, typename RP>
rgbaPixel operator()(const LP& background, const LP& bp, const RP& cp, size_t& npxchg) {
template <size_t FailureThreshold>
template <typename LP, typename RP>
rgbaPixel PixelComparisonModeExact<FailureThreshold>::ComparePixels(const LP& background, const LP& bp, const RP& cp, size_t& npxchg) {
rgbaPixel gp{};
if (!(bp == cp)) {
++npxchg;
Expand All @@ -61,13 +50,11 @@ struct __comparePixels<ComparisonMode::Exact> {
}

return gp;
}
};
}

template <>
struct __comparePixels<ComparisonMode::Mask> {
template <typename LP, typename RP>
rgbaPixel operator()(const LP& background, const LP& bp, const RP& cp, size_t& npxchg) {
template <size_t FailureThreshold>
template <typename LP, typename RP>
rgbaPixel PixelComparisonModeMask<FailureThreshold>::ComparePixels(const LP& background, const LP& bp, const RP& cp, size_t& npxchg) {
rgbaPixel gp{};
if (!(bp == cp)) {
++npxchg;
Expand All @@ -90,11 +77,10 @@ struct __comparePixels<ComparisonMode::Mask> {
}

return gp;
}
};
}

template <ComparisonMode Mode, size_t FailureThreshold>
ImageDelta PixelByPixelImageComparator<Mode, FailureThreshold>::CompareImages(CGImageRef left, CGImageRef right) {
template <typename PixelComparisonMode>
ImageDelta PixelByPixelImageComparator<PixelComparisonMode>::CompareImages(CGImageRef left, CGImageRef right) {
if (!left || !right) {
return { ImageComparisonResult::Incomparable };
}
Expand Down Expand Up @@ -133,12 +119,12 @@ ImageDelta PixelByPixelImageComparator<Mode, FailureThreshold>::CompareImages(CG
auto background = leftPixels[0];

size_t npxchg = 0;
__comparePixels<Mode> pixelComparitor{};
PixelComparisonMode mode;
for (off_t i = 0; i < leftLength / sizeof(rgbaPixel); ++i) {
auto& bp = leftPixels[i];
auto& cp = rightPixels[i];
auto& gp = deltaPixels[i];
gp = pixelComparitor(background, bp, cp, npxchg);
gp = mode.ComparePixels(background, bp, cp, npxchg);
}

woc::unique_cf<CFDataRef> deltaData{ CFDataCreateWithBytesNoCopy(nullptr, deltaBuffer.release(), leftLength, kCFAllocatorDefault) };
Expand All @@ -157,11 +143,14 @@ ImageDelta PixelByPixelImageComparator<Mode, FailureThreshold>::CompareImages(CG
kCGRenderingIntentDefault) };

return {
(npxchg < FailureThreshold ? ImageComparisonResult::Same : ImageComparisonResult::Different), npxchg, deltaImage.get(),
(npxchg < PixelComparisonMode::Threshold ? ImageComparisonResult::Same : ImageComparisonResult::Different), npxchg, deltaImage.get(),
};
}

// Force templates so they compile
template class PixelByPixelImageComparator<>;
template class PixelByPixelImageComparator<ComparisonMode::Mask>;
template class PixelByPixelImageComparator<ComparisonMode::Mask, 1024>;
template class PixelByPixelImageComparator<PixelComparisonModeMask<>>;
template class PixelByPixelImageComparator<PixelComparisonModeMask<2300>>;
template class PixelByPixelImageComparator<PixelComparisonModeMask<1024>>;
template class PixelByPixelImageComparator<PixelComparisonModeMask<512>>;
template class PixelByPixelImageComparator<PixelComparisonModeMask<64>>;
20 changes: 18 additions & 2 deletions tests/unittests/CoreGraphics.drawing/ImageComparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@

enum class ImageComparisonResult : unsigned int { Unknown = 0, Incomparable, Different, Same };

enum struct ComparisonMode { Exact, Mask };
struct rgbaPixel {
uint8_t r, g, b, a;
};

template <size_t FailureThreshold = 1>
struct PixelComparisonModeExact {
static constexpr size_t Threshold = FailureThreshold;
template <typename LP, typename RP>
rgbaPixel ComparePixels(const LP& background, const LP& bp, const RP& cp, size_t& npxchg);
};

template <size_t FailureThreshold = 1>
struct PixelComparisonModeMask {
static constexpr size_t Threshold = FailureThreshold;
template <typename LP, typename RP>
rgbaPixel ComparePixels(const LP& background, const LP& bp, const RP& cp, size_t& npxchg);
};

struct ImageDelta {
ImageComparisonResult result;
Expand All @@ -41,7 +57,7 @@ class ImageComparator {
virtual ImageDelta CompareImages(CGImageRef left, CGImageRef right) = 0;
};

template <ComparisonMode Mode = ComparisonMode::Exact, size_t FailureThreshold = 1>
template <typename PixelComparisonMode = PixelComparisonModeExact<1>>
class PixelByPixelImageComparator : public ImageComparator {
public:
ImageDelta CompareImages(CGImageRef left, CGImageRef right) override;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f194201

Please sign in to comment.