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