TCFlowLayout.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. unit TCFlowLayout;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, testregistry, TCFresnelCSS, Fresnel.Controls, Fresnel.DOM, Fresnel.Classes;
  6. type
  7. { TTestFlowLayout }
  8. TTestFlowLayout = class(TCustomTestFresnelCSS)
  9. published
  10. procedure TestFlowLayout_BodyDiv;
  11. procedure TestFlowLayout_Slider_WithoutRangePoint;
  12. procedure TestFlowLayout_SliderRangePoint;
  13. procedure TestMarginPercentage;
  14. procedure TestPaddingPercentage;
  15. procedure TestPositionAbsolute_Right_WidthAuto;
  16. procedure TestPositionAbsolute_DivDefaultPosBehindStatic;
  17. procedure TestPositionAbsolute_DivDefaultPosBehindRelative;
  18. procedure TestPositionAbsolute_Left_ScrollWidth;
  19. // todo procedure TestPositionAbsolute_DivTop100Percent;
  20. // todo: test break line
  21. end;
  22. implementation
  23. { TTestFlowLayout }
  24. procedure TTestFlowLayout.TestFlowLayout_BodyDiv;
  25. var
  26. Body: TBody;
  27. Div1: TDiv;
  28. r: TFresnelRect;
  29. begin
  30. Body:=TBody.Create(Viewport);
  31. Body.Name:='Body';
  32. Body.Parent:=Viewport;
  33. Div1:=TDiv.Create(Viewport);
  34. Div1.Name:='Div1';
  35. Div1.Parent:=Body;
  36. Viewport.Stylesheet.Text:=LinesToStr([
  37. '#Div1 { width: 20px; height: 10px; }'
  38. ]);
  39. Viewport.Draw;
  40. // body
  41. AssertEquals('Body.Rendered',true,Body.Rendered);
  42. AssertEquals('Body.GetComputedString(fcaBoxSizing)','content-box',Body.GetComputedString(fcaBoxSizing));
  43. AssertEquals('Body.GetComputedString(fcaDisplay)','block',Body.GetComputedString(fcaDisplay));
  44. AssertEquals('Body.GetComputedString(fcaFloat)','none',Body.GetComputedString(fcaFloat));
  45. AssertEquals('Body.GetComputedString(fcaLineHeight)','normal',Body.GetComputedString(fcaLineHeight));
  46. AssertEquals('Body.GetComputedString(fcaPosition)','static',Body.GetComputedString(fcaPosition));
  47. AssertEquals('Body.GetComputedString(fcaZIndex)','auto',Body.GetComputedString(fcaZIndex));
  48. AssertEquals('Body.GetComputedString(fcaMarginLeft)','8px',Body.GetComputedString(fcaMarginLeft));
  49. AssertEquals('Body.GetComputedString(fcaMarginTop)','8px',Body.GetComputedString(fcaMarginTop));
  50. r:=Body.UsedBorderBox;
  51. AssertEquals('Body.UsedBorderBox.Left',8,r.Left);
  52. AssertEquals('Body.UsedBorderBox.Top',8,r.Top);
  53. AssertEquals('Body.UsedBorderBox.Right',792,r.Right);
  54. AssertEquals('Body.UsedBorderBox.Bottom',18,r.Bottom);
  55. // div1
  56. AssertEquals('Div1.Rendered',true,Div1.Rendered);
  57. AssertEquals('Div1.GetComputedString(fcaWidth)','20px',Div1.GetComputedString(fcaWidth));
  58. AssertEquals('Div1.GetComputedString(fcaHeight)','10px',Div1.GetComputedString(fcaHeight));
  59. AssertEquals('Div1.GetComputedString(fcaBoxSizing)','content-box',Div1.GetComputedString(fcaBoxSizing));
  60. AssertEquals('Div1.GetComputedString(fcaDisplay)','block',Div1.GetComputedString(fcaDisplay));
  61. AssertEquals('Div1.GetComputedString(fcaFloat)','none',Div1.GetComputedString(fcaFloat));
  62. AssertEquals('Div1.GetComputedString(fcaLineHeight)','normal',Div1.GetComputedString(fcaLineHeight));
  63. AssertEquals('Div1.GetComputedString(fcaPosition)','static',Div1.GetComputedString(fcaPosition));
  64. AssertEquals('Div1.GetComputedString(fcaZIndex)','auto',Div1.GetComputedString(fcaZIndex));
  65. r:=Div1.UsedBorderBox;
  66. AssertEquals('Div1.UsedBorderBox.Left',0,r.Left);
  67. AssertEquals('Div1.UsedBorderBox.Top',0,r.Top);
  68. AssertEquals('Div1.UsedBorderBox.Right',20,r.Right);
  69. AssertEquals('Div1.UsedBorderBox.Bottom',10,r.Bottom);
  70. end;
  71. procedure TTestFlowLayout.TestFlowLayout_Slider_WithoutRangePoint;
  72. var
  73. Body: TBody;
  74. SliderDiv: TDiv;
  75. begin
  76. Body:=TBody.Create(Viewport);
  77. Body.Name:='Body';
  78. Body.Parent:=Viewport;
  79. SliderDiv:=TDiv.Create(Viewport);
  80. SliderDiv.Name:='SliderDiv';
  81. SliderDiv.Parent:=Body;
  82. Viewport.Stylesheet.Text:=LinesToStr([
  83. '#SliderDiv {',
  84. ' margin: 7px 0 5px;',
  85. ' position: relative;',
  86. ' border: 1px solid #5080e0;',
  87. ' height: 9px;',
  88. ' width: 100%;',
  89. '}']);
  90. Viewport.Draw;
  91. AssertEquals('SliderDiv.Rendered',true,SliderDiv.Rendered);
  92. AssertEquals('SliderDiv.GetComputedString(fcaPosition)','relative',SliderDiv.GetComputedString(fcaPosition));
  93. AssertEquals('SliderDiv.UsedBorderBox.Left',0,SliderDiv.UsedBorderBox.Left);
  94. AssertEquals('SliderDiv.UsedBorderBox.Top',7,SliderDiv.UsedBorderBox.Top);
  95. AssertEquals('SliderDiv.UsedBorderBox.Right',786,SliderDiv.UsedBorderBox.Right);
  96. AssertEquals('SliderDiv.UsedBorderBox.Bottom',18,SliderDiv.UsedBorderBox.Bottom);
  97. end;
  98. procedure TTestFlowLayout.TestFlowLayout_SliderRangePoint;
  99. var
  100. Body: TBody;
  101. SliderDiv, RangeDiv, PointDiv: TDiv;
  102. begin
  103. Body:=TBody.Create(Viewport);
  104. Body.Name:='Body';
  105. Body.Parent:=Viewport;
  106. SliderDiv:=TDiv.Create(Viewport);
  107. SliderDiv.Name:='SliderDiv';
  108. SliderDiv.Parent:=Body;
  109. RangeDiv:=TDiv.Create(Viewport);
  110. RangeDiv.Name:='RangeDiv';
  111. RangeDiv.Parent:=SliderDiv;
  112. PointDiv:=TDiv.Create(Viewport);
  113. PointDiv.Name:='PointDiv';
  114. PointDiv.Parent:=SliderDiv;
  115. Viewport.Stylesheet.Text:=LinesToStr([
  116. '#SliderDiv {',
  117. ' margin: 7px 0 5px;',
  118. ' position: relative;',
  119. ' border: 1px solid #5080e0;',
  120. ' height: 9px;',
  121. ' width: 100%;',
  122. '}',
  123. '#RangeDiv {',
  124. ' display: block;',
  125. ' position: absolute;',
  126. ' z-index: 1;',
  127. ' font-size: .7em;',
  128. ' border: 0;',
  129. ' background-color: #5ca0cc;',
  130. ' top: 0;',
  131. ' height: 100%;',
  132. ' width: 50%;',
  133. '}',
  134. '#PointDiv {',
  135. ' position: absolute;',
  136. ' z-index: 2;',
  137. ' width: 15px;',
  138. ' height: 15px;',
  139. ' border: 1px solid #385590;',
  140. ' border-radius: 50%;',
  141. ' background-color: #fff;',
  142. ' top: -.3em;',
  143. ' margin-left: -.6em;',
  144. ' left: 50%;',
  145. '}']);
  146. Viewport.Draw;
  147. AssertEquals('SliderDiv.Rendered',true,SliderDiv.Rendered);
  148. // first check computed values
  149. AssertEquals('SliderDiv.GetComputedString(fcaPosition)','relative',SliderDiv.GetComputedString(fcaPosition));
  150. AssertEquals('SliderDiv.GetComputedString(fcaWidth)','100%',SliderDiv.GetComputedString(fcaWidth));
  151. AssertEquals('SliderDiv.GetComputedString(fcaHeight)','9px',SliderDiv.GetComputedString(fcaHeight));
  152. AssertEquals('SliderDiv.GetComputedString(fcaMarginTop)','7px',SliderDiv.GetComputedString(fcaMarginTop));
  153. AssertEquals('SliderDiv.GetComputedString(fcaMarginRight)','0',SliderDiv.GetComputedString(fcaMarginRight));
  154. AssertEquals('SliderDiv.GetComputedString(fcaMarginBottom)','5px',SliderDiv.GetComputedString(fcaMarginBottom));
  155. AssertEquals('SliderDiv.GetComputedString(fcaMarginLeft)','0',SliderDiv.GetComputedString(fcaMarginLeft));
  156. AssertEquals('RangeDiv.GetComputedString(fcaDisplay)','block',RangeDiv.GetComputedString(fcaDisplay));
  157. AssertEquals('RangeDiv.GetComputedString(fcaPosition)','absolute',RangeDiv.GetComputedString(fcaPosition));
  158. AssertEquals('RangeDiv.GetComputedString(fcaZIndex)','1',RangeDiv.GetComputedString(fcaZIndex));
  159. AssertEquals('RangeDiv.Font.GetSize',7,RangeDiv.Font.GetSize);
  160. AssertEquals('RangeDiv.GetComputedString(fcaHeight)','100%',RangeDiv.GetComputedString(fcaHeight));
  161. AssertEquals('RangeDiv.GetComputedString(fcaWidth)','50%',RangeDiv.GetComputedString(fcaWidth));
  162. AssertEquals('PointDiv.GetComputedString(fcaPosition)','absolute',PointDiv.GetComputedString(fcaPosition));
  163. AssertEquals('PointDiv.GetComputedString(fcaZIndex)','2',PointDiv.GetComputedString(fcaZIndex));
  164. AssertEquals('PointDiv.GetComputedString(fcaWidth)','15px',PointDiv.GetComputedString(fcaWidth));
  165. AssertEquals('PointDiv.GetComputedString(fcaHeight)','15px',PointDiv.GetComputedString(fcaHeight));
  166. AssertEquals('PointDiv.GetComputedString(fcaTop)','-0.3em',PointDiv.GetComputedString(fcaTop));
  167. AssertEquals('PointDiv.GetComputedString(fcaLeft)','50%',PointDiv.GetComputedString(fcaLeft));
  168. AssertEquals('PointDiv.GetComputedString(fcaMarginLeft)','-0.6em',PointDiv.GetComputedString(fcaMarginLeft));
  169. // then check layout values
  170. //writeln('TTestFlowLayout.TestFlowLayout_SliderRangePoint Body.UsedBorderBox=',Body.UsedBorderBox.ToString);
  171. AssertEquals('Body.UsedBorderBox.Left',8,Body.UsedBorderBox.Left);
  172. AssertEquals('Body.UsedBorderBox.Top',8,Body.UsedBorderBox.Top);
  173. AssertEquals('Body.UsedContentBox.Width',784,Body.UsedContentBox.Width);
  174. AssertEquals('Body.UsedBorderBox.Right',792,Body.UsedBorderBox.Right); // 800-8
  175. AssertEquals('Body.UsedBorderBox.Bottom',31,Body.UsedBorderBox.Bottom); // 8(body margin)+Slider:7(margin)+1(border)+9(height)+1(border)+5(margin)
  176. //writeln('TTestFlowLayout.TestFlowLayout_SliderRangePoint SliderDiv.UsedBorderBox=',SliderDiv.UsedBorderBox.ToString);
  177. AssertEquals('SliderDiv.UsedBorderBox.Left',0,SliderDiv.UsedBorderBox.Left);
  178. AssertEquals('SliderDiv.UsedBorderBox.Top',7,SliderDiv.UsedBorderBox.Top);
  179. AssertEquals('SliderDiv.UsedBorderBox.Right',786,SliderDiv.UsedBorderBox.Right); // 784(width:100%) + 2*1(border) + 0(margin)
  180. AssertEquals('SliderDiv.UsedBorderBox.Bottom',18,SliderDiv.UsedBorderBox.Bottom);
  181. AssertEquals('SliderDiv.UsedContentBox.Width',784,SliderDiv.UsedContentBox.Width);
  182. AssertEquals('SliderDiv.UsedContentBox.Left',1,SliderDiv.UsedContentBox.Left);
  183. AssertEquals('SliderDiv.UsedContentBox.Top',8,SliderDiv.UsedContentBox.Top);
  184. AssertEquals('SliderDiv.UsedContentBox.Right',785,SliderDiv.UsedContentBox.Right);
  185. AssertEquals('SliderDiv.UsedContentBox.Bottom',17,SliderDiv.UsedContentBox.Bottom);
  186. //writeln('TTestFlowLayout.TestFlowLayout_SliderRangePoint RangeDiv.UsedBorderBox=',RangeDiv.UsedBorderBox.ToString);
  187. AssertEquals('RangeDiv.Rendered',true,RangeDiv.Rendered);
  188. AssertEquals('RangeDiv.UsedBorderBox.Left',0,RangeDiv.UsedBorderBox.Left);
  189. AssertEquals('RangeDiv.UsedBorderBox.Top',0,RangeDiv.UsedBorderBox.Top);
  190. AssertEquals('RangeDiv.UsedBorderBox.Right',392,RangeDiv.UsedBorderBox.Right);
  191. AssertEquals('RangeDiv.UsedBorderBox.Bottom',9,RangeDiv.UsedBorderBox.Bottom);
  192. AssertEquals('RangeDiv.UsedContentBox.Left',0,RangeDiv.UsedContentBox.Left);
  193. AssertEquals('RangeDiv.UsedContentBox.Top',0,RangeDiv.UsedContentBox.Top);
  194. AssertEquals('RangeDiv.UsedContentBox.Right',392,RangeDiv.UsedContentBox.Right);
  195. AssertEquals('RangeDiv.UsedContentBox.Bottom',9,RangeDiv.UsedContentBox.Bottom);
  196. AssertEquals('PointDiv.Rendered',true,PointDiv.Rendered);
  197. AssertEquals('PointDiv.LayoutNode.MarginLeft',-6,PointDiv.LayoutNode.MarginLeft);
  198. AssertEquals('PointDiv.LayoutNode.Width',15,PointDiv.LayoutNode.Width);
  199. AssertEquals('PointDiv.LayoutNode.Height',15,PointDiv.LayoutNode.Height);
  200. AssertEquals('PointDiv.LayoutNode.Top',-3,PointDiv.LayoutNode.Top);
  201. AssertEquals('PointDiv.LayoutNode.Left',392,PointDiv.LayoutNode.Left);
  202. AssertEquals('PointDiv.UsedBorderBox.Left',386,PointDiv.UsedBorderBox.Left);
  203. AssertEquals('PointDiv.UsedBorderBox.Top',-3,PointDiv.UsedBorderBox.Top);
  204. AssertEquals('PointDiv.UsedBorderBox.Right',403,PointDiv.UsedBorderBox.Right);
  205. AssertEquals('PointDiv.UsedBorderBox.Bottom',14,PointDiv.UsedBorderBox.Bottom);
  206. AssertEquals('PointDiv.UsedContentBox.Left',387,PointDiv.UsedContentBox.Left);
  207. AssertEquals('PointDiv.UsedContentBox.Top',-2,PointDiv.UsedContentBox.Top);
  208. AssertEquals('PointDiv.UsedContentBox.Right',402,PointDiv.UsedContentBox.Right);
  209. AssertEquals('PointDiv.UsedContentBox.Bottom',13,PointDiv.UsedContentBox.Bottom);
  210. end;
  211. procedure TTestFlowLayout.TestMarginPercentage;
  212. var
  213. Body: TBody;
  214. Div1: TDiv;
  215. begin
  216. Body:=TBody.Create(Viewport);
  217. Body.Name:='Body';
  218. Body.Parent:=Viewport;
  219. Div1:=TDiv.Create(Viewport);
  220. Div1.Name:='Div1';
  221. Div1.Parent:=Body;
  222. Viewport.Stylesheet.Text:=LinesToStr([
  223. 'body {',
  224. ' margin: 0;',
  225. '}',
  226. 'div {',
  227. ' margin: 5% 10% 15% 20%;',
  228. ' height: 10px;',
  229. '}']);
  230. Viewport.Draw;
  231. AssertEquals('Body.UsedContentBox.Width',800,Body.UsedContentBox.Width);
  232. AssertEquals('Div1.Rendered',true,Div1.Rendered);
  233. //writeln('TTestFlowLayout.TestPaddingPercentage ',Div1.UsedBorderBox.ToString);
  234. AssertEquals('Div1.LayoutNode.MarginLeft',160,Div1.LayoutNode.MarginLeft);
  235. AssertEquals('Div1.LayoutNode.MarginTop',40,Div1.LayoutNode.MarginTop);
  236. AssertEquals('Div1.LayoutNode.MarginRight',80,Div1.LayoutNode.MarginRight);
  237. AssertEquals('Div1.LayoutNode.MarginBottom',120,Div1.LayoutNode.MarginBottom);
  238. AssertEquals('Div1.UsedBorderBox.Left',160,Div1.UsedBorderBox.Left);
  239. AssertEquals('Div1.UsedBorderBox.Top',40,Div1.UsedBorderBox.Top);
  240. AssertEquals('Div1.UsedBorderBox.Right',720,Div1.UsedBorderBox.Right);
  241. AssertEquals('Div1.UsedBorderBox.Bottom',50,Div1.UsedBorderBox.Bottom);
  242. end;
  243. procedure TTestFlowLayout.TestPaddingPercentage;
  244. var
  245. Body: TBody;
  246. Div1: TDiv;
  247. begin
  248. Body:=TBody.Create(Viewport);
  249. Body.Name:='Body';
  250. Body.Parent:=Viewport;
  251. Div1:=TDiv.Create(Viewport);
  252. Div1.Name:='Div1';
  253. Div1.Parent:=Body;
  254. Viewport.Stylesheet.Text:=LinesToStr([
  255. 'body {',
  256. ' margin: 0;',
  257. '}',
  258. 'div {',
  259. ' padding: 5% 10% 15% 20%;',
  260. ' height: 10px;',
  261. '}']);
  262. Viewport.Draw;
  263. AssertEquals('Body.UsedContentBox.Width',800,Body.UsedContentBox.Width);
  264. AssertEquals('Div1.Rendered',true,Div1.Rendered);
  265. //writeln('TTestFlowLayout.TestPaddingPercentage ',Div1.UsedBorderBox.ToString);
  266. AssertEquals('Div1.LayoutNode.PaddingLeft',160,Div1.LayoutNode.PaddingLeft);
  267. AssertEquals('Div1.LayoutNode.PaddingTop',40,Div1.LayoutNode.PaddingTop);
  268. AssertEquals('Div1.LayoutNode.PaddingRight',80,Div1.LayoutNode.PaddingRight);
  269. AssertEquals('Div1.LayoutNode.PaddingBottom',120,Div1.LayoutNode.PaddingBottom);
  270. AssertEquals('Div1.UsedContentBox.Left',160,Div1.UsedContentBox.Left);
  271. AssertEquals('Div1.UsedContentBox.Top',40,Div1.UsedContentBox.Top);
  272. AssertEquals('Div1.UsedContentBox.Right',720,Div1.UsedContentBox.Right);
  273. AssertEquals('Div1.UsedContentBox.Bottom',50,Div1.UsedContentBox.Bottom);
  274. end;
  275. procedure TTestFlowLayout.TestPositionAbsolute_Right_WidthAuto;
  276. var
  277. Body: TBody;
  278. Div1, Div2, Div3: TDiv;
  279. begin
  280. Body:=TBody.Create(Viewport);
  281. Body.Name:='Body';
  282. Body.Parent:=Viewport;
  283. Div1:=TDiv.Create(Viewport);
  284. Div1.Name:='Div1';
  285. Div1.Parent:=Body;
  286. Div2:=TDiv.Create(Viewport);
  287. Div2.Name:='Div2';
  288. Div2.Parent:=Div1;
  289. Div3:=TDiv.Create(Viewport);
  290. Div3.Name:='Div3';
  291. Div3.Parent:=Div2;
  292. Viewport.Stylesheet.Text:=LinesToStr([
  293. 'body {',
  294. ' margin: 0;', // content width = 800
  295. '}',
  296. '#Div1 {',
  297. ' position: absolute;',
  298. ' height: 120px;', // content height = 120
  299. ' right: 30px;',
  300. // content-width is on top-down run 0, and on second run max-content, which is width of Div3, so 100px
  301. '}',
  302. '#Div2 {',
  303. ' margin: 10%;', // 10% of 0 on top-down run!
  304. ' padding: 10%;', // 10% of 0 on top-down run!
  305. ' box-sizing: border-box;',
  306. ' width: 80%;', // 30% of 360 = 108, content width = 108 - 2*padding = 88
  307. ' height: 60%;', // same as width 30%, content height = 88
  308. '}',
  309. '#Div3 {',
  310. ' width: 100px;',
  311. ' height: 30px;',
  312. '}']);
  313. Viewport.Draw;
  314. AssertEquals('Body.UsedContentBox.Width',800,Body.UsedContentBox.Width);
  315. AssertEquals('Div3.Rendered',true,Div3.Rendered);
  316. //writeln('TTestFlowLayout.TestPositionAbsolute_Right_WidthAuto Div3.UsedBorderBox=',Div3.UsedBorderBox.ToString);
  317. AssertEquals('Div3.LayoutNode.Width',100,Div3.LayoutNode.Width);
  318. AssertEquals('Div3.LayoutNode.Height',30,Div3.LayoutNode.Height);
  319. //writeln('TTestFlowLayout.TestPositionAbsolute_Right_WidthAuto Div1.UsedBorderBox=',Div1.UsedBorderBox.ToString);
  320. AssertEquals('Div1.GetComputedString(fcaPosition)','absolute',Div1.GetComputedString(fcaPosition));
  321. AssertEquals('Div1.GetComputedString(fcaBoxSizing)','content-box',Div1.GetComputedString(fcaBoxSizing));
  322. AssertEquals('Div1.GetComputedString(fcaWidth)','auto',Div1.GetComputedString(fcaWidth));
  323. AssertEquals('Div1.LayoutNode.Height',120,Div1.LayoutNode.Height);
  324. AssertEquals('Div1.LayoutNode.Width',100,Div1.LayoutNode.Width);
  325. AssertEquals('Div1.LayoutNode.Right',30,Div1.LayoutNode.Right);
  326. AssertEquals('Div1.LayoutNode.Left',670,Div1.LayoutNode.Left); // 800-30-100 = 670
  327. //writeln('TTestFlowLayout.TestPositionAbsolute_Right_WidthAuto Div2.UsedBorderBox=',Div2.UsedBorderBox.ToString);
  328. AssertEquals('Div2.GetComputedString(fcaPosition)','static',Div2.GetComputedString(fcaPosition));
  329. AssertEquals('Div2.GetComputedString(fcaBoxSizing)','border-box',Div2.GetComputedString(fcaBoxSizing));
  330. AssertEquals('Div2.GetComputedString(fcaWidth)','80%',Div2.GetComputedString(fcaWidth));
  331. AssertEquals('Div2.GetComputedString(fcaHeight)','60%',Div2.GetComputedString(fcaHeight));
  332. AssertEquals('Div2.GetComputedString(fcaMarginLeft)','10%',Div2.GetComputedString(fcaMarginLeft));
  333. AssertEquals('Div2.GetComputedString(fcaMarginTop)','10%',Div2.GetComputedString(fcaMarginTop));
  334. AssertEquals('Div2.GetComputedString(fcaMarginRight)','10%',Div2.GetComputedString(fcaMarginRight));
  335. AssertEquals('Div2.GetComputedString(fcaMarginBottom)','10%',Div2.GetComputedString(fcaMarginBottom));
  336. AssertEquals('Div2.GetComputedString(fcaPaddingLeft)','10%',Div2.GetComputedString(fcaPaddingLeft));
  337. AssertEquals('Div2.GetComputedString(fcaPaddingTop)','10%',Div2.GetComputedString(fcaPaddingTop));
  338. AssertEquals('Div2.GetComputedString(fcaPaddingRight)','10%',Div2.GetComputedString(fcaPaddingRight));
  339. AssertEquals('Div2.GetComputedString(fcaPaddingBottom)','10%',Div2.GetComputedString(fcaPaddingBottom));
  340. AssertEquals('Div2.LayoutNode.MarginLeft',10,Div2.LayoutNode.MarginLeft); // 10% of Div2.width 100
  341. AssertEquals('Div2.LayoutNode.MarginRight',10,Div2.LayoutNode.MarginRight);
  342. AssertEquals('Div2.LayoutNode.MarginTop',10,Div2.LayoutNode.MarginTop); // 10% of Div2.width 100
  343. AssertEquals('Div2.LayoutNode.MarginBottom',10,Div2.LayoutNode.MarginBottom);
  344. AssertEquals('Div2.LayoutNode.PaddingLeft',10,Div2.LayoutNode.PaddingLeft); // 10% of Div2.width 100
  345. AssertEquals('Div2.LayoutNode.PaddingRight',10,Div2.LayoutNode.PaddingRight);
  346. AssertEquals('Div2.LayoutNode.PaddingTop',10,Div2.LayoutNode.PaddingTop); // 10% of Div2.width 100
  347. AssertEquals('Div2.LayoutNode.PaddingBottom',10,Div2.LayoutNode.PaddingBottom);
  348. AssertEquals('Div2.LayoutNode.Width',60,Div2.LayoutNode.Width);
  349. AssertEquals('Div2.LayoutNode.Height',52,Div2.LayoutNode.Height);
  350. end;
  351. procedure TTestFlowLayout.TestPositionAbsolute_DivDefaultPosBehindStatic;
  352. var
  353. Body: TBody;
  354. Div1: TDiv;
  355. Label1, Label2: TLabel;
  356. begin
  357. Body:=TBody.Create(Viewport);
  358. Body.Name:='Body';
  359. Body.Parent:=Viewport;
  360. Label1:=TLabel.Create(Viewport);
  361. Label1.Name:='Label1';
  362. Label1.Caption:='Label1';
  363. Label1.Parent:=Body;
  364. Div1:=TDiv.Create(Viewport);
  365. Div1.Name:='Div1';
  366. Div1.Parent:=Body;
  367. Label2:=TLabel.Create(Viewport);
  368. Label2.Name:='Label2';
  369. Label2.Caption:='Label2';
  370. Label2.Parent:=Body;
  371. Viewport.Stylesheet.Text:=LinesToStr([
  372. 'body {',
  373. ' margin: 0;', // content width = 800
  374. ' font-size: 20px;',
  375. '}',
  376. '#Div1 {',
  377. ' position: absolute;',
  378. ' width: 30px; height: 20px;',
  379. '}']);
  380. // Div1 is absolute to the Viewport, because Body position is static
  381. // Div1 default position is below Label1, because Div1 display is block
  382. // Label2 is right behind Label1
  383. // Body width/height includes Label1 and Label2, but not Div1
  384. Viewport.Draw;
  385. AssertEquals('Body.UsedContentBox.Width',800,Body.UsedContentBox.Width);
  386. AssertEquals('Div1.Rendered',true,Div1.Rendered);
  387. //writeln('TTestFlowLayout.TestPositionAbsolute_DivDefaultPos Label1.UsedBorderBox=',Label1.UsedBorderBox.ToString);
  388. AssertEquals('Label1.GetComputedString(fcaPosition)','static',Label1.GetComputedString(fcaPosition));
  389. AssertEquals('Label1.GetComputedString(fcaDisplay)','inline flow',Label1.GetComputedString(fcaDisplay));
  390. AssertEquals('Label1.GetComputedString(fcaWidth)','auto',Label1.GetComputedString(fcaWidth));
  391. AssertEquals('Label1.UsedBorderBox.Top',0,Label1.UsedBorderBox.Left);
  392. AssertEquals('Label1.UsedBorderBox.Left',0,Label1.UsedBorderBox.Left);
  393. AssertEquals('Label1.UsedBorderBox.Height',23,Label1.UsedBorderBox.Height);
  394. //writeln('TTestFlowLayout.TestPositionAbsolute_DivDefaultPos Label2.UsedBorderBox=',Label2.UsedBorderBox.ToString);
  395. AssertEquals('Label2.GetComputedString(fcaPosition)','static',Label2.GetComputedString(fcaPosition));
  396. AssertEquals('Label2.GetComputedString(fcaDisplay)','inline flow',Label2.GetComputedString(fcaDisplay));
  397. AssertEquals('Label2.GetComputedString(fcaWidth)','auto',Label2.GetComputedString(fcaWidth));
  398. AssertEquals('Label2.UsedBorderBox.Height',23,Label2.UsedBorderBox.Height);
  399. AssertEquals('Label2.UsedBorderBox.Top',0,Label2.UsedBorderBox.Top);
  400. AssertEquals('Label2.UsedBorderBox.Left',Label1.UsedBorderBox.Right,Label2.UsedBorderBox.Left);
  401. //writeln('TTestFlowLayout.TestPositionAbsolute_DivDefaultPos Div1.UsedBorderBox=',Div1.UsedBorderBox.ToString);
  402. AssertEquals('Div1.UsedBorderBox.Width',30,Div1.UsedBorderBox.Width);
  403. AssertEquals('Div1.UsedBorderBox.Height',20,Div1.UsedBorderBox.Height);
  404. AssertEquals('Div1.UsedBorderBox.Left',0,Div1.UsedBorderBox.Left);
  405. AssertEquals('Div1.UsedBorderBox.Top',23,Div1.UsedBorderBox.Top);
  406. end;
  407. procedure TTestFlowLayout.TestPositionAbsolute_DivDefaultPosBehindRelative;
  408. var
  409. Body: TBody;
  410. Div1: TDiv;
  411. Label1: TLabel;
  412. begin
  413. Body:=TBody.Create(Viewport);
  414. Body.Name:='Body';
  415. Body.Parent:=Viewport;
  416. Label1:=TLabel.Create(Viewport);
  417. Label1.Name:='Label1';
  418. Label1.Caption:='Label1';
  419. Label1.Parent:=Body;
  420. Div1:=TDiv.Create(Viewport);
  421. Div1.Name:='Div1';
  422. Div1.Parent:=Body;
  423. Viewport.Stylesheet.Text:=LinesToStr([
  424. 'body {',
  425. ' margin: 0;', // content width = 800
  426. ' font-size: 20px;',
  427. '}',
  428. '#Label1 {',
  429. ' position: relative; top:10px;',
  430. '}',
  431. '#Div1 {',
  432. ' position: absolute;',
  433. ' width: 30px; height: 20px;',
  434. '}']);
  435. // Div1 is absolute to the Viewport, because Body position is static
  436. // Div1 default position is below Label1 static position, because Div1 display is block
  437. // Body width/height includes Label1 static bounds, and not Div1
  438. Viewport.Draw;
  439. AssertEquals('Body.UsedContentBox.Width',800,Body.UsedContentBox.Width);
  440. AssertEquals('Div1.Rendered',true,Div1.Rendered);
  441. //writeln('TTestFlowLayout.TestPositionAbsolute_DivDefaultPos Label1.UsedBorderBox=',Label1.UsedBorderBox.ToString);
  442. AssertEquals('Label1.GetComputedString(fcaPosition)','relative',Label1.GetComputedString(fcaPosition));
  443. AssertEquals('Label1.GetComputedString(fcaDisplay)','inline flow',Label1.GetComputedString(fcaDisplay));
  444. AssertEquals('Label1.GetComputedString(fcaWidth)','auto',Label1.GetComputedString(fcaWidth));
  445. AssertEquals('Label1.GetComputedString(fcaTop)','10px',Label1.GetComputedString(fcaTop));
  446. AssertEquals('Label1.UsedBorderBox.Top',10,Label1.UsedBorderBox.Top);
  447. AssertEquals('Label1.UsedBorderBox.Left',0,Label1.UsedBorderBox.Left);
  448. AssertEquals('Label1.UsedBorderBox.Height',23,Label1.UsedBorderBox.Height);
  449. AssertEquals('Body.UsedBorderBox.Height',23,Body.UsedBorderBox.Height);
  450. //writeln('TTestFlowLayout.TestPositionAbsolute_DivDefaultPos Div1.UsedBorderBox=',Div1.UsedBorderBox.ToString);
  451. AssertEquals('Div1.UsedBorderBox.Width',30,Div1.UsedBorderBox.Width);
  452. AssertEquals('Div1.UsedBorderBox.Height',20,Div1.UsedBorderBox.Height);
  453. AssertEquals('Div1.UsedBorderBox.Left',0,Div1.UsedBorderBox.Left);
  454. AssertEquals('Div1.UsedBorderBox.Top',23,Div1.UsedBorderBox.Top);
  455. end;
  456. procedure TTestFlowLayout.TestPositionAbsolute_Left_ScrollWidth;
  457. var
  458. Body: TBody;
  459. Div1: TDiv;
  460. begin
  461. Body:=TBody.Create(Viewport);
  462. Body.Name:='Body';
  463. Body.Parent:=Viewport;
  464. Div1:=TDiv.Create(Viewport);
  465. Div1.Name:='Div1';
  466. Div1.Parent:=Body;
  467. Viewport.Stylesheet.Text:=LinesToStr([
  468. 'body {',
  469. ' position: relative;', // make body a container
  470. ' width: 100px;',
  471. ' padding: 23px;', // padding is irrelevant for child positioned absolute
  472. ' overflow: auto;',
  473. '}',
  474. '#Div1 {',
  475. ' position: absolute;',
  476. ' margin: 2px;',
  477. ' border: 1px;',
  478. ' padding: 4px;',
  479. ' left: 140px;', // marginbox left, relative to container's paddingbox
  480. ' width: 30px;',
  481. ' height: 20px;',
  482. '}']);
  483. Viewport.Draw;
  484. AssertEquals('Body.GetComputedString(fcaOverflowX)','auto',Body.GetComputedString(fcaOverflowX));
  485. AssertEquals('Body.GetComputedString(fcaOverflowY)','auto',Body.GetComputedString(fcaOverflowY));
  486. AssertEquals('Body.GetComputedString(fcaOverflow)','auto',Body.GetComputedString(fcaOverflow));
  487. AssertEquals('Body.UsedContentBox.Width',100,Body.UsedContentBox.Width);
  488. AssertEquals('Body.UsedBorderBox.Width',146,Body.UsedBorderBox.Width); // width+padding
  489. AssertEquals('Body.UsedBorderBox.Height',46,Body.UsedBorderBox.Height); // padding is bigger than Div1
  490. if Div1.LayoutNode.Container<>Body then
  491. Fail('Div1.LayoutNode.Container<>Body');
  492. AssertEquals('Div1.UsedBorderBox.Left',142,Div1.UsedBorderBox.Left);
  493. AssertEquals('Div1.UsedBorderBox.Top',2,Div1.UsedBorderBox.Top);
  494. AssertEquals('Div1.UsedBorderBox.Width',40,Div1.UsedBorderBox.Width);
  495. AssertEquals('Div1.UsedBorderBox.Height',30,Div1.UsedBorderBox.Height);
  496. AssertEquals('Body.LayoutNode.ScrollWidth',184,Body.LayoutNode.ScrollWidth);
  497. AssertEquals('Body.LayoutNode.ScrollHeight',34,Body.LayoutNode.ScrollHeight);
  498. end;
  499. Initialization
  500. RegisterTests([TTestFlowLayout]);
  501. end.