michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #include michael@0: #include "nsColor.h" michael@0: #include "nsColorNames.h" michael@0: #include "prprf.h" michael@0: #include "nsString.h" michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: // define an array of all color names michael@0: #define GFX_COLOR(_name, _value) #_name, michael@0: static const char* const kColorNames[] = { michael@0: #include "nsColorNameList.h" michael@0: }; michael@0: #undef GFX_COLOR michael@0: michael@0: // define an array of all color name values michael@0: #define GFX_COLOR(_name, _value) _value, michael@0: static const nscolor kColors[] = { michael@0: #include "nsColorNameList.h" michael@0: }; michael@0: #undef GFX_COLOR michael@0: michael@0: using namespace mozilla; michael@0: michael@0: static const char* kJunkNames[] = { michael@0: nullptr, michael@0: "", michael@0: "123", michael@0: "backgroundz", michael@0: "zzzzzz", michael@0: "#@$&@#*@*$@$#" michael@0: }; michael@0: michael@0: static michael@0: void RunColorTests() { michael@0: nscolor rgb; michael@0: // First make sure we can find all of the tags that are supposed to michael@0: // be in the table. Futz with the case to make sure any case will michael@0: // work michael@0: michael@0: for (uint32_t index = 0 ; index < ArrayLength(kColorNames); index++) { michael@0: // Lookup color by name and make sure it has the right id michael@0: nsCString tagName(kColorNames[index]); michael@0: michael@0: // Check that color lookup by name gets the right rgb value michael@0: ASSERT_TRUE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) << michael@0: "can't find '" << tagName.get() << "'"; michael@0: ASSERT_TRUE((rgb == kColors[index])) << michael@0: "failed at index " << index << " out of " << ArrayLength(kColorNames); michael@0: michael@0: // fiddle with the case to make sure we can still find it michael@0: tagName.SetCharAt(tagName.CharAt(0) - 32, 0); michael@0: ASSERT_TRUE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tagName), &rgb)) << michael@0: "can't find '" << tagName.get() << "'"; michael@0: ASSERT_TRUE((rgb == kColors[index])) << michael@0: "failed at index " << index << " out of " << ArrayLength(kColorNames); michael@0: michael@0: // Check that parsing an RGB value in hex gets the right values michael@0: uint8_t r = NS_GET_R(rgb); michael@0: uint8_t g = NS_GET_G(rgb); michael@0: uint8_t b = NS_GET_B(rgb); michael@0: uint8_t a = NS_GET_A(rgb); michael@0: if (a != UINT8_MAX) { michael@0: // NS_HexToRGB() can not handle a color with alpha channel michael@0: rgb = NS_RGB(r, g, b); michael@0: } michael@0: char cbuf[50]; michael@0: PR_snprintf(cbuf, sizeof(cbuf), "%02x%02x%02x", r, g, b); michael@0: nscolor hexrgb; michael@0: ASSERT_TRUE(NS_HexToRGB(NS_ConvertASCIItoUTF16(cbuf), &hexrgb)) << michael@0: "hex conversion to color of '" << cbuf << "'"; michael@0: ASSERT_TRUE(hexrgb == rgb); michael@0: } michael@0: } michael@0: michael@0: static michael@0: void RunJunkColorTests() { michael@0: nscolor rgb; michael@0: // Now make sure we don't find some garbage michael@0: for (uint32_t i = 0; i < ArrayLength(kJunkNames); i++) { michael@0: nsCString tag(kJunkNames[i]); michael@0: ASSERT_FALSE(NS_ColorNameToRGB(NS_ConvertASCIItoUTF16(tag), &rgb)) << michael@0: "Failed at junk color " << kJunkNames[i]; michael@0: } michael@0: } michael@0: michael@0: TEST(Gfx, ColorNames) { michael@0: RunColorTests(); michael@0: } michael@0: michael@0: TEST(Gfx, JunkColorNames) { michael@0: RunJunkColorTests(); michael@0: }