TestDrawTargetBase.cpp (3455B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "TestDrawTargetBase.h" 8 #include <sstream> 9 10 using namespace mozilla; 11 using namespace mozilla::gfx; 12 13 TestDrawTargetBase::TestDrawTargetBase() { 14 REGISTER_TEST(TestDrawTargetBase, Initialized); 15 REGISTER_TEST(TestDrawTargetBase, FillCompletely); 16 REGISTER_TEST(TestDrawTargetBase, FillRect); 17 } 18 19 void TestDrawTargetBase::Initialized() { VERIFY(mDT); } 20 21 void TestDrawTargetBase::FillCompletely() { 22 mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), 23 ColorPattern(DeviceColor(0, 0.5f, 0, 1.0f))); 24 25 RefreshSnapshot(); 26 27 VerifyAllPixels(DeviceColor(0, 0.5f, 0, 1.0f)); 28 } 29 30 void TestDrawTargetBase::FillRect() { 31 mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), 32 ColorPattern(DeviceColor(0, 0.5f, 0, 1.0f))); 33 mDT->FillRect(Rect(50, 50, 50, 50), 34 ColorPattern(DeviceColor(0.5f, 0, 0, 1.0f))); 35 36 RefreshSnapshot(); 37 38 VerifyPixel(IntPoint(49, 49), DeviceColor(0, 0.5f, 0, 1.0f)); 39 VerifyPixel(IntPoint(50, 50), DeviceColor(0.5f, 0, 0, 1.0f)); 40 VerifyPixel(IntPoint(99, 99), DeviceColor(0.5f, 0, 0, 1.0f)); 41 VerifyPixel(IntPoint(100, 100), DeviceColor(0, 0.5f, 0, 1.0f)); 42 } 43 44 void TestDrawTargetBase::RefreshSnapshot() { 45 RefPtr<SourceSurface> snapshot = mDT->Snapshot(); 46 mDataSnapshot = snapshot->GetDataSurface(); 47 } 48 49 void TestDrawTargetBase::VerifyAllPixels(const DeviceColor& aColor) { 50 uint32_t* colVal = (uint32_t*)mDataSnapshot->GetData(); 51 52 uint32_t expected = RGBAPixelFromColor(aColor); 53 54 for (int y = 0; y < DT_HEIGHT; y++) { 55 for (int x = 0; x < DT_WIDTH; x++) { 56 if (colVal[y * (mDataSnapshot->Stride() / 4) + x] != expected) { 57 LogMessage("VerifyAllPixels Failed\n"); 58 mTestFailed = true; 59 return; 60 } 61 } 62 } 63 } 64 65 void TestDrawTargetBase::VerifyPixel(const IntPoint& aPoint, 66 mozilla::gfx::DeviceColor& aColor) { 67 uint32_t* colVal = (uint32_t*)mDataSnapshot->GetData(); 68 69 uint32_t expected = RGBAPixelFromColor(aColor); 70 uint32_t rawActual = 71 colVal[aPoint.y * (mDataSnapshot->Stride() / 4) + aPoint.x]; 72 73 if (rawActual != expected) { 74 stringstream message; 75 uint32_t actb = rawActual & 0xFF; 76 uint32_t actg = (rawActual & 0xFF00) >> 8; 77 uint32_t actr = (rawActual & 0xFF0000) >> 16; 78 uint32_t acta = (rawActual & 0xFF000000) >> 24; 79 uint32_t expb = expected & 0xFF; 80 uint32_t expg = (expected & 0xFF00) >> 8; 81 uint32_t expr = (expected & 0xFF0000) >> 16; 82 uint32_t expa = (expected & 0xFF000000) >> 24; 83 84 message << "Verify Pixel (" << aPoint.x << "x" << aPoint.y 85 << ") Failed." 86 " Expected (" 87 << expr << "," << expg << "," << expb << "," << expa 88 << ") " 89 " Got (" 90 << actr << "," << actg << "," << actb << "," << acta << ")\n"; 91 92 LogMessage(message.str()); 93 mTestFailed = true; 94 return; 95 } 96 } 97 98 uint32_t TestDrawTargetBase::RGBAPixelFromColor(const DeviceColor& aColor) { 99 return uint8_t((aColor.b * 255) + 0.5f) | 100 uint8_t((aColor.g * 255) + 0.5f) << 8 | 101 uint8_t((aColor.r * 255) + 0.5f) << 16 | 102 uint8_t((aColor.a * 255) + 0.5f) << 24; 103 }