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: /* This tests the margin parsing functionality in nsAttrValue.cpp, which michael@0: * is accessible via nsContentUtils, and is used in setting chromemargins michael@0: * to widget windows. It's located here due to linking issues in the michael@0: * content directory. michael@0: */ michael@0: michael@0: /* This test no longer compiles now that we've removed nsIContentUtils (bug michael@0: * 647273). We need to be internal code in order to include nsContentUtils.h, michael@0: * but defining MOZILLA_INTERNAL_API is not enough to make us internal. michael@0: */ michael@0: michael@0: #include "TestHarness.h" michael@0: michael@0: #ifndef MOZILLA_INTERNAL_API michael@0: // some of the includes make use of internal string types michael@0: #define nsAString_h___ michael@0: #define nsString_h___ michael@0: #define nsStringFwd_h___ michael@0: #define nsReadableUtils_h___ michael@0: class nsACString; michael@0: class nsAString; michael@0: class nsAFlatString; michael@0: class nsAFlatCString; michael@0: class nsAdoptingString; michael@0: class nsAdoptingCString; michael@0: class nsXPIDLString; michael@0: template class nsReadingIterator; michael@0: #endif michael@0: michael@0: #include "nscore.h" michael@0: #include "nsContentUtils.h" michael@0: michael@0: #ifndef MOZILLA_INTERNAL_API michael@0: #undef nsString_h___ michael@0: #undef nsAString_h___ michael@0: #undef nsReadableUtils_h___ michael@0: #endif michael@0: michael@0: struct DATA { michael@0: bool shouldfail; michael@0: const char* margins; michael@0: int top; michael@0: int right; michael@0: int bottom; michael@0: int left; michael@0: }; michael@0: michael@0: const bool SHOULD_FAIL = true; michael@0: const int SHOULD_PASS = false; michael@0: michael@0: const DATA Data[] = { michael@0: { SHOULD_FAIL, "", 1, 2, 3, 4 }, michael@0: { SHOULD_FAIL, "1,0,0,0", 1, 2, 3, 4 }, michael@0: { SHOULD_FAIL, "1,2,0,0", 1, 2, 3, 4 }, michael@0: { SHOULD_FAIL, "1,2,3,0", 1, 2, 3, 4 }, michael@0: { SHOULD_FAIL, "4,3,2,1", 1, 2, 3, 4 }, michael@0: { SHOULD_FAIL, "azsasdasd", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, ",azsasdasd", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, " ", 1, 2, 3, 4 }, michael@0: { SHOULD_FAIL, "azsdfsdfsdfsdfsdfsasdasd,asdasdasdasdasdasd,asdadasdasd,asdasdasdasd", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, "as,as,as,as", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, "0,0,0", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, "0,0", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, "4.6,1,1,1", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, ",,,,", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, "1, , , ,", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, "1, , ,", 0, 0, 0, 0 }, michael@0: { SHOULD_FAIL, "@!@%^&^*()", 1, 2, 3, 4 }, michael@0: { SHOULD_PASS, "4,3,2,1", 4, 3, 2, 1 }, michael@0: { SHOULD_PASS, "-4,-3,-2,-1", -4, -3, -2, -1 }, michael@0: { SHOULD_PASS, "10000,3,2,1", 10000, 3, 2, 1 }, michael@0: { SHOULD_PASS, "4 , 3 , 2 , 1", 4, 3, 2, 1 }, michael@0: { SHOULD_PASS, "4, 3 ,2,1", 4, 3, 2, 1 }, michael@0: { SHOULD_FAIL, "4,3,2,10000000000000 --", 4, 3, 2, 10000000000000 }, michael@0: { SHOULD_PASS, "4,3,2,1000", 4, 3, 2, 1000 }, michael@0: { SHOULD_PASS, "2147483647,3,2,1000", 2147483647, 3, 2, 1000 }, michael@0: { SHOULD_PASS, "2147483647,2147483647,2147483647,2147483647", 2147483647, 2147483647, 2147483647, 2147483647 }, michael@0: { SHOULD_PASS, "-2147483647,3,2,1000", -2147483647, 3, 2, 1000 }, michael@0: { SHOULD_FAIL, "2147483648,3,2,1000", 1, 3, 2, 1000 }, michael@0: { 0, nullptr, 0, 0, 0, 0 } michael@0: }; michael@0: michael@0: void DoAttrValueTest() michael@0: { michael@0: int idx = -1; michael@0: bool didFail = false; michael@0: while (Data[++idx].margins) { michael@0: nsAutoString str; michael@0: str.AssignLiteral(Data[idx].margins); michael@0: nsIntMargin values(99,99,99,99); michael@0: bool result = nsContentUtils::ParseIntMarginValue(str, values); michael@0: michael@0: // if the parse fails michael@0: if (!result) { michael@0: if (Data[idx].shouldfail) michael@0: continue; michael@0: fail(Data[idx].margins); michael@0: didFail = true; michael@0: printf("*1\n"); michael@0: continue; michael@0: } michael@0: michael@0: if (Data[idx].shouldfail) { michael@0: if (Data[idx].top == values.top && michael@0: Data[idx].right == values.right && michael@0: Data[idx].bottom == values.bottom && michael@0: Data[idx].left == values.left) { michael@0: // not likely michael@0: fail(Data[idx].margins); michael@0: didFail = true; michael@0: printf("*2\n"); michael@0: continue; michael@0: } michael@0: // good failure, parse failed and that's what we expected. michael@0: continue; michael@0: } michael@0: #if 0 michael@0: printf("%d==%d %d==%d %d==%d %d==%d\n", michael@0: Data[idx].top, values.top, michael@0: Data[idx].right, values.right, michael@0: Data[idx].bottom, values.bottom, michael@0: Data[idx].left, values.left); michael@0: #endif michael@0: if (Data[idx].top == values.top && michael@0: Data[idx].right == values.right && michael@0: Data[idx].bottom == values.bottom && michael@0: Data[idx].left == values.left) { michael@0: // good parse results michael@0: continue; michael@0: } michael@0: else { michael@0: fail(Data[idx].margins); michael@0: didFail = true; michael@0: printf("*3\n"); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: if (!didFail) michael@0: passed("nsAttrValue margin parsing tests passed."); michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: ScopedXPCOM xpcom(""); michael@0: if (xpcom.failed()) michael@0: return 1; michael@0: DoAttrValueTest(); michael@0: return 0; michael@0: }