Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / gfx / tests / TestColorNames.cpp
blob842e7fd993173e55a1c4e6c0d4e8a7ccd7fd576f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
40 #include <string.h>
41 #include "nsColor.h"
42 #include "nsColorNames.h"
43 #include "prprf.h"
44 #include "nsString.h"
46 // define an array of all color names
47 #define GFX_COLOR(_name, _value) #_name,
48 static const char* const kColorNames[] = {
49 #include "nsColorNameList.h"
51 #undef GFX_COLOR
53 // define an array of all color name values
54 #define GFX_COLOR(_name, _value) _value,
55 static const nscolor kColors[] = {
56 #include "nsColorNameList.h"
58 #undef GFX_COLOR
60 static const char* kJunkNames[] = {
61 nsnull,
62 "",
63 "123",
64 "backgroundz",
65 "zzzzzz",
66 "#@$&@#*@*$@$#"
69 int main(int argc, char** argv)
71 ScopedXPCOM xpcom("TestColorNames");
72 if (xpcom.failed())
73 return 1;
75 nscolor rgb;
76 int rv = 0;
78 // First make sure we can find all of the tags that are supposed to
79 // be in the table. Futz with the case to make sure any case will
80 // work
82 for (PRUint32 index = 0 ; index < NS_ARRAY_LENGTH(kColorNames); index++) {
83 // Lookup color by name and make sure it has the right id
84 nsCString tagName(kColorNames[index]);
86 // Check that color lookup by name gets the right rgb value
87 if (!NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) {
88 fail("can't find '%s'", tagName.get());
89 rv = 1;
91 if (rgb != kColors[index]) {
92 fail("name='%s' ColorNameToRGB=%x kColors[%d]=%08x",
93 tagName.get(), rgb, index, kColors[index]);
94 rv = 1;
97 // fiddle with the case to make sure we can still find it
98 tagName.SetCharAt(tagName.CharAt(0) - 32, 0);
99 if (!NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) {
100 fail("can't find '%s'", tagName.get());
101 rv = 1;
103 if (rgb != kColors[index]) {
104 fail("name='%s' ColorNameToRGB=%x kColors[%d]=%08x",
105 tagName.get(), rgb, index, kColors[index]);
106 rv = 1;
109 // Check that parsing an RGB value in hex gets the right values
110 PRUint8 r = NS_GET_R(rgb);
111 PRUint8 g = NS_GET_G(rgb);
112 PRUint8 b = NS_GET_B(rgb);
113 PRUint8 a = NS_GET_A(rgb);
114 if (a != PR_UINT8_MAX) {
115 // NS_HexToRGB() can not handle a color with alpha channel
116 rgb = NS_RGB(r, g, b);
118 char cbuf[50];
119 PR_snprintf(cbuf, sizeof(cbuf), "%02x%02x%02x", r, g, b);
120 nscolor hexrgb;
121 if (!NS_HexToRGB(NS_ConvertASCIItoUTF16(cbuf), &hexrgb)) {
122 fail("hex conversion to color of '%s'", cbuf);
123 rv = 1;
125 if (hexrgb != rgb) {
126 fail("rgb=%x hexrgb=%x", rgb, hexrgb);
127 rv = 1;
131 // Now make sure we don't find some garbage
132 for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(kJunkNames); i++) {
133 nsCString tag(kJunkNames[i]);
134 if (NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tag), &rgb)) {
135 fail("found '%s'", kJunkNames[i] ? kJunkNames[i] : "(null)");
136 rv = 1;
140 if (rv == 0)
141 passed("TestColorNames");
142 return rv;