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.

juce_GridUnitTests.cpp 11KB

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