Audio plugin host https://kx.studio/carla
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.

258 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. struct GridTests : public juce::UnitTest
  20. {
  21. GridTests() : juce::UnitTest ("Grid class") {}
  22. void runTest() override
  23. {
  24. using Fr = juce::Grid::Fr;
  25. using Tr = juce::Grid::TrackInfo;
  26. using Rect = juce::Rectangle<float>;
  27. using Grid = juce::Grid;
  28. {
  29. Grid grid;
  30. grid.templateColumns.add (Tr (1_fr));
  31. grid.templateRows.addArray ({ Tr (20_px), Tr (1_fr) });
  32. grid.items.addArray ({ GridItem().withArea (1, 1),
  33. GridItem().withArea (2, 1) });
  34. grid.performLayout (juce::Rectangle<int> (200, 400));
  35. beginTest ("Layout calculation test: 1 column x 2 rows: no gap");
  36. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 200.f, 20.0f));
  37. expect (grid.items[1].currentBounds == Rect (0.0f, 20.0f, 200.f, 380.0f));
  38. grid.templateColumns.add (Tr (50_px));
  39. grid.templateRows.add (Tr (2_fr));
  40. grid.items.addArray ( { GridItem().withArea (1, 2),
  41. GridItem().withArea (2, 2),
  42. GridItem().withArea (3, 1),
  43. GridItem().withArea (3, 2) });
  44. grid.performLayout (juce::Rectangle<int> (150, 170));
  45. beginTest ("Layout calculation test: 2 columns x 3 rows: no gap");
  46. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 100.0f, 20.0f));
  47. expect (grid.items[1].currentBounds == Rect (0.0f, 20.0f, 100.0f, 50.0f));
  48. expect (grid.items[2].currentBounds == Rect (100.0f, 0.0f, 50.0f, 20.0f));
  49. expect (grid.items[3].currentBounds == Rect (100.0f, 20.0f, 50.0f, 50.0f));
  50. expect (grid.items[4].currentBounds == Rect (0.0f, 70.0f, 100.0f, 100.0f));
  51. expect (grid.items[5].currentBounds == Rect (100.0f, 70.0f, 50.0f, 100.0f));
  52. grid.columnGap = 20_px;
  53. grid.rowGap = 10_px;
  54. grid.performLayout (juce::Rectangle<int> (200, 310));
  55. beginTest ("Layout calculation test: 2 columns x 3 rows: rowGap of 10 and columnGap of 20");
  56. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 130.0f, 20.0f));
  57. expect (grid.items[1].currentBounds == Rect (0.0f, 30.0f, 130.0f, 90.0f));
  58. expect (grid.items[2].currentBounds == Rect (150.0f, 0.0f, 50.0f, 20.0f));
  59. expect (grid.items[3].currentBounds == Rect (150.0f, 30.0f, 50.0f, 90.0f));
  60. expect (grid.items[4].currentBounds == Rect (0.0f, 130.0f, 130.0f, 180.0f));
  61. expect (grid.items[5].currentBounds == Rect (150.0f, 130.0f, 50.0f, 180.0f));
  62. }
  63. {
  64. Grid grid;
  65. grid.templateColumns.addArray ({ Tr ("first", 20_px, "in"), Tr ("in", 1_fr, "in"), Tr (20_px, "last") });
  66. grid.templateRows.addArray ({ Tr (1_fr),
  67. Tr (20_px)});
  68. {
  69. beginTest ("Grid items placement tests: integer and custom ident, counting forward");
  70. GridItem i1, i2, i3, i4, i5;
  71. i1.column = { 1, 4 };
  72. i1.row = { 1, 2 };
  73. i2.column = { 1, 3 };
  74. i2.row = { 1, 3 };
  75. i3.column = { "first", "in" };
  76. i3.row = { 2, 3 };
  77. i4.column = { "first", { 2, "in" } };
  78. i4.row = { 1, 2 };
  79. i5.column = { "first", "last" };
  80. i5.row = { 1, 2 };
  81. grid.items.addArray ({ i1, i2, i3, i4, i5 });
  82. grid.performLayout ({ 140, 100 });
  83. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  84. expect (grid.items[1].currentBounds == Rect (0.0f, 0.0f, 120.0f, 100.0f));
  85. expect (grid.items[2].currentBounds == Rect (0.0f, 80.0f, 20.0f, 20.0f));
  86. expect (grid.items[3].currentBounds == Rect (0.0f, 0.0f, 120.0f, 80.0f));
  87. expect (grid.items[4].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  88. }
  89. }
  90. {
  91. Grid grid;
  92. grid.templateColumns.addArray ({ Tr ("first", 20_px, "in"), Tr ("in", 1_fr, "in"), Tr (20_px, "last") });
  93. grid.templateRows.addArray ({ Tr (1_fr),
  94. Tr (20_px)});
  95. beginTest ("Grid items placement tests: integer and custom ident, counting forward, reversed end and start");
  96. GridItem i1, i2, i3, i4, i5;
  97. i1.column = { 4, 1 };
  98. i1.row = { 2, 1 };
  99. i2.column = { 3, 1 };
  100. i2.row = { 3, 1 };
  101. i3.column = { "in", "first" };
  102. i3.row = { 3, 2 };
  103. i4.column = { "first", { 2, "in" } };
  104. i4.row = { 1, 2 };
  105. i5.column = { "last", "first" };
  106. i5.row = { 1, 2 };
  107. grid.items.addArray ({ i1, i2, i3, i4, i5 });
  108. grid.performLayout ({ 140, 100 });
  109. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  110. expect (grid.items[1].currentBounds == Rect (0.0f, 0.0f, 120.0f, 100.0f));
  111. expect (grid.items[2].currentBounds == Rect (0.0f, 80.0f, 20.0f, 20.0f));
  112. expect (grid.items[3].currentBounds == Rect (0.0f, 0.0f, 120.0f, 80.0f));
  113. expect (grid.items[4].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  114. }
  115. {
  116. beginTest ("Grid items placement tests: areas");
  117. Grid grid;
  118. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (Fr (1_fr)), Tr (50_px) };
  119. grid.templateRows = { Tr (50_px),
  120. Tr (1_fr),
  121. Tr (50_px) };
  122. grid.templateAreas = { "header header header header",
  123. "main main . sidebar",
  124. "footer footer footer footer" };
  125. grid.items.addArray ({ GridItem().withArea ("header"),
  126. GridItem().withArea ("main"),
  127. GridItem().withArea ("sidebar"),
  128. GridItem().withArea ("footer"),
  129. });
  130. grid.performLayout ({ 300, 150 });
  131. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 300.f, 50.f));
  132. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  133. expect (grid.items[2].currentBounds == Rect (250.f, 50.f, 50.f, 50.f));
  134. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 300.f, 50.f));
  135. }
  136. {
  137. beginTest ("Grid implicit rows and columns: triggered by areas");
  138. Grid grid;
  139. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (1_fr), Tr (50_px) };
  140. grid.templateRows = { Tr (50_px),
  141. Tr (1_fr),
  142. Tr (50_px) };
  143. grid.autoRows = Tr (30_px);
  144. grid.autoColumns = Tr (30_px);
  145. grid.templateAreas = { "header header header header header",
  146. "main main . sidebar sidebar",
  147. "footer footer footer footer footer",
  148. "sub sub sub sub sub"};
  149. grid.items.addArray ({ GridItem().withArea ("header"),
  150. GridItem().withArea ("main"),
  151. GridItem().withArea ("sidebar"),
  152. GridItem().withArea ("footer"),
  153. GridItem().withArea ("sub"),
  154. });
  155. grid.performLayout ({ 330, 180 });
  156. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 330.f, 50.f));
  157. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  158. expect (grid.items[2].currentBounds == Rect (250.f, 50.f, 80.f, 50.f));
  159. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 330.f, 50.f));
  160. expect (grid.items[4].currentBounds == Rect (0.f, 150.f, 330.f, 30.f));
  161. }
  162. {
  163. beginTest ("Grid implicit rows and columns: triggered by areas");
  164. Grid grid;
  165. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (1_fr), Tr (50_px) };
  166. grid.templateRows = { Tr (50_px),
  167. Tr (1_fr),
  168. Tr (50_px) };
  169. grid.autoRows = Tr (1_fr);
  170. grid.autoColumns = Tr (1_fr);
  171. grid.templateAreas = { "header header header header",
  172. "main main . sidebar",
  173. "footer footer footer footer" };
  174. grid.items.addArray ({ GridItem().withArea ("header"),
  175. GridItem().withArea ("main"),
  176. GridItem().withArea ("sidebar"),
  177. GridItem().withArea ("footer"),
  178. GridItem().withArea (4, 5, 6, 7)
  179. });
  180. grid.performLayout ({ 350, 250 });
  181. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 250.f, 50.f));
  182. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  183. expect (grid.items[2].currentBounds == Rect (200.f, 50.f, 50.f, 50.f));
  184. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 250.f, 50.f));
  185. expect (grid.items[4].currentBounds == Rect (250.f, 150.f, 100.f, 100.f));
  186. }
  187. }
  188. };
  189. static GridTests gridUnitTests;