Lumiera  0.pre.03
»edit your freedom«
canvas-hook-test.cpp
Go to the documentation of this file.
1 /*
2  CanvasHook(Test) - verify abstracted canvas attachment
3 
4  Copyright (C)
5  2019, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
23 #include "lib/iter-explorer.hpp"
24 #include "lib/iter-adapter-stl.hpp"
25 #include "lib/util.hpp"
26 
27 #include <vector>
28 #include <forward_list>
29 #include <algorithm>
30 #include <random>
31 
32 
33 using util::isnil;
34 using util::contains;
35 using util::isSameObject;
37 using std::vector;
38 using std::forward_list;
39 using std::shuffle;
40 
41 
42 namespace stage{
43 namespace model{
44 namespace test {
45 
46 
47  namespace { // Test fixture...
48 
49  struct DummyWidget
50  {
51  int i = rani(); // "identity"
52 
53  friend bool
54  operator== (DummyWidget const& wa, DummyWidget const& wb)
55  {
56  return wa.i == wb.i; // compare identity
57  }
58  };
59 
61 
62 
63 
64  class FakeCanvas
65  : public CanvasHook<DummyWidget>
66  {
67  struct Attachment
68  {
69  DummyWidget& widget;
70  int posX, posY;
71  };
72  forward_list<Attachment> widgets_;
73 
74 
75  auto
76  allWidgetIDs() const
77  {
78  return lib::explore(widgets_)
79  .transform([](Attachment const& entry)
80  {
81  return entry.widget.i;
82  });
83  }
84 
85  auto
86  findEntry (DummyWidget const& someWidget)
87  {
88  return std::find_if (widgets_.begin()
89  ,widgets_.end()
90  , [&](Attachment const& a) { return a.widget == someWidget; });
91  }
92 
93  public:
94  /* === diagnostic functions for the test === */
95  bool
96  empty() const
97  {
98  return widgets_.empty();
99  }
100 
101  bool
102  testContains (int someWidgetID)
103  {
104  return util::linearSearch (allWidgetIDs(), someWidgetID);
105  }
106 
107  bool
108  testVerifyPos (DummyWidget const& someWidget, int x_expected, int y_expected)
109  {
110  auto end = widgets_.end();
111  auto pos = findEntry (someWidget);
112  return pos != end
113  and pos->posX == x_expected
114  and pos->posY == y_expected;
115  }
116 
117 
118  /* === Interface CanvasHook === */
119 
120  void
121  hook (DummyWidget& elm, int xPos, int yPos) override
122  {
123  widgets_.push_front (Attachment{elm, xPos,yPos});
124  }
125 
126  void
127  move (DummyWidget& elm, int xPos, int yPos) override
128  {
129  auto end = widgets_.end();
130  auto pos = findEntry (elm);
131  if (pos != end)
132  {
133  pos->posX = xPos;
134  pos->posY = yPos;
135  }
136  }
137 
138  void
139  remove (DummyWidget& elm) override
140  {
141  widgets_.remove_if ([&](Attachment const& a) { return a.widget == elm; });
142  }
143 
144  protected:
146  getMetric() const override
147  {
148  NOTREACHED ("Time to pixel translation not covered in this unit test");
149  }
150  };
151  }
152 
153 
154 
155 
156  /*************************************************************************************/
168  class CanvasHook_test : public Test
169  {
170 
171  virtual void
172  run (Arg)
173  {
174  seedRand();
175  attach2canvas();
176  relocateWidget();
177  }
178 
179 
183  void
185  {
186  FakeCanvas canvas;
187  CHECK (canvas.empty());
188 
189  HookedWidget widget{canvas.hookedAt(1,1)};
190  CHECK (canvas.testVerifyPos (widget, 1,1));
191  CHECK (not canvas.empty());
192 
193  int someID;
194  {
195  HookedWidget otherWidget{canvas.hookedAt(2,2)};
196  someID = otherWidget.i;
197  CHECK (canvas.testContains (someID));
198  CHECK (canvas.testContains (widget.i));
199  CHECK (canvas.testVerifyPos (widget, 1,1));
200  CHECK (canvas.testVerifyPos (otherWidget, 2,2));
201  }// hook goes out of scope...
202  CHECK (not canvas.testContains (someID));
203  CHECK (canvas.testContains (widget.i));
204  CHECK (not canvas.empty());
205  }
206 
207 
211  void
213  {
214  int x1 = rani (100);
215  int y1 = rani (100);
216  int x2 = rani (100);
217  int y2 = rani (100);
218  int x3 = rani (100);
219  int y3 = rani (100);
220 
221  FakeCanvas canvas;
222  HookedWidget w1{canvas.hookedAt(x1,y1)};
223  HookedWidget w3{canvas.hookedAt(x3,y3)};
224 
225  int id2;
226  {
227  HookedWidget w2{canvas.hookedAt(x2,y2)};
228  id2 = w2.i;
229  CHECK (canvas.testContains (id2));
230  CHECK (canvas.testVerifyPos (w2, x2,y2));
231 
232  int newX = ++x2;
233  int newY = --y2;
234  w2.moveTo (newX,newY);
235 
236  CHECK (canvas.testVerifyPos (w2, newX,newY));
237  CHECK (canvas.testVerifyPos (w1, x1,y1));
238  CHECK (canvas.testVerifyPos (w3, x3,y3));
239  }
240  CHECK (not canvas.testContains (id2));
241  CHECK (canvas.testVerifyPos (w1, x1,y1));
242  CHECK (canvas.testVerifyPos (w3, x3,y3));
243  }
244  };
245 
246 
248  LAUNCHER (CanvasHook_test, "unit gui");
249 
250 
251 }}} // namespace stage::model::test
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
Mix-in interface to allow for concrete CanvasHooked widgets to adapt themselves to the metric current...
Definition: canvas-hook.hpp:73
DisplayMetric & getMetric() const override
access the component to handle layout metric
AnyPair entry(Query< TY > const &query, typename WrapReturn< TY >::Wrapper &obj)
helper to simplify creating mock table entries, wrapped correctly
Definition: run.hpp:40
A widget attached onto a display canvas or similar central presentation context.
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
Interface to represent _"some presentation layout entity",_ with the ability to place widgets (manage...
Managing a collection of non-copyable polymorphic objects in compact storage.
Specialised (abstracted) presentation context with positioning by coordinates.
Simplistic test class runner.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:37
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
_SeqT< CON >::Range eachElm(CON &coll)
Building tree expanding and backtracking evaluations within hierarchical scopes.
Preconfigured adapters for some STL container standard usage situations.
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421