gfx/tests/gtest/TestColorNames.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial