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) Lumiera.org
5  2019, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
32 #include "lib/iter-explorer.hpp"
33 #include "lib/iter-adapter-stl.hpp"
34 #include "lib/util.hpp"
35 
36 #include <vector>
37 #include <forward_list>
38 #include <algorithm>
39 #include <random>
40 
41 
42 using util::isnil;
43 using util::contains;
44 using util::isSameObject;
46 using std::vector;
47 using std::forward_list;
48 using std::shuffle;
49 
50 
51 namespace stage{
52 namespace model{
53 namespace test {
54 
55 
56  namespace { // Test fixture...
57 
58  struct DummyWidget
59  {
60  int i = rand(); // "identity"
61 
62  friend bool
63  operator== (DummyWidget const& wa, DummyWidget const& wb)
64  {
65  return wa.i == wb.i; // compare identity
66  }
67  };
68 
70 
71 
72 
73  class FakeCanvas
74  : public CanvasHook<DummyWidget>
75  {
76  struct Attachment
77  {
78  DummyWidget& widget;
79  int posX, posY;
80  };
81  forward_list<Attachment> widgets_;
82 
83 
84  auto
85  allWidgetIDs() const
86  {
87  return lib::explore(widgets_)
88  .transform([](Attachment const& entry)
89  {
90  return entry.widget.i;
91  });
92  }
93 
94  auto
95  findEntry (DummyWidget const& someWidget)
96  {
97  return std::find_if (widgets_.begin()
98  ,widgets_.end()
99  , [&](Attachment const& a) { return a.widget == someWidget; });
100  }
101 
102  public:
103  /* === diagnostic functions for the test === */
104  bool
105  empty() const
106  {
107  return widgets_.empty();
108  }
109 
110  bool
111  testContains (int someWidgetID)
112  {
113  return util::linearSearch (allWidgetIDs(), someWidgetID);
114  }
115 
116  bool
117  testVerifyPos (DummyWidget const& someWidget, int x_expected, int y_expected)
118  {
119  auto end = widgets_.end();
120  auto pos = findEntry (someWidget);
121  return pos != end
122  and pos->posX == x_expected
123  and pos->posY == y_expected;
124  }
125 
126 
127  /* === Interface CanvasHook === */
128 
129  void
130  hook (DummyWidget& elm, int xPos, int yPos) override
131  {
132  widgets_.push_front (Attachment{elm, xPos,yPos});
133  }
134 
135  void
136  move (DummyWidget& elm, int xPos, int yPos) override
137  {
138  auto end = widgets_.end();
139  auto pos = findEntry (elm);
140  if (pos != end)
141  {
142  pos->posX = xPos;
143  pos->posY = yPos;
144  }
145  }
146 
147  void
148  remove (DummyWidget& elm) override
149  {
150  widgets_.remove_if ([&](Attachment const& a) { return a.widget == elm; });
151  }
152 
153  protected:
155  getMetric() const override
156  {
157  NOTREACHED ("Time to pixel translation not covered in this unit test");
158  }
159  };
160  }
161 
162 
163 
164 
165  /*************************************************************************************/
177  class CanvasHook_test : public Test
178  {
179 
180  virtual void
181  run (Arg)
182  {
183  attach2canvas();
184  relocateWidget();
185  }
186 
187 
191  void
193  {
194  FakeCanvas canvas;
195  CHECK (canvas.empty());
196 
197  HookedWidget widget{canvas.hookedAt(1,1)};
198  CHECK (canvas.testVerifyPos (widget, 1,1));
199  CHECK (not canvas.empty());
200 
201  int someID;
202  {
203  HookedWidget otherWidget{canvas.hookedAt(2,2)};
204  someID = otherWidget.i;
205  CHECK (canvas.testContains (someID));
206  CHECK (canvas.testContains (widget.i));
207  CHECK (canvas.testVerifyPos (widget, 1,1));
208  CHECK (canvas.testVerifyPos (otherWidget, 2,2));
209  }// hook goes out of scope...
210  CHECK (not canvas.testContains (someID));
211  CHECK (canvas.testContains (widget.i));
212  CHECK (not canvas.empty());
213  }
214 
215 
219  void
221  {
222  int x1 = rand() % 100;
223  int y1 = rand() % 100;
224  int x2 = rand() % 100;
225  int y2 = rand() % 100;
226  int x3 = rand() % 100;
227  int y3 = rand() % 100;
228 
229  FakeCanvas canvas;
230  HookedWidget w1{canvas.hookedAt(x1,y1)};
231  HookedWidget w3{canvas.hookedAt(x3,y3)};
232 
233  int id2;
234  {
235  HookedWidget w2{canvas.hookedAt(x2,y2)};
236  id2 = w2.i;
237  CHECK (canvas.testContains (id2));
238  CHECK (canvas.testVerifyPos (w2, x2,y2));
239 
240  int newX = ++x2;
241  int newY = --y2;
242  w2.moveTo (newX,newY);
243 
244  CHECK (canvas.testVerifyPos (w2, newX,newY));
245  CHECK (canvas.testVerifyPos (w1, x1,y1));
246  CHECK (canvas.testVerifyPos (w3, x3,y3));
247  }
248  CHECK (not canvas.testContains (id2));
249  CHECK (canvas.testVerifyPos (w1, x1,y1));
250  CHECK (canvas.testVerifyPos (w3, x3,y3));
251  }
252  };
253 
254 
256  LAUNCHER (CanvasHook_test, "unit gui");
257 
258 
259 }}} // 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:82
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:49
A widget attached onto a display canvas or similar central presentation context.
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.
Simple test class runner.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:46
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, bypassing any custom comparison operators.
Definition: util.hpp:372