gecko/gfx/2d/unittest/TestDrawTargetBase.cpp

142 lines
4.7 KiB
C++

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Corporation code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Bas Schouten <bschouten@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "TestDrawTargetBase.h"
#include <sstream>
using namespace mozilla;
using namespace mozilla::gfx;
using namespace std;
TestDrawTargetBase::TestDrawTargetBase()
{
REGISTER_TEST(TestDrawTargetBase, Initialized);
REGISTER_TEST(TestDrawTargetBase, FillCompletely);
REGISTER_TEST(TestDrawTargetBase, FillRect);
}
void
TestDrawTargetBase::Initialized()
{
VERIFY(mDT);
}
void
TestDrawTargetBase::FillCompletely()
{
mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), ColorPattern(Color(0, 0.5f, 0, 1.0f)));
RefreshSnapshot();
VerifyAllPixels(Color(0, 0.5f, 0, 1.0f));
}
void
TestDrawTargetBase::FillRect()
{
mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), ColorPattern(Color(0, 0.5f, 0, 1.0f)));
mDT->FillRect(Rect(50, 50, 50, 50), ColorPattern(Color(0.5f, 0, 0, 1.0f)));
RefreshSnapshot();
VerifyPixel(IntPoint(49, 49), Color(0, 0.5f, 0, 1.0f));
VerifyPixel(IntPoint(50, 50), Color(0.5f, 0, 0, 1.0f));
VerifyPixel(IntPoint(99, 99), Color(0.5f, 0, 0, 1.0f));
VerifyPixel(IntPoint(100, 100), Color(0, 0.5f, 0, 1.0f));
}
void
TestDrawTargetBase::RefreshSnapshot()
{
RefPtr<SourceSurface> snapshot = mDT->Snapshot();
mDataSnapshot = snapshot->GetDataSurface();
}
void
TestDrawTargetBase::VerifyAllPixels(const Color &aColor)
{
uint32_t *colVal = (uint32_t*)mDataSnapshot->GetData();
uint32_t expected = RGBAPixelFromColor(aColor);
for (int y = 0; y < DT_HEIGHT; y++) {
for (int x = 0; x < DT_WIDTH; x++) {
if (colVal[y * (mDataSnapshot->Stride() / 4) + x] != expected) {
LogMessage("VerifyAllPixels Failed\n");
mTestFailed = true;
return;
}
}
}
}
void
TestDrawTargetBase::VerifyPixel(const IntPoint &aPoint, mozilla::gfx::Color &aColor)
{
uint32_t *colVal = (uint32_t*)mDataSnapshot->GetData();
uint32_t expected = RGBAPixelFromColor(aColor);
uint32_t rawActual = colVal[aPoint.y * (mDataSnapshot->Stride() / 4) + aPoint.x];
if (rawActual != expected) {
stringstream message;
uint32_t actb = rawActual & 0xFF;
uint32_t actg = (rawActual & 0xFF00) >> 8;
uint32_t actr = (rawActual & 0xFF0000) >> 16;
uint32_t acta = (rawActual & 0xFF000000) >> 24;
uint32_t expb = expected & 0xFF;
uint32_t expg = (expected & 0xFF00) >> 8;
uint32_t expr = (expected & 0xFF0000) >> 16;
uint32_t expa = (expected & 0xFF000000) >> 24;
message << "Verify Pixel (" << aPoint.x << "x" << aPoint.y << ") Failed."
" Expected (" << expr << "," << expg << "," << expb << "," << expa << ") "
" Got (" << actr << "," << actg << "," << actb << "," << acta << ")\n";
LogMessage(message.str());
mTestFailed = true;
return;
}
}
uint32_t
TestDrawTargetBase::RGBAPixelFromColor(const Color &aColor)
{
return uint8_t((aColor.b * 255) + 0.5f) | uint8_t((aColor.g * 255) + 0.5f) << 8 |
uint8_t((aColor.r * 255) + 0.5f) << 16 | uint8_t((aColor.a * 255) + 0.5f) << 24;
}