Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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"
23#include "lib/iter-explorer.hpp"
25#include "lib/util.hpp"
26
27#include <vector>
28#include <forward_list>
29#include <algorithm>
30#include <random>
31
32
33using util::isnil;
34using util::contains;
37using std::vector;
38using std::forward_list;
39using std::shuffle;
40
41
42namespace stage{
43namespace model{
44namespace test {
45
46
47 namespace { // Test fixture...
48
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
65 : public CanvasHook<DummyWidget>
66 {
68 {
70 int posX, posY;
71 };
72 forward_list<Attachment> widgets_;
73
74
75 auto
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();
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
249
250
251}}} // namespace stage::model::test
Specialised (abstracted) presentation context with positioning by coordinates.
Interface to represent _"some presentation layout entity",_ with the ability to place widgets (manage...
A widget attached onto a display canvas or similar central presentation context.
Mix-in interface to allow for concrete CanvasHooked widgets to adapt themselves to the metric current...
DisplayMetric & getMetric() const override
access the component to handle layout metric
bool testVerifyPos(DummyWidget const &someWidget, int x_expected, int y_expected)
Preconfigured adapters for some STL container standard usage situations.
Building tree expanding and backtracking evaluations within hierarchical scopes.
_SeqT< CON >::Range eachElm(CON &coll)
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
Lumiera GTK UI implementation root.
Definition guifacade.cpp:37
Test runner and basic definitions for tests.
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee's memory identities.
Definition util.hpp:421
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
Definition util.hpp:230
bool isnil(lib::time::Duration const &dur)
bool linearSearch(IT iter, typename IT::value_type const &val)
use (and exhaust) a »Lumiera Forward Iterator« for linear search
Definition util.hpp:266
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Managing a collection of non-copyable polymorphic objects in compact storage.
A collection of frequently used helper functions to support unit testing.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...