michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestDrawTargetBase.h" michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::gfx; michael@0: using namespace std; michael@0: michael@0: TestDrawTargetBase::TestDrawTargetBase() michael@0: { michael@0: REGISTER_TEST(TestDrawTargetBase, Initialized); michael@0: REGISTER_TEST(TestDrawTargetBase, FillCompletely); michael@0: REGISTER_TEST(TestDrawTargetBase, FillRect); michael@0: } michael@0: michael@0: void michael@0: TestDrawTargetBase::Initialized() michael@0: { michael@0: VERIFY(mDT); michael@0: } michael@0: michael@0: void michael@0: TestDrawTargetBase::FillCompletely() michael@0: { michael@0: mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), ColorPattern(Color(0, 0.5f, 0, 1.0f))); michael@0: michael@0: RefreshSnapshot(); michael@0: michael@0: VerifyAllPixels(Color(0, 0.5f, 0, 1.0f)); michael@0: } michael@0: michael@0: void michael@0: TestDrawTargetBase::FillRect() michael@0: { michael@0: mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), ColorPattern(Color(0, 0.5f, 0, 1.0f))); michael@0: mDT->FillRect(Rect(50, 50, 50, 50), ColorPattern(Color(0.5f, 0, 0, 1.0f))); michael@0: michael@0: RefreshSnapshot(); michael@0: michael@0: VerifyPixel(IntPoint(49, 49), Color(0, 0.5f, 0, 1.0f)); michael@0: VerifyPixel(IntPoint(50, 50), Color(0.5f, 0, 0, 1.0f)); michael@0: VerifyPixel(IntPoint(99, 99), Color(0.5f, 0, 0, 1.0f)); michael@0: VerifyPixel(IntPoint(100, 100), Color(0, 0.5f, 0, 1.0f)); michael@0: } michael@0: michael@0: void michael@0: TestDrawTargetBase::RefreshSnapshot() michael@0: { michael@0: RefPtr snapshot = mDT->Snapshot(); michael@0: mDataSnapshot = snapshot->GetDataSurface(); michael@0: } michael@0: michael@0: void michael@0: TestDrawTargetBase::VerifyAllPixels(const Color &aColor) michael@0: { michael@0: uint32_t *colVal = (uint32_t*)mDataSnapshot->GetData(); michael@0: michael@0: uint32_t expected = RGBAPixelFromColor(aColor); michael@0: michael@0: for (int y = 0; y < DT_HEIGHT; y++) { michael@0: for (int x = 0; x < DT_WIDTH; x++) { michael@0: if (colVal[y * (mDataSnapshot->Stride() / 4) + x] != expected) { michael@0: LogMessage("VerifyAllPixels Failed\n"); michael@0: mTestFailed = true; michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: TestDrawTargetBase::VerifyPixel(const IntPoint &aPoint, mozilla::gfx::Color &aColor) michael@0: { michael@0: uint32_t *colVal = (uint32_t*)mDataSnapshot->GetData(); michael@0: michael@0: uint32_t expected = RGBAPixelFromColor(aColor); michael@0: uint32_t rawActual = colVal[aPoint.y * (mDataSnapshot->Stride() / 4) + aPoint.x]; michael@0: michael@0: if (rawActual != expected) { michael@0: stringstream message; michael@0: uint32_t actb = rawActual & 0xFF; michael@0: uint32_t actg = (rawActual & 0xFF00) >> 8; michael@0: uint32_t actr = (rawActual & 0xFF0000) >> 16; michael@0: uint32_t acta = (rawActual & 0xFF000000) >> 24; michael@0: uint32_t expb = expected & 0xFF; michael@0: uint32_t expg = (expected & 0xFF00) >> 8; michael@0: uint32_t expr = (expected & 0xFF0000) >> 16; michael@0: uint32_t expa = (expected & 0xFF000000) >> 24; michael@0: michael@0: message << "Verify Pixel (" << aPoint.x << "x" << aPoint.y << ") Failed." michael@0: " Expected (" << expr << "," << expg << "," << expb << "," << expa << ") " michael@0: " Got (" << actr << "," << actg << "," << actb << "," << acta << ")\n"; michael@0: michael@0: LogMessage(message.str()); michael@0: mTestFailed = true; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: uint32_t michael@0: TestDrawTargetBase::RGBAPixelFromColor(const Color &aColor) michael@0: { michael@0: return uint8_t((aColor.b * 255) + 0.5f) | uint8_t((aColor.g * 255) + 0.5f) << 8 | michael@0: uint8_t((aColor.r * 255) + 0.5f) << 16 | uint8_t((aColor.a * 255) + 0.5f) << 24; michael@0: }