Back to index

lightning-sunbird  0.9+nobinonly
TestColorNames.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 // No output means test passed.
00039 
00040 #include <stdio.h>
00041 #include <string.h>
00042 #include "nsColorNames.h"
00043 #include "prprf.h"
00044 #include "nsString.h"
00045 
00046 static const char* kJunkNames[] = {
00047   nsnull,
00048   "",
00049   "123",
00050   "backgroundz",
00051   "zzzzzz",
00052   "#@$&@#*@*$@$#"
00053 };
00054 
00055 int main(int argc, char** argv)
00056 {
00057   nsColorName id;
00058   nsColorName index;
00059   int rv = 0;
00060 
00061   // Everything appears to assert if we don't do this first...
00062   nsColorNames::AddRefTable();
00063 
00064   // First make sure we can find all of the tags that are supposed to
00065   // be in the table. Futz with the case to make sure any case will
00066   // work
00067     
00068   index = eColorName_UNKNOWN;
00069   while (PRInt32(index) < (PRInt32 (eColorName_COUNT) - 1)) {
00070     // Lookup color by name and make sure it has the right id
00071     index = nsColorName(PRInt32(index) + 1);
00072     nsCString tagName(nsColorNames::GetStringValue(index));
00073     if (tagName.IsEmpty()) {
00074       printf("bug: tagName for nsColorNames::GetStringValue(%d) is ''\n", index);
00075       rv = -1;
00076       continue;
00077     }
00078 
00079     id = nsColorNames::LookupName(NS_ConvertASCIItoUCS2(tagName));
00080     if (id == eColorName_UNKNOWN) {
00081       printf("bug: can't find '%s'\n", tagName.get());
00082       rv = -1;
00083     }
00084     if (id != index) {
00085       printf("bug: name='%s' id=%d index=%d\n", tagName.get(), id, index);
00086       rv = -1;
00087     }
00088 
00089     // fiddle with the case to make sure we can still find it
00090     tagName.SetCharAt(tagName.CharAt(0) - 32, 0);
00091     id = nsColorNames::LookupName(NS_ConvertASCIItoUCS2(tagName));
00092     if (id == eColorName_UNKNOWN) {
00093       printf("bug: can't find '%s'\n", tagName.get());
00094       rv = -1;
00095     }
00096     if (id != index) {
00097       printf("bug: name='%s' id=%d index=%d\n", tagName.get(), id, index);
00098       rv = -1;
00099     }
00100 
00101     // Check that color lookup by name gets the right rgb value
00102     nscolor rgb;
00103     if (!NS_ColorNameToRGB(NS_ConvertASCIItoUCS2(tagName), &rgb)) {
00104       printf("bug: name='%s' didn't NS_ColorNameToRGB\n", tagName.get());
00105       rv = -1;
00106     }
00107     if (nsColorNames::kColors[index] != rgb) {
00108       printf("bug: name='%s' ColorNameToRGB=%x kColors[%d]=%x\n",
00109              tagName.get(), rgb, nsColorNames::kColors[index],
00110              nsColorNames::kColors[index]);
00111       rv = -1;
00112     }
00113 
00114     // Check that parsing an RGB value in hex gets the right values
00115     PRUint8 r = NS_GET_R(rgb);
00116     PRUint8 g = NS_GET_G(rgb);
00117     PRUint8 b = NS_GET_B(rgb);
00118     char cbuf[50];
00119     PR_snprintf(cbuf, sizeof(cbuf), "%02x%02x%02x", r, g, b);
00120     nscolor hexrgb;
00121     if (!NS_HexToRGB(NS_ConvertASCIItoUCS2(cbuf), &hexrgb)) {
00122       printf("bug: hex conversion to color of '%s' failed\n", cbuf);
00123       rv = -1;
00124     }
00125     if (hexrgb != rgb) {
00126       printf("bug: rgb=%x hexrgb=%x\n", rgb, hexrgb);
00127       rv = -1;
00128     }
00129   }
00130 
00131   // Now make sure we don't find some garbage
00132   for (int i = 0; i < (int) (sizeof(kJunkNames) / sizeof(const char*)); i++) {
00133     const char* tag = kJunkNames[i];
00134     id = nsColorNames::LookupName(NS_ConvertASCIItoUCS2(tag));
00135     if (id > eColorName_UNKNOWN) {
00136       printf("bug: found '%s'\n", tag ? tag : "(null)");
00137       rv = -1;
00138     }
00139   }
00140 
00141   return rv;
00142 }