1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/tests/TestChromeMargin.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,155 @@ 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 +/* This tests the margin parsing functionality in nsAttrValue.cpp, which 1.10 + * is accessible via nsContentUtils, and is used in setting chromemargins 1.11 + * to widget windows. It's located here due to linking issues in the 1.12 + * content directory. 1.13 + */ 1.14 + 1.15 +/* This test no longer compiles now that we've removed nsIContentUtils (bug 1.16 + * 647273). We need to be internal code in order to include nsContentUtils.h, 1.17 + * but defining MOZILLA_INTERNAL_API is not enough to make us internal. 1.18 + */ 1.19 + 1.20 +#include "TestHarness.h" 1.21 + 1.22 +#ifndef MOZILLA_INTERNAL_API 1.23 +// some of the includes make use of internal string types 1.24 +#define nsAString_h___ 1.25 +#define nsString_h___ 1.26 +#define nsStringFwd_h___ 1.27 +#define nsReadableUtils_h___ 1.28 +class nsACString; 1.29 +class nsAString; 1.30 +class nsAFlatString; 1.31 +class nsAFlatCString; 1.32 +class nsAdoptingString; 1.33 +class nsAdoptingCString; 1.34 +class nsXPIDLString; 1.35 +template<class T> class nsReadingIterator; 1.36 +#endif 1.37 + 1.38 +#include "nscore.h" 1.39 +#include "nsContentUtils.h" 1.40 + 1.41 +#ifndef MOZILLA_INTERNAL_API 1.42 +#undef nsString_h___ 1.43 +#undef nsAString_h___ 1.44 +#undef nsReadableUtils_h___ 1.45 +#endif 1.46 + 1.47 +struct DATA { 1.48 + bool shouldfail; 1.49 + const char* margins; 1.50 + int top; 1.51 + int right; 1.52 + int bottom; 1.53 + int left; 1.54 +}; 1.55 + 1.56 +const bool SHOULD_FAIL = true; 1.57 +const int SHOULD_PASS = false; 1.58 + 1.59 +const DATA Data[] = { 1.60 + { SHOULD_FAIL, "", 1, 2, 3, 4 }, 1.61 + { SHOULD_FAIL, "1,0,0,0", 1, 2, 3, 4 }, 1.62 + { SHOULD_FAIL, "1,2,0,0", 1, 2, 3, 4 }, 1.63 + { SHOULD_FAIL, "1,2,3,0", 1, 2, 3, 4 }, 1.64 + { SHOULD_FAIL, "4,3,2,1", 1, 2, 3, 4 }, 1.65 + { SHOULD_FAIL, "azsasdasd", 0, 0, 0, 0 }, 1.66 + { SHOULD_FAIL, ",azsasdasd", 0, 0, 0, 0 }, 1.67 + { SHOULD_FAIL, " ", 1, 2, 3, 4 }, 1.68 + { SHOULD_FAIL, "azsdfsdfsdfsdfsdfsasdasd,asdasdasdasdasdasd,asdadasdasd,asdasdasdasd", 0, 0, 0, 0 }, 1.69 + { SHOULD_FAIL, "as,as,as,as", 0, 0, 0, 0 }, 1.70 + { SHOULD_FAIL, "0,0,0", 0, 0, 0, 0 }, 1.71 + { SHOULD_FAIL, "0,0", 0, 0, 0, 0 }, 1.72 + { SHOULD_FAIL, "4.6,1,1,1", 0, 0, 0, 0 }, 1.73 + { SHOULD_FAIL, ",,,,", 0, 0, 0, 0 }, 1.74 + { SHOULD_FAIL, "1, , , ,", 0, 0, 0, 0 }, 1.75 + { SHOULD_FAIL, "1, , ,", 0, 0, 0, 0 }, 1.76 + { SHOULD_FAIL, "@!@%^&^*()", 1, 2, 3, 4 }, 1.77 + { SHOULD_PASS, "4,3,2,1", 4, 3, 2, 1 }, 1.78 + { SHOULD_PASS, "-4,-3,-2,-1", -4, -3, -2, -1 }, 1.79 + { SHOULD_PASS, "10000,3,2,1", 10000, 3, 2, 1 }, 1.80 + { SHOULD_PASS, "4 , 3 , 2 , 1", 4, 3, 2, 1 }, 1.81 + { SHOULD_PASS, "4, 3 ,2,1", 4, 3, 2, 1 }, 1.82 + { SHOULD_FAIL, "4,3,2,10000000000000 --", 4, 3, 2, 10000000000000 }, 1.83 + { SHOULD_PASS, "4,3,2,1000", 4, 3, 2, 1000 }, 1.84 + { SHOULD_PASS, "2147483647,3,2,1000", 2147483647, 3, 2, 1000 }, 1.85 + { SHOULD_PASS, "2147483647,2147483647,2147483647,2147483647", 2147483647, 2147483647, 2147483647, 2147483647 }, 1.86 + { SHOULD_PASS, "-2147483647,3,2,1000", -2147483647, 3, 2, 1000 }, 1.87 + { SHOULD_FAIL, "2147483648,3,2,1000", 1, 3, 2, 1000 }, 1.88 + { 0, nullptr, 0, 0, 0, 0 } 1.89 +}; 1.90 + 1.91 +void DoAttrValueTest() 1.92 +{ 1.93 + int idx = -1; 1.94 + bool didFail = false; 1.95 + while (Data[++idx].margins) { 1.96 + nsAutoString str; 1.97 + str.AssignLiteral(Data[idx].margins); 1.98 + nsIntMargin values(99,99,99,99); 1.99 + bool result = nsContentUtils::ParseIntMarginValue(str, values); 1.100 + 1.101 + // if the parse fails 1.102 + if (!result) { 1.103 + if (Data[idx].shouldfail) 1.104 + continue; 1.105 + fail(Data[idx].margins); 1.106 + didFail = true; 1.107 + printf("*1\n"); 1.108 + continue; 1.109 + } 1.110 + 1.111 + if (Data[idx].shouldfail) { 1.112 + if (Data[idx].top == values.top && 1.113 + Data[idx].right == values.right && 1.114 + Data[idx].bottom == values.bottom && 1.115 + Data[idx].left == values.left) { 1.116 + // not likely 1.117 + fail(Data[idx].margins); 1.118 + didFail = true; 1.119 + printf("*2\n"); 1.120 + continue; 1.121 + } 1.122 + // good failure, parse failed and that's what we expected. 1.123 + continue; 1.124 + } 1.125 +#if 0 1.126 + printf("%d==%d %d==%d %d==%d %d==%d\n", 1.127 + Data[idx].top, values.top, 1.128 + Data[idx].right, values.right, 1.129 + Data[idx].bottom, values.bottom, 1.130 + Data[idx].left, values.left); 1.131 +#endif 1.132 + if (Data[idx].top == values.top && 1.133 + Data[idx].right == values.right && 1.134 + Data[idx].bottom == values.bottom && 1.135 + Data[idx].left == values.left) { 1.136 + // good parse results 1.137 + continue; 1.138 + } 1.139 + else { 1.140 + fail(Data[idx].margins); 1.141 + didFail = true; 1.142 + printf("*3\n"); 1.143 + continue; 1.144 + } 1.145 + } 1.146 + 1.147 + if (!didFail) 1.148 + passed("nsAttrValue margin parsing tests passed."); 1.149 +} 1.150 + 1.151 +int main(int argc, char** argv) 1.152 +{ 1.153 + ScopedXPCOM xpcom(""); 1.154 + if (xpcom.failed()) 1.155 + return 1; 1.156 + DoAttrValueTest(); 1.157 + return 0; 1.158 +}