The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. struct GridTests : public UnitTest
  22. {
  23. GridTests() : UnitTest ("Grid class") {}
  24. void runTest() override
  25. {
  26. using Fr = Grid::Fr;
  27. using Tr = Grid::TrackInfo;
  28. using Rect = Rectangle<float>;
  29. {
  30. Grid grid;
  31. grid.templateColumns.add (Tr (1_fr));
  32. grid.templateRows.addArray ({ Tr (20_px), Tr (1_fr) });
  33. grid.items.addArray ({ GridItem().withArea (1, 1),
  34. GridItem().withArea (2, 1) });
  35. grid.performLayout (Rectangle<int> (200, 400));
  36. beginTest ("Layout calculation test: 1 column x 2 rows: no gap");
  37. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 200.f, 20.0f));
  38. expect (grid.items[1].currentBounds == Rect (0.0f, 20.0f, 200.f, 380.0f));
  39. grid.templateColumns.add (Tr (50_px));
  40. grid.templateRows.add (Tr (2_fr));
  41. grid.items.addArray ( { GridItem().withArea (1, 2),
  42. GridItem().withArea (2, 2),
  43. GridItem().withArea (3, 1),
  44. GridItem().withArea (3, 2) });
  45. grid.performLayout (Rectangle<int> (150, 170));
  46. beginTest ("Layout calculation test: 2 columns x 3 rows: no gap");
  47. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 100.0f, 20.0f));
  48. expect (grid.items[1].currentBounds == Rect (0.0f, 20.0f, 100.0f, 50.0f));
  49. expect (grid.items[2].currentBounds == Rect (100.0f, 0.0f, 50.0f, 20.0f));
  50. expect (grid.items[3].currentBounds == Rect (100.0f, 20.0f, 50.0f, 50.0f));
  51. expect (grid.items[4].currentBounds == Rect (0.0f, 70.0f, 100.0f, 100.0f));
  52. expect (grid.items[5].currentBounds == Rect (100.0f, 70.0f, 50.0f, 100.0f));
  53. grid.columnGap = 20_px;
  54. grid.rowGap = 10_px;
  55. grid.performLayout (Rectangle<int> (200, 310));
  56. beginTest ("Layout calculation test: 2 columns x 3 rows: rowGap of 10 and columnGap of 20");
  57. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 130.0f, 20.0f));
  58. expect (grid.items[1].currentBounds == Rect (0.0f, 30.0f, 130.0f, 90.0f));
  59. expect (grid.items[2].currentBounds == Rect (150.0f, 0.0f, 50.0f, 20.0f));
  60. expect (grid.items[3].currentBounds == Rect (150.0f, 30.0f, 50.0f, 90.0f));
  61. expect (grid.items[4].currentBounds == Rect (0.0f, 130.0f, 130.0f, 180.0f));
  62. expect (grid.items[5].currentBounds == Rect (150.0f, 130.0f, 50.0f, 180.0f));
  63. }
  64. {
  65. Grid grid;
  66. grid.templateColumns.addArray ({ Tr ("first", 20_px, "in"), Tr ("in", 1_fr, "in"), Tr (20_px, "last") });
  67. grid.templateRows.addArray ({ Tr (1_fr),
  68. Tr (20_px)});
  69. {
  70. beginTest ("Grid items placement tests: integer and custom ident, counting forward");
  71. GridItem i1, i2, i3, i4, i5;
  72. i1.column = { 1, 4 };
  73. i1.row = { 1, 2 };
  74. i2.column = { 1, 3 };
  75. i2.row = { 1, 3 };
  76. i3.column = { "first", "in" };
  77. i3.row = { 2, 3 };
  78. i4.column = { "first", { 2, "in" } };
  79. i4.row = { 1, 2 };
  80. i5.column = { "first", "last" };
  81. i5.row = { 1, 2 };
  82. grid.items.addArray ({ i1, i2, i3, i4, i5 });
  83. grid.performLayout ({ 140, 100 });
  84. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  85. expect (grid.items[1].currentBounds == Rect (0.0f, 0.0f, 120.0f, 100.0f));
  86. expect (grid.items[2].currentBounds == Rect (0.0f, 80.0f, 20.0f, 20.0f));
  87. expect (grid.items[3].currentBounds == Rect (0.0f, 0.0f, 120.0f, 80.0f));
  88. expect (grid.items[4].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  89. }
  90. }
  91. {
  92. Grid grid;
  93. grid.templateColumns.addArray ({ Tr ("first", 20_px, "in"), Tr ("in", 1_fr, "in"), Tr (20_px, "last") });
  94. grid.templateRows.addArray ({ Tr (1_fr),
  95. Tr (20_px)});
  96. beginTest ("Grid items placement tests: integer and custom ident, counting forward, reversed end and start");
  97. GridItem i1, i2, i3, i4, i5;
  98. i1.column = { 4, 1 };
  99. i1.row = { 2, 1 };
  100. i2.column = { 3, 1 };
  101. i2.row = { 3, 1 };
  102. i3.column = { "in", "first" };
  103. i3.row = { 3, 2 };
  104. i4.column = { "first", { 2, "in" } };
  105. i4.row = { 1, 2 };
  106. i5.column = { "last", "first" };
  107. i5.row = { 1, 2 };
  108. grid.items.addArray ({ i1, i2, i3, i4, i5 });
  109. grid.performLayout ({ 140, 100 });
  110. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  111. expect (grid.items[1].currentBounds == Rect (0.0f, 0.0f, 120.0f, 100.0f));
  112. expect (grid.items[2].currentBounds == Rect (0.0f, 80.0f, 20.0f, 20.0f));
  113. expect (grid.items[3].currentBounds == Rect (0.0f, 0.0f, 120.0f, 80.0f));
  114. expect (grid.items[4].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  115. }
  116. {
  117. beginTest ("Grid items placement tests: areas");
  118. Grid grid;
  119. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (Fr (1_fr)), Tr (50_px) };
  120. grid.templateRows = { Tr (50_px),
  121. Tr (1_fr),
  122. Tr (50_px) };
  123. grid.templateAreas = { "header header header header",
  124. "main main . sidebar",
  125. "footer footer footer footer" };
  126. grid.items.addArray ({ GridItem().withArea ("header"),
  127. GridItem().withArea ("main"),
  128. GridItem().withArea ("sidebar"),
  129. GridItem().withArea ("footer"),
  130. });
  131. grid.performLayout ({ 300, 150 });
  132. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 300.f, 50.f));
  133. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  134. expect (grid.items[2].currentBounds == Rect (250.f, 50.f, 50.f, 50.f));
  135. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 300.f, 50.f));
  136. }
  137. {
  138. beginTest ("Grid implicit rows and columns: triggered by areas");
  139. Grid grid;
  140. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (1_fr), Tr (50_px) };
  141. grid.templateRows = { Tr (50_px),
  142. Tr (1_fr),
  143. Tr (50_px) };
  144. grid.autoRows = Tr (30_px);
  145. grid.autoColumns = Tr (30_px);
  146. grid.templateAreas = { "header header header header header",
  147. "main main . sidebar sidebar",
  148. "footer footer footer footer footer",
  149. "sub sub sub sub sub"};
  150. grid.items.addArray ({ GridItem().withArea ("header"),
  151. GridItem().withArea ("main"),
  152. GridItem().withArea ("sidebar"),
  153. GridItem().withArea ("footer"),
  154. GridItem().withArea ("sub"),
  155. });
  156. grid.performLayout ({ 330, 180 });
  157. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 330.f, 50.f));
  158. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  159. expect (grid.items[2].currentBounds == Rect (250.f, 50.f, 80.f, 50.f));
  160. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 330.f, 50.f));
  161. expect (grid.items[4].currentBounds == Rect (0.f, 150.f, 330.f, 30.f));
  162. }
  163. {
  164. beginTest ("Grid implicit rows and columns: triggered by areas");
  165. Grid grid;
  166. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (1_fr), Tr (50_px) };
  167. grid.templateRows = { Tr (50_px),
  168. Tr (1_fr),
  169. Tr (50_px) };
  170. grid.autoRows = Tr (1_fr);
  171. grid.autoColumns = Tr (1_fr);
  172. grid.templateAreas = { "header header header header",
  173. "main main . sidebar",
  174. "footer footer footer footer" };
  175. grid.items.addArray ({ GridItem().withArea ("header"),
  176. GridItem().withArea ("main"),
  177. GridItem().withArea ("sidebar"),
  178. GridItem().withArea ("footer"),
  179. GridItem().withArea (4, 5, 6, 7)
  180. });
  181. grid.performLayout ({ 350, 250 });
  182. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 250.f, 50.f));
  183. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  184. expect (grid.items[2].currentBounds == Rect (200.f, 50.f, 50.f, 50.f));
  185. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 250.f, 50.f));
  186. expect (grid.items[4].currentBounds == Rect (250.f, 150.f, 100.f, 100.f));
  187. }
  188. }
  189. };
  190. static GridTests gridUnitTests;
  191. } // namespace juce