CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / tests / TestChromeMargin.cpp
bloba06ae972693791b63dbe85bca74cdaf75f302763
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Jim Mathies <jmathies@mozilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 /* This tests the margin parsing functionality in nsAttrValue.cpp, which
40 * is accessible via nsIContentUtils, and is used in setting chromemargins
41 * to widget windows. It's located here due to linking issues in the
42 * content directory.
45 #include "TestHarness.h"
47 #ifndef MOZILLA_INTERNAL_API
48 // some of the includes make use of internal string types
49 #define nsAString_h___
50 #define nsString_h___
51 #define nsStringFwd_h___
52 #define nsReadableUtils_h___
53 class nsACString;
54 class nsAString;
55 class nsAFlatString;
56 class nsAFlatCString;
57 class nsAdoptingString;
58 class nsAdoptingCString;
59 class nsXPIDLString;
60 template<class T> class nsReadingIterator;
61 #endif
63 #include "nscore.h"
64 #include "nsIContentUtils.h"
66 #ifndef MOZILLA_INTERNAL_API
67 #undef nsString_h___
68 #undef nsAString_h___
69 #undef nsReadableUtils_h___
70 #endif
72 struct DATA {
73 bool shouldfail;
74 const char* margins;
75 int top;
76 int right;
77 int bottom;
78 int left;
81 const bool SHOULD_FAIL = true;
82 const int SHOULD_PASS = false;
84 const DATA Data[] = {
85 { SHOULD_FAIL, "", 1, 2, 3, 4 },
86 { SHOULD_FAIL, "1,0,0,0", 1, 2, 3, 4 },
87 { SHOULD_FAIL, "1,2,0,0", 1, 2, 3, 4 },
88 { SHOULD_FAIL, "1,2,3,0", 1, 2, 3, 4 },
89 { SHOULD_FAIL, "4,3,2,1", 1, 2, 3, 4 },
90 { SHOULD_FAIL, "azsasdasd", 0, 0, 0, 0 },
91 { SHOULD_FAIL, ",azsasdasd", 0, 0, 0, 0 },
92 { SHOULD_FAIL, " ", 1, 2, 3, 4 },
93 { SHOULD_FAIL, "azsdfsdfsdfsdfsdfsasdasd,asdasdasdasdasdasd,asdadasdasd,asdasdasdasd", 0, 0, 0, 0 },
94 { SHOULD_FAIL, "as,as,as,as", 0, 0, 0, 0 },
95 { SHOULD_FAIL, "0,0,0", 0, 0, 0, 0 },
96 { SHOULD_FAIL, "0,0", 0, 0, 0, 0 },
97 { SHOULD_FAIL, "4.6,1,1,1", 0, 0, 0, 0 },
98 { SHOULD_FAIL, ",,,,", 0, 0, 0, 0 },
99 { SHOULD_FAIL, "1, , , ,", 0, 0, 0, 0 },
100 { SHOULD_FAIL, "1, , ,", 0, 0, 0, 0 },
101 { SHOULD_FAIL, "@!@%^&^*()", 1, 2, 3, 4 },
102 { SHOULD_PASS, "4,3,2,1", 4, 3, 2, 1 },
103 { SHOULD_PASS, "-4,-3,-2,-1", -4, -3, -2, -1 },
104 { SHOULD_PASS, "10000,3,2,1", 10000, 3, 2, 1 },
105 { SHOULD_PASS, "4 , 3 , 2 , 1", 4, 3, 2, 1 },
106 { SHOULD_PASS, "4, 3 ,2,1", 4, 3, 2, 1 },
107 { SHOULD_FAIL, "4,3,2,10000000000000 --", 4, 3, 2, 10000000000000 },
108 { SHOULD_PASS, "4,3,2,1000", 4, 3, 2, 1000 },
109 { SHOULD_PASS, "2147483647,3,2,1000", 2147483647, 3, 2, 1000 },
110 { SHOULD_PASS, "2147483647,2147483647,2147483647,2147483647", 2147483647, 2147483647, 2147483647, 2147483647 },
111 { SHOULD_PASS, "-2147483647,3,2,1000", -2147483647, 3, 2, 1000 },
112 { SHOULD_FAIL, "2147483648,3,2,1000", 1, 3, 2, 1000 },
113 { 0, nsnull, 0, 0, 0, 0 }
116 void DoAttrValueTest()
118 nsCOMPtr<nsIContentUtils> utils =
119 do_GetService("@mozilla.org/content/contentutils;1");
121 if (!utils)
122 fail("No nsIContentUtils");
124 int idx = -1;
125 bool didFail = false;
126 while (Data[++idx].margins) {
127 nsAutoString str;
128 str.AssignLiteral(Data[idx].margins);
129 nsIntMargin values(99,99,99,99);
130 bool result = utils->ParseIntMarginValue(str, values);
132 // if the parse fails
133 if (!result) {
134 if (Data[idx].shouldfail)
135 continue;
136 fail(Data[idx].margins);
137 didFail = true;
138 printf("*1\n");
139 continue;
142 if (Data[idx].shouldfail) {
143 if (Data[idx].top == values.top &&
144 Data[idx].right == values.right &&
145 Data[idx].bottom == values.bottom &&
146 Data[idx].left == values.left) {
147 // not likely
148 fail(Data[idx].margins);
149 didFail = true;
150 printf("*2\n");
151 continue;
153 // good failure, parse failed and that's what we expected.
154 continue;
156 #if 0
157 printf("%d==%d %d==%d %d==%d %d==%d\n",
158 Data[idx].top, values.top,
159 Data[idx].right, values.right,
160 Data[idx].bottom, values.bottom,
161 Data[idx].left, values.left);
162 #endif
163 if (Data[idx].top == values.top &&
164 Data[idx].right == values.right &&
165 Data[idx].bottom == values.bottom &&
166 Data[idx].left == values.left) {
167 // good parse results
168 continue;
170 else {
171 fail(Data[idx].margins);
172 didFail = true;
173 printf("*3\n");
174 continue;
178 if (!didFail)
179 passed("nsAttrValue margin parsing tests passed.");
182 int main(int argc, char** argv)
184 ScopedXPCOM xpcom("");
185 if (xpcom.failed())
186 return 1;
187 DoAttrValueTest();
188 return 0;