1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/style/test/TestCSSPropertyLookup.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,170 @@ 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 +#include <stdio.h> 1.9 +#include "plstr.h" 1.10 +#include "nsCSSProps.h" 1.11 +#include "nsCSSKeywords.h" 1.12 +#include "nsString.h" 1.13 +#include "nsXPCOM.h" 1.14 + 1.15 +static const char* const kJunkNames[] = { 1.16 + nullptr, 1.17 + "", 1.18 + "123", 1.19 + "backgroundz", 1.20 + "zzzzzz", 1.21 + "#@$&@#*@*$@$#" 1.22 +}; 1.23 + 1.24 +static bool 1.25 +TestProps() 1.26 +{ 1.27 + bool success = true; 1.28 + nsCSSProperty id; 1.29 + nsCSSProperty index; 1.30 + 1.31 + // Everything appears to assert if we don't do this first... 1.32 + nsCSSProps::AddRefTable(); 1.33 + 1.34 + // First make sure we can find all of the tags that are supposed to 1.35 + // be in the table. Futz with the case to make sure any case will 1.36 + // work 1.37 + extern const char* const kCSSRawProperties[]; 1.38 + const char*const* et = &kCSSRawProperties[0]; 1.39 + const char*const* end = &kCSSRawProperties[eCSSProperty_COUNT]; 1.40 + index = eCSSProperty_UNKNOWN; 1.41 + while (et < end) { 1.42 + char tagName[100]; 1.43 + PL_strcpy(tagName, *et); 1.44 + index = nsCSSProperty(int32_t(index) + 1); 1.45 + 1.46 + id = nsCSSProps::LookupProperty(nsCString(tagName), 1.47 + nsCSSProps::eIgnoreEnabledState); 1.48 + if (id == eCSSProperty_UNKNOWN) { 1.49 + printf("bug: can't find '%s'\n", tagName); 1.50 + success = false; 1.51 + } 1.52 + if (id != index) { 1.53 + printf("bug: name='%s' id=%d index=%d\n", tagName, id, index); 1.54 + success = false; 1.55 + } 1.56 + 1.57 + // fiddle with the case to make sure we can still find it 1.58 + if (('a' <= tagName[0]) && (tagName[0] <= 'z')) { 1.59 + tagName[0] = tagName[0] - 32; 1.60 + } 1.61 + id = nsCSSProps::LookupProperty(NS_ConvertASCIItoUTF16(tagName), 1.62 + nsCSSProps::eIgnoreEnabledState); 1.63 + if (id < 0) { 1.64 + printf("bug: can't find '%s'\n", tagName); 1.65 + success = false; 1.66 + } 1.67 + if (index != id) { 1.68 + printf("bug: name='%s' id=%d index=%d\n", tagName, id, index); 1.69 + success = false; 1.70 + } 1.71 + et++; 1.72 + } 1.73 + 1.74 + // Now make sure we don't find some garbage 1.75 + for (int i = 0; i < (int) (sizeof(kJunkNames) / sizeof(const char*)); i++) { 1.76 + const char* const tag = kJunkNames[i]; 1.77 + id = nsCSSProps::LookupProperty(nsAutoCString(tag), 1.78 + nsCSSProps::eIgnoreEnabledState); 1.79 + if (id >= 0) { 1.80 + printf("bug: found '%s'\n", tag ? tag : "(null)"); 1.81 + success = false; 1.82 + } 1.83 + } 1.84 + 1.85 + nsCSSProps::ReleaseTable(); 1.86 + return success; 1.87 +} 1.88 + 1.89 +bool 1.90 +TestKeywords() 1.91 +{ 1.92 + nsCSSKeywords::AddRefTable(); 1.93 + 1.94 + bool success = true; 1.95 + nsCSSKeyword id; 1.96 + nsCSSKeyword index; 1.97 + 1.98 + extern const char* const kCSSRawKeywords[]; 1.99 + 1.100 + // First make sure we can find all of the tags that are supposed to 1.101 + // be in the table. Futz with the case to make sure any case will 1.102 + // work 1.103 + const char*const* et = &kCSSRawKeywords[0]; 1.104 + const char*const* end = &kCSSRawKeywords[eCSSKeyword_COUNT - 1]; 1.105 + index = eCSSKeyword_UNKNOWN; 1.106 + while (et < end) { 1.107 + char tagName[512]; 1.108 + char* underscore = &(tagName[0]); 1.109 + 1.110 + PL_strcpy(tagName, *et); 1.111 + while (*underscore) { 1.112 + if (*underscore == '_') { 1.113 + *underscore = '-'; 1.114 + } 1.115 + underscore++; 1.116 + } 1.117 + index = nsCSSKeyword(int32_t(index) + 1); 1.118 + 1.119 + id = nsCSSKeywords::LookupKeyword(nsCString(tagName)); 1.120 + if (id <= eCSSKeyword_UNKNOWN) { 1.121 + printf("bug: can't find '%s'\n", tagName); 1.122 + success = false; 1.123 + } 1.124 + if (id != index) { 1.125 + printf("bug: name='%s' id=%d index=%d\n", tagName, id, index); 1.126 + success = false; 1.127 + } 1.128 + 1.129 + // fiddle with the case to make sure we can still find it 1.130 + if (('a' <= tagName[0]) && (tagName[0] <= 'z')) { 1.131 + tagName[0] = tagName[0] - 32; 1.132 + } 1.133 + id = nsCSSKeywords::LookupKeyword(nsCString(tagName)); 1.134 + if (id <= eCSSKeyword_UNKNOWN) { 1.135 + printf("bug: can't find '%s'\n", tagName); 1.136 + success = false; 1.137 + } 1.138 + if (id != index) { 1.139 + printf("bug: name='%s' id=%d index=%d\n", tagName, id, index); 1.140 + success = false; 1.141 + } 1.142 + et++; 1.143 + } 1.144 + 1.145 + // Now make sure we don't find some garbage 1.146 + for (int i = 0; i < (int) (sizeof(kJunkNames) / sizeof(const char*)); i++) { 1.147 + const char* const tag = kJunkNames[i]; 1.148 + id = nsCSSKeywords::LookupKeyword(nsAutoCString(tag)); 1.149 + if (eCSSKeyword_UNKNOWN < id) { 1.150 + printf("bug: found '%s'\n", tag ? tag : "(null)"); 1.151 + success = false; 1.152 + } 1.153 + } 1.154 + 1.155 + nsCSSKeywords::ReleaseTable(); 1.156 + return success; 1.157 +} 1.158 + 1.159 +int 1.160 +main(void) 1.161 +{ 1.162 + nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.163 + NS_ENSURE_SUCCESS(rv, 2); 1.164 + 1.165 + bool testOK = true; 1.166 + testOK &= TestProps(); 1.167 + testOK &= TestKeywords(); 1.168 + 1.169 + rv = NS_ShutdownXPCOM(nullptr); 1.170 + NS_ENSURE_SUCCESS(rv, 2); 1.171 + 1.172 + return testOK ? 0 : 1; 1.173 +}