DefaultLayoutTest.cs 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using NUnit.Framework;
  5. #if NET_2_0
  6. using System.Collections.Generic;
  7. #endif
  8. namespace MonoTests.System.Windows.Forms
  9. {
  10. [TestFixture]
  11. public class DefaultLayoutTest : TestHelper
  12. {
  13. int event_count;
  14. LayoutEventArgs most_recent_args;
  15. void p_Layout (object sender, LayoutEventArgs e)
  16. {
  17. event_count ++;
  18. most_recent_args = e;
  19. }
  20. [Test]
  21. public void AnchorLayoutEvents ()
  22. {
  23. Panel p;
  24. Button b;
  25. p = new Panel ();
  26. b = new Button ();
  27. p.Controls.Add (b);
  28. p.Layout += new LayoutEventHandler (p_Layout);
  29. /* set the button's anchor to something different */
  30. b.Anchor = AnchorStyles.Bottom;
  31. Assert.AreEqual (1, event_count, "1");
  32. Assert.AreEqual ("Anchor", most_recent_args.AffectedProperty, "2");
  33. /* reset it to something new with the panel's layout suspended */
  34. event_count = 0;
  35. p.SuspendLayout ();
  36. b.Anchor = AnchorStyles.Top;
  37. Assert.AreEqual (0, event_count, "3");
  38. p.ResumeLayout ();
  39. Assert.AreEqual (1, event_count, "4");
  40. Assert.AreEqual (null, most_recent_args.AffectedProperty, "5");
  41. /* with the anchor style set to something, resize the parent */
  42. event_count = 0;
  43. p.Size = new Size (500, 500);
  44. Assert.AreEqual (1, event_count, "6");
  45. Assert.AreEqual ("Bounds", most_recent_args.AffectedProperty, "7");
  46. /* now try it with layout suspended */
  47. event_count = 0;
  48. p.SuspendLayout ();
  49. p.Size = new Size (400, 400);
  50. Assert.AreEqual (0, event_count, "8");
  51. p.ResumeLayout ();
  52. Assert.AreEqual (1, event_count, "9");
  53. Assert.AreEqual (null, most_recent_args.AffectedProperty, "10");
  54. /* with the anchor style set to something, resize the child */
  55. event_count = 0;
  56. b.Size = new Size (100, 100);
  57. Assert.AreEqual (1, event_count, "11");
  58. Assert.AreEqual ("Bounds", most_recent_args.AffectedProperty, "12");
  59. /* and again with layout suspended */
  60. event_count = 0;
  61. p.SuspendLayout ();
  62. b.Size = new Size (200, 200);
  63. Assert.AreEqual (0, event_count, "13");
  64. p.ResumeLayout ();
  65. Assert.AreEqual (1, event_count, "14");
  66. Assert.AreEqual (null, most_recent_args.AffectedProperty, "15");
  67. }
  68. [Test]
  69. public void AnchorTopLeftTest ()
  70. {
  71. Form f = new Form ();
  72. f.ShowInTaskbar = false;
  73. f.Size = new Size (200, 200);
  74. Button b = new Button ();
  75. b.Size = new Size (100, 100);
  76. b.Anchor = AnchorStyles.Top | AnchorStyles.Left;
  77. f.Controls.Add (b);
  78. Assert.AreEqual (0, b.Left, "1");
  79. Assert.AreEqual (0, b.Top, "2");
  80. f.Size = new Size (300, 300);
  81. Assert.AreEqual (0, b.Left, "3");
  82. Assert.AreEqual (0, b.Top, "4");
  83. f.Dispose ();
  84. }
  85. [Test]
  86. public void AnchorTopRightTest ()
  87. {
  88. Form f = new Form ();
  89. f.ShowInTaskbar = false;
  90. f.Size = new Size (200, 200);
  91. Button b = new Button ();
  92. b.Size = new Size (100, 100);
  93. b.Anchor = AnchorStyles.Top | AnchorStyles.Right;
  94. f.Controls.Add (b);
  95. Assert.AreEqual (0, b.Left, "1");
  96. Assert.AreEqual (0, b.Top, "2");
  97. f.Size = new Size (300, 300);
  98. Assert.AreEqual (100, b.Left, "3");
  99. Assert.AreEqual (0, b.Top, "4");
  100. f.Dispose ();
  101. }
  102. [Test]
  103. public void AnchorLeftRightTest ()
  104. {
  105. Form f = new Form ();
  106. f.ShowInTaskbar = false;
  107. f.Size = new Size (200, 200);
  108. Button b = new Button ();
  109. b.Size = new Size (100, 100);
  110. b.Anchor = AnchorStyles.Left | AnchorStyles.Right;
  111. f.Controls.Add (b);
  112. Assert.AreEqual (0, b.Left, "1");
  113. Assert.AreEqual (100, b.Right, "2");
  114. f.Size = new Size (300, 300);
  115. Assert.AreEqual (0, b.Left, "3");
  116. Assert.AreEqual (200, b.Right, "4");
  117. f.Dispose ();
  118. }
  119. [Test]
  120. public void AnchorBottomLeftTest ()
  121. {
  122. Form f = new Form ();
  123. f.ShowInTaskbar = false;
  124. f.Size = new Size (200, 200);
  125. Button b = new Button ();
  126. b.Size = new Size (100, 100);
  127. b.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
  128. f.Controls.Add (b);
  129. Assert.AreEqual (0, b.Left, "1");
  130. Assert.AreEqual (0, b.Top, "2");
  131. f.Size = new Size (300, 300);
  132. Assert.AreEqual (0, b.Left, "3");
  133. Assert.AreEqual (100, b.Top, "4");
  134. f.Dispose ();
  135. }
  136. [Test]
  137. public void AnchorBottomRightTest ()
  138. {
  139. Form f = new Form ();
  140. f.ShowInTaskbar = false;
  141. f.Size = new Size (200, 200);
  142. Button b = new Button ();
  143. b.Size = new Size (100, 100);
  144. b.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
  145. f.Controls.Add (b);
  146. Assert.AreEqual (0, b.Left, "1");
  147. Assert.AreEqual (0, b.Top, "2");
  148. f.Size = new Size (300, 300);
  149. Assert.AreEqual (100, b.Left, "3");
  150. Assert.AreEqual (100, b.Top, "4");
  151. f.Dispose ();
  152. }
  153. [Test]
  154. public void AnchorTopBottomTest ()
  155. {
  156. Form f = new Form ();
  157. f.ShowInTaskbar = false;
  158. f.Size = new Size (200, 200);
  159. Button b = new Button ();
  160. b.Size = new Size (100, 100);
  161. b.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
  162. f.Controls.Add (b);
  163. Assert.AreEqual (0, b.Top, "1");
  164. Assert.AreEqual (100, b.Bottom, "2");
  165. f.Size = new Size (300, 300);
  166. Assert.AreEqual (0, b.Top, "3");
  167. Assert.AreEqual (200, b.Bottom, "4");
  168. f.Dispose ();
  169. }
  170. // Unit test version of the test case in bug #80336
  171. [Test]
  172. public void AnchorSuspendLayoutTest ()
  173. {
  174. Form f = new Form ();
  175. f.ShowInTaskbar = false;
  176. f.SuspendLayout ();
  177. Button b = new Button ();
  178. b.Size = new Size (100, 100);
  179. f.Controls.Add (b);
  180. f.Size = new Size (200, 200);
  181. b.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  182. Assert.AreEqual (0, b.Top, "1");
  183. Assert.AreEqual (0, b.Left, "2");
  184. f.Size = new Size (300, 300);
  185. Assert.AreEqual (0, b.Top, "3");
  186. Assert.AreEqual (0, b.Left, "4");
  187. f.ResumeLayout();
  188. Assert.AreEqual (100, b.Top, "5");
  189. Assert.AreEqual (100, b.Left, "6");
  190. f.Dispose ();
  191. }
  192. // another variant of AnchorSuspendLayoutTest1, with
  193. // the SuspendLayout moved after the Anchor
  194. // assignment.
  195. [Test]
  196. public void AnchorSuspendLayoutTest2 ()
  197. {
  198. Form f = new Form ();
  199. f.ShowInTaskbar = false;
  200. Button b = new Button ();
  201. b.Size = new Size (100, 100);
  202. f.Controls.Add (b);
  203. f.Size = new Size (200, 200);
  204. b.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  205. Assert.AreEqual (0, b.Top, "1");
  206. Assert.AreEqual (0, b.Left, "2");
  207. f.SuspendLayout ();
  208. f.Size = new Size (300, 300);
  209. Assert.AreEqual (0, b.Top, "3");
  210. Assert.AreEqual (0, b.Left, "4");
  211. f.ResumeLayout();
  212. Assert.AreEqual (100, b.Top, "5");
  213. Assert.AreEqual (100, b.Left, "6");
  214. f.Dispose ();
  215. }
  216. // yet another variant, this time with no Suspend/Resume.
  217. [Test]
  218. public void AnchorSuspendLayoutTest3 ()
  219. {
  220. Form f = new Form ();
  221. f.ShowInTaskbar = false;
  222. Button b = new Button ();
  223. b.Size = new Size (100, 100);
  224. f.Controls.Add (b);
  225. f.Size = new Size (200, 200);
  226. b.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  227. Assert.AreEqual (0, b.Top, "1");
  228. Assert.AreEqual (0, b.Left, "2");
  229. f.Size = new Size (300, 300);
  230. Assert.AreEqual (100, b.Top, "5");
  231. Assert.AreEqual (100, b.Left, "6");
  232. f.Dispose ();
  233. }
  234. private string event_raised = string.Empty;
  235. [Test]
  236. public void TestAnchorDockInteraction ()
  237. {
  238. Panel p = new Panel ();
  239. p.DockChanged += new EventHandler (DockChanged_Handler);
  240. Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A1");
  241. Assert.AreEqual (DockStyle.None, p.Dock, "A2");
  242. p.Dock = DockStyle.Right;
  243. Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A3");
  244. Assert.AreEqual (DockStyle.Right, p.Dock, "A4");
  245. Assert.AreEqual ("DockStyleChanged", event_raised, "A5");
  246. event_raised = string.Empty;
  247. p.Anchor = AnchorStyles.Bottom;
  248. Assert.AreEqual (AnchorStyles.Bottom, p.Anchor, "A6");
  249. Assert.AreEqual (DockStyle.None, p.Dock, "A7");
  250. Assert.AreEqual (string.Empty, event_raised, "A8");
  251. p.Dock = DockStyle.Fill;
  252. Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A9");
  253. Assert.AreEqual (DockStyle.Fill, p.Dock, "A10");
  254. Assert.AreEqual ("DockStyleChanged", event_raised, "A11");
  255. event_raised = string.Empty;
  256. p.Anchor = AnchorStyles.Top | AnchorStyles.Left;
  257. Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A12");
  258. Assert.AreEqual (DockStyle.Fill, p.Dock, "A13");
  259. Assert.AreEqual (string.Empty, event_raised, "A14");
  260. p.Dock = DockStyle.None;
  261. Assert.AreEqual (AnchorStyles.Top | AnchorStyles.Left, p.Anchor, "A15");
  262. Assert.AreEqual (DockStyle.None, p.Dock, "A16");
  263. Assert.AreEqual ("DockStyleChanged", event_raised, "A17");
  264. event_raised = string.Empty;
  265. p.Anchor = AnchorStyles.Bottom;
  266. p.Dock = DockStyle.None;
  267. Assert.AreEqual (AnchorStyles.Bottom, p.Anchor, "A18");
  268. Assert.AreEqual (DockStyle.None, p.Dock, "A19");
  269. Assert.AreEqual (string.Empty, event_raised, "A20");
  270. }
  271. public void DockChanged_Handler (object sender, EventArgs e)
  272. {
  273. event_raised += "DockStyleChanged";
  274. }
  275. [Test] // bug #80917
  276. [Category ("NotWorking")]
  277. public void BehaviorOverriddenDisplayRectangle ()
  278. {
  279. Control c = new Control ();
  280. c.Anchor |= AnchorStyles.Bottom;
  281. c.Size = new Size (100, 100);
  282. Form f = new DisplayRectangleForm ();
  283. f.Controls.Add (c);
  284. f.ShowInTaskbar = false;
  285. f.Show ();
  286. Assert.AreEqual (new Size (100, 100), c.Size, "A1");
  287. f.Dispose ();
  288. }
  289. private class DisplayRectangleForm : Form
  290. {
  291. public override Rectangle DisplayRectangle
  292. {
  293. get { return Rectangle.Empty; }
  294. }
  295. }
  296. [Test] // bug 80912
  297. public void AnchoredControlWithZeroWidthAndHeight ()
  298. {
  299. Form f = new Form ();
  300. f.ShowInTaskbar = false;
  301. Control c = new Control ();
  302. c.Anchor = AnchorStyles.Left | AnchorStyles.Right;
  303. f.Controls.Add (c);
  304. Assert.AreEqual (new Rectangle (0, 0, 0, 0), c.Bounds, "N1");
  305. }
  306. [Test] // bug 81694
  307. public void TestNestedControls ()
  308. {
  309. MainForm f = new MainForm ();
  310. f.ShowInTaskbar = false;
  311. f.Show ();
  312. Assert.AreEqual (new Rectangle (210, 212, 75, 23), f._userControl._button2.Bounds, "K1");
  313. f.Dispose ();
  314. }
  315. [Test] // bug 81695
  316. public void TestNestedControls2 ()
  317. {
  318. MainForm f = new MainForm ();
  319. f.ShowInTaskbar = false;
  320. f.Show ();
  321. Size s = f.Size;
  322. f.Size = new Size (10, 10);
  323. f.Size = s;
  324. Assert.AreEqual (new Rectangle (210, 212, 75, 23), f._userControl._button2.Bounds, "K1");
  325. f.Dispose ();
  326. }
  327. private class MainForm : Form
  328. {
  329. public UserControl1 _userControl;
  330. public MainForm ()
  331. {
  332. SuspendLayout ();
  333. //
  334. // _userControl
  335. //
  336. _userControl = new UserControl1 ();
  337. _userControl.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);
  338. _userControl.BackColor = Color.White;
  339. _userControl.Location = new Point (8, 8);
  340. _userControl.Size = new Size (288, 238);
  341. _userControl.TabIndex = 0;
  342. Controls.Add (_userControl);
  343. //
  344. // MainForm
  345. //
  346. ClientSize = new Size (304, 280);
  347. Location = new Point (250, 100);
  348. StartPosition = FormStartPosition.Manual;
  349. Text = "bug #81694";
  350. ResumeLayout (false);
  351. }
  352. }
  353. private class UserControl1 : UserControl
  354. {
  355. private Button _button1;
  356. public Button _button2;
  357. public UserControl1 ()
  358. {
  359. SuspendLayout ();
  360. //
  361. // _button1
  362. //
  363. _button1 = new Button ();
  364. _button1.Location = new Point (4, 4);
  365. _button1.Size = new Size (75, 23);
  366. _button1.TabIndex = 0;
  367. _button1.Text = "Button1";
  368. Controls.Add (_button1);
  369. //
  370. // _button2
  371. //
  372. _button2 = new Button ();
  373. _button2.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
  374. _button2.Location = new Point (210, 212);
  375. _button2.Size = new Size (75, 23);
  376. _button2.TabIndex = 1;
  377. _button2.Text = "Button2";
  378. Controls.Add (_button2);
  379. //
  380. // UserControl1
  381. //
  382. BackColor = Color.White;
  383. ClientSize = new Size (288, 238);
  384. ResumeLayout (false);
  385. }
  386. }
  387. #if NET_2_0
  388. [Test]
  389. public void TestDockFillWithPadding ()
  390. {
  391. Form f = new Form ();
  392. f.ShowInTaskbar = false;
  393. f.Padding = new Padding (15, 15, 15, 15);
  394. Control c = new Control ();
  395. c.Dock = DockStyle.Fill;
  396. f.Controls.Add (c);
  397. f.Show ();
  398. Assert.AreEqual (new Size (f.ClientSize.Width - 30, f.ClientSize.Height - 30), c.Size, "K1");
  399. f.Dispose ();
  400. }
  401. #endif
  402. [Test]
  403. public void Bug82762 ()
  404. {
  405. if (TestHelper.RunningOnUnix)
  406. Assert.Ignore ("WM Size dependent");
  407. Form f = new Form ();
  408. f.ShowInTaskbar = false;
  409. Button b = new Button ();
  410. b.Size = new Size (100, 100);
  411. b.Anchor = AnchorStyles.None;
  412. f.Controls.Add (b);
  413. f.Show ();
  414. Assert.AreEqual (new Rectangle (0, 0, 100, 100), b.Bounds, "A1");
  415. f.ClientSize = new Size (600, 600);
  416. Assert.AreEqual (new Rectangle (158, 168, 100, 100), b.Bounds, "A2");
  417. f.Close ();
  418. f.Dispose ();
  419. }
  420. [Test]
  421. public void Bug82805 ()
  422. {
  423. Control c1 = new Control ();
  424. c1.Size = new Size (100, 100);
  425. Control c2 = new Control ();
  426. c2.Size = new Size (100, 100);
  427. c2.SuspendLayout ();
  428. c1.Anchor = AnchorStyles.Left | AnchorStyles.Right;
  429. c2.Controls.Add (c1);
  430. c2.Size = new Size (200, 200);
  431. c2.ResumeLayout ();
  432. Assert.AreEqual (200, c1.Width, "A1");
  433. }
  434. #if NET_2_0
  435. [Test]
  436. public void DockedAutoSizeControls ()
  437. {
  438. Form f = new Form ();
  439. f.ShowInTaskbar = false;
  440. Button b = new Button ();
  441. b.Text = "Yo";
  442. b.AutoSize = true;
  443. b.AutoSizeMode = AutoSizeMode.GrowAndShrink;
  444. b.Width = 200;
  445. b.Dock = DockStyle.Left;
  446. f.Controls.Add (b);
  447. f.Show ();
  448. if (b.Width >= 200)
  449. Assert.Fail ("button should be less than 200 width: actual {0}", b.Width);
  450. f.Close ();
  451. f.Dispose ();
  452. }
  453. #endif
  454. [Test] // bug #81199
  455. public void NestedControls ()
  456. {
  457. Form f = new Form ();
  458. f.ShowInTaskbar = false;
  459. MyUserControl c = new MyUserControl ();
  460. c.Dock = DockStyle.Fill;
  461. c.Size = new Size (500, 500);
  462. f.SuspendLayout ();
  463. f.Controls.Add (c);
  464. f.ClientSize = new Size (500, 500);
  465. f.ResumeLayout (false);
  466. f.Show ();
  467. Assert.AreEqual (new Size (600, 600), c.lv.Size, "I1");
  468. f.Close ();
  469. }
  470. private class MyUserControl : UserControl
  471. {
  472. public ListView lv;
  473. public MyUserControl ()
  474. {
  475. lv = new ListView ();
  476. SuspendLayout ();
  477. lv.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
  478. lv.Size = new Size (300, 300);
  479. Controls.Add (lv);
  480. Size = new Size (200, 200);
  481. ResumeLayout (false);
  482. }
  483. }
  484. }
  485. [TestFixture]
  486. public class DockingTests : TestHelper
  487. {
  488. Form form;
  489. Panel panel;
  490. int event_count;
  491. [SetUp]
  492. protected override void SetUp () {
  493. form = new Form ();
  494. form.ShowInTaskbar = false;
  495. form.Size = new Size (400, 400);
  496. panel = new Panel ();
  497. form.Controls.Add (panel);
  498. event_count = 0;
  499. base.SetUp ();
  500. }
  501. [TearDown]
  502. protected override void TearDown () {
  503. form.Dispose ();
  504. base.TearDown ();
  505. }
  506. void IncrementEventCount (object o, EventArgs args)
  507. {
  508. event_count++;
  509. }
  510. [Test]
  511. public void TestDockSizeChangedEvent ()
  512. {
  513. panel.SizeChanged += new EventHandler (IncrementEventCount);
  514. panel.Dock = DockStyle.Bottom;
  515. Assert.AreEqual (1, event_count);
  516. }
  517. [Test]
  518. public void TestDockLocationChangedEvent ()
  519. {
  520. panel.LocationChanged += new EventHandler (IncrementEventCount);
  521. panel.Dock = DockStyle.Bottom;
  522. Assert.AreEqual (1, event_count);
  523. }
  524. [Test]
  525. public void TestDockFillFirst ()
  526. {
  527. Form f = new Form ();
  528. f.ShowInTaskbar = false;
  529. Panel b1 = new Panel ();
  530. Panel b2 = new Panel ();
  531. b1.Dock = DockStyle.Fill;
  532. b2.Dock = DockStyle.Left;
  533. f.Controls.Add (b1);
  534. f.Controls.Add (b2);
  535. f.Show ();
  536. Assert.AreEqual (new Rectangle (b2.Width, 0, f.ClientRectangle.Width - b2.Width, f.ClientRectangle.Height), b1.Bounds, "A1");
  537. Assert.AreEqual (new Rectangle (0, 0, 200, f.ClientRectangle.Height), b2.Bounds, "A2");
  538. f.Dispose ();
  539. }
  540. [Test]
  541. public void TestDockFillLast ()
  542. {
  543. Form f = new Form ();
  544. f.ShowInTaskbar = false;
  545. Panel b1 = new Panel ();
  546. Panel b2 = new Panel ();
  547. b1.Dock = DockStyle.Fill;
  548. b2.Dock = DockStyle.Left;
  549. f.Controls.Add (b2);
  550. f.Controls.Add (b1);
  551. f.Show ();
  552. Assert.AreEqual (new Rectangle (0, 0, f.ClientRectangle.Width, f.ClientRectangle.Height), b1.Bounds, "B1");
  553. Assert.AreEqual (new Rectangle (0, 0, 200, f.ClientRectangle.Height), b2.Bounds, "B2");
  554. f.Dispose ();
  555. }
  556. [Test] // bug #81397
  557. public void TestDockingWithCustomDisplayRectangle ()
  558. {
  559. MyControl mc = new MyControl ();
  560. mc.Size = new Size (200, 200);
  561. Control c = new Control ();
  562. c.Dock = DockStyle.Fill;
  563. mc.Controls.Add (c);
  564. Form f = new Form ();
  565. f.ShowInTaskbar = false;
  566. f.Controls.Add (mc);
  567. f.Show ();
  568. Assert.AreEqual (new Point (20, 20), c.Location, "A1");
  569. Assert.AreEqual (new Size (160, 160), c.Size, "A2");
  570. f.Dispose ();
  571. }
  572. private class MyControl : Control
  573. {
  574. public override Rectangle DisplayRectangle {
  575. get { return new Rectangle (20, 20, this.Width - 40, this.Height - 40); }
  576. }
  577. }
  578. #if NET_2_0
  579. [Test]
  580. public void DockingPreferredSize ()
  581. {
  582. Form f = new Form ();
  583. f.ShowInTaskbar = false;
  584. f.ClientSize = new Size (300, 300);
  585. C1 c1 = new C1 ();
  586. c1.Size = new Size (100, 100);
  587. c1.Dock = DockStyle.Left;
  588. f.Controls.Add (c1);
  589. f.Show ();
  590. Assert.AreEqual (new Size (100, 300), c1.Size, "A1");
  591. f.Controls.Clear ();
  592. C2 c2 = new C2 ();
  593. c2.Size = new Size (100, 100);
  594. c2.Dock = DockStyle.Left;
  595. f.Controls.Add (c2);
  596. Assert.AreEqual (new Size (100, 300), c1.Size, "A2");
  597. f.Dispose ();
  598. }
  599. private class C1 : Panel
  600. {
  601. public override Size GetPreferredSize (Size proposedSize)
  602. {
  603. Console.WriteLine ("HOYO!");
  604. return new Size (200, 200);
  605. }
  606. }
  607. private class C2 : Panel
  608. {
  609. public override Size GetPreferredSize (Size proposedSize)
  610. {
  611. return Size.Empty;
  612. }
  613. }
  614. #endif
  615. [Test]
  616. public void ResettingDockToNone ()
  617. {
  618. Form f = new Form ();
  619. f.ShowInTaskbar = false;
  620. f.ClientSize = new Size (300, 300);
  621. Control c = new Control ();
  622. c.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  623. f.Controls.Add (c);
  624. f.Show ();
  625. f.ClientSize = new Size (350, 350);
  626. Assert.IsTrue (c.Left > 0, string.Format ("A1: c.Left ({0}) must be greater than 0", c.Left));
  627. Assert.IsTrue (c.Top > 0, string.Format ("A2: c.Top ({0}) must be greater than 0", c.Top));
  628. c.Dock = DockStyle.None;
  629. Assert.IsTrue (c.Left > 0, string.Format ("A3: c.Left ({0}) must be greater than 0", c.Left));
  630. Assert.IsTrue (c.Top > 0, string.Format ("A4: c.Top ({0}) must be greater than 0", c.Top));
  631. f.ClientSize = new Size (400, 400);
  632. Assert.IsTrue (c.Left > 70, string.Format ("A5: c.Left ({0}) must be greater than 70", c.Left));
  633. Assert.IsTrue (c.Top > 70, string.Format ("A6: c.Top ({0}) must be greater than 70", c.Top));
  634. f.Dispose ();
  635. }
  636. }
  637. [TestFixture]
  638. public class UndockingTests : TestHelper
  639. {
  640. class TestPanel : Panel {
  641. public void InvokeSetBoundsCore ()
  642. {
  643. SetBoundsCore (37, 37, 37, 37, BoundsSpecified.All);
  644. }
  645. public void InvokeUpdateBounds ()
  646. {
  647. UpdateBounds (37, 37, 37, 37);
  648. }
  649. }
  650. Form form;
  651. TestPanel panel;
  652. [SetUp]
  653. protected override void SetUp () {
  654. form = new Form ();
  655. form.ShowInTaskbar = false;
  656. form.Size = new Size (400, 400);
  657. panel = new TestPanel ();
  658. form.Controls.Add (panel);
  659. base.SetUp ();
  660. }
  661. [TearDown]
  662. protected override void TearDown ()
  663. {
  664. form.Dispose ();
  665. base.TearDown ();
  666. }
  667. [Test]
  668. public void TestUndockDefaultLocation ()
  669. {
  670. Point loc = panel.Location;
  671. panel.Dock = DockStyle.Bottom;
  672. panel.Dock = DockStyle.None;
  673. Assert.AreEqual (loc, panel.Location);
  674. }
  675. [Test]
  676. public void TestUndockDefaultLocationVisible ()
  677. {
  678. form.Show ();
  679. Point loc = panel.Location;
  680. panel.Dock = DockStyle.Bottom;
  681. panel.Dock = DockStyle.None;
  682. Assert.AreEqual (loc, panel.Location);
  683. }
  684. [Test]
  685. public void TestUndockExplicitLeft ()
  686. {
  687. panel.Left = 150;
  688. panel.Dock = DockStyle.Top;
  689. panel.Dock = DockStyle.None;
  690. Assert.AreEqual (150, panel.Left);
  691. }
  692. [Test]
  693. public void TestUndockExplicitTop ()
  694. {
  695. panel.Top = 150;
  696. panel.Dock = DockStyle.Top;
  697. panel.Dock = DockStyle.None;
  698. Assert.AreEqual (150, panel.Top);
  699. }
  700. [Test]
  701. public void TestUndockExplicitLocation ()
  702. {
  703. panel.Location = new Point (50, 50);
  704. Point loc = panel.Location;
  705. panel.Dock = DockStyle.Bottom;
  706. panel.Dock = DockStyle.None;
  707. Assert.AreEqual (loc, panel.Location);
  708. }
  709. [Test]
  710. public void TestUndockExplicitLeftVisible ()
  711. {
  712. form.Show ();
  713. panel.Left = 150;
  714. panel.Dock = DockStyle.Top;
  715. panel.Dock = DockStyle.None;
  716. Assert.AreEqual (150, panel.Left);
  717. }
  718. [Test]
  719. public void TestUndockExplicitTopVisible ()
  720. {
  721. form.Show ();
  722. panel.Top = 150;
  723. panel.Dock = DockStyle.Top;
  724. panel.Dock = DockStyle.None;
  725. Assert.AreEqual (150, panel.Top);
  726. }
  727. [Test]
  728. public void TestUndockExplicitLocationVisible ()
  729. {
  730. form.Show ();
  731. panel.Location = new Point (50, 50);
  732. Point loc = panel.Location;
  733. panel.Dock = DockStyle.Bottom;
  734. panel.Dock = DockStyle.None;
  735. Assert.AreEqual (loc, panel.Location);
  736. }
  737. [Test]
  738. public void TestUndockDefaultSize ()
  739. {
  740. Size sz = panel.Size;
  741. panel.Dock = DockStyle.Fill;
  742. panel.Dock = DockStyle.None;
  743. Assert.AreEqual (sz, panel.Size);
  744. }
  745. [Test]
  746. public void TestUndockExplicitHeight ()
  747. {
  748. panel.Height = 50;
  749. panel.Dock = DockStyle.Left;
  750. panel.Dock = DockStyle.None;
  751. Assert.AreEqual (50, panel.Height);
  752. }
  753. [Test]
  754. public void TestUndockExplicitSize ()
  755. {
  756. panel.Size = new Size (50, 50);
  757. Size sz = panel.Size;
  758. panel.Dock = DockStyle.Fill;
  759. panel.Dock = DockStyle.None;
  760. Assert.AreEqual (sz, panel.Size);
  761. }
  762. [Test]
  763. public void TestUndockExplicitWidth ()
  764. {
  765. panel.Width = 50;
  766. panel.Dock = DockStyle.Top;
  767. panel.Dock = DockStyle.None;
  768. Assert.AreEqual (50, panel.Width);
  769. }
  770. [Test]
  771. public void TestUndockExplicitHeightVisible ()
  772. {
  773. form.Show ();
  774. panel.Height = 50;
  775. panel.Dock = DockStyle.Left;
  776. panel.Dock = DockStyle.None;
  777. Assert.AreEqual (50, panel.Height);
  778. }
  779. [Test]
  780. public void TestUndockExplicitSizeVisible ()
  781. {
  782. form.Show ();
  783. panel.Size = new Size (50, 50);
  784. Size sz = panel.Size;
  785. panel.Dock = DockStyle.Fill;
  786. panel.Dock = DockStyle.None;
  787. Assert.AreEqual (sz, panel.Size);
  788. }
  789. [Test]
  790. public void TestUndockExplicitWidthVisible ()
  791. {
  792. form.Show ();
  793. panel.Width = 50;
  794. panel.Dock = DockStyle.Top;
  795. panel.Dock = DockStyle.None;
  796. Assert.AreEqual (50, panel.Width);
  797. }
  798. [Test]
  799. public void TestUndockSetBounds ()
  800. {
  801. panel.SetBounds (50, 50, 50, 50, BoundsSpecified.All);
  802. panel.Dock = DockStyle.Top;
  803. panel.Dock = DockStyle.None;
  804. Assert.AreEqual (50, panel.Height, "Height");
  805. Assert.AreEqual (50, panel.Left, "Left");
  806. Assert.AreEqual (50, panel.Top, "Top");
  807. Assert.AreEqual (50, panel.Width, "Width");
  808. }
  809. [Test]
  810. public void TestUndockSetBoundsVisible ()
  811. {
  812. form.Show ();
  813. panel.SetBounds (50, 50, 50, 50, BoundsSpecified.All);
  814. panel.Dock = DockStyle.Top;
  815. panel.Dock = DockStyle.None;
  816. Assert.AreEqual (50, panel.Height, "Height");
  817. Assert.AreEqual (50, panel.Left, "Left");
  818. Assert.AreEqual (50, panel.Top, "Top");
  819. Assert.AreEqual (50, panel.Width, "Width");
  820. }
  821. [Test]
  822. public void TestUndockSetBoundsCore ()
  823. {
  824. panel.InvokeSetBoundsCore ();
  825. panel.Dock = DockStyle.Top;
  826. panel.Dock = DockStyle.None;
  827. Assert.AreEqual (37, panel.Height, "Height");
  828. Assert.AreEqual (37, panel.Left, "Left");
  829. Assert.AreEqual (37, panel.Top, "Top");
  830. Assert.AreEqual (37, panel.Width, "Width");
  831. }
  832. [Test]
  833. public void TestUndockSetBoundsCoreVisible ()
  834. {
  835. form.Show ();
  836. panel.InvokeSetBoundsCore ();
  837. panel.Dock = DockStyle.Top;
  838. panel.Dock = DockStyle.None;
  839. Assert.AreEqual (37, panel.Height, "Height");
  840. Assert.AreEqual (37, panel.Left, "Left");
  841. Assert.AreEqual (37, panel.Top, "Top");
  842. Assert.AreEqual (37, panel.Width, "Width");
  843. }
  844. [Test]
  845. public void TestUndockUpdateBounds ()
  846. {
  847. panel.InvokeUpdateBounds ();
  848. panel.Dock = DockStyle.Top;
  849. panel.Dock = DockStyle.None;
  850. Assert.AreEqual (37, panel.Height, "Height");
  851. Assert.AreEqual (37, panel.Left, "Left");
  852. Assert.AreEqual (37, panel.Top, "Top");
  853. Assert.AreEqual (37, panel.Width, "Width");
  854. }
  855. [Test]
  856. public void TestUndockUpdateBoundsVisible ()
  857. {
  858. form.Show ();
  859. panel.InvokeUpdateBounds ();
  860. panel.Dock = DockStyle.Top;
  861. panel.Dock = DockStyle.None;
  862. Assert.AreEqual (37, panel.Height, "Height");
  863. Assert.AreEqual (37, panel.Left, "Left");
  864. Assert.AreEqual (37, panel.Top, "Top");
  865. Assert.AreEqual (37, panel.Width, "Width");
  866. }
  867. #if NET_2_0
  868. [Test]
  869. public void AutoSizeGrowOnlyControls_ShrinkWhenDocked ()
  870. {
  871. // For most controls that are AutoSized and support the
  872. // AutoSizeMode property, if they are set to GrowOnly,
  873. // they should not shrink. However, they will shrink
  874. // even in GrowOnly mode if they are docked.
  875. // Button is one exception to this rule.
  876. Form f = new Form ();
  877. f.ClientSize = new Size (300, 300);
  878. f.ShowInTaskbar = false;
  879. f.Show ();
  880. List<Type> types = new List<Type> ();
  881. types.Add (typeof (TableLayoutPanel));
  882. types.Add (typeof (FlowLayoutPanel));
  883. types.Add (typeof (ToolStripContentPanel));
  884. types.Add (typeof (Panel));
  885. types.Add (typeof (GroupBox));
  886. types.Add (typeof (UserControl));
  887. foreach (Type t in types) {
  888. Control c = t.GetConstructor (Type.EmptyTypes).Invoke (null) as Control;
  889. c.AutoSize = true;
  890. t.GetProperty ("AutoSizeMode").SetValue (c, AutoSizeMode.GrowOnly, null);
  891. c.Bounds = new Rectangle (5, 5, 100, 100);
  892. f.Controls.Add (c);
  893. Assert.AreEqual (100, c.Height, "1 " + t.Name);
  894. c.Dock = DockStyle.Top;
  895. Assert.IsFalse (100 == c.Height, "2 " + t.Name);
  896. f.Controls.Remove (c);
  897. c.Dispose ();
  898. }
  899. f.Dispose ();
  900. }
  901. [Test]
  902. public void AutoSizeGrowOnlyButtons_DoNotShrinkWhenDocked ()
  903. {
  904. // For most controls that are AutoSized and support the
  905. // AutoSizeMode property, if they are set to GrowOnly,
  906. // they should not shrink. However, they will shrink
  907. // even in GrowOnly mode if they are docked.
  908. // Button is one exception to this rule.
  909. Form f = new Form ();
  910. f.ClientSize = new Size (300, 300);
  911. f.ShowInTaskbar = false;
  912. f.Show ();
  913. List<Type> types = new List<Type> ();
  914. types.Add (typeof (Button));
  915. foreach (Type t in types) {
  916. Control c = t.GetConstructor (Type.EmptyTypes).Invoke (null) as Control;
  917. c.AutoSize = true;
  918. t.GetProperty ("AutoSizeMode").SetValue (c, AutoSizeMode.GrowOnly, null);
  919. c.Bounds = new Rectangle (5, 5, 100, 100);
  920. f.Controls.Add (c);
  921. Assert.AreEqual (100, c.Height, "1 " + t.Name);
  922. c.Dock = DockStyle.Top;
  923. Assert.AreEqual (100, c.Height, "2 " + t.Name);
  924. f.Controls.Remove (c);
  925. c.Dispose ();
  926. }
  927. f.Dispose ();
  928. }
  929. [Test]
  930. public void AnchoredAutoSizedControls_SizeInCorrectDirection ()
  931. {
  932. Form f = new Form ();
  933. f.ClientSize = new Size (300, 300);
  934. f.ShowInTaskbar = false;
  935. Panel p1 = new Panel ();
  936. p1.Bounds = new Rectangle (150, 150, 0, 0);
  937. p1.Anchor = AnchorStyles.Top | AnchorStyles.Left;
  938. p1.AutoSize = true;
  939. f.Controls.Add (p1);
  940. Panel p2 = new Panel ();
  941. p2.Bounds = new Rectangle (150, 150, 0, 0);
  942. p2.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  943. p2.AutoSize = true;
  944. f.Controls.Add (p2);
  945. Panel p3 = new Panel ();
  946. p3.Bounds = new Rectangle (150, 150, 0, 0);
  947. p3.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
  948. p3.AutoSize = true;
  949. f.Controls.Add (p3);
  950. Panel p4 = new Panel ();
  951. p4.Bounds = new Rectangle (150, 150, 0, 0);
  952. p4.Anchor = AnchorStyles.None;
  953. p4.AutoSize = true;
  954. f.Controls.Add (p4);
  955. f.Show ();
  956. // cause the panels to grow
  957. p1.Controls.Add (new TextBox ());
  958. p2.Controls.Add (new TextBox ());
  959. p3.Controls.Add (new TextBox ());
  960. p4.Controls.Add (new TextBox ());
  961. f.PerformLayout ();
  962. Assert.AreEqual (150, p1.Top, "1");
  963. Assert.AreEqual (150, p1.Left, "2");
  964. Assert.AreEqual (150, p2.Bottom, "3");
  965. Assert.AreEqual (150, p2.Right, "4");
  966. Assert.AreEqual (150, p3.Top, "5");
  967. Assert.AreEqual (150, p3.Left, "6");
  968. Assert.AreEqual (150, p4.Top, "7");
  969. Assert.AreEqual (150, p4.Left, "8");
  970. f.Dispose ();
  971. }
  972. #endif
  973. }
  974. }