gfx/tests/gtest/TestColorNames.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:ad56e4aabdbf
1 /* -*- Mode: C++; tab-width: 2; 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 "gtest/gtest.h"
7
8 #include <string.h>
9 #include "nsColor.h"
10 #include "nsColorNames.h"
11 #include "prprf.h"
12 #include "nsString.h"
13 #include "mozilla/ArrayUtils.h"
14
15 // define an array of all color names
16 #define GFX_COLOR(_name, _value) #_name,
17 static const char* const kColorNames[] = {
18 #include "nsColorNameList.h"
19 };
20 #undef GFX_COLOR
21
22 // define an array of all color name values
23 #define GFX_COLOR(_name, _value) _value,
24 static const nscolor kColors[] = {
25 #include "nsColorNameList.h"
26 };
27 #undef GFX_COLOR
28
29 using namespace mozilla;
30
31 static const char* kJunkNames[] = {
32 nullptr,
33 "",
34 "123",
35 "backgroundz",
36 "zzzzzz",
37 "#@$&@#*@*$@$#"
38 };
39
40 static
41 void RunColorTests() {
42 nscolor rgb;
43 // First make sure we can find all of the tags that are supposed to
44 // be in the table. Futz with the case to make sure any case will
45 // work
46
47 for (uint32_t index = 0 ; index < ArrayLength(kColorNames); index++) {
48 // Lookup color by name and make sure it has the right id
49 nsCString tagName(kColorNames[index]);
50
51 // Check that color lookup by name gets the right rgb value
52 ASSERT_TRUE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) <<
53 "can't find '" << tagName.get() << "'";
54 ASSERT_TRUE((rgb == kColors[index])) <<
55 "failed at index " << index << " out of " << ArrayLength(kColorNames);
56
57 // fiddle with the case to make sure we can still find it
58 tagName.SetCharAt(tagName.CharAt(0) - 32, 0);
59 ASSERT_TRUE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) <<
60 "can't find '" << tagName.get() << "'";
61 ASSERT_TRUE((rgb == kColors[index])) <<
62 "failed at index " << index << " out of " << ArrayLength(kColorNames);
63
64 // Check that parsing an RGB value in hex gets the right values
65 uint8_t r = NS_GET_R(rgb);
66 uint8_t g = NS_GET_G(rgb);
67 uint8_t b = NS_GET_B(rgb);
68 uint8_t a = NS_GET_A(rgb);
69 if (a != UINT8_MAX) {
70 // NS_HexToRGB() can not handle a color with alpha channel
71 rgb = NS_RGB(r, g, b);
72 }
73 char cbuf[50];
74 PR_snprintf(cbuf, sizeof(cbuf), "%02x%02x%02x", r, g, b);
75 nscolor hexrgb;
76 ASSERT_TRUE(NS_HexToRGB(NS_ConvertASCIItoUTF16(cbuf), &hexrgb)) <<
77 "hex conversion to color of '" << cbuf << "'";
78 ASSERT_TRUE(hexrgb == rgb);
79 }
80 }
81
82 static
83 void RunJunkColorTests() {
84 nscolor rgb;
85 // Now make sure we don't find some garbage
86 for (uint32_t i = 0; i < ArrayLength(kJunkNames); i++) {
87 nsCString tag(kJunkNames[i]);
88 ASSERT_FALSE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tag), &rgb)) <<
89 "Failed at junk color " << kJunkNames[i];
90 }
91 }
92
93 TEST(Gfx, ColorNames) {
94 RunColorTests();
95 }
96
97 TEST(Gfx, JunkColorNames) {
98 RunJunkColorTests();
99 }

mercurial