1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/unittest/TestDrawTargetBase.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "TestDrawTargetBase.h" 1.10 +#include <sstream> 1.11 + 1.12 +using namespace mozilla; 1.13 +using namespace mozilla::gfx; 1.14 +using namespace std; 1.15 + 1.16 +TestDrawTargetBase::TestDrawTargetBase() 1.17 +{ 1.18 + REGISTER_TEST(TestDrawTargetBase, Initialized); 1.19 + REGISTER_TEST(TestDrawTargetBase, FillCompletely); 1.20 + REGISTER_TEST(TestDrawTargetBase, FillRect); 1.21 +} 1.22 + 1.23 +void 1.24 +TestDrawTargetBase::Initialized() 1.25 +{ 1.26 + VERIFY(mDT); 1.27 +} 1.28 + 1.29 +void 1.30 +TestDrawTargetBase::FillCompletely() 1.31 +{ 1.32 + mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), ColorPattern(Color(0, 0.5f, 0, 1.0f))); 1.33 + 1.34 + RefreshSnapshot(); 1.35 + 1.36 + VerifyAllPixels(Color(0, 0.5f, 0, 1.0f)); 1.37 +} 1.38 + 1.39 +void 1.40 +TestDrawTargetBase::FillRect() 1.41 +{ 1.42 + mDT->FillRect(Rect(0, 0, DT_WIDTH, DT_HEIGHT), ColorPattern(Color(0, 0.5f, 0, 1.0f))); 1.43 + mDT->FillRect(Rect(50, 50, 50, 50), ColorPattern(Color(0.5f, 0, 0, 1.0f))); 1.44 + 1.45 + RefreshSnapshot(); 1.46 + 1.47 + VerifyPixel(IntPoint(49, 49), Color(0, 0.5f, 0, 1.0f)); 1.48 + VerifyPixel(IntPoint(50, 50), Color(0.5f, 0, 0, 1.0f)); 1.49 + VerifyPixel(IntPoint(99, 99), Color(0.5f, 0, 0, 1.0f)); 1.50 + VerifyPixel(IntPoint(100, 100), Color(0, 0.5f, 0, 1.0f)); 1.51 +} 1.52 + 1.53 +void 1.54 +TestDrawTargetBase::RefreshSnapshot() 1.55 +{ 1.56 + RefPtr<SourceSurface> snapshot = mDT->Snapshot(); 1.57 + mDataSnapshot = snapshot->GetDataSurface(); 1.58 +} 1.59 + 1.60 +void 1.61 +TestDrawTargetBase::VerifyAllPixels(const Color &aColor) 1.62 +{ 1.63 + uint32_t *colVal = (uint32_t*)mDataSnapshot->GetData(); 1.64 + 1.65 + uint32_t expected = RGBAPixelFromColor(aColor); 1.66 + 1.67 + for (int y = 0; y < DT_HEIGHT; y++) { 1.68 + for (int x = 0; x < DT_WIDTH; x++) { 1.69 + if (colVal[y * (mDataSnapshot->Stride() / 4) + x] != expected) { 1.70 + LogMessage("VerifyAllPixels Failed\n"); 1.71 + mTestFailed = true; 1.72 + return; 1.73 + } 1.74 + } 1.75 + } 1.76 +} 1.77 + 1.78 +void 1.79 +TestDrawTargetBase::VerifyPixel(const IntPoint &aPoint, mozilla::gfx::Color &aColor) 1.80 +{ 1.81 + uint32_t *colVal = (uint32_t*)mDataSnapshot->GetData(); 1.82 + 1.83 + uint32_t expected = RGBAPixelFromColor(aColor); 1.84 + uint32_t rawActual = colVal[aPoint.y * (mDataSnapshot->Stride() / 4) + aPoint.x]; 1.85 + 1.86 + if (rawActual != expected) { 1.87 + stringstream message; 1.88 + uint32_t actb = rawActual & 0xFF; 1.89 + uint32_t actg = (rawActual & 0xFF00) >> 8; 1.90 + uint32_t actr = (rawActual & 0xFF0000) >> 16; 1.91 + uint32_t acta = (rawActual & 0xFF000000) >> 24; 1.92 + uint32_t expb = expected & 0xFF; 1.93 + uint32_t expg = (expected & 0xFF00) >> 8; 1.94 + uint32_t expr = (expected & 0xFF0000) >> 16; 1.95 + uint32_t expa = (expected & 0xFF000000) >> 24; 1.96 + 1.97 + message << "Verify Pixel (" << aPoint.x << "x" << aPoint.y << ") Failed." 1.98 + " Expected (" << expr << "," << expg << "," << expb << "," << expa << ") " 1.99 + " Got (" << actr << "," << actg << "," << actb << "," << acta << ")\n"; 1.100 + 1.101 + LogMessage(message.str()); 1.102 + mTestFailed = true; 1.103 + return; 1.104 + } 1.105 +} 1.106 + 1.107 +uint32_t 1.108 +TestDrawTargetBase::RGBAPixelFromColor(const Color &aColor) 1.109 +{ 1.110 + return uint8_t((aColor.b * 255) + 0.5f) | uint8_t((aColor.g * 255) + 0.5f) << 8 | 1.111 + uint8_t((aColor.r * 255) + 0.5f) << 16 | uint8_t((aColor.a * 255) + 0.5f) << 24; 1.112 +}