widget/tests/TestChromeMargin.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /* This tests the margin parsing functionality in nsAttrValue.cpp, which
     7  * is accessible via nsContentUtils, and is used in setting chromemargins
     8  * to widget windows. It's located here due to linking issues in the
     9  * content directory.
    10  */
    12 /* This test no longer compiles now that we've removed nsIContentUtils (bug
    13  * 647273).  We need to be internal code in order to include nsContentUtils.h,
    14  * but defining MOZILLA_INTERNAL_API is not enough to make us internal.
    15  */
    17 #include "TestHarness.h"
    19 #ifndef MOZILLA_INTERNAL_API
    20 // some of the includes make use of internal string types
    21 #define nsAString_h___
    22 #define nsString_h___
    23 #define nsStringFwd_h___
    24 #define nsReadableUtils_h___
    25 class nsACString;
    26 class nsAString;
    27 class nsAFlatString;
    28 class nsAFlatCString;
    29 class nsAdoptingString;
    30 class nsAdoptingCString;
    31 class nsXPIDLString;
    32 template<class T> class nsReadingIterator;
    33 #endif
    35 #include "nscore.h"
    36 #include "nsContentUtils.h"
    38 #ifndef MOZILLA_INTERNAL_API
    39 #undef nsString_h___
    40 #undef nsAString_h___
    41 #undef nsReadableUtils_h___
    42 #endif
    44 struct DATA {
    45   bool shouldfail;
    46   const char* margins;
    47   int top;
    48   int right;
    49   int bottom;
    50   int left;
    51 };
    53 const bool SHOULD_FAIL = true;
    54 const int SHOULD_PASS = false;
    56 const DATA Data[] = {
    57   { SHOULD_FAIL, "", 1, 2, 3, 4 },
    58   { SHOULD_FAIL, "1,0,0,0", 1, 2, 3, 4 },
    59   { SHOULD_FAIL, "1,2,0,0", 1, 2, 3, 4 },
    60   { SHOULD_FAIL, "1,2,3,0", 1, 2, 3, 4 },
    61   { SHOULD_FAIL, "4,3,2,1", 1, 2, 3, 4 },
    62   { SHOULD_FAIL, "azsasdasd", 0, 0, 0, 0 },
    63   { SHOULD_FAIL, ",azsasdasd", 0, 0, 0, 0 },
    64   { SHOULD_FAIL, "           ", 1, 2, 3, 4 },
    65   { SHOULD_FAIL, "azsdfsdfsdfsdfsdfsasdasd,asdasdasdasdasdasd,asdadasdasd,asdasdasdasd", 0, 0, 0, 0 },
    66   { SHOULD_FAIL, "as,as,as,as", 0, 0, 0, 0 },
    67   { SHOULD_FAIL, "0,0,0", 0, 0, 0, 0 },
    68   { SHOULD_FAIL, "0,0", 0, 0, 0, 0 },
    69   { SHOULD_FAIL, "4.6,1,1,1", 0, 0, 0, 0 },
    70   { SHOULD_FAIL, ",,,,", 0, 0, 0, 0 },
    71   { SHOULD_FAIL, "1, , , ,", 0, 0, 0, 0 },
    72   { SHOULD_FAIL, "1, , ,", 0, 0, 0, 0 },
    73   { SHOULD_FAIL, "@!@%^&^*()", 1, 2, 3, 4 },
    74   { SHOULD_PASS, "4,3,2,1", 4, 3, 2, 1 },
    75   { SHOULD_PASS, "-4,-3,-2,-1", -4, -3, -2, -1 },
    76   { SHOULD_PASS, "10000,3,2,1", 10000, 3, 2, 1 },
    77   { SHOULD_PASS, "4  , 3   , 2 , 1", 4, 3, 2, 1 },
    78   { SHOULD_PASS, "4,    3   ,2,1", 4, 3, 2, 1 },
    79   { SHOULD_FAIL, "4,3,2,10000000000000 --", 4, 3, 2, 10000000000000 },
    80   { SHOULD_PASS, "4,3,2,1000", 4, 3, 2, 1000 },
    81   { SHOULD_PASS, "2147483647,3,2,1000", 2147483647, 3, 2, 1000 },
    82   { SHOULD_PASS, "2147483647,2147483647,2147483647,2147483647", 2147483647, 2147483647, 2147483647, 2147483647 },
    83   { SHOULD_PASS, "-2147483647,3,2,1000", -2147483647, 3, 2, 1000 },
    84   { SHOULD_FAIL, "2147483648,3,2,1000", 1, 3, 2, 1000 },
    85   { 0, nullptr, 0, 0, 0, 0 }
    86 };
    88 void DoAttrValueTest()
    89 {
    90   int idx = -1;
    91   bool didFail = false;
    92   while (Data[++idx].margins) {
    93     nsAutoString str;
    94     str.AssignLiteral(Data[idx].margins);
    95     nsIntMargin values(99,99,99,99);
    96     bool result = nsContentUtils::ParseIntMarginValue(str, values);
    98     // if the parse fails
    99     if (!result) {
   100       if (Data[idx].shouldfail)
   101         continue;
   102       fail(Data[idx].margins);
   103       didFail = true;
   104       printf("*1\n");
   105       continue;
   106     }
   108     if (Data[idx].shouldfail) {
   109       if (Data[idx].top == values.top &&
   110           Data[idx].right == values.right &&
   111           Data[idx].bottom == values.bottom &&
   112           Data[idx].left == values.left) {
   113         // not likely
   114         fail(Data[idx].margins);
   115         didFail = true;
   116         printf("*2\n");
   117         continue;
   118       }
   119       // good failure, parse failed and that's what we expected.
   120       continue;
   121     }
   122 #if 0
   123     printf("%d==%d %d==%d %d==%d %d==%d\n",
   124       Data[idx].top, values.top,
   125       Data[idx].right, values.right,
   126       Data[idx].bottom, values.bottom,
   127       Data[idx].left, values.left);
   128 #endif
   129     if (Data[idx].top == values.top &&
   130         Data[idx].right == values.right &&
   131         Data[idx].bottom == values.bottom &&
   132         Data[idx].left == values.left) {
   133       // good parse results
   134       continue;
   135     }
   136     else {
   137       fail(Data[idx].margins);
   138       didFail = true;
   139       printf("*3\n");
   140       continue;
   141     }
   142   }
   144   if (!didFail)
   145     passed("nsAttrValue margin parsing tests passed.");
   146 }
   148 int main(int argc, char** argv)
   149 {
   150   ScopedXPCOM xpcom("");
   151   if (xpcom.failed())
   152     return 1;
   153   DoAttrValueTest();
   154   return 0;
   155 }

mercurial