1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/tests/gtest/TestColorNames.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; 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 "gtest/gtest.h" 1.10 + 1.11 +#include <string.h> 1.12 +#include "nsColor.h" 1.13 +#include "nsColorNames.h" 1.14 +#include "prprf.h" 1.15 +#include "nsString.h" 1.16 +#include "mozilla/ArrayUtils.h" 1.17 + 1.18 +// define an array of all color names 1.19 +#define GFX_COLOR(_name, _value) #_name, 1.20 +static const char* const kColorNames[] = { 1.21 +#include "nsColorNameList.h" 1.22 +}; 1.23 +#undef GFX_COLOR 1.24 + 1.25 +// define an array of all color name values 1.26 +#define GFX_COLOR(_name, _value) _value, 1.27 +static const nscolor kColors[] = { 1.28 +#include "nsColorNameList.h" 1.29 +}; 1.30 +#undef GFX_COLOR 1.31 + 1.32 +using namespace mozilla; 1.33 + 1.34 +static const char* kJunkNames[] = { 1.35 + nullptr, 1.36 + "", 1.37 + "123", 1.38 + "backgroundz", 1.39 + "zzzzzz", 1.40 + "#@$&@#*@*$@$#" 1.41 +}; 1.42 + 1.43 +static 1.44 +void RunColorTests() { 1.45 + nscolor rgb; 1.46 + // First make sure we can find all of the tags that are supposed to 1.47 + // be in the table. Futz with the case to make sure any case will 1.48 + // work 1.49 + 1.50 + for (uint32_t index = 0 ; index < ArrayLength(kColorNames); index++) { 1.51 + // Lookup color by name and make sure it has the right id 1.52 + nsCString tagName(kColorNames[index]); 1.53 + 1.54 + // Check that color lookup by name gets the right rgb value 1.55 + ASSERT_TRUE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) << 1.56 + "can't find '" << tagName.get() << "'"; 1.57 + ASSERT_TRUE((rgb == kColors[index])) << 1.58 + "failed at index " << index << " out of " << ArrayLength(kColorNames); 1.59 + 1.60 + // fiddle with the case to make sure we can still find it 1.61 + tagName.SetCharAt(tagName.CharAt(0) - 32, 0); 1.62 + ASSERT_TRUE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) << 1.63 + "can't find '" << tagName.get() << "'"; 1.64 + ASSERT_TRUE((rgb == kColors[index])) << 1.65 + "failed at index " << index << " out of " << ArrayLength(kColorNames); 1.66 + 1.67 + // Check that parsing an RGB value in hex gets the right values 1.68 + uint8_t r = NS_GET_R(rgb); 1.69 + uint8_t g = NS_GET_G(rgb); 1.70 + uint8_t b = NS_GET_B(rgb); 1.71 + uint8_t a = NS_GET_A(rgb); 1.72 + if (a != UINT8_MAX) { 1.73 + // NS_HexToRGB() can not handle a color with alpha channel 1.74 + rgb = NS_RGB(r, g, b); 1.75 + } 1.76 + char cbuf[50]; 1.77 + PR_snprintf(cbuf, sizeof(cbuf), "%02x%02x%02x", r, g, b); 1.78 + nscolor hexrgb; 1.79 + ASSERT_TRUE(NS_HexToRGB(NS_ConvertASCIItoUTF16(cbuf), &hexrgb)) << 1.80 + "hex conversion to color of '" << cbuf << "'"; 1.81 + ASSERT_TRUE(hexrgb == rgb); 1.82 + } 1.83 +} 1.84 + 1.85 +static 1.86 +void RunJunkColorTests() { 1.87 + nscolor rgb; 1.88 + // Now make sure we don't find some garbage 1.89 + for (uint32_t i = 0; i < ArrayLength(kJunkNames); i++) { 1.90 + nsCString tag(kJunkNames[i]); 1.91 + ASSERT_FALSE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tag), &rgb)) << 1.92 + "Failed at junk color " << kJunkNames[i]; 1.93 + } 1.94 +} 1.95 + 1.96 +TEST(Gfx, ColorNames) { 1.97 + RunColorTests(); 1.98 +} 1.99 + 1.100 +TEST(Gfx, JunkColorNames) { 1.101 + RunJunkColorTests(); 1.102 +}